d3.brushSelection()的正确参数是什么?

Jon*_*Jon 6 d3.js

我正在尝试获取一维画笔的选择值,但在理解d3.brushSelection()的参数时遇到了麻烦。我看到文档中说该参数应该是一个节点,但是我不确切知道它的含义。它应该是调用画笔的特定HTML元素,还是保存画笔的svg元素?我都尝试过,并且都返回null。

var xBrush = d3.brushX()
  .extent([[0,0], [xWidth,xHeight]])
  .on("brush", brushed);

xChart.append("g")
  .attr("class", "brush")
  .call(xBrush);
Run Code Online (Sandbox Code Playgroud)

如果这是我创建画笔的方式,那么如何获得选择的值?谢谢。

小智 7

参数中所需的节点d3.brushSelection是与g笔刷相对应的元素。

因此,您可以通过以下方式访问选择

d3.brushSelection(d3.select(".brush").node())

或者,对您的代码稍作更改,

var xBrush = d3.brushX()
  .extent([[0,0], [xWidth,xHeight]])
  .on("brush", brushed);

var theBrush = xChart.append("g")
  .attr("class", "brush")
  .call(xBrush);

d3.brushSelection(theBrush.node());
Run Code Online (Sandbox Code Playgroud)

请注意,如果笔刷的选择为空,则该函数返回null