2013-01-23 47 views
1

讀我有一個簡單的JSON文件,該文件是:d3js強制執導不能從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} 
] 
} 

當我保存它使用完全相同的HTML代碼中http://bl.ocks.org/4062045#index.html顯示並解決上述JSON,沒有出現在cancas 。

我很感激,如果你幫我這個,因爲我不是很熟悉它。此外,如果我知道使用json繪製圖形所需的最小代碼,那將是非常好的。

最佳,

+0

對於像我這樣誰有同樣的問題,節點ID必須從0開始 – Moe

回答

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例如複製爲最小代碼。 Remenber將json文件更改爲您自己的json文件。

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

謝謝你,@lephix – Moe

+0

@Moe如果這個回答你的問題,請註明它如此。 – Parker