简体   繁体   English

google.visualization [$ attr.googleChart]不是构造函数

[英]google.visualization[$attr.googleChart] is not a constructor

I am working on angularjs google charts. 我正在研究angularjs谷歌图表。

In the example i'm trying to show Line chart and Gantt chart, but facing issue displaying the Gantt chart. 在示例中,我试图显示折线图和甘特图,但是在显示甘特图时遇到了问题。

Gantt chart is not shown on the webpage, below is the error shown on the console. 甘特图未显示在网页上,下面是控制台上显示的错误。

angular.js:5754 TypeError: google.visualization[$attr.googleChart] is not a constructor

Any inputs would be helpful. 任何输入都会有所帮助。

js code: js代码:

"use strict";

/*We need to manually start angular as we need to
wait for the google charting libs to be ready*/
google.setOnLoadCallback(function () {
    angular.bootstrap(document.body, ['my-app']);
});
google.load('visualization', '1', { packages: ['corechart'] });
 function daysToMilliseconds(days) {
      return days * 24 * 60 * 60 * 1000;
    } 

var myApp = myApp || angular.module("my-app", ["google-chart"]);

myApp.controller("IndexCtrl", function ($scope) {
    $scope.data1 = {};
    $scope.data1.dataTable = new google.visualization.DataTable();
     $scope.data1.dataTable.addColumn('string', 'Task ID');
     $scope.data1.dataTable.addColumn('string', 'Task Name');
      $scope.data1.dataTable.addColumn('date', 'Start Date');
      $scope.data1.dataTable.addColumn('date', 'End Date');
     $scope.data1.dataTable.addColumn('number', 'Duration');
     $scope.data1.dataTable.addColumn('number', 'Percent Complete');
     $scope.data1.dataTable.addColumn('string', 'Dependencies');

   $scope.data1.dataTable.addRows([
        ['Research', 'Find sources',
         new Date(2015, 0, 1), new Date(2015, 0, 5), null,  100,  null],
        ['Write', 'Write paper',
         null, new Date(2015, 0, 9), daysToMilliseconds(3), 25, 'Research,Outline'],
        ['Cite', 'Create bibliography',
         null, new Date(2015, 0, 7), daysToMilliseconds(1), 20, 'Research'],
        ['Complete', 'Hand in paper',
         null, new Date(2015, 0, 10), daysToMilliseconds(1), 0, 'Cite,Write'],
        ['Outline', 'Outline paper',
         null, new Date(2015, 0, 6), daysToMilliseconds(1), 100, 'Research']
      ]);


    $scope.data3 = {};
    $scope.data3.dataTable = new google.visualization.DataTable();
    $scope.data3.dataTable.addColumn("string", "Name")
    $scope.data3.dataTable.addColumn("number", "Qty")
    $scope.data3.dataTable.addRow(["Test", 1]);
    $scope.data3.dataTable.addRow(["Test2", 2]);
    $scope.data3.dataTable.addRow(["Test3", 3]);
});

You seem to be using an old version of the API. 您似乎正在使用旧版本的API。 Use loader.js, not jsapi.js 使用loader.js,而不是jsapi.js

using loader.js, you can load all the libs you needs by specifiying in the packages array. 使用loader.js,可以通过在packages数组中指定来加载所需的所有库。 In this case, you need packages: ['corechart', 'gantt'] 在这种情况下,您需要使用以下packages: ['corechart', 'gantt']

Here's the demo, updated https://plnkr.co/edit/Fxz9xP7UXqgBs3m0EfSg?p=preview 这是演示,已更新https://plnkr.co/edit/Fxz9xP7UXqgBs3m0EfSg?p=preview

In index.html 在index.html中

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 

and in main.js 和在main.js中

    google.charts.load('visualization', '1', { packages: ['corechart', 'gantt'] });
    google.charts.setOnLoadCallback(function () {
        angular.bootstrap(document.body, ['my-app']);
    });

Things to note: 注意事项:

  1. It's google.charts namespaced, and not google.load directly 它是google.charts命名空间,而不是google.load直接
  2. Make sure you call google.charts.load before google.charts.setOnLoadCallback 确保在google.charts.load之前先致电google.charts.setOnLoadCallback
  3. Make sure you load all the packages you need 确保加载了所有需要的软件包

You can see old version loading info, & limitations on loading charts in Google documentation here 您可以在此处查看旧版本的加载信息以及Google文档中的加载图表限制

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

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