简体   繁体   English

如何在d3.js中使用叠加线在堆叠条形图上的x轴对齐

[英]How to line up x axis on stacked bar chart with a line overlay in d3.js

I'm trying to create a stacked bar chart with a line graph over the top to show the relationship between total emissions and cost. 我正在尝试在顶部创建一个带有折线图的堆积条形图,以显示总排放量和成本之间的关系。

My example is here: 我的例子在这里:

http://bl.ocks.org/4754261 http://bl.ocks.org/4754261

As you can see from the chart, the dots of the line chart don't line up with the ticks on the bottom and thus don't line up nicely with the center of the bars. 从图表中可以看到,折线图的点与底部的刻度线不对齐,因此与条形的中心对齐不佳。

I've been digging around quite a bit on google and I haven't been able to find something that helps me to solve my problem yet. 我一直在谷歌上挖掘相当多,我还没有找到能帮我解决问题的东西。 Do I need a second x axis and if so what would it look like? 我是否需要第二个x轴,如果需要,它将是什么样?

Oh, and if this is a bad or misleading way to show the relationship between cost and emissions, I'm receptive to other ideas. 哦,如果这是显示成本和排放之间关系的一种不好或误导的方式,我会接受其他想法。

You need to add on half the width of each bar to the x point of your line/dot calculation. 您需要在线条/点计算的x点上增加每个条形的一半宽度。 As you've defined the width of each bar to be x.rangeBand(), this would mean changing the x property to something like: 当您将每个条形的宽度定义为x.rangeBand()时,这意味着将x属性更改为:

var valueline = d3.svg.line()
    .x(function(d) {
        return x(d.property) + x.rangeBand() / 2;
    })
    .y(function(d) { return yCost(d.normalised_cost); })
    .interpolate("step-after");

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

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