我正在学习 d3。有certain ways of loading the data在 d3 js 中。但他们似乎都做了一个 HTTP GET。在我的场景中,我已经在字符串中有 json 数据。如何使用此字符串而不是发出另一个 http 请求?我试图为此寻找文档,但没有找到。
这行得通:
d3.json("/path/flare.json", function(json) {
//rendering logic here
}
现在,如果我有:
//assume this json comes from a server (on SAME DOMAIN)
var myjson = '{"name": "flare","children": [{"name": "analytics","children": [{"name": "cluster","children": [{"name": "MergeEdge", "size": 10 }]}]}]}';
如何在 d3 中使用已计算的“myjson”并避免对服务器的异步调用?谢谢。
最佳答案
只需将 d3.json
调用替换为
json = JSON.parse( myjson );
IE:
var myjson = '{"name": "flare","children": [{"name": "analytics","children": [{"name": "cluster","children": [{"name": "MergeEdge", "size": 10 }]}]}]}';
// d3.json("/path/flare.json", function(json) { #delete this line
json = JSON.parse( myjson ); //add this line
//rendering logic here
//} #delete this line
更新 09/2013
原始代码已更改。所以 varname json
应该是 root
:
// d3.json("flare.json", function(error, root) { #delete this line
root = JSON.parse( myjson ); //add this line
//rendering logic here
//} #delete this line
关于javascript - d3 js - 在没有 http get 的情况下加载 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10934853/