Argument for d3.brushSelection()

269 views
Skip to first unread message

Jon

unread,
Jul 21, 2016, 1:48:33 PM7/21/16
to d3-js

I'm trying to get the selection values for a 1-dimensional brush, but I'm having trouble understanding what the argument for d3.brushSelection() should be. I see that the documentation says that the argument should be a node, but I don't know exactly what it means. Is it supposed to be the specific HTML element on which the brush is called on, or the svg element that holds the brush? I have tried both and both return null.


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

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

If this is how I create my brush, how could I get the value of xBrush's selection? Thanks.

Mike Bostock

unread,
Jul 21, 2016, 1:54:45 PM7/21/16
to d3...@googlegroups.com
Within the context of a brush event listener, you can use d3.event.selection.

Elsewhere and more generally you can use d3.brushSelection, passing in a node which you previously applied the brush to. So, in your example:

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

Note that in a brush event listener, this node is the same as the node that has the brush behavior, which is this, so you can instead say:

d3.brushSelection(this)

But that is typically not necessary as you can access it as d3.event.selection.

Mike

Jon

unread,
Jul 21, 2016, 2:11:30 PM7/21/16
to d3-js, mi...@ocks.org
Thank you, that answer was really helpful. 
Reply all
Reply to author
Forward
0 new messages