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 callingformData()
at anytime? I assume that you must since it hasgoogle.charts.setOnLoadCallback(drawChart(dataArr));
IsdataArr
derived fromformData()
? Thisworksheet
parameter...what is it? Are you trying to add the data ofworksheet
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
1 Answer
Reset to default 8setOnLoadCallback
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条)