Skip to content

Instantly share code, notes, and snippets.

@me1er
Last active September 26, 2017 11:20
Show Gist options
  • Save me1er/09bc07824fed205f94392949a15fdc1e to your computer and use it in GitHub Desktop.
Save me1er/09bc07824fed205f94392949a15fdc1e to your computer and use it in GitHub Desktop.
RUL Chart area and income distribution by tenant (Treemap)
border: no
height: 600
license: gpl-3.0
{
"name": "area",
"children": [
{"name": "Coop", "size":45},
{"name": "Andere", "size": 15},
{"name": "Eugen Müller", "size": 15},
{"name": "Aldi", "size": 15},
{"name": "Hans Huber", "size": 6},
{"name": "Migros", "size": 40}
]
}
<!DOCTYPE html>
<style>
form {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
svg {
font: 10px sans-serif;
}
</style>
<svg width="350" height="200"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
x = d3.scaleLinear().range([0, width]),
y = d3.scaleLinear().range([0, height]),
node,
root;
var wupColorScale = d3.scaleOrdinal()
.domain(["Coop","Migros","Andere","Eugen Müller","Aldi","Hans Huber"])
//.range(["#EA4424", "#008DD2", "#00A859", "#FDBA02", "#565656", "#C5B3A5"]);
.range(["#ED6E52", "#35A5D9", "#34A87F", "#F9C826", "#C5B3A5", "#C8C8C8"]);
var fader = function(color) { return d3.interpolateRgb(color, "#fff")(0.2); };
// color = d3.scaleOrdinal(d3.schemeCategory20.map(fader)),
//var color = d3.scaleOrdinal().range(d3.schemeCategory20c);
var color = wupColorScale;
var format = d3.format(",d");
var treemap = d3.treemap()
.tile(d3.treemapResquarify)
.size([width, height])
.round(true)
.paddingInner(1)
;
d3.json("flare.json", function(error, data) {
if (error) throw error;
root = d3.hierarchy(data)
.eachBefore(function(d) { d.data.id = (d.parent ? d.parent.data.id + "." : "") + d.data.name; })
.sum(d => d.size)
.sort(function(a, b) { return b.height - a.height || b.value - a.value; });
node = root;
treemap(root);
var cell = svg.selectAll("g")
.data(root.leaves())
.enter().append("g")
.attr("class", "cell")
.attr("transform", function(d) { return "translate(" + d.x0 + "," + d.y0 + ")"; });
cell.append("rect")
.attr("id", function(d) { return d.data.id; })
.attr("width", function(d) { return d.x1 - d.x0; })
.attr("height", function(d) { return d.y1 - d.y0; })
.attr("fill", function(d) {
if (d.depth == 1) {
return color(d.data.name);
} else {
return color(d.parent.data.name);
}
});
cell.append("text")
// .attr("x", function(d) { return (d.x1 - d.x0) / 2; })
// .attr("y", function(d) { return (d.y1 - d.y0) / 2; })
.attr("dy", "1em")
.text(function(d) { return d.data.name; });
cell.append("title")
.text(function(d) { return d.data.id + "\n" + format(d.value); });
d3.select(window).on("click", function() { zoom(root); });
});
/*
function zoom(d) {
var kx = width / (d.x1 - d.x0), ky = height / (d.y1 - d.y0);
x.domain([d.x0, d.x0 + d.x1]);
y.domain([d.y0, d.y0 + d.y1]);
var t = svg.selectAll("g.cell").transition()
.duration(d3.event.altKey ? 7500 : 750)
.attr("transform", function(d) { return "translate(" + x(d.x0) + "," + y(d.y0) + ")"; });
t.select("rect")
.attr("width", function(d) { return kx * (d.x1 - d.x0) - 1; })
.attr("height", function(d) { return ky * (d.y1 - d.y0) - 1; })
t.select("text")
.attr("x", function(d) { return kx * (d.x1 - d.x0) / 2; })
.attr("y", function(d) { return ky * (d.y1 - d.y0) / 2; });
node = d;
d3.event.stopPropagation();
}
*/
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment