Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a D3.js Collapsible Tree from CSV data

This might be a slightly silly question for those of you more familiar with d3 but I'm fairly new to it and I can't quite figure out how to get this thing to work:

What I'm trying to achieve is this: http://bl.ocks.org/robschmuecker/7880033

But I'd like to feed it the data from a flat CSV rather than a JSON.

The problem is that the CSV I have is formatted like so:

Parent Name | Child Name
-------------------------
Parent Name | Child Name
-------------------------
Parent Name | Child Name

so on...

Could someone please point me in the right direction? I know that the d3.csv function works somehow, but I have no idea how to 'plug it' into the example above.

I do apologise, I know this very much sounds like "Do my homework for me", but I've honestly given it a good go and I think I'm stuck.

Thank you. Appreciated.

like image 694
Tim Avatar asked Jan 12 '15 23:01

Tim


1 Answers

I haven't seen what you are looking for done before, but it is a combination of creating a tree from flat data (which requires a bit of data manipulation to finesse it into the correct structure) and the standard loading data from and external source with d3.

Sadly I'm not able to set up a bl.ock for you to demonstrate live code,EDIT: Here is a live version of the running code on bl.ocks.org, and the following is the html file which is the combination of the two techniques;

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">

    <title>Collapsible Tree Example</title>

    <style>

	.node circle {
	  fill: #fff;
	  stroke: steelblue;
	  stroke-width: 3px;
	}

	.node text { font: 12px sans-serif; }

	.link {
	  fill: none;
	  stroke: #ccc;
	  stroke-width: 2px;
	}
	
    </style>

  </head>

  <body>

<!-- load the d3.js library -->	
<script src="http://d3js.org/d3.v3.min.js"></script>
	
<script>

// ************** Generate the tree diagram	 *****************
var margin = {top: 20, right: 120, bottom: 20, left: 120},
	width = 960 - margin.right - margin.left,
	height = 500 - margin.top - margin.bottom;
	
var i = 0;

var tree = d3.layout.tree()
	.size([height, width]);

var diagonal = d3.svg.diagonal()
	.projection(function(d) { return [d.y, d.x]; });

var svg = d3.select("body").append("svg")
	.attr("width", width + margin.right + margin.left)
	.attr("height", height + margin.top + margin.bottom)
  .append("g")
	.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

// load the external data
d3.csv("treedata.csv", function(error, data) {

// *********** Convert flat data into a nice tree ***************
// create a name: node map
var dataMap = data.reduce(function(map, node) {
	map[node.name] = node;
	return map;
}, {});

// create the tree array
var treeData = [];
data.forEach(function(node) {
	// add to parent
	var parent = dataMap[node.parent];
	if (parent) {
		// create child array if it doesn't exist
		(parent.children || (parent.children = []))
			// add node to child array
			.push(node);
	} else {
		// parent is null or missing
		treeData.push(node);
	}
});

  root = treeData[0];
  update(root);
});

function update(source) {

  // Compute the new tree layout.
  var nodes = tree.nodes(root).reverse(),
	  links = tree.links(nodes);

  // Normalize for fixed-depth.
  nodes.forEach(function(d) { d.y = d.depth * 180; });

  // Declare the nodes…
  var node = svg.selectAll("g.node")
	  .data(nodes, function(d) { return d.id || (d.id = ++i); });

  // Enter the nodes.
  var nodeEnter = node.enter().append("g")
	  .attr("class", "node")
	  .attr("transform", function(d) { 
		  return "translate(" + d.y + "," + d.x + ")"; });

  nodeEnter.append("circle")
	  .attr("r", 10)
	  .style("fill", "#fff");

  nodeEnter.append("text")
	  .attr("x", function(d) { 
		  return d.children || d._children ? -13 : 13; })
	  .attr("dy", ".35em")
	  .attr("text-anchor", function(d) { 
		  return d.children || d._children ? "end" : "start"; })
	  .text(function(d) { return d.name; })
	  .style("fill-opacity", 1);

  // Declare the links…
  var link = svg.selectAll("path.link")
	  .data(links, function(d) { return d.target.id; });

  // Enter the links.
  link.enter().insert("path", "g")
	  .attr("class", "link")
	  .attr("d", diagonal);

}

</script>
	
  </body>
</html>

And the following is the csv file I tested it with (named treedata.csv in the html file);

name,parent
Level 2: A,Top Level
Top Level,null
Son of A,Level 2: A
Daughter of A,Level 2: A
Level 2: B,Top Level

Kudos should go to nrabinowitz for describing the data transformation here.

like image 145
d3noob Avatar answered Sep 18 '22 20:09

d3noob