简体   繁体   English

将json代码集成到d3 html文件中

[英]Integrate json code into a d3 html file

I'm testing the sandey chart from the following link: https://gist.github.com/d3noob/c2637e28b79fb3bfea13我正在从以下链接测试桑迪图表: https ://gist.github.com/d3noob/c2637e28b79fb3bfea13

But I can only see the the chart in Firefox, not in Chrome.但是我只能在 Firefox 中看到图表,在 Chrome 中看不到。 I suspect because Chrome doesn't allow to access data from an external file.我怀疑是因为 Chrome 不允许从外部文件访问数据。

Therefore, I'm trying to integrate the json code, but when I try it, the chart doesn't show up in Firefox因此,我正在尝试集成 json 代码,但是当我尝试时,图表没有显示在 Firefox 中

I have added a variable with all the json code like:我添加了一个包含所有 json 代码的变量,例如:

var dataset = {"nodes":[{"node":0,"name":"node0"},{"node":1,"name":"node1"},{"node":2,"name":"node2"},{"node":3,"name":"node3"},{"node":4,"name":"node4"}],"links":[{"source":0,"target":2,"value":2},{"source":1,"target":2,"value":2},{"source":1,"target":3,"value":2},{"source":0,"target":4,"value":2},{"source":2,"target":3,"value":2},{"source":2,"target":4,"value":2},{"source":3,"target":4,"value":4}]};

And I've replaced this line of code:我已经替换了这行代码:

d3.json("sankey-formatted.json", function(error, graph) {

For this one (just replacing the name of the file by the dataset variable):对于这个(只需用数据集变量替换文件名):

d3.json(dataset, function(error, graph) {

,,, but not the chart is not showing. ,,, 但不是图表没有显示。 Any ideas why the sankey chart is not showing?有什么想法为什么桑基图没有显示?

Thanks谢谢

The problem you're facing is that d3.json ...您面临的问题是d3.json ...

...returns a new request to get the JSON file at the specified url with the default mime type application/json. ...返回一个新请求,以使用默认的 mime 类型 application/json 在指定的 url 处获取 JSON 文件。

That being said, the solution is simpler than you think: since you already have a variable with all the data, just name it as graph (your d3.json function loads the file and populates a data array called graph ):话虽如此,解决方案比您想象的要简单:因为您已经有了一个包含所有数据的变量,只需将其命名为graph (您的d3.json函数加载文件并填充名为graph的数据数组):

var graph = {"nodes":[{"node":0,"name":"node0"},...

And drop the d3.json function, don't forgetting to remove the closing curly bracket/parenthesis:并删除d3.json函数,不要忘记删除d3.json花括号/圆括号:

d3.json("sankey-formatted.json", function(error, graph) {//remove this...

    //your function

});//...and don't fortget to remove this too.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM