AngularJS Google Charts -Timeline

AngularJS Google Charts -Timeline

本文关键字:-Timeline Charts Google AngularJS      更新时间:2023-09-26

在使用以下代码使用 AngularJS 生成时间线图时,我遇到了一个问题。

基本上我找不到导入时间线包的正确方法。就像下面导入核心图表包的导入一样,我该如何导入时间线......

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

我尝试使用

<script type="text/javascript"  src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['timeline']}]}"></script>

屁股它没有用。

控制器

"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']});

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","Name")
    $scope.data1.dataTable.addColumn("number","Qty")
    $scope.data1.dataTable.addRow(["Test",1]);
    $scope.data1.dataTable.addRow(["Test2",2]);
    $scope.data1.dataTable.addRow(["Test3",3]);
    $scope.data1.title="My Pie"
    $scope.data2 = {};
    $scope.data2.dataTable = new google.visualization.DataTable();
    $scope.data2.dataTable.addColumn("string","Name")
    $scope.data2.dataTable.addColumn("number","Qty")
    $scope.data2.dataTable.addRow(["Test",1]);
    $scope.data2.dataTable.addRow(["Test2",2]);
    $scope.data2.dataTable.addRow(["Test3",3]);

    $scope.data4 = {};
    $scope.data4.dataTable = new google.visualization.DataTable();
    $scope.data4.dataTable.addColumn({ type: 'string', id: 'President' })
    $scope.data4.dataTable.addColumn({ type: 'date', id: 'Start' })
    $scope.data4.dataTable.addColumn({ type: 'date', id: 'End' })
    $scope.data4.dataTable.addRows([[ 'Down',      new Date(1797, 2, 3,20.20),  new Date(1797, 2, 4,21.20) ],
          [ 'Jefferson',  new Date(1801, 2, 4,20.20),  new Date(1801, 2, 4,20.20) ]]);
    $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]);
});

网页

<!DOCTYPE html>  
<html lang="en">  
    <head>
         <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js" type="text/javascript"></script>
         <script src="https://www.google.com/jsapi" type="text/javascript"></script>
         <script src="main.js" type="text/javascript"></script>
         <script src="ngGoogleCharts.js" type="text/javascript"></script>
         <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['timeline']}]}"></script>
         <style type="text/css">
             .bigGraph {width:500px;height:500px;float:left;}
             .mediumGraph {width:400px;height:400px;float:left;}
             .smallGraph {width:200px;height:200px;float:left;}
         </style>
    </head>
    <body ng-controller="IndexCtrl">
        <div google-chart="PieChart" ng-model="data1" class="bigGraph"></div>
        <div google-chart="BarChart" ng-model="data2" class="mediumGraph"></div>
        <div google-chart="LineChart" ng-model="data3" class="smallGraph"></div>
        <div google-chart="TimeLine" ng-model="data4" class="smallGraph"></div>
    </body>
</html>  

谷歌图表.js

"use strict";
var googleChart = googleChart || angular.module("google-chart",[]);
googleChart.directive("googleChart",function(){  
    return{
        restrict : "A",
        link: function($scope, $elem, $attr){
            var dt = $scope[$attr.ngModel].dataTable;
            var options = {};
            if($scope[$attr.ngModel].title)
                options.title = $scope[$attr.ngModel].title;
            console.log($scope[$attr.ngModel]+' 't envirName= ', $attr.googleChart+' Ele 't '+$elem[0]);
            var googleChart = new google.visualization[$attr.googleChart]($elem[0]);
            googleChart.draw(dt,options)
        }
    }
});

很可能您收到错误,因为google-chart指令的值设置为 TimeLine,并且找不到具有此类键的对象:

var googleChart = new google.visualization[$attr.googleChart]($elem[0]);

时间轴谷歌图表的类名是google.visualization.Timeline

因此,解决方案是将值从 TimeLine 替换为 Timeline

<div google-chart="Timeline" ng-model="data4" class="smallGraph"></div>

普伦克