如何从日期获取年份并在angularjs图表中使用它

时间:2017-02-07 15:29:27

标签: angularjs fusioncharts angular-chart

你好我正在使用angularjs图表。此处呈现的图表数据位于date format ("dd-mm-yyyy")date我必须获取year并在图表中显示。但我不确定如何获得,只提供所提供数据中的year。在图表图像中,x轴表示特定年份的份额,y轴表示百分比。在发布的图像中,您可以看到沿y轴的红色框显示完整的date,而我只想在y轴上显示年份。请帮帮我。

HTML:

<html>
<head>
  <title> 
   <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-charts/0.2.7/angular-charts.min.js"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.js"></script>
  </title>
  <body>
  <div ng-app="chartApp" ng-controller="chartController">
    <canvas width="600" height="400" type="column2d" dataformat="json"
               dataSource="{{myDataSource}}"></canvas>
   </div>
   </body>
   </html>

控制器:

var chartApp =angular.module("chartApp",["chart.js"]);
chartApp.controller("chartController",function($scope){

$scope.myDataSource = {
          chart:{ caption : "Share Check with previous years"},
          data :[{
                  label: "26/01/2010/", 
                  value: "80"},

                 {label : "26/01/2011" ,
                  value : "70"},

                 {label : "26/01/2012",
                  value : "52"},

                 {label : "26/01/2013",
                  value : "5"},

                 {label : "26/01/2014",
                  value : "33"},

                 {label : "26/01/2015",
                  value : "47"}]};
           });

In the image x-axis represented in percentage and y-axis represent the year of sales

3 个答案:

答案 0 :(得分:1)

为什么不在将data分配给图表之前修改它?

var data = [{
  "label": "20/11/2012",
  "value": "880000"
}];

angular.forEach(data, function(item) {
  item.label = item.label.split('/')[2];
});

$scope.myDataSource.data = data;

答案 1 :(得分:1)

FusionCharts不允许直接使用日期和时间值进行操作。

虽然对于您描述的方案,可以通过将tf.train.batch (日期值)拆分为年份值来实现。

你也可以check this JSFiddle with your data

答案 2 :(得分:0)

您好,您可以通过拆分日期来完成,在JS文件中尝试此代码

.jpg

Here is working fiddle