d3js强制导演无法从json读取

时间:2013-01-23 02:05:51

标签: json d3.js

我有一个简单的json文件:

{
 "nodes":[ 
{"name":"Moe","group":1},
{"name":"Madih1","group":1},
{"name":"Madih2","group":1},
{"name":"Nora","group":1},
{"name":"Myna","group":1}
 ],
 "links":[
{"source":35,"target":44,"value":1},
{"source":44,"target":35,"value":1},
{"source":45,"target":35,"value":1},
{"source":45,"target":44,"value":1},
{"source":35,"target":49,"value":1},
{"source":49,"target":35,"value":1}
 ] 
 }

当我保存时,使用http://bl.ocks.org/4062045#index.html中显示的html代码并解决上面的json,cancas上没有任何内容。

如果你帮我解决这个问题我很感激,因为我对它并不熟悉。而且,如果我知道使用json绘制这样的图形所需的最小代码,那就太棒了。

最佳,

1 个答案:

答案 0 :(得分:0)

“source”和“target”的数量是指nodes数组中项目的索引。 所以你可以把你的json改成以下:

{
 "nodes":[ 
    {"name":"Moe","group":1},
    {"name":"Madih1","group":1},
    {"name":"Madih2","group":1},
    {"name":"Nora","group":1},
    {"name":"Myna","group":1}
 ],
 "links":[
    {"source":0,"target":1,"value":1},
    {"source":1,"target":2,"value":1},
    {"source":2,"target":3,"value":1},
    {"source":3,"target":4,"value":1},
 ] 
 }

然后您可以将http://bl.ocks.org/4062045#index.html示例中的代码复制为最小代码。 Remenber将json文件更改为您自己的json文件。

d3.json("path/to/your/json", function(error, graph) {
    //codes
});