简体   繁体   English

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

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

Trying to get some data off of a webservice and display it in a chart. 尝试从Web服务中获取一些数据并将其显示在图表中。 I figured chart js would be a good means to do so. 我认为图表js将是一个很好的方法。 (actually using tc-angular-chartjs). (实际上使用tc-angular-chartjs)。 The $http.get( ) call I am using to test is: 我用来测试的$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);
  });
});

and here is the whole js file for just the doughnut chart: 这是整个甜甜圈图的整个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>'

    };

  });

The issue I am running into is that most of these examples use the same format to supply the chart with data (static data with the same labels like value/label/color/highlight). 我遇到的问题是,大多数示例都使用相同的格式为图表提供数据(带有相同标签(如value / label / color / highlight)的静态数据)。

For my needs the colors or highlight dont really matter but I need the data pulled from a wesbervice where the value I need for the chart is called name and the label for the chart is called score . 对于我的需要,颜色或高光并不重要,但是我需要从wesbervice提取数据,在该数据中,图表所需的值称为name ,图表的标签称为score

So I was thinking I could do the $http.get( ) call and put the labels and scores into 2 different arrays and then the data portion in the js would look something like: 所以我想我可以进行$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]
};

I saw something like this done for a Chart.js bar graph, but not for a doughnut graph, and I cannot seem to get it to work. 我看到对Chart.js条形图完成了类似的操作,但对于甜甜圈图却看不到,而且似乎无法正常工作。 Maybe it isnt possible? 也许不可能吗?

Are there any other alternatives? 还有其他选择吗? I mean I cannot be the only person who needs to display dynamic data into a nice responsive chart, but all of the examples use static data. 我的意思是我不是唯一需要将动态数据显示为漂亮的响应式图表的人,但是所有示例都使用静态数据。

EDIT ANSWER I took dubhov's advice. 编辑答复我接受了杜波夫的建议。 Also found out my webservice link was messed up so I wasnt getting any data :p. 还发现我的网络服务链接被弄乱了,所以我没有得到任何数据:p。 Here is the new js for future reference: 这是新的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 expects the data to be formatted like it is with the static examples. Chart.js希望数据的格式与静态示例一样。 Can't you just add an object to the data array with the data you need? 您是否可以仅将具有所需数据的对象添加到数据数组? Like this: 像这样:

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

As far as the colors, which may or may not be required by the API (I think they will be), you can either go random, or if you know that your dataset is limited, you can select from a static list of colors. 至于API可能需要或可能不需要的颜色(我认为它们是必需的),您可以随机选择,或者如果您知道数据集有限,则可以从静态颜色列表中进行选择。 Here's some ideas on randoms: https://stackoverflow.com/a/25709983/769971 这是一些关于随机的想法: https ://stackoverflow.com/a/25709983/769971

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

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