D3.js D3力布局:旋转文本
我正在使用一个示例,该示例用于在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;
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)