javascript - d3 js - 在没有 http get 的情况下加载 json

标签 javascript json d3.js bubble-chart

我正在学习 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/

相关文章:

javascript - 将时间范围从几天更改为几周,如何更新和求和数据值?

javascript - 在 addclass/removeclass 事件上添加淡入或淡出

Javascript:如何保持持久数组数据

jquery - getJSON 返回错误 'object is undefined'

php - 使用 $.ajax 从 json_encode 接收响应时获取 parseerror

javascript - 从 json 文件打印数据

javascript - 你能在 Ruby 中使用 Case 语句进行无限计数吗?

javascript - 使用 anchor 时使用后退按钮刷新

javascript - 简单的线图 - 动态调整大小

javascript - 为什么鼠标悬停在回调中返回所有数据