Reputation: 345
I am new to D3 and I am trying to convert a D3 Treemap from V3 to V4. I have already read some information update the updated code but I can´t figure out to get it working.
The treemap is from the Pivottable.js D3 renderer.
This is the part.
color = d3.scale.category10();
width = opts.d3.width();
height = opts.d3.height();
treemap = d3.layout.treemap().size([width, height]).sticky(true).value(function(d) {
return d.size;
});
d3.select(result[0]).append("div").style("position", "relative").style("width", width + "px").style("height", height + "px").datum(tree).selectAll(".node").data(treemap.padding([15, 0, 0, 0]).value(function(d) {
return d.value;
}).nodes).enter().append("div").attr("class", "node").style("background", function(d) {
if (d.children != null) {
return "lightgrey";
} else {
return color(d.name);
}
}).text(function(d) {
return d.name;
}).call(function() {
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";
});
});
With D3 V4 I get some errors.
For example "d3.scale.category10" is not a function. I replaced it with "d3.scaleOrdinal(d3.schemeCategory10)".
Then "sticky is not a function". I replaced it with "tile(d3.treemapResquarify)".
But I also get "value is not a function" and "enter is not a function" which I can´t solve. And I am not sure if have to change anything else to get it working with D3 V4.
This is my code for now. But it is not working.
// Updated to V4
color = d3.scaleOrdinal(d3.schemeCategory10);
width = opts.d3.width();
height = opts.d3.height();
// Updated sticky for V4
treemap = d3.treemap().size([width, height]).tile(d3.treemapResquarify).value(function(d) {
return d.size;
});
d3.select(result[0]).append("div").style("position", "relative").style("width", width + "px").style("height", height + "px").datum(tree).selectAll(".node").data(treemap.padding([15, 0, 0, 0]).value(function(d) {
return d.value;
}).nodes).enter().append("div").attr("class", "node").style("background", function(d) {
if (d.children != null) {
return "lightgrey";
} else {
return color(d.name);
}
}).text(function(d) {
return d.name;
}).call(function() {
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";
});
});
Can anyone help me with converting the rest this?
// Edit Here is the working D3 v3 example fiddle. The d3_renderer is in the JS part. https://jsfiddle.net/qntc8e7w/
And here is the same example but with D3 v4 https://jsfiddle.net/ybctz0a8/
Upvotes: 8
Views: 768
Reputation: 45
/* The following code is written on the d3.v4.js plugin */
treemap = d3.treemap().size([width, height]).paddingTop(15)
.paddingInner(1);
var div = d3.select(result[0]).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");
var root = d3.hierarchy(tree)
root.sum(function(d) {
return d.value;
});
var treeObj = treemap(root);
node = div.datum(root).selectAll(".node")
.data(treeObj.descendants())
.enter().append("div")
.attr("class", "node")
.style("left", (d) => d.x0 + "px")
.style("top", (d) => d.y0 + "px")
.style("width", (d) => Math.max(0, d.x1 - d.x0 - 1) + "px")
.style("height", (d) => Math.max(0, d.y1 - d.y0 - 1) + "px")
.style("cursor","pointer")
.style("background", function(d) {
if (d.children != null) {
return "lightgrey";
} else {
return color(d.data.name);
}
})
.text(function(d) {
return d.data.name +" ( "+d.value +" )";
})
Upvotes: 0