Attaching 'onclick' event to D3 chart background

后端 未结 2 1373
旧巷少年郎
旧巷少年郎 2021-01-31 13:54

I have a D3 histogram chart, onto which I have attached an \'onclick\' event to the bars:

...
var bar = svg.selectAll(\".bar\")
        .data(data)
        .ente         


        
2条回答
  •  温柔的废话
    2021-01-31 14:43

    In this example (line 246: http://tributary.io/inlet/8361294) I append a new rect with width & height equal to the total chart area, then attach my mouse (or click) events.

    svg.append("rect")
            .attr({"class": "overlay" , "width": width , "height": height})
            .on({
              "mouseover": function() { /* do stuff */ },
              "mouseout":  function() { /* do stuff */ }, 
              "click":  function() { /* do stuff */ }, 
            });
    

提交回复
热议问题