【问题标题】:How can I get this d3 tree chart to work locally?我怎样才能让这个 d3 树形图在本地工作?
【发布时间】:2013-08-11 23:23:33
【问题描述】:

我正在尝试学习使用 d3 并制作类似于此可折叠树的东西: http://bl.ocks.org/mbostock/4339083

如何使这个交互式树状图在本地工作?

我已将网络上的以下文件全部保存在同一目录中:

   d3_graph/tree_files/tree.html
   d3_graph/tree_files/d3.js
   d3_graph/tree_files/d3.layout.js
   d3_graph/tree_files/flare.json
   d3_graph/tree_files/style.css

这产生了图表,但看起来 chrome 保存了一个由 javascript 在 tree.html 中创建的已经绘制的 SVG,并且树不像上面的链接那样交互。

在网上查看了工作版本的源代码后,我发现代码不同,所以我将其粘贴到 tree.html 并将文件更改为指向 d3.js、d3.layout 的正确位置.js、style.css 和flare.json

这仍然产生了一个空白的白页,我不知道还能做什么。我怎样才能做到这一点?

这是我编辑后的 ​​tree.html 版本:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.node {
  cursor: pointer;
}

.node circle {
  fill: #fff;
  stroke: steelblue;
  stroke-width: 1.5px;
}

.node text {
  font: 10px sans-serif;
}

.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 1.5px;
}

</style>
<body>
    <link type="text/css" rel="stylesheet" href="style.css">
    <script type="text/javascript" src="d3.js"></script>
    <script type="text/javascript" src="d3.layout.js"></script>
    <style type="text/css">


.node circle {
  cursor: pointer;
  fill: #fff;
  stroke: steelblue;
  stroke-width: 1.5px;
}

.node text {
  font-size: 11px;
}

path.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 1.5px;
}

    </style>
<script>

var margin = {top: 20, right: 120, bottom: 20, left: 120},
    width = 960 - margin.right - margin.left,
    height = 800 - margin.top - margin.bottom;

var i = 0,
    duration = 750,
    root;

var tree = d3.layout.tree()
    .size([height, width]);

var diagonal = d3.svg.diagonal()
    .projection(function(d) { return [d.y, d.x]; });

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.right + margin.left)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

d3.json("flare.json", function(error, flare) {
  root = flare;
  root.x0 = height / 2;
  root.y0 = 0;

  function collapse(d) {
    if (d.children) {
      d._children = d.children;
      d._children.forEach(collapse);
      d.children = null;
    }
  }

  root.children.forEach(collapse);
  update(root);
});

d3.select(self.frameElement).style("height", "800px");

function update(source) {

  // Compute the new tree layout.
  var nodes = tree.nodes(root).reverse(),
      links = tree.links(nodes);

  // Normalize for fixed-depth.
  nodes.forEach(function(d) { d.y = d.depth * 180; });

  // Update the nodes…
  var node = svg.selectAll("g.node")
      .data(nodes, function(d) { return d.id || (d.id = ++i); });

  // Enter any new nodes at the parent's previous position.
  var nodeEnter = node.enter().append("g")
      .attr("class", "node")
      .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
      .on("click", click);

  nodeEnter.append("circle")
      .attr("r", 1e-6)
      .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });

  nodeEnter.append("text")
      .attr("x", function(d) { return d.children || d._children ? -10 : 10; })
      .attr("dy", ".35em")
      .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
      .text(function(d) { return d.name; })
      .style("fill-opacity", 1e-6);

  // Transition nodes to their new position.
  var nodeUpdate = node.transition()
      .duration(duration)
      .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });

  nodeUpdate.select("circle")
      .attr("r", 4.5)
      .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });

  nodeUpdate.select("text")
      .style("fill-opacity", 1);

  // Transition exiting nodes to the parent's new position.
  var nodeExit = node.exit().transition()
      .duration(duration)
      .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
      .remove();

  nodeExit.select("circle")
      .attr("r", 1e-6);

  nodeExit.select("text")
      .style("fill-opacity", 1e-6);

  // Update the links…
  var link = svg.selectAll("path.link")
      .data(links, function(d) { return d.target.id; });

  // Enter any new links at the parent's previous position.
  link.enter().insert("path", "g")
      .attr("class", "link")
      .attr("d", function(d) {
        var o = {x: source.x0, y: source.y0};
        return diagonal({source: o, target: o});
      });

  // Transition links to their new position.
  link.transition()
      .duration(duration)
      .attr("d", diagonal);

  // Transition exiting nodes to the parent's new position.
  link.exit().transition()
      .duration(duration)
      .attr("d", function(d) {
        var o = {x: source.x, y: source.y};
        return diagonal({source: o, target: o});
      })
      .remove();

  // Stash the old positions for transition.
  nodes.forEach(function(d) {
    d.x0 = d.x;
    d.y0 = d.y;
  });
}

// Toggle children on click.
function click(d) {
  if (d.children) {
    d._children = d.children;
    d.children = null;
  } else {
    d.children = d._children;
    d._children = null;
  }
  update(d);
}

</script>



</body></html>

编辑: 我刚刚在chrome中检查了控制台发现这个错误:

XMLHttpRequest cannot load file:///home/user/Downloads/d3_graph/tree_files/flare.json. Cross origin requests are only supported for HTTP. 

会不会是本地无法运行,需要通过网络服务器访问tree.html?

【问题讨论】:

    标签: javascript html svg d3.js


    【解决方案1】:

    其他答案有效,但如果您不想启动网络服务器,还有另一种选择:

    • 默认情况下,Firefox 应该会打开您的文件并加载本地 json,因此无需执行任何操作。

    • 您可以使用标志 --allow-file-access-from-files 启动 Chrome,然后将加载 JSON。

    【讨论】:

    • 我也鼓励避免使用本地服务器,只在 mozilla 或 google chrome 中打开它。就像@mef 说的那样,如果您在其中任何一个浏览器中打开.html,它应该像本地托管的网页一样显示它。另外,不要用IE,里面的svg渲染是垃圾。
    【解决方案2】:

    .json 文件需要通过 http 请求加载,因此将文件放在 Web 服务器上并通过 http://myserver/tree.html 访问 tree.html 可以解决此问题。

    【讨论】:

    • 我没有使用本地/网络服务器,我可以毫无问题地加载本地 JSON 文件......我 认为 实际发生的是:如果你为您的 HTML 文档使用本地/网络 HTTP 服务器,您必须通过 HTTP 请求加载 JSON。如果您在脚本中使用静态 HTML 文件,加载 本地 JSON 文件也可以,只要您在本地也存储了 d3.js
    【解决方案3】:

    我在使用 d3 时遇到了同样的问题。该文件可能无法加载,因为某些浏览器具有阻止它们通过 JavaScript 加载本地文件的限制(出于安全考虑)。 我知道您已经回答了您的问题,但为了完整起见,您也可以使用自己的本地计算机来托管和提供文件给自己。

    • 打开终端
    • 导航到您正在使用的文件目录 (使用cd,如果您的文件位于桌面上名为 项目:cd Desktop/project)
    • 使用 Python 运行服务器: typepython -m SimpleHTTPServer 1234 &amp;. 数字是端口,你可以使用任何你喜欢的四位数字。
    • 转到您的浏览器并键入 localhost:1234
    • localhost:1234/page2.html 其他文件

    【讨论】:

      猜你喜欢
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多