I have a scatter plot that is generated using D3. Points (SVG circles) on the plot can be selected by clicking on them and regions can be selected using a D3 brush.
Use selection.on
: http://jsfiddle.net/NH6zD/1
var target,
dimensions = {width: 200, height: 200},
svg = d3.select("body").append("svg").attr(dimensions),
rect = svg.append("rect").attr(dimensions); // Must be beneath circles
svg
.on("mousedown", function() {
target = d3.event.target || d3.event.srcElement;
if ( target === rect.node() ) {
/* Brush */
} else {
/* Circle */
}
})
.on("mousemove", function() {
if (!target) return;
if ( target === svg.rect() ) {
/* Brush */
} else {
var mouse = d3.mouse(svg.node());
target.attr({x: mouse[0], y: mouse[1]});
}
});
(function(exit) {
for (var i in exit) svg.on(exit[i], function() { target = undefined; });
})(["mouseout", "mouseup"]);