javascript - D3 - 循环包装多个数据

标签 javascript json d3.js circle-pack

我对 D3 有点陌生,在理解它方面仍然存在一些问题。

我正在使用本教程Zoomable Circle Packing :

但是,我不知道如何加载多个数据集。

例如,我需要类似 ( you can see on jsfiddle ) 的内容,但按下按钮时,会加载不同的 .JSON 文件(两个文件中的名称相同,但值不同)。

解决方案可能是 mbostock 的“Thinking with Joins”,但我真的不知道如何使用它。

如有任何帮助,我们将不胜感激。

最佳答案

您可以使用函数来调用 json 文件的加载,如下所示:

var callJson = function (json) {
  d3.json(json, function(error, root) {
    if (error) return console.error(error);

    svg.selectAll("circle").remove();
    svg.selectAll("text").remove();

    var focus = root,
        nodes = pack.nodes(root),
        view;

    var circle = svg.selectAll("circle")
        .data(nodes)
      .enter().append("circle")
        .attr("class", function(d) { return d.parent ? d.children ? "node" : "node node--leaf" : "node node--root"; })
        .style("fill", function(d) { return d.children ? color(d.depth) : null; })
        .on("click", function(d) { if (focus !== d) zoom(d), d3.event.stopPropagation(); });

    var text = svg.selectAll("text")
        .data(nodes)
      .enter().append("text")
        .attr("class", "label")
        .style("fill-opacity", function(d) { return d.parent === root ? 1 : 0; })
        .style("display", function(d) { return d.parent === root ? null : "none"; })
        .text(function(d) { return d.name; });

    var node = svg.selectAll("circle,text");

    d3.select("body")
        .style("background", color(-1))
        .on("click", function() { zoom(root); });

    zoomTo([root.x, root.y, root.r * 2 + margin]);

    function zoom(d) {
      var focus0 = focus; focus = d;

      var transition = d3.transition()
          .duration(d3.event.altKey ? 7500 : 750)
          .tween("zoom", function(d) {
            var i = d3.interpolateZoom(view, [focus.x, focus.y, focus.r * 2 + margin]);
            return function(t) { zoomTo(i(t)); };
          });

      transition.selectAll("text")
        .filter(function(d) { return d.parent === focus || this.style.display === "inline"; })
          .style("fill-opacity", function(d) { return d.parent === focus ? 1 : 0; })
          .each("start", function(d) { if (d.parent === focus) this.style.display = "inline"; })
          .each("end", function(d) { if (d.parent !== focus) this.style.display = "none"; });
    }

    function zoomTo(v) {
      var k = diameter / v[2]; view = v;
      node.attr("transform", function(d) { return "translate(" + (d.x - v[0]) * k + "," + (d.y - v[1]) * k + ")"; });
      circle.attr("r", function(d) { return d.r * k; });
    }
  });
};

...然后用 callJson("flare.json"); 调用它;

这里是多个 json 文件的工作示例 - http://bl.ocks.org/chule/74e95deeadd353e42034

关于javascript - D3 - 循环包装多个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23595436/

相关文章:

javascript - Servlet 过滤器不允许在 jsp 文件中上传指向 js 库的链接

c# - 有什么理由不使用 JSONP 吗?

javascript - JSON Cookie 问题

javascript - 将解析的百分比数据转换为新数组

javascript - 来自外部 json 文件的 D3js 条形图

javascript - d3 中的气泡树?

javascript - 即使调整窗口大小时如何确保元素正确对齐?

javascript - 使用 JavaScript 功能获取命令行 URL

javascript - $pull mongodb 没有删除项目

php - 通过 xpath 按属性值搜索更改 SimpleXML 中的元素值