Noob Player
Noob Player

Reputation: 289

Labeled nodes, arrow on edges in D3.js

I am working on d3.js and facing two issue. I am unable to label nodes and show arrow on edges.

Here is my code :

<!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>

Here on this line node.append("title").text(function(d) { return d.number; }); It clearly show the nodes label on mouse over in tool tip BUT I want to show label beside OR on node. I have tried to replace title with text and nothing happens.

Second issue is I need to show arrow, for this I use:

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

and added this attribute in links .attr("marker-end", "url(#end)"); BUT nothing shows on edges.

These are the answers from almost all of the questions asked here related to my issue. I have gone through almost all of the questions BUT nothing solves my problem. Please any idea, how to do it ? What i am doing wrong here ?

Upvotes: 1

Views: 767

Answers (2)

Noob Player
Noob Player

Reputation: 289

Working code for above question with labeled nodes and arrows on edges

Thought, I Should post my complete solution my self here.

<!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>

Upvotes: 0

Gerardo Furtado
Gerardo Furtado

Reputation: 102219

In SVG, circles are not container elements. That means that you cannot append a text element to a circle element.

The traditional solution here is turning node a group selection, to which you can append both circles and texts:

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

Then, inside the ticked function, translate the whole groups.

Here is your code with that change:

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;
}
.links line {
  stroke: #999;
  stroke-opacity: 0.6;
}

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

Upvotes: 1

Related Questions