D3.js, force-graph, cannot display text/label of nodes

自作多情 提交于 2019-11-28 07:41:08

问题


I cannot display labels of nodes using a force-layout in d3.js.

I'm trying with this example http://d3js.org/d3.v3.min.js

I updated that code only adding zoom, like this:

var svg = d3.select("body").append("svg").attr("width", width).attr("height", height).append('svg:g').call(d3.behavior.zoom().on("zoom", redraw));

function redraw() {
    console.log("here", d3.event.translate, d3.event.scale);
    svg.attr("transform", "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")");
    node.attr("transform", function(d) {
        return "translate(" + d.x + "," + d.y + ")";
    });
}

Why are labels not displayed?


回答1:


You need to separately add the text:

node.append("text")
    .attr("dx", ".10em")
    .attr("dy", ".10em")
    .text(function(d) { return d.name; });

see these examples:

http://bl.ocks.org/mbostock/2706022

http://bl.ocks.org/mbostock/1153292



来源:https://stackoverflow.com/questions/15714738/d3-js-force-graph-cannot-display-text-label-of-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!