0

Building on from this question here

I have the following code that draws 2 charts based on 2 ajax calls.

google.charts.load('current', {
  packages: ['corechart', 'bar']
}).then(function () {
  $.ajax({
    url: 'data.php',
    dataType: 'json'
  }).done(drawChart1);

  $.ajax({
    url: 'data.php',
    dataType: 'json'
  }).done(drawChart2);
});

function drawChart1(result) {
  ...
}

function drawChart2(result) {
  ...
}

I am looking at code reuse here. How can I pass variables from each ajax call into the 1 drawChart function? And where would I define the variables in each ajax call before passing them to the draw chart function?

I imagine it would look something like:

  $.ajax({
    url: 'data.php',
    dataType: 'json'
  }).done(drawChart,variable1,variable2);
...

function drawChart(result,variable1,variable2) {
  ...
  //draw chart at variable1 position
  //draw chart at variable2 position
  //variable 1 & 2 would be ids for where I want the charts to be drawn
}

Note: I am currently reading here to try solve this.

EDIT1 - Answer is something like below, kudos

              $.ajax({
            url: 'data.php',
            dataType: 'json'
          }).done(function(jsonData) {
            drawChart(jsonData, "variable1", "variable2");
          })

          function drawChart(result, v1, v2) {

            console.log("drawChart called")
            console.log("result")
            console.log(result)
            console.log("v1")
            console.log(v1)
            console.log("v2")
            console.log(v2)
          }
HattrickNZ
  • 4,373
  • 15
  • 54
  • 98

0 Answers0