如何将抖动添加到 d3 图? [英] How to add jitter to a d3 plot?

查看:31
本文介绍了如何将抖动添加到 d3 图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制点并添加它们的抖动.这些点有文本标签,所以一旦我计算出抖动的点位置,我想保存它并将其用作文本标签.我正在考虑为每个点添加一个新的 jitter 属性,然后使用它来设置 cx(点)和 x(标签):

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")
});

有没有更好的办法?

推荐答案

我会做

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