标记的节点,D3.js边缘上的箭头

时间:2017-08-03 09:53:05

标签: javascript d3.js

我正在研究d3.js并面临两个问题。我无法标记节点并在边缘显示箭头。

这是我的代码:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.links line {
  stroke: #999;
  stroke-opacity: 0.6;
}

.nodes circle {
  stroke: #fff;
  stroke-width: 1.5px;
}

</style>
<svg width="960" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

var graph = {
  "nodes": [
    {"number": "3006307180"},
    {"number": "3215838129"},
    {"number": "3216716348"},
    {"number": "3217209263"},
    {"number": "3212901630"},
    {"number": "3035289939"}
  ],
  "links": [
    {"source": "3006307180", "target": "3215838129"},
    {"source": "3216716348", "target": "3215838129"},
    {"source": "3216716348", "target": "3217209263"},
    {"source": "3212901630", "target": "3217209263"},
    {"source": "3212901630", "target": "3035289939"}
  ]
};

var svg = d3.select("svg"),
    width = +svg.attr("width"),
    height = +svg.attr("height");
var color = d3.scaleOrdinal(d3.schemeCategory20);

var simulation = d3.forceSimulation()
    .force("link", d3.forceLink().id(function(d) { return d.number; }))
    .force("charge", d3.forceManyBody()
        .strength(-400))
    .force("center", d3.forceCenter(width / 2, height / 2));



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

var node = svg.append("g")
    .attr("class", "nodes")
    .selectAll("circle")
    .data(graph.nodes)
    .enter().append("circle")
    .attr("r", 15)
    .attr("fill", function(d) { return color(d.group); })
    .call(d3.drag()
        .on("start", dragstarted)
        .on("drag", dragged)
        .on("end", dragended));

node.append("title")
    .text(function(d) { return d.number; });


simulation
    .nodes(graph.nodes)
    .on("tick", ticked);

simulation.force("link")
    .links(graph.links);

function ticked() {
    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 dragstarted(d) {
  if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  d.fx = d.x;
  d.fy = d.y;
}

function dragged(d) {
  d.fx = d3.event.x;
  d.fy = d3.event.y;
}

function dragended(d) {
  if (!d3.event.active) simulation.alphaTarget(0);
  d.fx = null;
  d.fy = null;
}

</script>

这一行node.append("title").text(function(d) { return d.number; });它清楚地显示了鼠标悬停在工具提示上的节点标签但是我想在节点旁边的OR旁边显示标签。我试图用文本替换标题,没有任何反应。

第二个问题是我需要显示箭头,为此我使用:

svg.append("g").selectAll("marker")
    .data(["end"])   
    .enter().append("svg:marker")
    .attr("id", String)
    .attr("viewBox", "0 -5 10 10")
    .attr("refX", 15)
    .attr("refY", -1.5)
    .attr("markerWidth", 6)
    .attr("markerHeight", 6)
    .attr("orient", "auto")
    .append("svg:path")
    .attr("d", "M0,-5L10,0L0,5");

并在链接.attr("marker-end", "url(#end)");中添加了此属性,但边缘上没有显示任何内容。

这是几乎所有与我的问题相关的问题的答案。我几乎完成了所有问题,但没有解决我的问题。 请问任何想法,怎么做?我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

在SVG中,圈子不是容器元素。这意味着您无法将文本元素附加到圆形元素。

此处的传统解决方案正在转变node一个群组选择,您可以在其中附加圈子和文字:

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

var circle = node.append("circle")
  .attr("r", 15)
  .attr("fill", function(d) {
    return color(d.group);
  }).call(d3.drag()
    .on("start", dragstarted)
    .on("drag", dragged)
    .on("end", dragended));

var text = node.append("text")
  .text(function(d) {
    return d.number;
  });

然后,在ticked函数内,翻译整个组。

以下是您更改的代码:

&#13;
&#13;
var graph = {
  "nodes": [{
    "number": "3006307180"
  }, {
    "number": "3215838129"
  }, {
    "number": "3216716348"
  }, {
    "number": "3217209263"
  }, {
    "number": "3212901630"
  }, {
    "number": "3035289939"
  }],
  "links": [{
    "source": "3006307180",
    "target": "3215838129"
  }, {
    "source": "3216716348",
    "target": "3215838129"
  }, {
    "source": "3216716348",
    "target": "3217209263"
  }, {
    "source": "3212901630",
    "target": "3217209263"
  }, {
    "source": "3212901630",
    "target": "3035289939"
  }]
};

var svg = d3.select("svg"),
  width = +svg.attr("width"),
  height = +svg.attr("height");
var color = d3.scaleOrdinal(d3.schemeCategory20);

var simulation = d3.forceSimulation()
  .force("link", d3.forceLink().id(function(d) {
    return d.number;
  }))
  .force("charge", d3.forceManyBody()
    .strength(-400))
  .force("center", d3.forceCenter(width / 2, height / 2));



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

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

var circle = node.append("circle")
  .attr("r", 15)
  .attr("fill", function(d) {
    return color(d.group);
  })
  .call(d3.drag()
    .on("start", dragstarted)
    .on("drag", dragged)
    .on("end", dragended));

var text = node.append("text")
  .attr("dx", "1em")
  .text(function(d) {
    return d.number;
  });


simulation
  .nodes(graph.nodes)
  .on("tick", ticked);

simulation.force("link")
  .links(graph.links);

function ticked() {
  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 + ")";
    });
}

