【问题标题】:Adding arrows and text to nodes in d3js Force Layout在 d3js 强制布局中向节点添加箭头和文本
【发布时间】:2015-12-21 14:30:37
【问题描述】:

我一直在尝试将文本和箭头添加到 d3js 强制布局图中,但无法获得所需的结果。

我的脚本如下所示:

var width = 600;
height = 400;

var color = d3.scale.category20();

var force = d3.layout.force()
    .charge(-120)
    .linkDistance(100)
    .size([width, height]);

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

d3.json("topology.json", function(error, graph) {
  if (error) throw error;

  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 2*(d.value); });//Math.sqrt

  var node = svg.selectAll(".node")
      .data(graph.nodes)
      .enter().append("circle")
      .attr("class", "node")
      .attr("r", 10)
      .style("fill", function(d) { return color(d.group); })
      .call(force.drag);
  node.append("title")
      .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("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
  });
});

我尝试添加以下sn-p来添加文字

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

示例json数据为

{
    "nodes": [{
        "name": "0",
        "group": 2
    }, {
        "name": "1",
        "group": 2
    }, {
        "name": "2",
        "group": 2
    }, {
        "name": "3",
        "group": 1
    }],
    "links": [{
        "source": 0,
        "target": 1,
        "value": 1
    }, {
        "source": 1,
        "target": 3,
        "value": 2
    }, {
        "source": 2,
        "target": 3,
        "value": 1
    }]
}

向圆圈和箭头添加文本的任何帮助都会很棒。对不起,如果我听起来很菜鸟。

谢谢:) :)

【问题讨论】:

  • 你见过这个example吗,它包含箭头和文字...
  • 谢谢马克。是的,我查看了它并添加了上面添加文本的 sn-p。但不幸的是没有显示文字:(
  • 再仔细看看那个例子。您正在向圆圈附加 circle 和 text,这是格式错误的 SVG。该示例附加一个g(组),然后将circle 和text 附加到它。

标签: javascript html d3.js


【解决方案1】:

Mobashyr,我已使用以下示例来满足我的要求:

// Restart the force layout.
  var force.nodes(nodes) 
        .links(links)
        .charge(-1000)
        .linkDistance(120)
        .alpha(-15)
        .start();   

    var link = vis.selectAll(".link")
        .data(links);

    link.enter().insert("svg:line", ".node")
        .attr("class", "link")
        .style("stroke", "#ccc")
        .attr("cursor", "pointer")
        .style("stroke-width", "0")
        .style("stroke-width", function(d) { return d.weight;})
        .on("mouseover", function() { d3.select(this).style("stroke", "#555555").attr("stroke-opacity", "1.0").attr("stroke-width","10");})
        .on("mouseout", function() { d3.select(this).style("stroke", "#ccc").attr("stroke-opacity", "1.0").attr("stroke-width","4") });

    link.exit().remove();

    var node = vis.selectAll("g.node")
        .data(nodes)

    var groups = node.enter().append("g")
        .attr("class", "node")
        .attr("id", function (d) {
            return d.entityType;
        })
        .on('click', click)

    groups.append("circle")
       .attr("cursor", "pointer")
       .style("fill", function(d) { return color(d.entityType); })
       .style("fill", "#fff")
       .style("stroke-width", "0")
       .style("stroke", "#ddd")
     .attr("r", 20);

 groups.append("text")
        .attr("dy", 18)
        .style("font-size", "2.5px")
        .style("text-anchor", "middle")
        .style('fill','#000')
        .attr("refX", 15)
        .attr("refY", -1.5)
        .text(function (d) {
            return d.text;
        });

node.exit().remove();

 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) {
            return "translate(" + d.x + "," + d.y + ")";
        });

【讨论】:

    猜你喜欢
    • 2016-11-13
    • 1970-01-01
    • 2015-12-04
    • 2012-03-21
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    相关资源
    最近更新 更多