想要实现的效果(文字长度不固定,初始位置居中;节点拖动连线,文字随连线变化居中): 【图 1】现在实现的效果(文字初始位置未居中,不会随着连线的变化而变化): 【图 1】请问如何修改代码来实现【图 1】 效果:a. 文字初始位置居中; b.连线伸缩时,文字会自适应居中?
4 回答
![?](http://img1.sycdn.imooc.com/5333a0aa000121d702000200-100-100.jpg)
王益达
TA贡献12条经验 获得超15个赞
问题已自行解决:
lineText.attr({ 'dx': d => getLineTextDx(d) });
function getLineTextDx(d) { const sr = d.radius; const sx = d.source.x; const sy = d.source.y; const tx = d.target.x; const ty = d.target.y; const distance = Math.sqrt(Math.pow(tx - sx, 2) + Math.pow(ty - sy, 2)); const textLength = d.alllabel.length; const deviation = 8; // 调整误差 const dx = (distance - sr - textLength * lineTextFontSize) / 2 + deviation; return dx; }
添加回答
举报
0/150
提交
取消