Skip to content

Instantly share code, notes, and snippets.

@peterrcook
Created January 27, 2020 16:52
Show Gist options
  • Save peterrcook/21c0582665261a5f9d270b01d1a12d76 to your computer and use it in GitHub Desktop.
Save peterrcook/21c0582665261a5f9d270b01d1a12d76 to your computer and use it in GitHub Desktop.
Treemap layout (treemapBinary strategy) outputs NaN co-ordinates
<!DOCTYPE html>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.15.0/d3.min.js"></script>
<script>
var data = [
{id: 'A', val1: 0, val2: 125},
{id: 'B', val1: 0, val2: 150},
{id: 'C', val1: 1, val2: 100}
];
var nested = d3.nest()
.key(function(d) {return d.id})
.rollup(function(d) {
return d3.sum(d, function(d) {
return +d.val1; // is ok using val2
});
})
.entries(data);
var root = d3.hierarchy({
name: 'root',
children: nested
});
root.sum(function(d) {
return d.value;
});
var treemapLayout = d3.treemap().tile(d3.treemapBinary); // is ok using default tile strategy
treemapLayout(root);
// Some nodes have NaN x0 or x1
root.descendants().forEach(function(d) {
console.log(d.data.key || 'root', 'x0,y0', d.x0, d.y0, 'x1,y1', d.x1, d.y1);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment