Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing in parameter in Google Charts function

I am using Google Charts to display some data. I initialize the chart I want and then call a function that formats my data. That function will then call the drawChart(parameter) function to draw the chart. The google.charts.setOnLoadCallback(drawChart(parameter)); function does this. When I do this, however, it gives me this error: Uncaught TypeError: Cannot read property 'DataTable' of undefined Here's my code:

HTML

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
        <script type="text/javascript" src="chart.js"></script>
    </head>
    <body>
        <div id="chart_div"></div>

        <script>
            google.charts.load('current', {
            'packages': ['annotationchart']
            });

        </script>
    </body>
</html>

chart.js

function formatData(worksheet) {
       //excess function removed to make question simpler.

       google.charts.setOnLoadCallback(drawChart(dataArr));
   }

  function drawChart(dataArr) {
       var data = new google.visualization.DataTable();
       data.addColumn('datetime', 'Time');
       data.addColumn('number', 'In');

       data.addColumn('number', 'Out');

       data.addRows([
          [new Date(2017, 2, 15, 06, 00, 00), 12400,
            10645],
          [new Date(2017, 2, 15, 07, 00, 00), 24045,
            12374
          ],
          [new Date(2017, 2, 15, 08, 00, 00), 35022,
            15766
          ],
          [new Date(2017, 2, 15, 09, 00, 00), 12284,
            34334
          ],
          [new Date(2017, 2, 15, 10, 00, 00), 8476,
            66467
          ],
          [new Date(2017, 2, 15, 11, 00, 00), 0,
            79463
          ]
        ]);

       var chart = new google.visualization.AnnotationChart(document.getElementById('chart_div'));

       chart.draw(data);
   }

When I remove the parameter, the chart gets displayed and there is no error. How can I make it so that it is able to display the chart when I pass in a parameter and not give me that error?

like image 213
user2896120 Avatar asked Mar 08 '23 11:03

user2896120


1 Answers

setOnLoadCallback takes a reference to a --> function
not the result of a --> function()

try it like this...

function formatData(worksheet) {
   google.charts.setOnLoadCallback(function () {
     drawChart(dataArr);
   });
}
like image 190
WhiteHat Avatar answered Mar 14 '23 23:03

WhiteHat