如何添加抖动到d3图? [英] How to add jitter to a d3 plot?
本文介绍了如何添加抖动到d3图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图绘制点和添加他们的抖动。点有文本标签,所以一旦计算抖动点位置,我想保存它,并将其用于文本标签。我想在每个点添加一个新的抖动属性,然后使用它来设置cx(点)和x(标签):
#计算抖动位置
points.attr(x_jitter,function(d){
return x_scale(dx)+ my_random_jitter_function()
}
#将它们设置为点和标签
points.attr(cx,function(d,i){
return points.attr(x_jitter)
} );
text_labels.attr(x,function(d,i){
return points.attr(x_jitter)
});有没有更好的方法?
< >解决方案我会做
points.each(function(d,i){
d.jitter = xscale(dx)+ random_jitter();
})
points.attr(cx,function(d,i){return d.jitter});
text_labels.attr(x,function(d,i){return d.jitter});
I'm trying to plot points and adding them jitter. The points have text labels, so once I compute the jittered point position, I'd like to save it and use it for the text label. I was thinking of adding a new jitter attribute to each point and then using it to set cx (points) and x (labels):
# 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")
});
Is there a better way?
解决方案 I would just do
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 });
这篇关于如何添加抖动到d3图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文