Javascript d3 js - 在没有 http get 的情况下加载 json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10934853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
d3 js - loading json without a http get
提问by Ravi
I am learning d3. There are certain ways of loading the datain d3 js. But all of them seem to make a HTTP GET. In my scenario, I already have the json data in a string. How can I use this string instead of making another http request? I tried to look for documentation for this but found none.
我正在学习 d3。在 d3 js 中有一些加载数据的方法。但他们似乎都在做一个 HTTP GET。在我的场景中,我已经有一个字符串中的 json 数据。如何使用此字符串而不是发出另一个 http 请求?我试图为此寻找文档,但没有找到。
This works:
这有效:
d3.json("/path/flare.json", function(json) {
//rendering logic here
}
Now, if I have:
现在,如果我有:
//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 }]}]}]}';
How do I use already computed 'myjson' in d3 & avoid a async call to server? Thanks.
如何在 d3 中使用已计算的“myjson”并避免对服务器的异步调用?谢谢。
回答by Luca Rainone
Simply replace d3.json
call with
只需将d3.json
call替换为
json = JSON.parse( myjson );
IE:
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
UPDATE 09/2013
更新 09/2013
Original code has changed. So varname json
should be root
:
原始代码已更改。所以 varnamejson
应该是root
:
// d3.json("flare.json", function(error, root) { #delete this line
root = JSON.parse( myjson ); //add this line
//rendering logic here
//} #delete this line
回答by dardo
According to this example:
根据这个例子:
http://phrogz.net/JS/d3-playground/#StockPrice_HTML
http://phrogz.net/JS/d3-playground/#StockPrice_HTML
Here they are storing the graph data within the variable $data, and setting it via the .data($data) function.
在这里,他们将图形数据存储在变量 $data 中,并通过 .data($data) 函数进行设置。
I'd apply this method to whatever graph you are using.
我会将此方法应用于您正在使用的任何图表。
回答by John Sharp
The answer from chumkiu worked great for me but needed a couple of tweaks - in the latest version of the d3 bubble chart, you need to define root rather than json, as in
chumkiu 的回答对我很有用,但需要进行一些调整 - 在最新版本的 d3 气泡图中,您需要定义 root 而不是 json,如
root = JSON.parse( myjson );
Alternatively, you could replace "root" with "json" in the rest of the code of course. :-)
或者,您当然可以在其余代码中用“json”替换“root”。:-)
For anyone coming to this answer with questions about d3 node-link trees that utilize local data sets, this answer worked great for me - many thanks to the contributors on this page.
对于任何关于使用本地数据集的 d3 节点链接树的问题来回答这个答案的人,这个答案对我很有用 - 非常感谢这个页面上的贡献者。