0

I am trying to display data on a bar chart using angular-chart. I am trying retrieving the data from a database. I want to display all data on the chart for the last five (5) years including this year and also get the data to correspond with the years. So far I am only using 2 yrs, as shown below.

Json Array enter image description here

Angularjs

app.controller('ChartCtrl', ['$scope', '$http', function ($scope, $http) {

  var currentYear = new Date().getFullYear();
  var allYrs = [];

  // get data from database
  $http.get('/dashboard/incident').success(function(incidents) {
    $scope.incidentCnt = incidents;
    console.log($scope.incidentCnt);

    for (var i = 0; $scope.incidentCnt.length; i++) {

      // Gets the current year and last 4 years (5 yrs total)
      for(var year = currentYear; year >= currentYear-4; year--) {

        allYrs.push(year);
        $scope.labels = allYrs; // display years

        $scope.series = [
          'Aggravated Assault', 
          'Arson', 
          'Burglary', 
          'Forcible Sex Offense', 
          'Hate Crime', 
          'Motor Vehicle Theft', 
          'Murder or Non-Negligent Manslaughter', 
          'Negligent Manslaughter', 
          'Non-Forcible Sex Offense', 
          'Relationship/Dating Violence', 
          'Robbery', 
          'Stalking'
        ];

        $scope.data = [
           [
            $scope.incidentCnt[i].assault, 
            $scope.incidentCnt[i].arson, 
            $scope.incidentCnt[i].burglary, 
            $scope.incidentCnt[i].fSexOffense, 
            $scope.incidentCnt[i].hateCrime, 
            $scope.incidentCnt[i].vehicleTheft, 
            $scope.incidentCnt[i].nonNegligentMaslaughter, 
            $scope.incidentCnt[i].negligentMaslaughter, 
            $scope.incidentCnt[i].nonForcibleSexOffense, 
            $scope.incidentCnt[i].rshipDatingViolence, 
            $scope.incidentCnt[i].robbery, 
            $scope.incidentCnt[i].stalking
           ]
         ];
      }
    };
  });

}]);

Chart's current look

enter image description here

Any help would be much appreciated. Thank you...

user2538755
  • 314
  • 1
  • 6
  • 21

1 Answers1

0

I would simply create a custom filter to show last 5 years data

Filter

app.filter('for5Years', function(){
  return function(values){
    var returnValues = [], 
    date = new Date(), 
    currentYear = date.getFullYear(),
    limitYear = currentYear - 4;
    angular.forEach(values, function(value, index){
       if(value.incidentyear <= currentYear && value.incidentyear >= limitYear){
          returnValues.push(value);
       }
    });
    return returnValues;
  }
});

Controller

app.controller('ChartCtrl', ['$scope', '$http', '$filter', function($scope, $http, $filter) {

    var currentYear = new Date().getFullYear();
    var allYrs = [];

    // get data from database
    $http.get('/dashboard/incident').success(function(incidents) {
        $scope.incidentCnt = $filter('for5Years')(incidents); //this line will do filtering for you.
        console.log($scope.incidentCnt);

        //$scope.labels = allYrs; // display years logic to do
        for (var i = 0; $scope.incidentCnt.length; i++) {
            $scope.series = [
                'Aggravated Assault',
                'Arson',
                'Burglary',
                'Forcible Sex Offense',
                'Hate Crime',
                'Motor Vehicle Theft',
                'Murder or Non-Negligent Manslaughter',
                'Negligent Manslaughter',
                'Non-Forcible Sex Offense',
                'Relationship/Dating Violence',
                'Robbery',
                'Stalking'
            ];

            $scope.data = [
                [
                    $scope.incidentCnt[i].assault,
                    $scope.incidentCnt[i].arson,
                    $scope.incidentCnt[i].burglary,
                    $scope.incidentCnt[i].fSexOffense,
                    $scope.incidentCnt[i].hateCrime,
                    $scope.incidentCnt[i].vehicleTheft,
                    $scope.incidentCnt[i].nonNegligentMaslaughter,
                    $scope.incidentCnt[i].negligentMaslaughter,
                    $scope.incidentCnt[i].nonForcibleSexOffense,
                    $scope.incidentCnt[i].rshipDatingViolence,
                    $scope.incidentCnt[i].robbery,
                    $scope.incidentCnt[i].stalking
                ]
            ];
        }
    });

}]);
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299