How do i add two different shapes to D3 forced directed graph based on shape field value?

Maddy Chavda

I am newbie to D3. I am using force directed graph. I want to add two different type of shapes at node's places.

My json is following:

{
  "nodes":[
    {"name":"00:00:00:00:00:00:00:01","group":0,"shape":1},
    {"name":"00:00:00:00:00:00:00:02","group":1,"shape":1},
    {"name":"00:00:00:00:00:00:00:03","group":2,"shape":1},
    {"name":"00:00:00:00:00:00:00:11","group":0,"shape":0},
    {"name":"00:00:00:00:00:00:00:21","group":1,"shape":0},
    {"name":"00:00:00:00:00:00:00:31","group":2,"shape":0},
    {"name":"00:00:00:00:00:00:00:32","group":2,"shape":0},
    {"name":"00:00:00:00:00:00:00:12","group":0,"shape":0},
    {"name":"00:00:00:00:00:00:00:22","group":1,"shape":0}
  ],
  "links":[
    { "source":  0,  "target":  0,  "value":  5 },
    { "source":  1,  "target":  1,  "value":  5 },
    { "source":  2,  "target":  2,  "value":  5 },
    { "source":  3,  "target":  0,  "value":  5 },
    { "source":  4,  "target":  1,  "value":  5 },
    { "source":  5,  "target":  2,  "value":  5 },
    { "source":  6,  "target":  2,  "value":  5 },
    { "source":  7,  "target":  0,  "value":  5 },
    { "source":  8,  "target":  1,  "value":  5 }
  ]
}

If shape value is 1, then draw circle, and if shape value is 0, then draw rectangle. Force directed graph example link is: http://bl.ocks.org/mbostock/4062045

I have tried example link JSFiddle: http://jsfiddle.net/mayurchavda87/Sc2xC/3/

Lars Kotthoff

You can do this, as shown in e.g. this example, by using a symbol generator and path elements instead of SVG elements for specific shapes. The code to add shapes becomes

var node = svg.selectAll(".node")
  .data(data.nodes)
  .enter().append("path")
  .attr("class", "node")
  .attr("d", d3.svg.symbol()
    .type(function(d) { return d3.svg.symbolTypes[d.s]; }))
  .style("fill", function(d) { return color(d.group); })
  .call(force.drag);

Then you also need to change your tick handler to change the transform attribute of the path elements:

node.attr("transform", function(d) {
    return "translate(" + d.x + "," + d.y + ")";
});

Complete jsfiddle 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

How do I add a click event to a directed graph using dagre d3.js(javascript library)?

From Dev

How do I add a click event to a directed graph using dagre d3.js(javascript library)?

From Dev

How do I zoom in on my force-directed graph in d3?

From Dev

How to add different shape marker in d3?

From Dev

How to add a dynamic legend to a D3 force directed graph in Apex?

From Dev

How do I find a cycle in a directed graph using topological sort?

From Dev

D3 How do I create a Star Layout for a force directed map?

From Dev

D3 How do I create a Star Layout for a force directed map?

From Dev

D3 - How do I put x-axis value and labels between two ticks

From Dev

Output in two different columns based on field value

From Dev

Transition from one forced-directed graph to another graph in d3js

From Dev

How do I compare two different values of the same field in MySQL?

From Dev

How do i call two variable from two different function and add them?(Python3)

From Dev

Yii Framework - How do I get a different value for a field?

From Dev

How do I add two different arrays and print the result?

From Dev

how do i add or increase a numeric value in an input text field?

From Dev

How do I add 30 to this form field value?

From Dev

How do I prevent graph elements from reaching the axis of my graph in D3 and scale properly?

From Dev

d3 Directed Graph Editor additions

From Dev

D3 force directed graph direction

From Dev

Shape Context Error by calculating distance of two different shapes

From Dev

Introducing Arrow(directed), in Force Directed Graph d3

From Dev

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

From Dev

d3.js - How can I expand force directed graph horizontally?

From Dev

How to draw a simple Force-directed graph in D3 Javascript

From Dev

How to restrict number of nodes initially in d3 Force directed graph

From Dev

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

From Dev

How to make it so D3 Force Directed Graph generates nodes in same position each time

From Dev

How to choose which JSON file to use in your D3 force directed graph

Related Related

  1. 1

    How do I add a click event to a directed graph using dagre d3.js(javascript library)?

  2. 2

    How do I add a click event to a directed graph using dagre d3.js(javascript library)?

  3. 3

    How do I zoom in on my force-directed graph in d3?

  4. 4

    How to add different shape marker in d3?

  5. 5

    How to add a dynamic legend to a D3 force directed graph in Apex?

  6. 6

    How do I find a cycle in a directed graph using topological sort?

  7. 7

    D3 How do I create a Star Layout for a force directed map?

  8. 8

    D3 How do I create a Star Layout for a force directed map?

  9. 9

    D3 - How do I put x-axis value and labels between two ticks

  10. 10

    Output in two different columns based on field value

  11. 11

    Transition from one forced-directed graph to another graph in d3js

  12. 12

    How do I compare two different values of the same field in MySQL?

  13. 13

    How do i call two variable from two different function and add them?(Python3)

  14. 14

    Yii Framework - How do I get a different value for a field?

  15. 15

    How do I add two different arrays and print the result?

  16. 16

    how do i add or increase a numeric value in an input text field?

  17. 17

    How do I add 30 to this form field value?

  18. 18

    How do I prevent graph elements from reaching the axis of my graph in D3 and scale properly?

  19. 19

    d3 Directed Graph Editor additions

  20. 20

    D3 force directed graph direction

  21. 21

    Shape Context Error by calculating distance of two different shapes

  22. 22

    Introducing Arrow(directed), in Force Directed Graph d3

  23. 23

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

  24. 24

    d3.js - How can I expand force directed graph horizontally?

  25. 25

    How to draw a simple Force-directed graph in D3 Javascript

  26. 26

    How to restrict number of nodes initially in d3 Force directed graph

  27. 27

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

  28. 28

    How to make it so D3 Force Directed Graph generates nodes in same position each time

  29. 29

    How to choose which JSON file to use in your D3 force directed graph

HotTag

Archive