繁体   English   中英

D3条形图并使用多个刻度

[英]D3 bar chart and using multiple scales

我正在使用d3创建“趋势”分析条形图。 此图表具有在中间的X轴,并且可以延伸高于或低于该轴杆以表示百分比变化。 为X轴序,并为Y轴的两个线性刻度 - 我用3台秤做到了这一点。 我不确定这两个线性刻度的用法。 看起来不雅,只是“不正确”。

有问题的秤:

this.pyscale.domain([0, 1]);
this.pyscale.rangeRound([this.height/2, 0]);

this.nyscale.domain([-1, 0]);
this.nyscale.rangeRound([this.height, this.height/2]);

与绘制条有关的功能:

bar.append("rect")
   .attr("y", function(d) { 
        if (d[that.ycol] >= 0) {
            return that.pyscale(d[that.ycol]);
        }
        return that.height/2; 
    })
   .attr("height", function(d) { 
        if (d[that.ycol] >= 0) {
            return that.height/2 - that.pyscale(d[that.ycol]);
        }
        return that.nyscale(d[that.ycol]) - that.height/2;
    })
   .attr("width", this.xscale.rangeBand())

这是一种体面的方式吗? 还是有一种使用我缺少的Y刻度的更优雅的方式?

谢谢你的时间。

从您的描述看来,您应该可以像这样用一个秤代替两个秤:

this.yscale.domain([-1, 1]);
this.yscale.rangeRound([this.height, 0]);

并在其余代码中使用this.yscale

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM