javascript - Passing in parameter in Google Charts function - Stack Overflow

I am using Google Charts to display some data. I initialize the chart I want and then call a function t

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=".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?

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./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?

Share Improve this question edited Nov 27, 2017 at 2:18 WhiteHat 61.3k7 gold badges53 silver badges136 bronze badges asked Nov 27, 2017 at 1:25 user2896120user2896120 3,2828 gold badges46 silver badges109 bronze badges 2
  • What exactly is dataArr? Are you calling formData() at anytime? I assume that you must since it has google.charts.setOnLoadCallback(drawChart(dataArr)); Is dataArr derived from formData() ? This worksheet parameter...what is it? Are you trying to add the data of worksheet to the chart? It's a lot more plex than just shoving it into a variable. – zer00ne Commented Nov 27, 2017 at 1:49
  • @zer00ne I wanted to make things simpler by removing some things. I am reading from an excel sheet. It creates a JSON object and then calls formatData(worksheet), worksheet is the JSON object. formatData will format the data into an array. dataArr is that formatted data. – user2896120 Commented Nov 27, 2017 at 1:52
Add a ment  | 

1 Answer 1

Reset to default 8

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);
   });
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744326820a4568688.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信