【问题标题】:How to load local JSON to create a d3 chart without a local server?如何在没有本地服务器的情况下加载本地 JSON 以创建 d3 图表?
【发布时间】:2016-04-28 19:10:58
【问题描述】:

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

如何加载 JSON 文件?

【问题讨论】:

  • 我的答案和小提琴是否解决了问题?如果是,请采纳。

标签: javascript html json d3.js


【解决方案1】:

您不能对文件系统执行 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 变量的赋值。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 2012-10-11
  • 1970-01-01
  • 2020-02-01
相关资源
最近更新 更多