Skip to content

Instantly share code, notes, and snippets.

@patperu
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save patperu/d335cc3ae605b7b0cc25 to your computer and use it in GitHub Desktop.

Select an option

Save patperu/d335cc3ae605b7b0cc25 to your computer and use it in GitHub Desktop.
Simple Treemap -- Confiscati bene // see http://bl.ocks.org/mbostock/4063582
{
"name": "flare",
"children": [
{
"name": "ABRUZZO",
"children": [
{
"name": "Villa",
"size": 3
},
{
"name": "Terreno con fabbricato rurale",
"size": 1
},
{
"name": "Terreno agricolo",
"size": 10
},
{
"name": "Box, garage, autorimessa, posto auto",
"size": 8
},
{
"name": "Appartamento in condominio",
"size": 19
},
{
"name": "Altro",
"size": 6
},
{
"name": "Abitazione indipendente",
"size": 6
}
]
},
{
"name": "BASILICATA",
"children": [
{
"name": "Terreno con fabbricato rurale",
"size": 1
},
{
"name": "Appartamento in condominio",
"size": 4
},
{
"name": "Altro",
"size": 4
},
{
"name": "Albergo, Pensione",
"size": 1
},
{
"name": "Abitazione indipendente",
"size": 1
}
]
},
{
"name": "CALABRIA",
"children": [
{
"name": "Villa",
"size": 31
},
{
"name": "Terreno edificabile",
"size": 39
},
{
"name": "Terreno con fabbricato rurale",
"size": 60
},
{
"name": "Terreno agricolo",
"size": 405
},
{
"name": "Fabbricato/Locale per esercizi sportivi",
"size": 1
},
{
"name": "Box, garage, autorimessa, posto auto",
"size": 74
},
{
"name": "Appartamento in condominio",
"size": 662
},
{
"name": "Altro",
"size": 375
},
{
"name": "Albergo, Pensione",
"size": 2
},
{
"name": "Abitazione indipendente",
"size": 10
}
]
},
{
"name": "CAMPANIA",
"children": [
{
"name": "Villa",
"size": 114
},
{
"name": "Terreno edificabile",
"size": 31
},
{
"name": "Terreno con fabbricato rurale",
"size": 48
},
{
"name": "Terreno agricolo",
"size": 359
},
{
"name": "Box, garage, autorimessa, posto auto",
"size": 108
},
{
"name": "Appartamento in condominio",
"size": 580
},
{
"name": "Altro",
"size": 318
},
{
"name": "Albergo, Pensione",
"size": 3
},
{
"name": "Abitazione indipendente",
"size": 15
}
]
},
{
"name": "EMILIA-ROMAGNA",
"children": [
{
"name": "Terreno edificabile",
"size": 4
},
{
"name": "Terreno agricolo",
"size": 11
},
{
"name": "Box, garage, autorimessa, posto auto",
"size": 10
},
{
"name": "Appartamento in condominio",
"size": 34
},
{
"name": "Altro",
"size": 26
},
{
"name": "Abitazione indipendente",
"size": 1
}
]
},
{
"name": "FRIULI VENEZIA GIULIA",
"children": [
{
"name": "Villa",
"size": 1
},
{
"name": "Terreno edificabile",
"size": 1
},
{
"name": "Terreno agricolo",
"size": 1
},
{
"name": "Box, garage, autorimessa, posto auto",
"size": 6
},
{
"name": "Appartamento in condominio",
"size": 5
},
{
"name": "Altro",
"size": 3
},
{
"name": "Abitazione indipendente",
"size": 1
}
]
},
{
"name": "LAZIO",
"children": [
{
"name": "Villa",
"size": 34
},
{
"name": "Terreno edificabile",
"size": 27
},
{
"name": "Terreno con fabbricato rurale",
"size": 9
},
{
"name": "Terreno agricolo",
"size": 58
},
{
"name": "Fabbricato/Locale per esercizi sportivi",
"size": 4
},
{
"name": "Box, garage, autorimessa, posto auto",
"size": 62
},
{
"name": "Appartamento in condominio",
"size": 163
},
{
"name": "Altro",
"size": 141
},
{
"name": "Albergo, Pensione",
"size": 1
},
{
"name": "Abitazione indipendente",
"size": 6
}
]
},
{
"name": "LIGURIA",
"children": [
{
"name": "Villa",
"size": 1
},
{
"name": "Terreno edificabile",
"size": 1
},
{
"name": "Terreno agricolo",
"size": 3
},
{
"name": "Box, garage, autorimessa, posto auto",
"size": 3
},
{
"name": "Appartamento in condominio",
"size": 21
},
{
"name": "Altro",
"size": 10
},
{
"name": "Abitazione indipendente",
"size": 4
}
]
},
{
"name": "LOMBARDIA",
"children": [
{
"name": "Villa",
"size": 53
},
{
"name": "Terreno edificabile",
"size": 17
},
{
"name": "Terreno con fabbricato rurale",
"size": 8
},
{
"name": "Terreno agricolo",
"size": 40
},
{
"name": "Box, garage, autorimessa, posto auto",
"size": 278
},
{
"name": "Appartamento in condominio",
"size": 404
},
{
"name": "Altro",
"size": 201
},
{
"name": "Albergo, Pensione",
"size": 3
},
{
"name": "Abitazione indipendente",
"size": 25
}
]
},
{
"name": "MARCHE",
"children": [
{
"name": "Villa",
"size": 2
},
{
"name": "Terreno con fabbricato rurale",
"size": 2
},
{
"name": "Terreno agricolo",
"size": 3
},
{
"name": "Box, garage, autorimessa, posto auto",
"size": 3
},
{
"name": "Appartamento in condominio",
"size": 6
},
{
"name": "Altro",
"size": 1
}
]
},
{
"name": "MOLISE",
"children": [
{
"name": "Box, garage, autorimessa, posto auto",
"size": 1
},
{
"name": "Appartamento in condominio",
"size": 1
}
]
},
{
"name": "PIEMONTE",
"children": [
{
"name": "Villa",
"size": 11
},
{
"name": "Terreno con fabbricato rurale",
"size": 4
},
{
"name": "Terreno agricolo",
"size": 45
},
{
"name": "Box, garage, autorimessa, posto auto",
"size": 27
},
{
"name": "Appartamento in condominio",
"size": 41
},
{
"name": "Altro",
"size": 35
},
{
"name": "Abitazione indipendente",
"size": 5
}
]
},
{
"name": "PUGLIA",
"children": [
{
"name": "Villa",
"size": 55
},
{
"name": "Terreno edificabile",
"size": 17
},
{
"name": "Terreno con fabbricato rurale",
"size": 27
},
{
"name": "Terreno agricolo",
"size": 189
},
{
"name": "Box, garage, autorimessa, posto auto",
"size": 94
},
{
"name": "Appartamento in condominio",
"size": 291
},
{
"name": "Altro",
"size": 205
},
{
"name": "Albergo, Pensione",
"size": 3
},
{
"name": "Abitazione indipendente",
"size": 70
}
]
},
{
"name": "SARDEGNA",
"children": [
{
"name": "Terreno edificabile",
"size": 2
},
{
"name": "Terreno con fabbricato rurale",
"size": 1
},
{
"name": "Terreno agricolo",
"size": 15
},
{
"name": "Box, garage, autorimessa, posto auto",
"size": 9
},
{
"name": "Appartamento in condominio",
"size": 19
},
{
"name": "Altro",
"size": 8
},
{
"name": "Abitazione indipendente",
"size": 6
}
]
},
{
"name": "SICILIA",
"children": [
{
"name": "Villa",
"size": 105
},
{
"name": "Terreno edificabile",
"size": 129
},
{
"name": "Terreno con fabbricato rurale",
"size": 222
},
{
"name": "Terreno agricolo",
"size": 1103
},
{
"name": "Box, garage, autorimessa, posto auto",
"size": 602
},
{
"name": "Appartamento in condominio",
"size": 1604
},
{
"name": "Altro",
"size": 1074
},
{
"name": "Albergo, Pensione",
"size": 3
},
{
"name": "Abitazione indipendente",
"size": 272
}
]
},
{
"name": "TOSCANA",
"children": [
{
"name": "Villa",
"size": 1
},
{
"name": "Terreno con fabbricato rurale",
"size": 5
},
{
"name": "Terreno agricolo",
"size": 14
},
{
"name": "Box, garage, autorimessa, posto auto",
"size": 15
},
{
"name": "Appartamento in condominio",
"size": 19
},
{
"name": "Altro",
"size": 4
},
{
"name": "Albergo, Pensione",
"size": 1
},
{
"name": "Abitazione indipendente",
"size": 12
}
]
},
{
"name": "TRENTINO-ALTO ADIGE/SUDTIROL",
"children": [
{
"name": "Terreno agricolo",
"size": 1
},
{
"name": "Box, garage, autorimessa, posto auto",
"size": 5
},
{
"name": "Appartamento in condominio",
"size": 10
},
{
"name": "Albergo, Pensione",
"size": 1
}
]
},
{
"name": "UMBRIA",
"children": [
{
"name": "Terreno agricolo",
"size": 3
}
]
},
{
"name": "VENETO",
"children": [
{
"name": "Villa",
"size": 7
},
{
"name": "Terreno edificabile",
"size": 4
},
{
"name": "Terreno con fabbricato rurale",
"size": 1
},
{
"name": "Terreno agricolo",
"size": 8
},
{
"name": "Box, garage, autorimessa, posto auto",
"size": 22
},
{
"name": "Appartamento in condominio",
"size": 22
},
{
"name": "Altro",
"size": 17
},
{
"name": "Albergo, Pensione",
"size": 1
},
{
"name": "Abitazione indipendente",
"size": 2
}
]
}
]}
<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
stroke: #fff;
fill-rule: evenodd;
}
text {
font-family: Arial, sans-serif;
font-size: 10px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
<!-- source http://blog.luzid.com/2013/extending-the-d3-zoomable-sunburst-with-labels/ -->
var width = 960,
height = 800,
radius = Math.min(width, height) / 2;
var x = d3.scale.linear()
.range([0, 2 * Math.PI]);
var y = d3.scale.linear()
.range([0, radius]);
var color = d3.scale.category20c();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + (height / 2 + 10) + ")");
var partition = d3.layout.partition()
.value(function(d) { return d.size; });
var arc = d3.svg.arc()
.startAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x))); })
.endAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x + d.dx))); })
.innerRadius(function(d) { return Math.max(0, y(d.y)); })
.outerRadius(function(d) { return Math.max(0, y(d.y + d.dy)); });
d3.json("flare.json", function(error, root) {
var g = svg.selectAll("g")
.data(partition.nodes(root))
.enter().append("g");
var path = g.append("path")
.attr("d", arc)
.style("fill", function(d) { return color((d.children ? d : d.parent).name); })
.on("click", click);
var text = g.append("text")
.attr("transform", function(d) { return "rotate(" + computeTextRotation(d) + ")"; })
.attr("x", function(d) { return y(d.y); })
.attr("dx", "6") // margin
.attr("dy", ".35em") // vertical-align
.text(function(d) { return d.name; });
function click(d) {
// fade out all text elements
text.transition().attr("opacity", 0);
path.transition()
.duration(750)
.attrTween("d", arcTween(d))
.each("end", function(e, i) {
// check if the animated element's data e lies within the visible angle span given in d
if (e.x >= d.x && e.x < (d.x + d.dx)) {
// get a selection of the associated text element
var arcText = d3.select(this.parentNode).select("text");
// fade in the text element and recalculate positions
arcText.transition().duration(750)
.attr("opacity", 1)
.attr("transform", function() { return "rotate(" + computeTextRotation(e) + ")" })
.attr("x", function(d) { return y(d.y); });
}
});
}
});
d3.select(self.frameElement).style("height", height + "px");
// Interpolate the scales!
function arcTween(d) {
var xd = d3.interpolate(x.domain(), [d.x, d.x + d.dx]),
yd = d3.interpolate(y.domain(), [d.y, 1]),
yr = d3.interpolate(y.range(), [d.y ? 20 : 0, radius]);
return function(d, i) {
return i
? function(t) { return arc(d); }
: function(t) { x.domain(xd(t)); y.domain(yd(t)).range(yr(t)); return arc(d); };
};
}
function computeTextRotation(d) {
return (x(d.x + d.dx / 2) - Math.PI / 2) / Math.PI * 180;
}
</script>
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
margin: auto;
position: relative;
width: 960px;
}
form {
position: absolute;
right: 10px;
top: 10px;
}
.node {
border: solid 1px white;
font: 10px sans-serif;
line-height: 12px;
overflow: hidden;
position: absolute;
text-indent: 2px;
}
</style>
<form>
<label><input type="radio" name="mode" value="size" checked> Size</label>
<label><input type="radio" name="mode" value="count"> Count</label>
</form>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 40, right: 10, bottom: 10, left: 10},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var color = d3.scale.category20c();
var treemap = d3.layout.treemap()
.size([width, height])
.sticky(true)
.value(function(d) { return d.size; });
var div = d3.select("body").append("div")
.style("position", "relative")
.style("width", (width + margin.left + margin.right) + "px")
.style("height", (height + margin.top + margin.bottom) + "px")
.style("left", margin.left + "px")
.style("top", margin.top + "px");
d3.json("flare.json", function(error, root) {
var node = div.datum(root).selectAll(".node")
.data(treemap.nodes)
.enter().append("div")
.attr("class", "node")
.call(position)
.style("background", function(d) { return d.children ? color(d.name) : null; })
.text(function(d) { return d.children ? null : d.name; });
d3.selectAll("input").on("change", function change() {
var value = this.value === "count"
? function() { return 1; }
: function(d) { return d.size; };
node
.data(treemap.value(value).nodes)
.transition()
.duration(1500)
.call(position);
});
});
function position() {
this.style("left", function(d) { return d.x + "px"; })
.style("top", function(d) { return d.y + "px"; })
.style("width", function(d) { return Math.max(0, d.dx - 1) + "px"; })
.style("height", function(d) { return Math.max(0, d.dy - 1) + "px"; });
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment