1

我有一个简单的 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 绘制这样的图形所需的最少代码,那就太好了。

最好的,

4

1 回答 1

0

“源”和“目标”的数量是指节点数组中项目的索引。因此,您可以将 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示例中的代码作为最低代码。请记住将 json 文件更改为您自己的 json 文件。

d3.json("path/to/your/json", function(error, graph) {
    //codes
});
于 2013-01-24T05:39:51.393 回答