我正在尝试在本地复制以下 d3 图表: http://bl.ocks.org/eesur/be2abfb3155a38be4de4
如何加载 JSON 文件?
最佳答案
您无法对文件系统执行 ajax。您可以设置本地网络服务器,也可以将 marvel.json
文件的内容复制到脚本中:
var json = {
"name": "marvel",
"img": "https://dl.dropboxusercontent.com/u/19954023/marvel_force_chart_img/marvel.png",
"children": [
{
"name": "Heroes",
"children": [
{
"hero": "Spider-Man",
....
vis = d3.select("#vis").append("svg").attr("width", w).attr("height", h);
root = json;
root.fixed = true;
root.x = w / 2;
root.y = h / 4;
// Build the path
var defs = vis.insert("svg:defs")
.data(["end"]);
您需要将函数 d3.json("marvel.json", function(json) {
替换为对 json
变量的赋值。
关于javascript - 如何在没有本地服务器的情况下加载本地 JSON 以创建 d3 图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34942402/