我有两个显示相同数据的条形图。每个 x 轴标签显示一个条形,它是输出集和输入集的总和。我的另一个需要将每个 x 轴标签的输入和输出数据分成两个相邻的条形。
从我在其他示例中看到的数据被合并到一个数组中,然后成对绘制。我尝试将两组合并为一组,但无法使其工作。
这是我绘制条形的方法:
svg.selectAll(".bar")
.data(options.series[i].data)
.enter().append("rect")
.attr("class", "bar")
.attr("class", function(d) {
if (d < 0) {
return "bar positive";
} else {
return "bar negative";
}
})
.attr("height", function(d) {
return Math.abs(yScale(d) - yScale(0));
})
.attr("y", function(d) {
if (d > 0) {
return yScale(d);
} else {
return yScale(0);
}
// return yScale(d)
})
.attr("width", (xScale.bandwidth()))
.attr("x", function(d, j) {
return xScale(options.labels[j])
})
.on('mouseover', function(d, j){
d3.select(this).style("opacity", 0.6);
tip.show(d, j);
})
.on('mouseout', function(d, j){ …Run Code Online (Sandbox Code Playgroud)