如何在没有本地服务器的情况下加载本地 JSON 以创建 d3 图表

How to load local JSON to create a d3 chart without a local server?

本文关键字:加载 JSON 图表 d3 创建 情况下 服务器      更新时间:2023-09-26

我正在尝试在本地复制以下 d3 图表:http://bl.ocks.org/eesur/be2abfb3155a38be4de4

如何加载 JSON 文件?

你不能对文件系统执行 ajax。您可以设置本地 Web 服务器,也可以将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变量的赋值。