我尝试使用 d3.json()
导入本地 .json 文件。
文件 filename.json
与我的 html 文件存储在同一文件夹中。
然而 (json) 参数为空。
d3.json("filename.json", function(json) {
root = json;
root.x0 = h / 2;
root.y0 = 0;});
. . .
}
我的代码和这个d3.js example里的基本一样
最佳答案
如果您在浏览器中运行,则 cannot load local files .
但是在命令行上运行开发服务器相当容易,只需 cd
进入包含文件的目录,然后:
python -m SimpleHTTPServer
(或 python -m http.server
使用 python 3)
现在在您的浏览器中,转到 localhost:3000
(或 :8000
或命令行上显示的任何内容)。
以下内容曾经在旧版本的 d3 中工作:
var json = {"my": "json"};
d3.json(json, function(json) {
root = json;
root.x0 = h / 2;
root.y0 = 0;
});
https://stackoverflow.com/questions/17214293/