Skip to content

Instantly share code, notes, and snippets.

@shotahorii
Created November 16, 2013 06:49
Show Gist options
  • Save shotahorii/7496797 to your computer and use it in GitHub Desktop.
Save shotahorii/7496797 to your computer and use it in GitHub Desktop.
patternables.com
{
"name":"tourism",
"size":8358105,
"children":[
{
"name":"Asia",
"size":6387977,
"children":[
{"name":"Korea","size":2042775},
{"name":"China","size":1425100},
{"name":"Taiwan","size":1465753},
{"name":"HongKong","size":481665},
{"name":"Thai","size":260640},
{"name":"Singapore","size":142201},
{"name":"Malaysia","size":130183},
{"name":"Indonesia","size":101460},
{"name":"Philippines","size":85037},
{"name":"India","size":68914},
{"name":"Vietnam","size":55156},
{"name":"Israel","size":10413}
]
},
{
"name":"Europe",
"size":775840,
"children":[
{"name":"UK","size":173994},
{"name":"France","size":130412},
{"name":"Germany","size":108898},
{"name":"Italy","size":51801},
{"name":"Russia","size":50176},
{"name":"Spain","size":35207},
{"name":"Netherlands","size":30266},
{"name":"Sweden","size":30458},
{"name":"Switzerland","size":24329},
{"name":"Finland","size":15529},
{"name":"Belgium","size":14608},
{"name":"Denmark","size":13594},
{"name":"Austria","size":11633},
{"name":"Ireland","size":10358},
{"name":"Portugal","size":8408},
{"name":"Norway","size":11447}
]
},
{
"name":"Africa",
"size":24725,
"children":null
},
{
"name":"NorthAmerica",
"size":876401,
"children":[
{"name":"US","size":716709},
{"name":"Canada","size":135355},
{"name":"Mexico","size":18502}
]
},
{
"name":"SouthAmerica",
"size":51151,
"children":[
{"name":"Brazil","size":32111}
]
},
{
"name":"Oceania",
"size":241513,
"children":[
{"name":"Australia","size":206404},
{"name":"NewZealand","size":31853}
]
},
{
"name":"Others",
"size":498,
"children":null
}
]
}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.legend {
font-size: 12px;
}
.title {
font-size: 18px;
}
.label {
font-size: 10px;
z-index: 1000;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script>
var margin =40,
w = 1200-2*margin,
h = 1200-2*margin,
radius = Math.min(w, h)/2.5,
colour = d3.scale.category20();
var partition = d3.layout.partition()
.sort(null)
.size([2*Math.PI, radius*radius])
.value(function(d){return d.size;});
var arc = d3.svg.arc()
.startAngle(function(d){return d.x;})
.endAngle(function(d){return d.x+d.dx;})
.innerRadius(function(d){return Math.sqrt(d.y);})
.outerRadius(function(d){return Math.sqrt(d.y+d.dy);});
//read data.
d3.json("data.json", function(error, root){
//create svg.
var svg = d3.select("body")
.append("svg")
.attr({
width:w-2*margin,
height:h-2*margin,
transform: "translate("+margin+","+margin+")"
})
//create a group for drawing.
var sunburst = svg.selectAll("g")
.data(partition.nodes(root))
.enter()
.append("g")
.attr({
display: function(d){return d.depth != 0 ? null : "none";}
});
/*
Here, sunburst is a selection.
It returns 'Array[1]'.
The array contains all objects in the json with the structure.
Here, Array[42].
Each object has
children, depth, dx, x, y, value, name, size and parents.
children: all objects under the object. For example, Children of 'Asia' are 'Korea', 'China' and so on.
depth: the depth of the object in the structure. depth 0 is only for the root (here, 'tourism'). For example, Asia has depth 1. Korea has depth2.
dx: the angle range of the object. For example, root has x=2*3.14.
dy: the band(radius) range.
x: the starting angle.
y: the starting radius.
value: here, the value is the total of its childrens' size. For example, value of Asia is sum of Korea size + China size + ...
parent: its parent object.
*/
sunburst.append("path")
.attr({
class: "path",
d: arc,
transform: "translate("+radius+","+radius+")"
})
.style("stroke", "#fff")
.style("fill", function(d){return colour((d.children ? d : d.parent).name);})
.style("fill-rule", "evenodd");
sunburst.append("text")
.attr({
class: "label",
transform: function(d){return "translate("+radius+","+radius+") rotate(" + (d.x + d.dx/2 - Math.PI/2)/Math.PI*180 + ")";},
x: function(d){return Math.sqrt(d.y);},
dx: 10
})
.style("font-size", function(d){return d.depth==1? "12px" : "10px";})
.text(function(d){return d.name;});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment