繁体   English   中英

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

[英]Fill Chart js with $http.get() data

尝试从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>'

    };

  });

我遇到的问题是,大多数示例都使用相同的格式为图表提供数据(带有相同标签(如value / label / color / highlight)的静态数据)。

对于我的需要,颜色或高光并不重要,但是我需要从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条形图完成了类似的操作,但对于甜甜圈图却看不到,而且似乎无法正常工作。 也许不可能吗?

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

编辑答复我接受了杜波夫的建议。 还发现我的网络服务链接被弄乱了,所以我没有得到任何数据: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>'

    };

  });

Chart.js希望数据的格式与静态示例一样。 您是否可以仅将具有所需数据的对象添加到数据数组? 像这样:

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

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

暂无
暂无

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

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