Assuming you have only leaf nodes, I'd use a structure like this:
var leaves = [
{name: "A.1", value: .5},
{name: "A.2", value: .3},
{name: "B.1.1", value: .7},
{name: "B.1.2", value: .1},
{name: "B.2", value: .9}
];
Next you can write a memoizing function that returns the node for a
given name. If that node was previously seen, it returns the existing
node rather than creating a new one. Furthermore, it also memoizes all
parent nodes recursively. That looks like this:
var root = {children: []};
function memoize(node) {
var i = node.name.lastIndexOf("."),
p = i < 0 ? root : memoize({name: node.name.substring(0, i),
children: []}),
n = p.children.length;
for (i = -1; ++i < n;) {
if (p.children[i].name === node.name) {
return p.children[i];
}
}
p.children.push(node);
return node;
}
leaves.forEach(memoize);
Mike
That was the case in the long-long ago, but not currently. I updated
flare.json to use the standard representation so that no tricky value
accessor was needed:
https://github.com/mbostock/d3/blob/master/examples/data/flare.json
Related commit here:
https://github.com/mbostock/d3/commit/6bdbe4b8634f56276fa37fa418f7d7d5e629248d
Mike
You're passing a map (USA) rather than an array to the data operator
(vis.data). You don't need to call the data operator twice. It'd be
easier if you just passed USA (the root node of your hierarchy) to the
treemap.nodes method, as documented. For example:
var cell = vis.selectAll("g")
.data(treemap.nodes(USA))
.enter().append("svg:g")
…
Mike