JSON.write in DagreJS

I am trying to create nodes for a graph using DagreJS. I am following a demo that uses this as the data set:

g.setNode("kspacey",    { label: "Kevin Spacey",  width: 144, height: 
100 });
g.setNode("swilliams",  { label: "Saul Williams", width: 160, height: 
100 });
g.setNode("bpitt",      { label: "Brad Pitt",     width: 108, height: 
100 });
g.setNode("hford",      { label: "Harrison Ford", width: 168, height: 
100 });
g.setNode("lwilson",    { label: "Luke Wilson",   width: 144, height: 
100 });
g.setNode("kbacon",     { label: "Kevin Bacon",   width: 121, height: 
100 });

I would like to replace this hardcoded data with data I retrieve via an AJAX call to a php page that creates well-formed JSON from a mySQL database.

My idea is to have that AJAX call do retrieve the data object from PHP like this:

$.ajax({
 dataType: "json",
 url: "php/graph_test.php",
 success: function(data) {

for (var i = 0; i < data.length; i++) {
  var d = data[i].author; 
  g.setNode(d);

console.log(dagre.graphlib.json.write(g));

//end for loop
 }
//end success function
}
 //end ajax call
 });

Here is one line from graph_test.php:

 {"title":"The Final Empire: Mistborn","author":"Brandon 
    Sanderson","duration":"1479","readingOrder":"1"}

When I run my console log, I see this:

console log output

If you look at the 'bpitt' entry, the additional information of his full name ('label:' and the width/height variables appear under the 'value' key associated with his node.

In my data object, I can only figure out how to return the 'v' key (in this case the author's name - Brandon Sanderson). I would like to have additional variables from my php data object (title, duration, etc) to appear in the 'value' key but that is where I am stuck.

I believe the function

dagre.graphlib.json.write(g)

is some form of json.stringify?

Thanks in advance for any help you can provide!

728x90

0 Answers JSON.write in DagreJS