我正在尝试绘制点并添加抖动。这些点有文本标签,所以一旦我计算了抖动的点位置,我想保存它并将其用于文本标签。我想为每个点添加一个新的抖动属性,然后用它来设置cx(点)和x(标签):
# compute the jittered positions
points.attr("x_jitter", function (d){
return x_scale(d.x) + my_random_jitter_function()
});
# set them to the points and the labels
points.attr("cx", function (d, i){
return points.attr("x_jitter")
});
text_labels.attr("x", function(d, i){
return points.attr("x_jitter")
});
有更好的方法吗?
答案 0 :(得分:3)
我会做的
points.each(function(d,i) {
d.jitter = xscale(d.x) + random_jitter();
})
points.attr("cx", function(d,i) { return d.jitter });
text_labels.attr("x", function(d,i) { return d.jitter });