用$ http.get()数据填充图表js

时间:2015-05-04 19:11:26

标签: javascript jquery json angularjs charts

尝试从Web服务中获取一些数据并将其显示在图表中。我认为图表js是一个很好的方法。 (实际上使用tc-angular-chartjs)。我用来测试的$http.get( )电话是:

$http.get('http://myjson.com/1chr1').success(function(data2) {
  data2.forEach(function(r) {
    $scope.labels.push(r.name);
    $scope.scores.push(r.score);
  });
});

这里只是圆环图的整个js文件:

'use strict';
angular
  .module( 'app.doughnut', [] )
  .controller( 'DoughnutCtrl', function ( $scope ) {
    $scope.labels = [];
    $scope.scores = [];

    $scope.data = [
      {
        value: 700,
        color:'#F7464A',
        highlight: '#FF5A5E',
        label: 'Red'
      },
      {
        value: 50,
        color: '#46BFBD',
        highlight: '#5AD3D1',
        label: 'Green'
      },
      {
        value: 100,
        color: '#FDB45C',
        highlight: '#FFC870',
        label: 'Yellow'
      }
    ];

    $scope.options =  {

      // Sets the chart to be responsive
      responsive: true,

      //Boolean - Whether we should show a stroke on each segment
      segmentShowStroke : true,

      //String - The colour of each segment stroke
      segmentStrokeColor : '#fff',

      //Number - The width of each segment stroke
      segmentStrokeWidth : 2,

      //Number - The percentage of the chart that we cut out of the middle
      percentageInnerCutout : 50, // This is 0 for Pie charts

      //Number - Amount of animation steps
      animationSteps : 100,

      //String - Animation easing effect
      animationEasing : 'easeOutBounce',

      //Boolean - Whether we animate the rotation of the Doughnut
      animateRotate : true,

      //Boolean - Whether we animate scaling the Doughnut from the centre
      animateScale : false,

      //String - A legend template
      legendTemplate : '<ul class="tc-chart-js-legend"><% for (var i=0; i<segments.length; i++){%><li><span style="background-color:<%=segments[i].fillColor%>"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>'

    };

  });

我遇到的问题是,大多数这些示例使用相同的格式为图表提供数据(静态数据具有相同的标签,如值/标签/颜色/突出显示)。

根据我的需要,颜色或突出显示并不重要,但我需要从wesbervice中提取的数据,其中我需要的图表名称为name,图表的标签称为score

所以我认为我可以进行$http.get( )调用并将标签和分数放入2个不同的数组中,然后js中的数据部分看起来像:

$scope.data = {
    labels : ["option 1","option 2","option 3"],
    values : [ 10, 20, 30 ],
    datasets: [ value : values, color : #F7484A, highlight : #FF5A5E, label : labels]
};

我在Chart.js条形图中看到过这样的事情,但不是圆形图,我似乎无法让它工作。也许这不可能?

还有其他选择吗?我的意思是我不能成为唯一需要将动态数据显示在一个漂亮的响应图表中的人,但所有示例都使用静态数据。

编辑答案我接受了dubhov的建议。还发现我的webservice链接搞砸了所以我没有得到任何数据:p。以下是未来参考的新js:

'use strict';
angular
  .module('app.doughnut', [])
  .controller('DoughnutCtrl', function($scope, $http) {

    $http.get('https://api.myjson.com/bins/1chr1').success(function(data2) {
      $scope.data = [];
      data2.forEach(function(r) {
        $scope.data.push({
          'value': r.score,
          'color': '#F7464A',
          'highlight': '#FF5A5E',
          'label': r.name
        });
      });
    });


    $scope.options = {

      // Sets the chart to be responsive
      responsive: true,

      //Boolean - Whether we should show a stroke on each segment
      segmentShowStroke: true,

      //String - The colour of each segment stroke
      segmentStrokeColor: '#fff',

      //Number - The width of each segment stroke
      segmentStrokeWidth: 2,

      //Number - The percentage of the chart that we cut out of the middle
      percentageInnerCutout: 50, // This is 0 for Pie charts

      //Number - Amount of animation steps
      animationSteps: 100,

      //String - Animation easing effect
      animationEasing: 'easeOutBounce',

      //Boolean - Whether we animate the rotation of the Doughnut
      animateRotate: true,

      //Boolean - Whether we animate scaling the Doughnut from the centre
      animateScale: false,

      //String - A legend template
      legendTemplate: '<ul class="tc-chart-js-legend"><% for (var i=0; i<segments.length; i++){%><li><span style="background-color:<%=segments[i].fillColor%>"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>'

    };

  });

1 个答案:

答案 0 :(得分:2)

Chart.js期望数据的格式与静态示例一样。你不能只用你需要的数据将对象添加到数据数组吗?像这样:

$scope.data.push({'value':r.score,
                  'label':r.name,
                  'color':'#RANDOMCOLOR',
                  'highlight':'#SLIGHTLYSHADEDRANDOMCOLOR'});

对于API可能需要或可能不需要的颜色(我认为它们将是),您可以随机,或者如果您知道数据集有限,则可以从静态列表中进行选择的颜色。以下是关于randoms的一些想法:https://stackoverflow.com/a/25709983/769971