简体   繁体   English

D3条形图并使用多个刻度

[英]D3 bar chart and using multiple scales

I'm creating a "trending" analysis bar chart using d3. 我正在使用d3创建“趋势”分析条形图。 This chart has an X axis in the middle, and bars that can extend either above or below this axis to represent percentage change. 此图表具有在中间的X轴,并且可以延伸高于或低于该轴杆以表示百分比变化。 I've done this by using 3 scales - an ordinal for the X axis, and two linear scales for the Y axis. 为X轴序,并为Y轴的两个线性刻度 - 我用3台秤做到了这一点。 The usage of the two linear scales is what I'm uncertain about. 我不确定这两个线性刻度的用法。 It seems inelegant and just "not right." 看起来不雅,只是“不正确”。

The scales in question: 有问题的秤:

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]);

Functions related to drawing the bars: 与绘制条有关的功能:

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())

Is this a decent way to do this? 这是一种体面的方式吗? Or is there a more elegant way using one Y scale that I'm missing? 还是有一种使用我缺少的Y刻度的更优雅的方式?

Thanks for your time. 谢谢你的时间。

From your description, it sounds like you should be able to replace your two scales with one scale simply like this: 从您的描述看来,您应该可以像这样用一个秤代替两个秤:

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

and use this.yscale in the rest of the code. 并在其余代码中使用this.yscale

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

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