I'm new to working with D3, and I'm finding it very humbling.
My goal is to make a treemap from a CSV file. I want to use CSV format because I'll be working with values in a spreadsheet, and it's easy for me to save the files that way.
I'm trying to store the data in a hierarchical format, something like this (hier.csv):
parent,child,value
Homer Simpson,Bart,20
Homer Simpson,Lisa,14
Homer Simpson,Maggie,6
Peter Griffin,Chris,19
Peter Griffin,Meg,12
Peter Griffin,Stewie,9
And I'm using this Zoomable Treemap example.
I'd like for the tree to go arbitrarily deep, i.e. if Bart had children in my example, and to accumulate parent/child relationship accordingly based on names.
I found a great example of this for Sankey Diagrams, but I haven't found the equivalent for Zoomable Treemaps.
Is there a way to insert some code between lines 124 and 126 of Bostock's example to get my data formatted properly for a Zoomable Treemap? (I can change the layout of my CSV, but would like to keep CSV formatting). Something like this approach using nest()
, but obviously this doesn't work:
d3.csv("./hier.csv", function(hier) {
var root = {
"name": "myrootnode",
"children": d3.nest()
.key(function (d) { return d.parent; })
.key(function (d) { return d.child; })
.entries(hier)
};
initialize(root);
accumulate(root);
layout(root);
display(root);
//etc...
I see examples and StackOverflow questions that address bits and pieces of this, but haven't been able to pull it together end-to-end. And I've been researching and hacking to no avail. I'd welcome some help. Thanks!
FIDDLE HERE
Fiddle Notes:
- I'm looking for help near line #90 where the comments indicate
- I setup an inline variable to hold a pared down copy of flare.json
- I plan to keep the data in a separate file in real life, but for JSFiddle, it had to be pulled inline; this will, of course, be easily adaptable back to a separate data file if I can figure out the main logic.
- This example doesn't appear to work with D3 version 3.0.4, which is the current built-in version for JSFiddle. I imported v2.x for the example, since that's what works with Bostock's example. That's a potential separate question...
Update
I overcame part of my issue. I wasn't too far off with my thinking that nest()
was necessary, but I didn't update my accessors properly. Here's a very sloppy look at something that mostly works:
http://bl.ocks.org/maw246/7303963
Main Difference(s) Between my Example and Bostock's:
- CSV gets nested after it's loaded, using
d3.nest()
. This coerces it into a hierarchical JSON object format - Many (if not all) references to "children" have been replaced by "values" because
d3.nest()
builds trees withkey
andvalues
properties instead of Treemap's expectedname
andchildren
properties. Thevalue
property, which is used for block sizing, stays intact.
Issues Remaining
- It doesn't go arbitrarily deep. I'm considering a way to reorganize my data to make better use of the
d3.nest()
capability, but haven't tried it yet. - The names of children are showing up
undefined
. I'm sure I'm just overlooking something simple, but my primary concern was to get the core features working, so I'll hack at this piece a little when I have a minute.
Note: I'm still seeking help with a clean and idiomatic approach to doing this, including advice on how best to organize the CSV for hierarchical nesting to arbitrary depths!
myData
, is that one should get Bart's children as Homer Simpson's grandchildren, and we should be able to click through the treemap that way. – Abdullah