【发布时间】:2015-07-16 06:55:13
【问题描述】:
如何将 self $(this) 参数传递给 d3 选择器?
function d3_bar(self, dataset, barPadding) {
var w = parseInt(d3.select(".barChart").style("width"),10), // select(self)
h = parseInt(d3.select(".barChart").style("height"),10); // select(self)
var svg = d3.select(".barChart") // select(self)
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(d, i) {
return i * (w / dataset.length);
})
.attr("y", function(d) {
return h - (d * 4);
})
.attr("width", w / dataset.length - barPadding)
.attr("height", function(d) {
return d * 4;
})
.attr("fill", function(d) {
return "rgb(0, 0, " + (d * 10) + ")";
});
}
$('.barChart').each(function(i) {
var self = $(this),
nums = self.data('value').split(',').map(Number);
d3_bar(self, nums, 1);
});
【问题讨论】:
-
您是否尝试像
var svg = d3.select(self[0])那样传入元素? -
除非我遗漏了一些东西,否则您可以直接发送至
d3.select(this)。 -
那怎么知道它在哪个起始点呢?
标签: javascript jquery d3.js