0

Im kinda new to jquery.

I have a graph in jquery and the graph get the data statically.

and I want it dynamically

this is the static data.

  var data = [
    { label: "New Request",  data: 1.7, color: "#68BC31"},
    { label: "On-Going",  data: 24.5, color: "#2091CF"},
    { label: "Deadlines",  data: 8.2, color: "red"},            
  ]     

I have ajax that returns this value.

{"label":"active","data":"4", color: "#68BC31"}

this is my ajax.

jQuery.ajax({
  type: "POST", 
  url: "dashboard/graph_data/",
  success:function(response){                                                                       

     return response;

     // the response returns this value

     // {"label":"active","data":"4", color: "#68BC31"}

  }
});

how do I replace or convert the static data to my ajax function.

thanks in advance

Marty
  • 5
  • 4
  • 1
    [You can't return a value from an async callback](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Arun P Johny Jul 31 '14 at 06:56

2 Answers2

1
var data = [
    { label: "New Request",  data: 1.7, color: "#68BC31"},
    { label: "On-Going",  data: 24.5, color: "#2091CF"},
    { label: "Deadlines",  data: 8.2, color: "red"},            
  ];

jQuery.ajax({
  type: "POST", // Are you sure you want POST and not GET ?
  url: "dashboard/graph_data/",
  dataType: "json", // If you know the return value type, explicitely type it

  success: function(response){
     data.push(response); // Maybe you'll need to JSON.parse() the response, but not sure
     console.log(data); // Your data array has been updated asynchronously
  }
});
Oliboy50
  • 2,661
  • 3
  • 27
  • 36
0

you have to re fill the data source for graph and then rebind it. For eg

Case 1 : Adding a new data item

jQuery.ajax({
  type: "POST", 
  url: "dashboard/graph_data/",
    success:function(response){                                                                       

data.push(response);

// Rebind function for graph
     // the response returns this value

     // {"label":"active","data":"4", color: "#68BC31"}

  }
});

Case 2 : Fresh Datasource

 jQuery.ajax({
      type: "POST", 
      url: "dashboard/graph_data/",
        success:function(response){                                                                       
    data.removeAll();
    data.push(response);

    // Rebind function for graph
         // the response returns this value

         // {"label":"active","data":"4", color: "#68BC31"}

      }
    });

Make sure you get the json in response, otherwise convert to json before pushing into data

Abhishek Sharma
  • 281
  • 2
  • 6