简体   繁体   English

从多个json文件更新d3包布局

[英]update d3 pack layout from multiple json files

I'm trying to update my flat circle pack layout with data from different json files. 我正在尝试使用来自不同json文件的数据更新我的扁平圆圈包布局。 I adapted the code from this tutorial to work with my code: Updating the data of a pack layout from JSON call and redrawing 我改编了本教程中的代码以使用我的代码: 从JSON调用更新包布局的数据并重新绘制

When you 'inspect element', it shows that the data is being updated, but the nodes still correlate to the first JSON file. 当您“检查元素”时,它表明数据正在更新,但是节点仍与第一个JSON文件相关。 I have a feeling it has to do with my d3.json calling back "currentUrl" instead of "currentJson". 我觉得这与我的d3.json回调“ currentUrl”而不是“ currentJson”有关。 Any help would be appreciated!! 任何帮助,将不胜感激!!

This is my generatenewdata code: 这是我的generatenewdata代码:

var refresh = function() {


d3.json(currentUrl, function(error, root){
var node = svg.selectAll(".node")
      .data(bubble.nodes(classes(root))
      .filter(function(d){ return !d.children; }));
node.enter().append("g")
        .attr("class", "node")
        .on("click", getNewData)
        .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

node.exit().remove();

node.append("circle")
        .attr("r", 0)
        .on("click", getNewData)
        .transition()
        .duration(2000)
        .style("fill", function(d) { return color(d.packageName); })
        .attr("r", function(d) { return d.r; });

node.append("title")
  .text(function(d) {return "Census Tract " + d.className + " : " + format(d.value) + " " + d.packageName ; });

node.append("text")
  .style("fill", "#8fb4ae")
  .on("click", getNewData)
  .transition()
  .duration(2000)
  .attr("dy", ".3em")
  .style("text-anchor", "middle")
  .style("fill", "#000000")
  .text(function(d) { return d.className.substring(0, d.r/3); });

node.append("text")
  .style("fill", "#8fb4ae")
  .on("click", getNewData)
  .transition()
  .duration(2000)
  .attr("y", 500)
  .attr("x", 800)
  .attr("class", "year label")
  .style("fill", "#000000")
  .text(function(d) { if  ( !d.children) {return d.name ; } else { return ("hi") ; };  });

node.transition()
    .duration(2000)
    .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

node.select("circle")
    .attr("r", 0)
    .on("click", getNewData)
    .transition()
    .duration(2000)
    .style("fill", function(d) { return color(d.packageName); })
    .attr("r", function(d) { return d.r; })
      });
}

You are appending new elements even for the existing groups that would need to be updated. 您甚至将为需要更新的现有组添加新元素。 Select and update the existing elements instead. 选择并更新现有元素。

For example instead of 例如代替

node.append("circle")
    // set attributes

do

node.enter().append("g")
    // ...
    .append("circle);

node.select("circle")
   // set attributes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM