How do I change nodes to be rectangles instead of circles in a d3 force layout?

12,740

Solution 1

You have to append a rect SVG element instead of a circle.

So, in the script, where it shows this:

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

You should change it to maybe this:

var node = svg.selectAll(".node")
      .data(graph.nodes)
    .enter().append("rect")
      .attr("class", "node")
      .attr("width", 40)
      .attr("height", 20)
      .style("fill", function(d) { return color(d.group); })
      .call(force.drag);

And, where it shows:

node.attr("cx", function(d) { return d.x; })
    .attr("cy", function(d) { return d.y; });

Change it to:

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

Solution 2

Change circle to rect. Remove r attribute. Add width and height attibutes. Change cx and cy in fdg to x and y:

var width = 960, height = 500;

var rectWidth = 20, rectHeight = 10;

var color = d3.scale.category20();

var force = d3.layout.force()
  .charge(-120)
  .linkDistance(30)
  .size([width, height]);

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

d3.json("https://raw.githubusercontent.com/d3/d3-plugins/master/graph/data/miserables.json", function(error, graph) {

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

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

  var node = svg.selectAll(".node")
    .data(graph.nodes)
    .enter().append("rect")
    .attr("class", "node")
    .attr("width", rectWidth)
    .attr("height", rectHeight)
    .style("fill", function(d) { return color(d.group); })
    .call(force.drag);

  node.append("title")
    .text(function(d) { return d.name; });

  force.on("tick", function() {
    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("x", function(d) { return d.x - rectWidth / 2; })
      .attr("y", function(d) { return d.y - rectHeight / 2; });
  });
});
.node {
  stroke: #fff;
  stroke-width: 1.5px;
}

.link {
  stroke: #999;
  stroke-opacity: .6;
}
<script src="http://d3js.org/d3.v3.min.js"></script>

Solution 3

Here is a simple example of using Rectangles with a json file :

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.link {
  stroke: #000;
  stroke-width: 1.5px;
}

.node {
  cursor: move;
  fill: #ccc;
  stroke: #000;
  stroke-width: 1.5px;
}

.node.fixed {
  fill: #f00;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>

<script>

var width = 960,
    height = 800;

var force = d3.layout.force()
    .size([width, height])
    .charge(-400)
    .linkDistance(100)
    .on("tick", tick);

var drag = force.drag();
    //.on("dragstart", dragstart);

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

var link = svg.selectAll(".link"),
    node = svg.selectAll(".node");

d3.json("graph.json", function(error, graph) {
  if (error) throw error;

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

  link = link.data(graph.links)
    .enter().append("line")
      .attr("class", "link");

  node = node.data(graph.nodes)
      .enter().append("rect")
      .attr("class", "node")
      .attr("width", 60)
      .attr("height", 60)
      .on("dblclick", dblclick)
      .call(drag);
});

function tick() {
  link.attr("x1", function(d) { return d.source.x+25; })
      .attr("y1", function(d) { return d.source.y+25; })
      .attr("x2", function(d) { return d.target.x+25; })
      .attr("y2", function(d) { return d.target.y+25; });

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

function dblclick(d) {
  d3.select(this).classed("fixed", d.fixed = false);
}

function dragstart(d) {
  d3.select(this).classed("fixed", d.fixed = true);
}

</script>

The Json file is bellow. Give the exact path of your json file in the code:

{
  "nodes": [
    {"x": 469, "y": 410},
    {"x": 493, "y": 364},
    {"x": 442, "y": 365},
    {"x": 467, "y": 314},
    {"x": 477, "y": 248},
    {"x": 425, "y": 207},
    {"x": 402, "y": 155},
    {"x": 369, "y": 196},
    {"x": 350, "y": 148},
    {"x": 539, "y": 222},
    {"x": 594, "y": 235},
    {"x": 582, "y": 185},
    {"x": 633, "y": 200}
  ],
  "links": [
    {"source":  0, "target":  1},
    {"source":  0, "target":  2},
    {"source":  0, "target":  3},
    {"source":  1, "target":  4},
    {"source":  1, "target":  5},
    {"source":  2, "target":  6},
    {"source":  2, "target":  7},
    {"source":  3, "target":  8},
    {"source":  3, "target":  9},
    {"source":  4, "target":  10},
    {"source":  5, "target":  11},
    {"source":  6, "target":  12}
  ]
}
Share:
12,740
Anthati Nagaraju
Author by

Anthati Nagaraju

Updated on July 08, 2022

Comments

  • Anthati Nagaraju
    Anthati Nagaraju almost 2 years

    How can I change the nodes to be rectangles instead of circles in the following d3 forced directed graph?

  • Eduscho
    Eduscho almost 11 years
    If it helped/solved your problem, please vote/accept the answer.
  • Eduscho
    Eduscho almost 11 years
    You should search SO and post another question for that because it is a different subject.
  • RickyA
    RickyA almost 11 years
    @Journ A square is a special case of a rectangle ;)
  • Eduscho
    Eduscho about 10 years
    @AnthatiNagaraju you never got to mark the accepted answer, if it solved your problem, please mark it as answered by accepting one of the answers.