I have downloaded this script http://bl.ocks.org/mbostock/4339083 which works just fine as an ERB in my Sinatra application to visualize the provided file (as an example).
I would like now to be able to load a JS variable that contains a JSON structure but I am finding a hard time to understand where to do that.
This guy is trying to do EXACTLY what I am after: Render D3 graph from a string of JSON instead of a JSON file
However I am still sort of missing the proper syntax. If you look at the script I linked in the first line, I understand the section that needs re-work is this:
d3.json("/d/4063550/flare.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);
});
In the post linked it's being suggested that I remove the d3.json function but I am not still sure about the end-result (nor where I should put my variable?
I have tried:
function(error, MYVARIABLE) {
root = MYVARIABLE;
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);
};
But that doesn't quite work.
Any hint? Thanks!