0
  1. I have the json object in a js file(eg MyFirstMashup.js) and I have copied the script for bubble chart in the same js file. In all the examples avaliable on the web, they have used json files eg. flare.json, xyz.tsv etc. I cannot afford to have this file as I am getting the data dynamically in MyFirstMashup.js file. Instead I have attached the bubble chart code below my existing js codes. But unfortunately the bubble chart code isn't working specially in data.json() part. I think I don't have to use this because I already have the JSON object. If not then what needs to be edited in the bubble chart code. The MyFirstMashup.js with the bubble chart part and json object**(var jsonObj)** is---

    var jsonStr = JSON.stringify(matrix);
    var jsonObj = JSON.parse(jsonStr); //json object

    var bubble = d3.layout.pack()
          .sort(null)
          .size([diameter, diameter])
          .padding(1.5);
    
    var svg = d3.select("body").append("svg")
          .attr("width", diameter)
          .attr("height", diameter)
          .attr("class", "bubble");
    
    d3.json("flare.json", function(error, root) { //hw to replace flare.json
        if (error) throw error;                   //with jsonObj(any json object)   
    
        var node = svg.selectAll(".node")
            .data(bubble.nodes(classes(root))
            .filter(function(d) { return !d.children; }))
          .enter().append("g")
            .attr("class", "node")
            .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
    
        node.append("title")
            .text(function(d) { return d.className + ": " + format(d.value); });
    
        node.append("circle")
            .attr("r", function(d) { return d.r; })
            .style("fill", function(d) { return color(d.packageName); });
    
        node.append("text")
            .attr("dy", ".3em")
            .style("text-anchor", "middle")
            .text(function(d) { return d.className.substring(0, d.r / 3); });
      });
    
     // Returns a flattened hierarchy containing all leaf nodes under the root.
      function classes(root) {
        var classes = [];
    
        function recurse(name, node) {
          if (node.children) node.children.forEach(function(child) { recurse(node.name, child); });
          else classes.push({packageName: name, className: node.name, value: node.size});
        }
    
        recurse(null, root);
        return {children: classes};
      }
    
      d3.select(self.frameElement).style("height", diameter + "px");
    

1 Answers1

1

Then, instead of using the d3.json function you can create a render function like this:

var jsonStr = JSON.stringify(matrix);
var jsonObj = JSON.parse(jsonStr); //json object

var bubble = d3.layout.pack()
      .sort(null)
      .size([diameter, diameter])
      .padding(1.5);

var svg = d3.select("body").append("svg")
      .attr("width", diameter)
      .attr("height", diameter)
      .attr("class", "bubble");

render(jsonObj); // And simply call it like this.

function render(root) {  

    var node = svg.selectAll(".node")
        .data(bubble.nodes(classes(root))
        .filter(function(d) { return !d.children; }))
      .enter().append("g")
        .attr("class", "node")
        .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

    node.append("title")
        .text(function(d) { return d.className + ": " + format(d.value); });

    node.append("circle")
        .attr("r", function(d) { return d.r; })
        .style("fill", function(d) { return color(d.packageName); });

    node.append("text")
        .attr("dy", ".3em")
        .style("text-anchor", "middle")
        .text(function(d) { return d.className.substring(0, d.r / 3); });
  };

// Returns a flattened hierarchy containing all leaf nodes under the root.
  function classes(root) {
    var classes = [];

    function recurse(name, node) {
      if (node.children) node.children.forEach(function(child) { recurse(node.name, child); });
      else classes.push({packageName: name, className: node.name, value: node.size});
    }

    recurse(null, root);
    return {children: classes};
  }



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

or remove the render function and directly use jsonObj inside .data(bubble.nodes(classes(jsonObj ))

var jsonStr = JSON.stringify(matrix);
var jsonObj = JSON.parse(jsonStr); //json object

var bubble = d3.layout.pack()
      .sort(null)
      .size([diameter, diameter])
      .padding(1.5);

var svg = d3.select("body").append("svg")
      .attr("width", diameter)
      .attr("height", diameter)
      .attr("class", "bubble");  

var node = svg.selectAll(".node")
        .data(bubble.nodes(classes(jsonObj))
        .filter(function(d) { return !d.children; }))
      .enter().append("g")
        .attr("class", "node")
        .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });

node.append("title")
        .text(function(d) { return d.className + ": " + format(d.value); });

node.append("circle")
        .attr("r", function(d) { return d.r; })
        .style("fill", function(d) { return color(d.packageName); });

node.append("text")
        .attr("dy", ".3em")
        .style("text-anchor", "middle")
        .text(function(d) { return d.className.substring(0, d.r / 3); });


// Returns a flattened hierarchy containing all leaf nodes under the root.
  function classes(root) {
    var classes = [];

    function recurse(name, node) {
      if (node.children) node.children.forEach(function(child) { recurse(node.name, child); });
      else classes.push({packageName: name, className: node.name, value: node.size});
    }

    recurse(null, root);
    return {children: classes};
  }



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

Example fiddle:http://jsfiddle.net/mzz9B/47/

eko
  • 39,722
  • 10
  • 72
  • 98
  • You mean to remove the entire render() and insert .data(bubble.nodes(classes(jsonObj )). If so where exactly should I insert it. Sorry for being lame but I am a total newbie. – Ronnie Das Sep 06 '16 at 13:03
  • @RonnieDas I've updated my answer for the second part – eko Sep 06 '16 at 13:17