Fine-grained event handling with D3 brushes

前端 未结 2 866
刺人心
刺人心 2020-12-25 08:37

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.

2条回答
  •  时光说笑
    2020-12-25 08:53

    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"]);
    

提交回复
热议问题