【问题标题】:How do I get the coordinate of a d3 line? [duplicate]如何获得 d3 线的坐标? [复制]
【发布时间】:2019-09-12 11:32:57
【问题描述】:

这似乎是一件非常简单的事情,但我似乎无法弄清楚。假设我有这样的事情:

svg.append('line')
  .attr('y1', function(d) { return d.value; });

(还有其他东西补充,但本题不需要)

所以基本上我将行的 y1 值设置为从我的数据中获取的数字。但是,如果我需要它用于 if 语句之类的其他东西,我该如何获取该值:

if ( (the value of y1) < 20 ) {
  Do something
}

【问题讨论】:

    标签: d3.js coordinates


    【解决方案1】:

    没关系,我想通了。只需使用您定义的值编写 .attr,如下所示:

    if (d3.selectAll('line').attr('y1') > 20) {
      Do something
    }
    

    【讨论】:

    • 因为你的getter只会返回first元素的值,你可以使用select而不是selectAll
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 2012-07-22
    • 2020-09-12
    相关资源
    最近更新 更多