mercredi 6 mai 2015

Doughnut chart updates with twice the data

Working with chart js and got it to do what I needed which is pull data from a webservice and use $interval to update it. When I update the chart using the built in update function it displays again as expected but with the old data in it as well. I thought I was clearing the data out of the array, I am newer to angular so maybe I overlooked something.

Here is the js for what is going on.

angular.module('myapp', [])
  .controller('MainCtrl', function($scope, $http, $interval) {
    $scope.doughnutData = [];
    var myDoughnut = '';
    onload = function() {
      $http.get('http://ift.tt/1JOZRb4').success(function(returnedData) {

        $scope.username = returnedData.name;
        $scope.totalscore = returnedData.totalScore.toFixed(3);

        returnedData.models.forEach(function(x) {
          var score = x.score.toFixed(3);
          console.debug(score);
          if (score >= 75) {
            $scope.doughnutData.push({
              'value': x.score,
              'color': '#F7464A',
              'highlight': '#FF5A5E',
              'label': x.name
            });
          } else if (score >= 50) {
            $scope.doughnutData.push({
              'value': x.score,
              'color': '#FDB45C',
              'highlight': '#FFC870',
              'label': x.name
            });
          } else {
            $scope.doughnutData.push({
              'value': x.score,
              'color': '#424242',
              'highlight': '#686868',
              'label': x.name
            });
          }
        });
        $scope.doughnutData = sortByKey($scope.doughnutData, 'value');
        var ctx = document.getElementById("chart-area").getContext("2d");
        myDoughnut = new Chart(ctx).Doughnut($scope.doughnutData, {
          responsive: true
        });
      });
      mainInterval = $interval(function() {
         doughnutData = '';
        $http.get('http://ift.tt/1JOZRb4').success(function(returnedData) {
          $scope.username = returnedData.name;
          $scope.totalscore = returnedData.totalScore.toFixed(3);

          returnedData.models.forEach(function(x) {
            var score = x.score.toFixed(3);
            console.debug(score);
            if (score >= 75) {
              $scope.doughnutData.push({
                'value': x.score,
                'color': '#F7464A',
                'highlight': '#FF5A5E',
                'label': x.name
              });
            } else if (score >= 50) {
              $scope.doughnutData.push({
                'value': x.score,
                'color': '#FDB45C',
                'highlight': '#FFC870',
                'label': x.name
              });
            } else {
              $scope.doughnutData.push({
                'value': x.score,
                'color': '#424242',
                'highlight': '#686868',
                'label': x.name
              });
            }
          });
          $scope.doughnutData = sortByKey($scope.doughnutData, 'value');
          myDoughnut.segments = doughnutData;
          myDoughnut.update();
        });
      }, 5000);
      $scope.$apply();
    };

    function sortByKey(array, key) {
      return array.sort(function(a, b) {
        var x = a[key];
        var y = b[key];
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
      });
    }
  });

So it starts out with 9 values, and when it updates it has 18, etc.. There is nothing currently that will update (I just have it in there now for when I move it to my production webservice). I was thinking the doughnutData = ''; in the $interval section would clear it out?

Wanted to include the html as well to give a better idea of what is going on:

<html>

  <head>
    <script src="http://ift.tt/1Gufy1b"></script>
    <script src="../Chart.js"></script>
    <script src="../src/Chart.Doughnut.js"></script>
    <script src="http://ift.tt/198QQJG"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="../script.js"></script>
  </head>

  <body ng-app="myapp" ng-controller="MainCtrl">
    <div id="canvas-holder">
      <canvas id="chart-area" width="50" height="50"></canvas>
    </div>
  </body>

</html>

Aucun commentaire :

Enregistrer un commentaire