javascript - d3js 文本未显示在节点图上

标签 javascript html d3.js

想知道您是否可以提供帮助。我尝试了不同的样式和不同的 d3js 示例,但它们都无法在节点上显示文本。有什么想法吗?

任务确实很简单。在节点顶部显示文本。我真的不知道为什么这不起作用,所以非常感谢您的帮助。

非常感谢您提前提供的帮助。

下面是代码,但为了方便起见,这里是 fiddle http://jsfiddle.net/rg5pmrz1/

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <title>Force Layout Example 1</title>
    <style>

.node {
    fill: #ccc;
    stroke: #fff;
    stroke-width: 2px;
}

.link {
    stroke: #777;
    stroke-width: 2px;
}

.node text {
  font: 10px sans-serif;
  pointer-events: none;
}

text {
  font: 10px sans-serif;
  pointer-events: none;
}

    </style>
</head>
<body>
    <script src='http://d3js.org/d3.v3.min.js'></script>
    <script>

// Define the dimensions of the visualization. We're using
// a size that's convenient for displaying the graphic on
// http://jsDataV.is

var width = window.innerWidth - 20,
    height = innerHeight - 20;

var color = d3.scale.category10();

var force = d3.layout.force()
    .charge(-300)
    .linkDistance(function (l) { return l.value; })
    .gravity(0.03)
    .friction(0.9)
    .size([width, height]);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

var graph = getData();

var nodeMap = {};

graph.nodes.forEach(function(d) { 

    nodeMap[d.name] = d;
    //centering first element   
   if(d.main == true)
   {
        graph.nodes[0].x = width / 2;
        graph.nodes[0].y = height / 2;
    }

});

graph.links.forEach(function(l) {
    l.source = nodeMap[l.source];
    l.target = nodeMap[l.target];
    l.distance = l.value;
    l.size = l.value;
})

force.nodes(graph.nodes)
    .links(graph.links)
    .start();

var link = svg.selectAll(".link")
    .data(graph.links)
    .enter().append("line")
    .attr("class", "link")
    .style("stroke-width", function(d) {
        return 1 / (d.value / 1000);
    });



var node = svg.selectAll(".node")
    .data(graph.nodes)
    .enter().append("circle")
    .attr("class", "node")
    .attr("r", 30)
    .style("fill", function(d) { return color(d.group); })

    .on("mouseover", mouseover)
    .on("mouseout", mouseout)
    .call(force.drag);


node.append("title")
      .attr("dy", ".35em")
      .attr("text-anchor", "middle")
      .text(function(d) { return d.name});

node.append("text")
    .attr("x", 12)
    .attr("dy", ".35em")
    .text(function(d) { return d.name;console.log("name: " + d.name); });

force.on("tick", function() {

    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; });

    node.attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
});

function mouseover() {
    console.log("mouse over");
  d3.select(this).transition()
      .duration(250)
      .attr("r", 60);
}

function mouseout() {
    console.log("mouse out");
  d3.select(this).transition()
      .duration(250)
      .attr("r", 30);
}

function click() {
    console.log("clicky clicky");
  d3.select(this).transition()
      .duration(250)
      .attr("r", 60);

}



function getData() {

  return {
  "nodes":[
      {"name":"John","group":1, "value": 1, "main": true},
    {"name":"Mike","group":2, "value": 2},
    {"name":"Ian","group":1, "value": 3},
    {"name":"James","group":2, "value": 4},
    {"name":"Sarah","group":2,"value": 5}
  ],
  "links":[
    {"source":"John","target":"Mike","value":100},
    {"source":"Ian","target":"John","value":200},
    {"source":"John","target":"James","value":100},
    {"source":"Sarah","target":"John","value":300},
  ] };    

}



    </script>
</body>
</html>

最佳答案

来自重复问题的答案herehere ,这是因为:

You can't add svg text to a svg circle. You should first create an svg g object (g stands for group) for each node, and than add a circle and a text for each g element, like in this code:

jsFiddle 。请注意勾选函数中的附加代码。

var width = window.innerWidth - 20,
    height = innerHeight - 20;

var color = d3.scale.category10();

var force = d3.layout.force()
    .charge(-300)
    .linkDistance(function (l) { return l.value; })
    .gravity(0.03)
    .friction(0.9)
    .size([width, height]);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

var graph = getData();

var nodeMap = {};

graph.nodes.forEach(function(d) { 

    nodeMap[d.name] = d;
    //centering first element   
   if(d.main == true)
   {
        graph.nodes[0].x = width / 2;
        graph.nodes[0].y = height / 2;
    }

});

graph.links.forEach(function(l) {
    l.source = nodeMap[l.source];
    l.target = nodeMap[l.target];
    l.distance = l.value;
    l.size = l.value;
})

force.nodes(graph.nodes)
    .links(graph.links)
    .start();

var link = svg.selectAll(".link")
    .data(graph.links)
    .enter().append("line")
    .attr("class", "link")
    .style("stroke-width", function(d) {
        return 1 / (d.value / 1000);
    });

var node = svg.selectAll(".node")
    .data(graph.nodes)
    .enter().append("g");

var circle = node.append("circle")
    .attr("class", "node")
    .attr("id", function (d) { return d.name; })
    .attr("r", 30)
    .style("fill", function (d) {
        return color(d.group);
    })
    .on("mouseover", mouseover)
    .on("mouseout", mouseout)
    .call(force.drag);

var label = node.append("svg:text")
    .text(function(d) { return d.name; });

force.on("tick", function() {
    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; });

    node.attr("transform", function(d) {
        console.log(d);
        return 'translate(' + [d.x, d.y] + ')'; 
    });
});

function mouseover() {
    console.log("mouse over");
  d3.select(this).transition()
      .duration(250)
      .attr("r", 60);
}

function mouseout() {
    console.log("mouse out");
  d3.select(this).transition()
      .duration(250)
      .attr("r", 30);
}

function click() {
    console.log("clicky clicky");
  d3.select(this).transition()
      .duration(250)
      .attr("r", 60);
}



function getData() {

  return {
  "nodes":[
      {"name":"John","group":1, "value": 1, "main": true},
    {"name":"Mike","group":2, "value": 2},
    {"name":"Ian","group":1, "value": 3},
    {"name":"James","group":2, "value": 4},
    {"name":"Sarah","group":2,"value": 5}
  ],
  "links":[
    {"source":"John","target":"Mike","value":100},
    {"source":"Ian","target":"John","value":200},
    {"source":"John","target":"James","value":100},
    {"source":"Sarah","target":"John","value":300},
  ] };    

}

关于javascript - d3js 文本未显示在节点图上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29112197/

相关文章:

javascript - MongoDB 中的通配符搜索

html - 添加背景图像后 Div 元素未正确对齐

javascript - 如何将拖放结果存储为图像

html - 如何让图像显示在面板的左侧(都在同一行)

javascript - 使用 d3js 根据用户选择调用备用图表绘制函数

javascript - 使用 D3.js 单击元素后防止鼠标移出操作

javascript - D3 节点的标签和图像错误地显示在屏幕的左上角

javascript - 如何扩展正则表达式对象

javascript - 类型错误 : dispatch is not a function when I try to test a method using JEST

javascript - 在多个文件中声明时,使函数在 Javascript IFFE 中公开