1
我想在每个条的后面添加另一个rect
元素,但我努力做到这一点,因为d3.js不允许为每个数据点添加另一个元素。d3.js为每个数据点创建两个元素
http://jsfiddle.net/g5hpwf0m/2/
var w = parseInt(d3.select(self).style("width")),
h = parseInt(d3.select(self).style("height")),
svg = d3.select(self)
.append("svg")
.attr("width", w)
.attr("height", h),
yScale = d3.scale.linear()
.domain([0, d3.max(dataset)])
.range([0,h]);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("fill", "green")
.attr("x", function(d, i) {
return i * (w/dataset.length);
})
.attr("y", function(d) {
return h - yScale(d);
})
.attr("width", w/dataset.length - barPadding)
.attr("height", function(d){return yScale(d);});
我尝试添加这一点,但它不会做任何事情。
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("fill", "black")
.attr("x", function(d, i) {
return i * (w/dataset.length);
})
.attr("y", function(d) {
return h - yScale(d);
})
.attr("width", w/dataset.length - barPadding)
.attr("height", "100%");
需要在同一水平增加2个rects? – Fawzan
就像小提琴上的图像一样,我想要1个矩形作为条形的背景灰色框,然后是另一个代表实际数据点的矩形。 – ditto