Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different node symbols for d3.js force-directed graph

How do I display nodes as different symbols in d3.js's force-directed library? I wanted to implement something similar to what I wrote below:

  var node = svg.selectAll(".node") 
      .data(graph.nodes) 
    .enter().append(function(d){return d.shape;}) 
      .attr("class", "node") 
      .attr("r", 5) 
      .style("fill", function(d) { return color(d.group); }) 
      .call(force.drag); 

Each node would have an encoded shape ("rect", "circle", etc.). However, I get the error:

Uncaught TypeError: Object function (d){return "circle";} has no method 'indexOf' 

The other question I have related to that is this: how would I toggle between applying different attributes for each shape? Circles need an "r" attribute refined, but rects require "height" and "width". Thanks!

like image 228
user1114864 Avatar asked Apr 06 '13 20:04

user1114864


1 Answers

Use d3.svg.symbol, as in the force-directed symbols example.

like image 104
mbostock Avatar answered Sep 28 '22 04:09

mbostock