Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
D3.js D3力布局:旋转文本_D3.js_Svg - Fatal编程技术网

D3.js D3力布局:旋转文本

D3.js D3力布局:旋转文本,d3.js,svg,D3.js,Svg,我正在使用一个示例,该示例用于在d3的强制布局中旋转标签。我正在尝试显示标签文本“使用Math.atan2而不是Math.atan labelLine.attr("transform", function(d) { if (d) { const dsty = d.source.y - d.target.y; let angle = Math.atan2(dsty, (d.source.x - d.target.x)) * 180 / Math.PI;

我正在使用一个示例,该示例用于在d3的强制布局中旋转标签。我正在尝试显示标签文本“使用
Math.atan2
而不是
Math.atan

labelLine.attr("transform", function(d) {
    if (d) {
      const dsty = d.source.y - d.target.y;
      let angle = Math.atan2(dsty, (d.source.x - d.target.x)) * 180 / Math.PI;
      return 'translate(' + [((d.source.x + d.target.x) / 2), ((d.source.y + d.target.y) / 2)] + ')rotate(' + angle + ')';
    }
  });

使用
Math.atan2
代替
Math.atan

labelLine.attr("transform", function(d) {
    if (d) {
      const dsty = d.source.y - d.target.y;
      let angle = Math.atan2(dsty, (d.source.x - d.target.x)) * 180 / Math.PI;
      return 'translate(' + [((d.source.x + d.target.x) / 2), ((d.source.y + d.target.y) / 2)] + ')rotate(' + angle + ')';
    }
  });

非常感谢您的解决方案!更新了小提琴:以显示实际的解决方案。如果有人想知道为什么它与atan2一起工作而不是完全与atan一起工作,请看这里:(对atan2/atan的解释也适用于javascript)非常感谢这个解决方案!更新了小提琴:以显示实际的解决方案。如果有人想知道为什么它可以使用atan2,而不能完全使用atan,请看这里:(对atan2/atan的解释也适用于javascript)