function dragstarted(d) {
  if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  d.fx = d.x;
  d.fy = d.y;
}

function dragged(d) {
  d.fx = d3.event.x;
  d.fy = d3.event.y;
}

function dragended(d) {
  if (!d3.event.active) simulation.alphaTarget(0);
  d.fx = null;
  d.fy = null;
}
&#13;
.links line {
  stroke: #999;
  stroke-opacity: 0.6;
}

.nodes circle {
  stroke: #fff;
  stroke-width: 1.5px;
}
&#13;
<svg width="600" height="400"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

带有标记节点和边缘箭头的上述问题的工作代码

我想,我应该在这里发布我的完整解决方案。

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.links line {
  stroke: #999;
  stroke-width: 3px;
  stroke-opacity: 0.6;
}

.nodes circle {
  stroke: #fff;
  stroke-width: 1.5px;
}

</style>
<svg width="1000" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>

var graph = {
  "nodes": [
    {"number": "3006307180"},
    {"number": "3215838129"},
    {"number": "3216716348"},
    {"number": "3217209263"},
    {"number": "3212901630"},
    {"number": "3035289939"}
  ],
  "links": [
    {"source": "3006307180", "target": "3215838129", "value": "1440"},
    {"source": "3216716348", "target": "3215838129", "value": "19870"},
    {"source": "3216716348", "target": "3217209263", "value": "6177"},
    {"source": "3212901630", "target": "3217209263", "value": "17236"},
    {"source": "3212901630", "target": "3035289939", "value": "600"}
  ]
};

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

var color = d3.scaleOrdinal(d3.schemeCategory20c);

var simulation = d3.forceSimulation()
    .force("link", d3.forceLink().id(function(d) { return d.number; }))
    .force("charge", d3.forceManyBody()
        .strength(-400))
    .force("center", d3.forceCenter(width / 2, height / 2));



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

var node = svg.append("g")
    .attr("class", "nodes")
    .selectAll("circle")
    .data(graph.nodes)
    .enter().append("circle")
    .attr("r", 15)
    .attr("fill", function(d) { return color(d.group); })
    .call(d3.drag()
        .on("start", dragstarted)
        .on("drag", dragged)
        .on("end", dragended));

var label = svg.selectAll(".mytext")
    .data(graph.nodes)
    .enter()
    .append("text")
    .text(function (d) { return d.number; })
    .style("text-anchor", "middle")
    .style("fill", "#555")
    .style("font-family", "Arial")
    .style("font-size", 10);


simulation
    .nodes(graph.nodes)
    .on("tick", ticked);

simulation.force("link")
    .links(graph.links);

svg.append("svg:defs").selectAll("marker")
    .data(["end"])
    .enter().append("svg:marker")
    .attr("id", String)
    .attr("viewBox", "0 -5 10 10")
    .attr("refX", 18)
    .attr("refY", 0)
    .attr("markerWidth", 6)
    .attr("markerHeight", 6)
    .attr("orient", "auto")
    .append("svg:path")
    .attr("d", "M0,-5L10,0L0,5");

function ticked() {
    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; })
        .attr("marker-end", "url(#end)");

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

    label
        .attr("x", function(d){ return d.x; })
        .attr("y", function (d) {return d.y - 10; });
}

function dragstarted(d) {
  if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  d.fx = d.x;
  d.fy = d.y;
}

function dragged(d) {
  d.fx = d3.event.x;
  d.fy = d3.event.y;
}

function dragended(d) {
  if (!d3.event.active) simulation.alphaTarget(0);
  d.fx = null;
  d.fy = null;
}

</script>
相关问题