【问题标题】:Using a variable instead of an external file for d3 script使用变量而不是 d3 脚本的外部文件
【发布时间】:2015-01-28 15:42:51
【问题描述】:

我真的很想离开我的舒适区 - 尝试使用 Mike Bostocks 可折叠树来表示一棵树。 (http://bl.ocks.org/mbostock/4339083 原创)。

我的代码工作正常,并且引用我自己的 json 数据文件工作正常,但我需要变量而不是外部文件中的数据。 (由于我使用可视化的平台的限制)。

以下代码调用了一个名为 Objecttree.json 的文件,我想将其替换为一个名为 Objecttree 的类似名称的变量。

这一定很简单,但我已经搜索了现有的答案,但无法使用我的代码中给出的答案。我猜技能甚至低于最低的新手门槛,所以请记住这一点!

无论如何,提前谢谢。

<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>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>

var margin = {top: 20, right: 120, bottom: 20, left: 120},
    width = 3000 - margin.right - margin.left,
    height = 1000 - 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("Objecttree.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>

【问题讨论】:

  • 只需去掉对d3.json的调用,将回调主体移至主代码,将root = flare替换为root = Objecttree..
  • 感谢@PauloScardine,我想我几乎可以完成这项工作。回调主体,是从“root =flare”到并包括“update(root);”的所有内容吗? - 如果是这样,我会遗漏一些东西,因为那似乎还行不通。我有没有提到过这段代码让我很头疼?无论如何,感谢您的耐心..
  • 感谢@LarsKotthoff,我实际上找到了所有这些,但我无法将其转换为我的代码,我可能遗漏了一些小的和语义的东西。

标签: javascript json d3.js


【解决方案1】:

试试这个:

var Objecttree = '{"type":"Topology","transform":{"scale":[0.06690766172651032,0.060532461291637356],"translate":[-7.240804475075604,51.182450466981884]},"objects":{"outerLine":{"type":"GeometryCollection","geometries":[{"type":"LineString","arcs":[0,1]},{"type":"LineString","arcs":[2,1]}]}},"arcs":[[[0,269],[41,82]],[[41,351],[115,70],[68,10],[35,3],[26,1],[11,-4]],[[295,429],[99,-39],[59,-48],[8,-82],[-34,-91],[-39,-84],[-63,-74],[-54,-11],[-54,12],[-41,8],[-103,82],[-56,66],[-10,114],[34,69]]]}';
var parsedObjecttree = JSON.parse(Objecttree);

这肯定是我自己的 json。用您的数据替换 json。

【讨论】:

  • 去掉周围的引号不会得到同样的结果吗?即 var Objecttree = { "type":"Topology",....} 然后使用它而不是 parsedObjectree?当然,假设您信任变量中的内容。如果您有任何疑问,请按照建议使用 JSON.parse。
  • 这对于大多数情况来说都是正确的。但有时 json 对象可能包含一些损害代码的迹象。因此,使用特殊功能确保它始终有效会更安全。我不知道它是哪个符号,但我有一个非常大的 json 文件,并且在没有 JSON.parse 的情况下使用它,但它不起作用。
猜你喜欢
  • 1970-01-01
  • 2021-04-25
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多