0

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!

Community
  • 1
  • 1
mreferre
  • 5,464
  • 3
  • 22
  • 29
  • It's unclear what you mean by "load a JS variable that contains a JSON structure". Do you mean you already have available client-side a JS String variable whose content is a valid JSON? Is that what `MYVARIABLE` is? If so, can you just `JSON.parse(MYVARIABLE)`? – meetamit Jan 23 '15 at 00:31
  • yes @meetamit that's it. MYVARIABLE is a variable available within JS (I can console.log it for example) that contans a valid JSON. Where should I put ` JSON.parse(MYVARIABLE)` and instead of what (in the script I linked above)? Thanks. – mreferre Jan 23 '15 at 07:25

0 Answers0