1

我正在使用角谷歌圖表https://github.com/angular-google-chart/angular-google-chartGoogle-Chart-angularjs支持「日期」類型嗎?

下面的代碼工作和圖表加載。

x_axis = {id: "t", label: "Date", type: "string"}; 
y_axis = {id: "s", label: "Value (%)", type: "number"};    

ChartObj.data = 
      {"cols": [ 
       horizontal_axis, 
       vertical_axis 
      ], 
       "rows": [ 
        {c:[{v: 1},{v: 98}]},{c:[{v: 2},{v: 90}]},{c:[{v: 3},{v: 120} ]} 
       ] 
      };   

如果我將x_axis從字符串更改爲日期類型;

x_axis = {id: "t", label: "Date", type: "date"}; 

圖表無法加載並出現錯誤。

谷歌的可視化-錯誤-0" ,消息:「C [本人]不是函數

當我檢查文檔https://developers.google.com/chart/interactive/docs/datesandtimes, 日期類型似乎支持。我錯過了什麼嗎?哪裏可以找到角度 - 谷歌圖表的準確文件?迄今爲止的例子似乎是代碼示例的形式。

回答

2

下面的例子演示瞭如何使用Google Chart Tools AngularJS Directive Module指定日期:

angular.module("chartApp", ["googlechart"]) 
 
.controller("GenericChartCtrl", function ($scope) { 
 
    $scope.chartObject = {}; 
 

 
    $scope.chartObject.type = "BarChart"; 
 

 

 
     $scope.chartObject.data = { 
 
      "cols": [ 
 
       { id: "s", label: "Date", type: "date" }, 
 
       { id: "t", label: "Precipitation (mm)", type: "number" }, 
 
      ], 
 
      "rows": [ 
 
       { 
 
        c: [ 
 
         { v: new Date(2000, 0, 1) }, 
 
         { v: 40 }, 
 
        ] 
 
       }, 
 
       { 
 
        c: [ 
 
         { v: new Date(2000, 1, 2) }, 
 
         { v: 50 }, 
 
        ] 
 
       }, 
 
       { 
 
        c: [ 
 
         { v: new Date(2000, 2, 1) }, 
 
         { v: 35 }, 
 
        ] 
 
       }, 
 
       { 
 
        c: [ 
 
         { v: new Date(2000, 3, 1) }, 
 
         { v: 30 }, 
 
        ] 
 
       } 
 
      ] 
 
     }; 
 

 
    $scope.chartObject.options = { 
 
     'title': 'AVERAGE MONTHLY PRECIPITATION OVER THE YEAR (RAINFALL, SNOW)', 
 
     vAxis: { 
 
      format: 'MMM', //display month part 
 
     }, 
 
    }; 
 
});
<script type="text/javascript" src="https://www.google.com/jsapi"></script> 
 
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.18/angular.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-google-chart/0.0.11/ng-google-chart.js"></script> 
 

 
<body ng-app='chartApp' ng-controller="GenericChartCtrl"> 
 
    <div google-chart chart="chartObject" style="height:600px; width:100%;"></div> 
 
</body>