Google Visualization API google.charts.Bar(chartDiv)返回undefined(Google Visualization API google.char

编程入门 行业动态 更新时间:2024-10-15 00:25:30
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"));

更多推荐

本文发布于:2023-07-30 17:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338881.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:API   google   Google   Visualization   charts

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!