Google Charts API shows blank screen with $(document).ready method

前端 未结 9 1137
清酒与你
清酒与你 2021-02-05 03:36

I\'ve got several functions that instantiate various charts using Google Charts API.

When I call them without jQuery\'s $(document).ready method, everything

9条回答
  •  猫巷女王i
    2021-02-05 03:44

    Here is the paradigm I am using. Set a callback for the google.load method, and don't use the google.setOnLoadCallback at all (AFAIK this is not needed).

    MyChart.prototype.render = function() {
        var self = this;
        google.load("visualization", 
                    "1", 
                    { callback: function() { self.visualize(); }, 
                      packages: ["corechart"] }
                   );
    }
    
    MyChart.prototype.visualize = function() {
    
        var data = google.visualization.arrayToDataTable(
        [
            ['Year', 'Sales', 'Expenses'],  
            ['2004',  1000,      400],
            ['2005',  1170,      460],
            ['2006',  660,       1120],
            ['2007',  1030,      540]   
        ]);
    
        var options = {
            title: 'Company Performance',
            hAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
        };
    
        var chart = new google.visualization.ColumnChart(document.getElementById("mychart_div"));    
        chart.draw(data, options);
    }
    

提交回复
热议问题