2017-04-25 72 views
-1

我正在尝试使用d3js绘制图表(https://bl.ocks.org/mbostock/raw/4339083/)。这里是有问题的代码:因为崩溃是没有定义无法读取每个属性undefined - d3js

root.children.forEach(collapse); 

d._children.forEach(collapse); 

给予错误:

d3.json("/MyController/MyAction", function (error, flare) { 
    if (error) throw error; 

    console.log(flare); 

    root = flare; 
    root.x0 = height/2; 
    root.y0 = 0; 

    function collapse(d) { 
    if (d.children) { 
     d._children = d.children; 
     d._children.forEach(collapse); 
     d.children = null; 
    } 
    } 

    root.children.forEach(collapse); 
    update(root); 
}); 

的线条。我是否收到错误,因为我的JSON对象不正确?谢谢。

这里是我的控制台日志:

{"name":"Country","children":[{"name":"City","children":[{"name":"County","children":[{"name":"Child1","size":2},{"name":"Child2","size":2},{"name":"Child3","size":2}]}]}]} 

你能告诉我有什么错呢?

+1

你可以发布完整的工作示例,它给出错误吗?这段代码在我的控制台中正常工作 – bumbeishvili

回答

0

你是从别的地方得到的错误,你的代码发布作品:

var height = 10; 
 
var root = { 
 
    "name": "Country", 
 
    "children": [{ 
 
    "name": "City", 
 
    "children": [{ 
 
     "name": "County", 
 
     "children": [{ 
 
     "name": "Child1", 
 
     "size": 2 
 
     }, { 
 
     "name": "Child2", 
 
     "size": 2 
 
     }, { 
 
     "name": "Child3", 
 
     "size": 2 
 
     }] 
 
    }] 
 
    }] 
 
} 
 

 
root.x0 = height/2; 
 
root.y0 = 0; 
 

 
function collapse(d) { 
 
    if (d.children) { 
 
    d._children = d.children; 
 
    d._children.forEach(collapse); 
 
    d.children = null; 
 
    } 
 
} 
 

 
root.children.forEach(collapse); 
 
console.log(root);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

+0

但是它在这些行上给出了错误。我该怎么办? – starrr

+0

也许如果你发布了更多的代码和更多的控制台输出,它会帮助你 – thedude

0

奇怪的是,当我保存了相同的JSON在以.json文件,它的工作..