Google Visualization API google.charts.Bar(chartDiv)返回undefined(Google Visualization API google.charts.Bar(chartDiv) returns undefined)
我有一个代码,通过AJAX向数据源发送get请求,然后绘制条形图,如下所示:
google.charts.load("current", {packages: ["bar"]}); google.charts.setOnLoadCallback(drawChart); function drawChart() { $.get("/pop-pyramid", function(data) { var table = new google.visualization.DataTable(data); var chart = google.charts.Bar(document.getElementById("popPyramid")); console.log(chart); chart.draw(table, {stacked: true}); }, "json"); } $(document).ready(function() { $('button#downloadData').click(function(event) { event.preventDefault(); window.location.assign('/path/to/downloadable/file'); }); });这不幸抛出
Uncaught TypeError: Cannot read property 'draw' of undefined at Object.success (statistics.js:11) at i (jquery-3.2.0.min.js:2) at Object.fireWith [as resolveWith] (jquery-3.2.0.min.js:2) at A (jquery-3.2.0.min.js:4) at XMLHttpRequest.<anonymous> (jquery-3.2.0.min.js:4)I have a code that sends a get request to a data source via AJAX then draw a bar chart, like so:
google.charts.load("current", {packages: ["bar"]}); google.charts.setOnLoadCallback(drawChart); function drawChart() { $.get("/pop-pyramid", function(data) { var table = new google.visualization.DataTable(data); var chart = google.charts.Bar(document.getElementById("popPyramid")); console.log(chart); chart.draw(table, {stacked: true}); }, "json"); } $(document).ready(function() { $('button#downloadData').click(function(event) { event.preventDefault(); window.location.assign('/path/to/downloadable/file'); }); });This unfortunately throws
Uncaught TypeError: Cannot read property 'draw' of undefined at Object.success (statistics.js:11) at i (jquery-3.2.0.min.js:2) at Object.fireWith [as resolveWith] (jquery-3.2.0.min.js:2) at A (jquery-3.2.0.min.js:4) at XMLHttpRequest.<anonymous> (jquery-3.2.0.min.js:4)最满意答案
您需要创建一个新实例:
var chart = new google.charts.Bar(document.getElementById("popPyramid"));You need to create a new instance:
var chart = new google.charts.Bar(document.getElementById("popPyramid"));更多推荐
发布评论