2017-06-16 196 views
0

我需要在此D3力导向图表中添加文本标签(来自Zoomable Force Directed Graph d3v4 link)。 但是我不明白将标签添加到圆圈和链接所需的编辑。节点标签为属性“node.label”,边缘标签为属性“edge.label”。 这d3 Node Labeling解释了需要编辑,但我一直无法得到它的工作(不幸的是,我不是一个JS程序员)。如何编辑此项,以便可以为节点添加属性“node.label”,并将“edge.label”属性添加到边缘?D3网络图:添加文字

//add encompassing group for the zoom 
var g = svg.append("g") 
    .attr("class", "everything"); 

//draw lines for the links 
var link = g.append("g") 
    .attr("class", "links") 
    .selectAll("line") 
    .data(links_data) 
    .enter().append("line") 
    .attr("stroke-width", 2) 
    .style("stroke", linkColour); 

//draw circles for the nodes 
var node = g.append("g") 
     .attr("class", "nodes") 
     .selectAll("circle") 
     .data(nodes_data) 
     .enter() 
     .append("circle") 
     .attr("r", radius) 
     .attr("fill", circleColour); 

回答

0

你可以做的是让你的节点成为一个圆圈,然后向它添加一个文本。你也必须改变tickActions功能,因此其移动<克>:

图纸节点G:

var node = g.append("g") 
     .attr("class", "nodes") 
     .selectAll(".node") 
     .data(nodes_data) 
     .enter() 
     .append("g") 
     .attr("class","node") 
     .each(function(d){ 
      d3.select(this) 
        .append("circle") 
        .attr("r", radius) 
        .attr("fill", circleColour(d)); 
      d3.select(this) 
        .append("text").text(d.name) 
        .attr("dy",radius+10) 
        .style("text-anchor","middle"); 
     }); 

蜱功能:

function tickActions() { 
    //update node positions each tick of the simulation 
    node 
      .attr("transform", function(d) { return "translate("+d.x+","+ d.y+")"; }); 

    //update link positions 
    link 
      .attr("x1", function(d) { return d.source.x; }) 
      .attr("y1", function(d) { return d.source.y; }) 
      .attr("x2", function(d) { return d.target.x; }) 
      .attr("y2", function(d) { return d.target.y; }); 
} 
+0

我使用d.name为节点,但你可以使用你的数据集属性。你可以做同样的链接。但你明白了。 –