简体   繁体   English

Google可视化循环-返回google.visualization未定义

[英]Google visualization loop - returns google.visualization is undefined

I am trying to create one function that can handle all the data that is sent to the page to build google charts in different points on the page. 我正在尝试创建一个函数,该函数可以处理发送到页面的所有数据,以在页面的不同位置构建google图表。 The first think that I am doing is properly setting up the data array that the chart will use. 我首先想到的是正确设置图表将使用的数据数组。 That is ok. 那没问题。 The issue that I am having is when the prdocess gets to the "google.visualization.arrayToDataTable([data])" the error I receive is "google.visualization is undefined". 我遇到的问题是当prdocess到达“ google.visualization.arrayToDataTable([data])”时,我收到的错误是“ google.visualization未定义”。 Any help with what I may be doing wrong will be appreciated. 对于我可能做错的任何帮助将不胜感激。

//$(document).ready(function () {
var chart_pie = $('.chart-pie');
var dataObj = [];
var Chart = {} || [];
//console.log(chart_pie);

google.load('visualization', '1.0', {'packages':['corechart']});

        // Set a callback to run when the Google Visualization API is loaded.
        // google.setOnLoadCallback(Chart.draw_chart);

        var chartColors = ['#f0ca41','#c97038', '#14425c',  '#3d799d', '#625e79'];
        var pieHeight = 300;
        var pieWidth = 400;
        var columnHeight = 230;
        var columnWidth = 775;

Chart = {

    init: function () {
        this.pie_chart();
    },

    pie_chart: function () {
        chart_pie.each(function () {
            var chartID = $(this).attr('id');
            chartID = chartID.replace('chart-', '');
            console.log(chartID +': '+ JSON.stringify(chart_data[chartID]));
            Chart.populate_object(chart_data[chartID], chartID);
            google.setOnLoadCallback(Chart.draw_chart(dataObj[chartID]));
            // console.log(JSON.stringify(chart_data[chartID]));
        });
    }, 

    populate_object: function (obj, chartID) {


        for (var i=0;i<obj.length;i++) {
            dataObj[obj.chartID] = [obj[i].Label, obj[i].Value];
        }
    },

    draw_chart: function (data) {
        var chartData = google.visualization.arrayToDataTable([data]);

        var options = {'title':chart_data[chartID].title,
                 'width':pieWidth,
                 'height':pieWidth,
                 borderColor: '#ffffff',
                 'colors': chartColors};

        var chart = new google.visualization.PieChart(document.getElementById('chart-' + chartID));

        chart.draw(chartData, options);
    }
};

Chart.init();
// });

If some one has a function that they created to handle this type of operation for google charts please share. 如果有人拥有他们创建的用于处理Google图表此类操作的功能,请分享。

Your callback is not valid: 您的回叫无效:

google.setOnLoadCallback(Chart.draw_chart(dataObj[chartID]));

Is actually invoking your draw_chart method, so its being called before onLoad has been triggered. 实际上是在调用您的draw_chart方法,因此在触发onLoad之前调用它。 You would need to do something like this: 您将需要执行以下操作:

google.setOnLoadCallback(function () {
  Chart.draw_chart(dataObj[chartID]);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM