JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://d3js.org/d3.v3.min.js"></script>
JavaScript
var data = [{
"Continent": "Europe",
"Country": "Spain",
"City": "Madrid",
"value": "3"
}, {
"Continent": "Europe",
"Country": "Spain",
"City": "Barcelona",
"value": "30"
}, {
"Continent": "America",
"Country": "Argentina",
"City": "Buenos Aires",
"value": "300"
}, {
"Continent": "America",
"Country": "Argentina",
"City": "Buenos Aires",
"value": "250"
}, {
"Continent": "America",
"Country": "Argentina",
"City": "Rosario",
"value": "200"
}]
var root = {
"key": "World",
"values": d3.nest()
.key(function (d) {
return d.Continent;
})
.key(function (d) {
return d.Country;
})
.key(function (d) {
return d.City;
}).rollup(function (leaves) {
return {
"size": d3.sum(leaves, function (d) {
return d.value;
})
}
})
.entries(data)
};
var root = {
"name": "World",
"children": root.values.map(function (major) {
return {
"name": major.key,
"children": major.values.map(function (region) {
return {
"name": region.key,
"children": region.values.map(function (country) {
return {
"name": country.key,
"size": country.values.size
};
}) //end of map(function(country){
};
}) //end of map(function(region){
};
}) //end of map(function(major){
}; //end of var declaration
$("body").text(JSON.stringify(root));
//I would like to have the data in this format:
/*
[
{"name":"Europe",
"children":[
{"name":"Spain",
"children":[
{"name":"Madrid","size":"size1"}
]
},
{"name":"Barcelona", {"size":"size2"}
...
*/