2016-06-24 76 views
0

我正在嘗試獲取相對於group元素的鼠標座標。下面的代碼:d3.mouse()需要什麼參數

var backRect = chart.append("g").attr("class", "rect"); 
var g = backRect.append('rect') 
     .style('stroke', 'none') 
     .style('fill', '#FFF') 
     .style('fill-opacity', 0) 
     .attr({ 
      width: width, 
      height: height, 
      'pointer-events': 'none', 
      'class': 'backRect' 
     }); 

// the code below is inside another container's event; but I want mouse coordinates relative to the above rect, hence can't use d3.mouse(this) 
// get mouse pointer location 
     var coordinates = [0, 0]; 
     coordinates = d3.mouse(backRect); // d3.select(".rect") does not work either 

但出現以下錯誤:

d3.min.js:1 Uncaught TypeError: n.getBoundingClientRect is not a function

按照D3 mouse文檔d3.mouse()需要可以是svgg元素的容器。

我應該將哪個參數傳遞給d3.mouse()?我試過d3.select(".rect"),這也不起作用。

回答

1

使用d3.mouse(backRect.node())的伎倆。

+1

接受你的答案(你可以做到這一點) –

+1

是的,我會一次48小時過去了.. :)謝謝 – akshayKhot

0

您應該在事件中使用d3.mouse()以獲取相對於傳遞容器的值。

檢查此塊

http://bl.ocks.org/hlucasfranca/f133da4493553963e710

svg.on("click", function() { 
      var coords = d3.mouse(this); 
      ........ 
      ........ 
}) 
+0

感謝您的反饋,但我已經在事件中使用'd3.mouse()'(上面的代碼只是原始的乾淨版本),並且需要相對於不同容器(backRect)的鼠標座標。謝謝 – akshayKhot

+0

你應該編輯你的問題來清楚說明。 'd3.mouse(backRect)'似乎不是事件函數的一部分。 – Sundeep

+0

更新了代碼 – akshayKhot

相關問題