Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我如何让d3树形图在本地工作?_Javascript_Html_Svg_D3.js - Fatal编程技术网

Javascript 我如何让d3树形图在本地工作?

Javascript 我如何让d3树形图在本地工作?,javascript,html,svg,d3.js,Javascript,Html,Svg,D3.js,我正在尝试学习使用d3,并制作类似于这棵可折叠树的东西: 如何使此交互式树形图在本地工作 我已从web将以下文件全部保存在同一目录中: 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 这生成了图形,但看起来chro

我正在尝试学习使用d3,并制作类似于这棵可折叠树的东西:

如何使此交互式树形图在本地工作

我已从web将以下文件全部保存在同一目录中:

   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保存了一个已经绘制好的SVG,该SVG是由tree.html中的javascript创建的,并且树不像上面的链接那样是交互式的

查看web上工作版本的源代码后,我发现代码有所不同,因此我将其粘贴到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>

可能是因为我无法在本地运行此程序,需要通过web服务器访问tree.html吗?

需要通过http请求加载.json文件,因此将文件放在web服务器上并通过
http://myserver/tree.html
修复了这个问题。

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

  • 开放式终端
  • 导航到您正在使用的文件目录 (如果您的文件位于桌面上名为 项目:
    cd桌面/project
  • 使用Python运行服务器: 键入
    python-msimplehttpserver 1234&。
    数字是端口,您可以使用任意四位数字
  • 转到浏览器并键入
    localhost:1234
  • localhost:1234/page2.html
    用于其他文件

其他答案也可以,但如果您不想启动web服务器,还有一种选择:

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

  • 您可以启动带有标志“允许从文件访问文件”的Chrome,JSON将被加载


我没有使用本地/web服务器,我可以毫无问题地在本地加载JSON文件。。。我认为实际发生的是:如果您对HTML文档使用本地/web HTTP服务器,则必须通过HTTP请求加载JSON。如果您在脚本中使用静态HTML文件,只要您在本地存储了
d3.js
,就可以加载本地JSON文件。我还建议您避免使用本地服务器,只需在mozilla或google chrome中打开它。就像@mef所说的,如果你在这两种浏览器中打开
.html
,它应该像本地托管的网页一样显示它。另外,不要使用IE,其中的svg呈现是垃圾。
XMLHttpRequest cannot load file:///home/user/Downloads/d3_graph/tree_files/flare.json. Cross origin requests are only supported for HTTP.