【问题标题】:D3 Graph - changing y-axis to integerD3 Graph - 将 y 轴更改为整数
【发布时间】:2019-07-22 08:15:30
【问题描述】:

我正在尝试在 y 轴上创建整数波段。

已尝试将 .scaleband 更改为 .scalelinear 并且 .ticks “arbitraryMetric”存储为整数。

代码:

const categories = vizData.map(d => d.arbitraryMetric);

const yScale = d3
.scaleBand()
.domain(categories)
.range([0, vizHeight - timelineMargin.bottom]);

const labels = vizCanvas
.selectAll('text')
.data(categories)
.enter()
.append('text')
.style('font-family', style.fontFamily)
.style('fill', '#3C4043')
.attr('x', timelineMargin.left - 10)
.attr('y', d => yScale(d) + yScale.bandwidth() / 2)
.attr('text-anchor', 'end')
.text(d => d);

【问题讨论】:

    标签: javascript d3.js data-studio-custom-visuals


    【解决方案1】:

    以下解决方案可以解决问题 - 归功于 this answer

    const yAxisTicks = yScale.ticks()
        .filter(tick => Number.isInteger(tick));
    
    const yAxis = d3.axisLeft(yScale)
        .tickValues(yAxisTicks)
        .tickFormat(d3.format('d'));
    

    【讨论】:

      猜你喜欢
      • 2015-09-14
      • 2013-03-23
      • 2021-10-15
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      • 2021-07-22
      • 2020-06-26
      • 1970-01-01
      相关资源
      最近更新 更多