2

http://bl.ocks.org/mbostock/4339083 am using this

instead of d3.json("/d/4063550/flare.json", function(error, flare) {

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

var jsonData = [{
 "name": "A",
 "children": [  
  {"name": "A1", "children": [{"name": "A12"},{"name": "A13"},{"name": "A14"}]  },
  {"name": "A2",    "children": [{"name": "A22"},{"name": "A23"},{"name": "A24"}]  }
 ]
}];

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

1 Answer 1

3

Solution:

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

2.use one function to get the JSON data

Fiddle for 1 solution

Fiddle for 2 solution

    var root = getData(),
    nodes = cluster.nodes(root),
    links = cluster.links(nodes);
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for the reply, am not well versed with js. d3.json("mydata.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); }); i see that the file is called here, and the function update elsewhere too . how do i update this piece of code with a json array.
basically can you make the changes in bl.ocks.org/mbostock/4339083 ? i want to use the var i declared instead of the json file. thanks man
perfect, understood how you made use of the collapse function now. thanks a ton bro, works like a charm !!
what is the use of size in that data structure? for visualization is it used anywhere?
It's a while down the line, but I just found this one useful - making d3 documents with user specific data means inlining the JSON is actually one of the better approaches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.