【发布时间】:2019-01-05 08:09:23
【问题描述】:
问题:
组数是动态的,竖线(分隔符)需要动态填充。我使用 x0.rangeBand() 获得组宽度。有没有办法动态获取两组之间的空间宽度?
代码和平:
.....
var slice = svg.selectAll(".chart")
.data(data)
.enter().append("g")
.attr("class", "g")
.attr("transform", function (d) {
return "translate(" + x0(d.category) + ",0)";
});
// Create rectangles of the correct width
slice.selectAll("rect")
.data(function (d) {
return d.values;
})
.enter().append("rect")
.attr("width", x1.rangeBand())
.attr("x", function (d) {
return x1(d.rate);
})
.style("fill", function (d) {
return color(d.rate)
})
.attr("y", function (d) {
return y(0);
})
.attr("height", function (d) {
return height - y(0);
})
.on("mouseover", function (d) {
d3.select(this).style("fill", d3.rgb(color(d.rate)).darker(2));
tip.show(d);
})
.on("mouseout", function (d) {
tip.hide
d3.select(this).style("fill", color(d.rate));
})
slice.append("line")
.attr("class", "blabla")
.attr("x1", x0.rangeBand()+20)
.attr("x2", x0.rangeBand()+20)
.attr("y1", 0)
.attr("y2", height + margin.top + margin.bottom)
.style("stroke-width", 1)
.style("stroke", "#000");
.....
这就是少数组的样子
【问题讨论】:
-
如果将
paddingInner()设置为x0()并在此间隙的中间画线会怎样? -
v3 没有 paddingInner() 函数。github.com/d3/d3-3.x-api-reference/blob/master/…
-
0.1中的rangeRoundBands([0, width], .1);是填充。我不得不猜测没有完整的源代码。需要在计算中添加空间大小。 -
我已经为 d3 API 链接添加了书签。可能对 d3 示例有用。谢谢。
标签: javascript d3.js svg graph