Adding label to the links in D3 force directed graph

analyticalpicasso

I have created a force directed graph but I'm unable to add text to the links created.

How can I do so?

Following is my code link

I have used the following line to append the titles on the link's, but its not coming.

link.append("title")
  .text(function (d) {
     return d.value;
   });

What am I doing wrong with this ?

VividD

This link contains the solution that you need.

enter image description here

The key point here is that "title" adds tooltip. For label, you must provide slightly more complex (but not overly complicated) code, like this one from the example from the link above:

// Append text to Link edges
var linkText = svgCanvas.selectAll(".gLink")
    .data(force.links())
    .append("text")
    .attr("font-family", "Arial, Helvetica, sans-serif")
    .attr("x", function(d) {
        if (d.target.x > d.source.x) {
            return (d.source.x + (d.target.x - d.source.x)/2); }
        else {
            return (d.target.x + (d.source.x - d.target.x)/2); }
    })
    .attr("y", function(d) {
        if (d.target.y > d.source.y) {
            return (d.source.y + (d.target.y - d.source.y)/2); }
        else {
            return (d.target.y + (d.source.y - d.target.y)/2); }
    })
    .attr("fill", "Black")
    .style("font", "normal 12px Arial")
    .attr("dy", ".35em")
    .text(function(d) { return d.linkName; });

The idea of the code is simple: It calculates the midpoint of the link, and displays some text at that place (you can decide what that text actually is). There are some additional calculations and conditions, you can figure it out from the code, however you'll anyway want to change them depending on your needs and aesthetics.

EDIT: Important note here is that "gLink" is the name of the class of links, previously defined with this code:

// Draw lines for Links between Nodes
var link = svgCanvas.selectAll(".gLink")
    .data(force.links())

In your example, it may be different, you need to adjust the code.



Here is a guide how to incorporate solution from example above to another example of force layout that doesn't have link labels:

SVG Object Organization and Data Binding

In D3 force-directed layouts, layout must be supplied with array of nodes and links, and force.start() must be called. After that, visual elements may be created as requirements and desing say. In our case, following code initializes SVG "g" element for each link. This "g" element is supposed to contain a line that visually represent link, and the text that corresponds to that link as well.

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

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

var linkText = svg.selectAll(".link")
    .append("text")
    .attr("class", "link-label")
   .attr("font-family", "Arial, Helvetica, sans-serif")
    .attr("fill", "Black")
    .style("font", "normal 12px Arial")
    .attr("dy", ".35em")
    .attr("text-anchor", "middle")
    .text(function(d) {
        return d.value;
    });

"g" elements have class "link", lines have class "link-line", ad labels have class "link-label". This is done so that "g" elements may be easily selected, and lines and labels can be styled in CSS file conveninetly via classes "link-line" and "link-label" (though such styling is not used in this example).

Initialization of positions of lines and text is not done here, since they will be updated duting animation anyway.

Force-directed Animation

In order for animation to be visible, "tick" function must contain code that determine position of lines and text:

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

    linkText
        .attr("x", function(d) {
            return ((d.source.x + d.target.x)/2);
        })
        .attr("y", function(d) {
            return ((d.source.y + d.target.y)/2);
        });

Here is the resulting example: plunker

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

d3 force directed graph downward force simulation

From Dev

Introducing Arrow(directed), in Force Directed Graph d3

From Dev

D3js - Force directed graph - advanced highlighting of neigbour nodes and links, is it possible?

From Dev

D3 force graph rendering nodes and links

From Dev

The links between the nodes in Force-Directed Graph in D3 Javascript

From Dev

Dynamically adding nodes to d3.js Force directed graph

From Dev

semantic zooming of the force directed graph in d3

From Dev

Iteratively appending links to a D3 force directed network visualization

From Dev

modifying the d3 force-directed graph example

From Dev

Adding OnClick event to D3 force layout graph

From Dev

Unable to Delete links from a D3 force directed graph with arrows

From Dev

d3 force directed graph remove text cursor

From Dev

d3 force directed graph don't select text

From Dev

d3 force directed graph, links not being drawn

From Dev

"Pinning" nodes in a D3 force-directed graph

From Dev

How to render links as elbow connectors in d3 force directed graph

From Dev

D3 force directed graph moving text

From Dev

d3 force graph using node names for links

From Dev

D3v4 force directed graph - localStorage disconnects links and nodes

From Dev

D3 Force Directed Graph ajax update

From Dev

D3 force-directed graph: update node position

From Dev

D3 force directed graph: Performance issue in a complex graph

From Dev

Dynamically adding nodes to d3.js Force directed graph

From Dev

modifying the d3 force-directed graph example

From Dev

Load JSON object into D3 Force Directed Graph

From Dev

Dynamically change color of D3 Node (force directed graph)

From Dev

D3 force directed graph direction

From Dev

D3 Force directed Graph: Why don't the links appear and why is the simulation on the left?

From Dev

d3.js force directed graph: How to make node size depends on the value of the links?

Related Related

  1. 1

    d3 force directed graph downward force simulation

  2. 2

    Introducing Arrow(directed), in Force Directed Graph d3

  3. 3

    D3js - Force directed graph - advanced highlighting of neigbour nodes and links, is it possible?

  4. 4

    D3 force graph rendering nodes and links

  5. 5

    The links between the nodes in Force-Directed Graph in D3 Javascript

  6. 6

    Dynamically adding nodes to d3.js Force directed graph

  7. 7

    semantic zooming of the force directed graph in d3

  8. 8

    Iteratively appending links to a D3 force directed network visualization

  9. 9

    modifying the d3 force-directed graph example

  10. 10

    Adding OnClick event to D3 force layout graph

  11. 11

    Unable to Delete links from a D3 force directed graph with arrows

  12. 12

    d3 force directed graph remove text cursor

  13. 13

    d3 force directed graph don't select text

  14. 14

    d3 force directed graph, links not being drawn

  15. 15

    "Pinning" nodes in a D3 force-directed graph

  16. 16

    How to render links as elbow connectors in d3 force directed graph

  17. 17

    D3 force directed graph moving text

  18. 18

    d3 force graph using node names for links

  19. 19

    D3v4 force directed graph - localStorage disconnects links and nodes

  20. 20

    D3 Force Directed Graph ajax update

  21. 21

    D3 force-directed graph: update node position

  22. 22

    D3 force directed graph: Performance issue in a complex graph

  23. 23

    Dynamically adding nodes to d3.js Force directed graph

  24. 24

    modifying the d3 force-directed graph example

  25. 25

    Load JSON object into D3 Force Directed Graph

  26. 26

    Dynamically change color of D3 Node (force directed graph)

  27. 27

    D3 force directed graph direction

  28. 28

    D3 Force directed Graph: Why don't the links appear and why is the simulation on the left?

  29. 29

    d3.js force directed graph: How to make node size depends on the value of the links?

HotTag

Archive