【问题标题】:How to add jitter to a d3 plot?如何将抖动添加到 d3 图?
【发布时间】:2013-06-13 17:59:53
【问题描述】:

我正在尝试绘制点并添加它们的抖动。这些点有文本标签,所以一旦我计算出抖动点的位置,我想保存它并将其用于文本标签。我正在考虑为每个点添加一个新的 jitter 属性,然后使用它来设置 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")
});

有没有更好的办法?

【问题讨论】:

  • 您可以将点和标签放在g 元素中,并将抖动添加到其中,而无需存储它。
  • @LarsKotthoff 这很优雅!我可以将它们添加到 g 元素并使用 x_jitter 翻译它们。谢谢!

标签: d3.js label


【解决方案1】:

我会做的

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

【讨论】:

    猜你喜欢
    • 2015-04-16
    • 1970-01-01
    • 2017-04-23
    • 2015-05-29
    • 2020-05-07
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多