The best way to convert a nest to a treemap is specifying children accessor function with Treemap.children().
In the zoomable treemap example , it requires not only "children" but also "name" and "size". You can do either:
1)change the accessor functions of those properties so that keep using "key" and "value".
Let's change the source code.
1.1)line 79 & 86:
.style("fill", function(d) { return color(d.parent.name); });
.text(function(d) { return d.name; })
Replace ".name" with ".YOUR_NAME_KEY"(i.e. ".key")
.style("fill", function(d) { return color(d.parent.YOUR_NAME_KEY); });
.text(function(d) { return d.YOUR_NAME_KEY; })
1.2)line 47:
var treemap = d3.layout.treemap()
.round(false)
.size([w, h])
.sticky(true)
.value(function(d) { return d.size; });
Append a line to specify children accessor function.(i.e ".values")
var treemap = d3.layout.treemap()
.round(false)
.size([w, h])
.sticky(true)
.value(function(d) { return d.YOUR_SIZE_KEY; })
.children(function(d){return d.YOUR_CHILDREN_KEY});
1.3)line 97 & 51:
function size(d) {
return d.size;
}
Replace ".size" with ".YOUR_SIZE_KEY"(you didn't mention in your resulting JSON)
function size(d) {
return d.YOUR_SIZE_KEY;
}
P.S. Maybe something omitted, you need verify it yourself.
2)convert your JSON structure to fit the example with Array.map().
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…