14

I'm using the amazing D3JS to build a graph. The graph is rendered, but I want my nodes to have each one its size.

The data is of this form :

{source: "Antony Hoppkins", target: "Woody Allen", value: 3}

Here's the code :

var links = graph.links;
var nodes = {};

links.forEach(function(link) {
  link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
  link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
});

var width = 1200,
    height = 1500;

var force = d3.layout.force()
    .nodes(d3.values(nodes))
    .links(links)
    .size([width, height])
    .linkDistance(50)
    .charge(-200)
    .on("tick", tick)
    .start();

var svg = d3.select("#network").append("svg")
    .attr("width", width)
    .attr("height", height);

var link = svg.selectAll(".link")
    .data(force.links())
  .enter().append("line")
    .attr("class", "link");

var node = svg.selectAll(".node")
    .data(force.nodes())
  .enter().append("g")
    .attr("class", "node")
    .style("stroke-width", function(d) { return (d.value)*5; })
    .on("mouseover", mouseover)
    .on("mouseout", mouseout)
    .call(force.drag);

node.append("circle")
    .attr("r", 5);

node.append("text")
    .attr("x", 12)
    .attr("dy", ".35em")
    .text(function(d) { return d.name; });

function tick() {
  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 mouseover() {
  d3.select(this).select("circle").transition()
      .duration(750)
      .attr("r", 10)
      ;
}

function mouseout() {
  d3.select(this).select("circle").transition()
      .duration(750)
      .attr("r", 5)
      ;
}

Any thoughts ?

2 Answers 2

15

I'm assuming that you want to set the size of each node (i.e. radius) according to .value. You do this like this:

node.append("circle")
  .attr("r", function(d) { return d.value * 3; });

You can obviously adjust the factor, or use a scale instead.

2
  • 1
    Already tried it. This is what I get : Error: Invalid value for <circle> attribute r="NaN" I've even added a parseInt(d.value) but in vain.
    – devio
    Dec 21, 2013 at 22:20
  • 1
    If getting a complete working example running isn't feasible, at least post what the data structure looks like when you do function(d) { console.log(d); console.log(d.value) }. Most of the d3 layout methods create a d.value object containing the original data; if your original data was also an object with a .value attribute, you might need to use d.value.value.
    – AmeliaBR
    Dec 24, 2013 at 2:28
3

I'm searching for this too and tried to workaround it a little bit. I think that every object in your db has to be a unique id (or just iterate through). Then in your code at node object you could write something like this:

var node = svg.selectAll(".node")
    .data(graph.nodes)
    .attr("r", function(d){
        if (d.value === myUniqueId){
          return 3;
    } else {
          return 10;
    }
})

here the "r" is for the radious.The value is a name in my db (it can be anything you want). There you can change the nodes sizes. I know it is not a clean code but I'm just a newbie. Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.