D3.js不重叠行,但链接文本不显示


D3.js Don't Overlapping line but linktext not show

不重叠行,但链接文本不显示

我在 stackoverflow.com 的问题如何在 D3 中显示许多按值分隔的链接.js强制有向图

将来我认为线

可能会重叠,但我改变了博士公式,因为线是半圆形曲线。

我搜索如何用另一种方式创建曲线。我看到 https://bl.ocks.org/mbostock/4600693.我尝试将其应用于我的数据。查看我的新演示 http://bl.ocks.org/Lovekiizzk/ab6fdec08beef4999839

//Appends link 
var link = svg.selectAll(".link")
.data(links)
.enter()
.append("g") // circle
.attr("class", "link")
.append("polyline") //if "line" It not show path
.attr("class", "link-line")
.style("stroke-width", 1)
//.attr("fill", "Black")
//.style("stroke-width", function (d) {  return Math.sqrt(d.value); })
.style("marker-mid",  "url(#end)");
//Appends link text
var linkText = svg.selectAll(".link")
.append("text")
.attr("class", "link-label")
//.attr("font-family", "tahoma, verdana, sans-serif")
.attr("fill", "Black")
.style("font", "normal 10px tahoma")
.attr("dy", ".15em")
.attr("text-anchor", "middle")
.text(function(d) { return d.value; });  

现在,行(链接)不重叠。很好。节点上的文本,图像而不是圆形和链接按方向箭头显示在我的图表中。 但链接文本未显示。

我想按 d.value 显示链接文本

 "links":[
{"source":1,"target":3,"prop":"coach","value":"coach"},
{"source":4,"target":2,"prop":"title","value":"title"},
{"source":4,"target":0,"prop":"menCurrent","value":"menCurrent"},
{"source":3,"target":1,"prop":"coachplayers","value":"coachplayers"},
{"source":5,"target":0,"prop":"bronze","value":"bronze"},
{"source":5,"target":1,"prop":"gold","value":"gold"},
{"source":5,"target":1,"prop":"goldMedalist","value":"goldMedalist"},
{"source":5,"target":1,"prop":"TestRelation3","value":"TestRelation3"},
{"source":5,"target":0,"prop":"bronzeMedalist","value":"bronzeMedalist"},
{"source":6,"target":0,"prop":"menCurrent","value":"menCurrent"},
{"source":6,"target":2,"prop":"title","value":"title"},
{"source":7,"target":0,"prop":"caption","value":"caption"},
{"source":7,"target":1,"prop":"caption","value":"caption"},
{"source":3,"target":2,"prop":"coachtournamentrecord","value":"coachtournamentrecord"}
]

但它不是D不显示

请告诉我为什么。我的英语很差。希望你不要介意。谢谢

您的问题是,当您执行svg.selectAll以获取所有.link元素时,不再有任何数据连接到您的选择。

试试这个:

//Appends link 
var link = svg.selectAll(".link")
.data(links)
.enter()
.append("g") // circle
.attr("class", "link");
//Append circle
link.append("polyline") //if "line" It not show path
.attr("class", "link-line")
.style("stroke-width", 1)
//.attr("fill", "Black")
//.style("stroke-width", function (d) {  return Math.sqrt(d.value); })
.style("marker-mid",  "url(#end)");
//Appends link text
link.append("text")
.attr("class", "link-label")
//.attr("font-family", "tahoma, verdana, sans-serif")
.attr("fill", "Black")
.style("font", "normal 10px tahoma")
.attr("dy", ".15em")
.attr("text-anchor", "middle")
.text(function(d) { return d.value; });