2014-07-09 99 views
0

我試圖在數據更新時動態更新我的d3樹形圖。我有兩個函數,一個是我最初調用的構建樹形圖,另一個是重繪樹形圖。當我重新繪製樹形圖時,我在樹形圖上獲得了一個細小的黑色條,而且我正在繪製的數組中的第一個元素變成黑色。如果我點擊兩個黑色元素中的任何一個。我得到錯誤...動態更新d3.js樹形圖

Uncaught TypeError: Cannot read property 'dx' of undefined 

所以傳遞給單元的數據是不確定的。

此外,當我調用regraph時,我已檢查並且數據已更改,但圖形與最初構建樹圖時除了兩個黑色元素之外沒有任何變化。

構建樹形圖的代碼如下。另外createObj函數接受兩個數組並創建一個Json對象。

function drawTreeMap(array1,array2){ 
     console.log("got to drawing"); 
     nestedJson=createObj(array1, array2); 
      w = 1880 - 80, 
      h = 900 - 180, 
      x = d3.scale.linear().range([0, w]), 
      y = d3.scale.linear().range([0, h]), 
      color = d3.scale.linear() 
       .range(['lightgreen', 'darkgreen']) // or use hex values 
       .domain([computeMin(array2), computeMaxNum(array2)]); 
      root, 
      node; 


      treemap = d3.layout.treemap() 
       .round(false) 
       .size([w, h]) 
       .sticky(true) 
       .padding([10, 0, 0, 0]) 
       .value(function(d) { return d.size; }); 

      svg = d3.select("#body").append("div") 
       .attr("class", "chart") 
       .style("width", w + "px") 
       .style("height", h + "px") 
       .append("svg:svg") 
       .attr("width", w) 
       .attr("height", h) 
       .append("svg:g") 
       .attr("transform", "translate(.5,.5)"); 


       node = root = nestedJson; 

       var nodes = treemap.nodes(root) 
        .filter(function(d) { return !d.children; }); 

       var cell = svg.selectAll("g") 
        .data(nodes) 
        .enter().append("svg:g") 
        .attr("class", "cell") 
        .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }) 
        .on("click", function(d) { return zoom(node == d.parent ? root : d.parent); }); 

       cell.append("svg:rect") 
        .attr("width", function(d) { return d.dx - 1; }) 
        .attr("height", function(d) { return d.dy ; }) 
        .style("fill", function(d) { return color(d.size)}); 

       cell.append("svg:text") 
        .attr("x", function(d) { return d.dx/2; }) 
        .attr("y", function(d) { return d.dy/2; }) 
        .attr("dy", ".35em") 
        .attr("text-anchor", "middle") 
        .text(function(d) { return (d.name+",-- "+d.size); }) 
        .style("opacity", function(d) { d.w = this.getComputedTextLength(); return d.dx > d.w ? 1 : 0; }); 




       d3.select(window).on("click", function() { zoom(root); }); 

       d3.select("select").on("change", function() { 
       treemap.value(this.value == "size" ? size : count).nodes(root); 
       zoom(node); 
       }); 

    } 

重畫的代碼如下。

function redrawGraphFromJson(data) { 
     treemap = d3.layout.treemap() 
     .round(false) 
     .size([w,h]) 
     .value(function(d) { return d.size; }) 
     .sticky(true); 

     // Draw the graph 
     node = root = data; 
     var nodes = treemap.nodes(root) 
        .filter(function(d) { return !d.children; }); 

     var cell = svg.selectAll("g") 
     .data(nodes) 
     .attr("class", "cell") 
     .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; }) 
     .on("click", function(d) { return zoom(node == d.parent ? root : d.parent); }); 

    cell.append("svg:rect") 
     .attr("width", function(d) { return d.dx - 1; }) 
     .attr("height", function(d) { return d.dy ; }) 
     .style("fill", function(d) { return color(d.size);}); 

    cell.append("svg:text") 
     .attr("x", function(d) { return d.dx/2; }) 
     .attr("y", function(d) { return d.dy/2; }) 
     .attr("dy", ".35em") 
     .attr("text-anchor", "middle") 
     .text(function(d) { return (d.name+",-- "+d.size); }) 
     .style("opacity", function(d) { d.w = this.getComputedTextLength(); return d.dx > d.w ? 1 : 0; }); 

    } 

謝謝。

回答

1

在重新繪製圖形時,您需要在初始圖形上使用.exit(),然後使用.enter()使用新數據更新組。這將用新的替換舊地圖。

http://bost.ocks.org/mike/join/完美地解釋它,並有一個非常好的例子來看看。