Skip to content

Instantly share code, notes, and snippets.

@d3indepth
Last active August 2, 2017 10:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save d3indepth/379926741d6600ab6c8af7f5f8707373 to your computer and use it in GitHub Desktop.
Partition layout (rotated)
license: gpl-3.0
height: 230
border: no
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Partition layout (rotated)</title>
</head>
<style>
rect {
fill: #333;
opacity: 0.3;
stroke: white;
}
</style>
<body>
<svg width="420" height="220">
<g></g>
</svg>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script>
var data = {
"name": "A1",
"children": [
{
"name": "B1",
"children": [
{
"name": "C1",
"size": 100
},
{
"name": "C2",
"size": 300
},
{
"name": "C3",
"size": 200
}
]
},
{
"name": "B2",
"size": 200
}
]
};
var partitionLayout = d3.partition()
.size([200, 400]);
var rootNode = d3.hierarchy(data)
rootNode.sum(function(d) {
return d.size;
});
partitionLayout(rootNode);
d3.select('svg g')
.selectAll('rect')
.data(rootNode.descendants())
.enter()
.append('rect')
.attr('x', function(d) { return d.y0; })
.attr('y', function(d) { return d.x0; })
.attr('width', function(d) { return d.y1 - d.y0; })
.attr('height', function(d) { return d.x1 - d.x0; })
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment