javascript - d3.js-翻译<g>后如何添加<circle>

标签 javascript d3.js

我正在尝试添加新的<circle>之后<g>被翻译了。 circle坐标我正在尝试使用鼠标事件 mousedown函数得到 在翻译之前协调我可以正确添加 <circle>在 mousedown 坐标中但翻译后 <g>我无法正确添加 <circle> 在鼠标按下的坐标中。

所以我想在<g>之后已翻译,我需要重新定义新坐标。但我只是不知道该怎么做!

这是我的代码(我使用鼠标右键添加圆圈):
JSbin

最佳答案

您需要找到相对于组平移的鼠标位置。

  var point = document.getElementById('root').createSVGPoint();
  point.x = event.pageX;//mouse position X
  point.y = event.pageY;//mouse position Y
  var newPoint = point.matrixTransform(container.node().getCTM().inverse());
  //newPoint is the place where you will need to draw the circle
      container.append('g')
              .append('circle')
              .attr('cx', newPoint.x)
              .attr('cy', newPoint.y)
              .attr('r', '20');

工作代码here

希望这有帮助!

关于javascript - d3.js-翻译<g>后如何添加<circle>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33512926/

相关文章:

d3.js - D3 删除 exit 返回的元素的父元素

javascript - d3.js 3D数组插值

javascript - 将 d3.js 代码设置为指向我的 .csv 而不是#csv?

javascript - 从 JS 调用 html 中的图标

javascript - 无法读取未定义的属性 'action'

javascript - 未捕获的类型错误 : undefined is not a function on using jQuery. 惰性()

javascript - 无法在 D3 旋转地球仪上绘制折线

javascript - Ajax Json 属性未定义

javascript - 如何在预先存在的折线上放置标记?

jquery - 具有多个数据和各个数据点作为图例的堆叠条形图