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/