FeKuLa
FeKuLa

Reputation: 402

embed json file within d3.js (d3.layout.partition)

I am using this: http://mbostock.github.io/d3/talk/20111018/partition.html

instead of d3.json("flare.json", function(root) {

how do i make it use the json file within the html, like say i have

var json = [{
"name": "flare",
"children": [
{"name": "analytics",
"children": [
{"name": "cluster","children": [{"name": "AgglomerativeCluster", "size": 3938},
{"name": "CommunityStructure", "size": 3812},
{"name": "HierarchicalCluster", "size": 6714},
{"name": "MergeEdge", "size": 743}
]
},  

and i want to use this instead of an external json file, how do i achieve this?

Link to json file: http://mbostock.github.io/d3/talk/20111018/flare.json

Any JSFiddle example please?

Thank you.

Upvotes: 0

Views: 644

Answers (1)

Manoj
Manoj

Reputation: 1890

You will achieve this by two method:

1.you can assign the JSON data to variable name then You can build any layout

2.use one function to get the JSON data

Fiddle for 1 solution

var root = json;

Fiddle for 2 solution

var root = getData();
  var g = vis.selectAll("g")
      .data(partition.nodes(root))
    .enter().append("svg:g")
      .attr("transform", function(d) { return "translate(" + x(d.y) + "," + y(d.x) + ")"; })
      .on("click", click);

Upvotes: 2

Related Questions