Search code examples
javascriptjsond3.jscircle-pack

D3 - circle packing multiple data


I am a bit new to D3 and still have some problems with understanding it.

I am using this tutorial Zoomable Circle Packing:

However, I don't know how to load multiple data sets.

For example I need something like (you can see on jsfiddle) but when the button is pressed, a different .JSON file is loaded (the names in both files are the same, but values are different).

The solution might be "Thinking with Joins" by mbostock but I really dont know how to use it.

Any help would be appreciated.


Solution

  • You can use function to call loading of json file like this:

    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; });
        }
      });
    };
    

    ... and then you call it with callJson("flare.json");

    Here is working example with multiple json files - http://bl.ocks.org/chule/74e95deeadd353e42034