0

Hi i have this variable myJSON that i want to use in my d3 force graph this is the output of the variable. i was wondering if there any way to use a variable to populate my graph.

enter image description here

var myJSON;

$.ajax({
type: "POST",
url: "http://localhost/web/search.php",
async: false,
data: {
    "dataA": string2
},
cache: false,
success: function(data) {
    myJSON = JSON.parse(data);
},
});

By just putting the variable into the graphs code but it keeps giving me this error uncaught exception: [object ProgressEvent] does any one know why isnt this working? any suggestion would be appreciated.

d3.json("myJSON", function(error, graph) { if (error) throw error;
5
  • Can you post the rest of your d3js code? I believe it should read d3.json("http://localhost/web/search.php", function(error, data) { console.log(data); // this is your data }); You shouldn't need to ajax it in first. D3 will load it for you. Commented May 25, 2018 at 8:26
  • @Lex Im passing data from my front end to back end "localhost/web/search.php" to get the result from a sql query thats why im using the ajax. Commented May 25, 2018 at 8:31
  • @Lex the only problem i have is not being able to populate the graph with my variable "myJSON" Commented May 25, 2018 at 8:33
  • Yip, its good to have server side assistance. But do you have more D3 code? D3 will handle the ajax for you. Commented May 25, 2018 at 8:34
  • @Lex This is the exact code im using only that i want to populate it with my variable bl.ocks.org/GerardoFurtado/30220ae7281145b32c9e2bf090a5ae1a/… Commented May 25, 2018 at 8:35

2 Answers 2

1

'd3.json' function load data from file or url. To use a local json variable, just make some change.

For example, you have a demo from d3 website, and data in file "data.json" is

{
  "nodes": [
    {"id": "Myriel", "group": 1},
    {"id": "Napoleon", "group": 1},
    {"id": "Mlle.Baptistine", "group": 1}
   ],
    "links": [
      {"source": "Napoleon", "target": "Myriel", "value": 1}
    ]
}

the demo is something like this

d3.json("data.json", function(error, data){
    if(error) throw error;
    console.log(data);

    //some code...
});

It means load data from a file called "data.json".

Now if you already have json variable data

var data = {
  "nodes": [
    {"id": "Myriel", "group": 1},
    {"id": "Napoleon", "group": 1},
    {"id": "Mlle.Baptistine", "group": 1}
   ],
    "links": [
      {"source": "Napoleon", "target": "Myriel", "value": 1}
    ]
};

just use it, change the code to:

console.log(data);
//some code...

Suggestion:

You can load data from url, just like what the comment says. Use AJAX to load data from url, and parse it to d3 code can be done with only d3. d3 will get the data from url.

d3.json("http://localhost/web/search.php", function(error, data){
    if(error) throw error;
    console.log(data);

    //some code...
});
Sign up to request clarification or add additional context in comments.

Comments

1

So as mentioned in the comments. D3 will handle the AJAX for you. If you load the example and look at the network tab in chrome. You can see miserables.json is being loaded in.

enter image description here

This code is actually creating an AJAX GET request for you.

d3.json("miserables.json", function(error, graph) {
  if (error) throw error;
  ...

You can try d3.json("http://localhost/web/search.php?dataA=string2", ... Or optionally load it yourself. And replace graph.nodes with your JSON.

.selectAll("circle")
    .data(graph.nodes)

3 Comments

How do i replace the graph.nodes? with my variable "myJSON"?
replace .data(graph.nodes) with .data(myJSON). You'll need to ensure your variable has loaded before initialising D3. This is starting to drift away from your question though.
bacause i just want to load the " myJSON" javascript object into my graph the ajax is just helping me retrieve the data i need from my database.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.