Я пытался получить древовидную карту d3 для анимации, и у меня было что-то вроде
App.svg = d3.select("#medals-tree-map").append("svg:svg")
.style("width", App.canvasWidth)
.style("height", App.canvasHeight)
.append("svg:g")
.attr("transform", "translate(-.5,-.5)")
.attr("id", "container");
App.treemap = d3.layout.treemap()
.size([App.canvasWidth + 1, App.canvasHeight + 1])
.value(function(d) { return d.number; })
.sticky(true);
function drawGraphFromJson(data) {
// Draw the graph
var leaves = App.treemap(data);
var cell = App.svg.selectAll("g.cell")
.data(leaves);
// More rendering code
}
На основе этого ответа: https://stackoverflow.com/a/9650825/111884
Однако карта дерева вообще не меняется, когда я вызываю drawGraphFromJson
с новыми данными.
Я исправил проблему, определив App.treemap
внутри drawGraphFromJson
, т.е.
function drawGraphFromJson(data) {
App.treemap = d3.layout.treemap()
.size([App.canvasWidth + 1, App.canvasHeight + 1])
.value(function(d) { return d.number; })
.sticky(true);
// Draw the graph
var leaves = App.treemap(data);
var cell = App.svg.selectAll("g.cell")
.data(leaves);
// More rendering code
}
Зачем мне это делать? Кэшируется ли treemap
при передаче корневого узла?