d3 js - 加载json没有http get [英] d3 js - loading json without a http get
问题描述
我在学习d3。在d3 js中有某些加载数据的方式。但所有的人似乎都做一个HTTP GET。在我的场景中,我已经有一个字符串中的json数据。如何使用此字符串而不是另一个http请求?
这样做:
d3.json(/ path / flare.json,function(json){
//渲染逻辑
}
现在,如果我有:
//假设这个json来自一个服务器(在同一个域上)
var myjson ='{name:flare,children:[{name:analytics,children:[{name cluster,children:[{name:MergeEdge,size:10}]}]}]}';
如何在d3中使用已经计算的'myjson',避免对服务器的异步调用?
解决方案只需用
<$ c $替换d3.json
c> 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); //添加此行
//渲染逻辑
//}#删除此行
UPDATE 09/2013
原始程式码已变更。因此varname
json
应为root
:// d3.json(flare.json,function(error,root){#delete this line
root = JSON.parse(myjson); // add this line
//渲染逻辑
//}#删除此行
I am learning d3. There are certain ways of loading the data in 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.
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.
解决方案Simply replace
d3.json
call withjson = 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
UPDATE 09/2013
Original code has changed. So varname
json
should beroot
:// d3.json("flare.json", function(error, root) { #delete this line root = JSON.parse( myjson ); //add this line //rendering logic here //} #delete this line
这篇关于d3 js - 加载json没有http get的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!