1
votes

I want to build a simple bar chart using Chart.js (1.0.2) and angular-chart.js (0.8.5). I read lots of other articles about this problem, but nothing helped. So this is the case:

I get "shipdata" from my RequestService which include a type, an hour and a count per type&hour. So it looks something like that:

Object {hour: "0", type: "9", count: "66"} Object {hour: "0", type: "3", count: "195"}

Then i select unique hours and types: hours are my labels and type is the series. After that I build the data array. This works perfectly fine and as a result i have in the end:

$scope.labels: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "17", "18"]

$scope.series: ["0", "2", "3", "4", "6", "7", "8", "9"]

$scope.data: 0: Array[10]1: Array[10]2: Array[10]3: Array[10]4: Array[10]5: Array[10]6: Array[10]7: Array[10]8: Array[10]9: Array[10]10: Array[10]11: Array[10]12: Array[10]17: Array[10]18: Array[10]

I printed those results in the command line.

Now everytime I call the $scope.getChartData() function, i get the following error:

TypeError: Cannot read property 'label' of undefined

Here is the code to the $scope.getChartData() : http://pastebin.com/7kWQ6W09

Here is the view: http://pastebin.com/0DvDcMBa

I tried serveral times the example provided on the angular-js github page (http://jtblin.github.io/angular-chart.js/). However I don't get why their example works pretty fine and mine, which provides exactly the same data, doesn't.

1

1 Answers

0
votes

You are initializing label outside the call to the service, why not trying to initialize after?

please try to change this

    var allData = [];
    $scope.series = [];
    $scope.labels = [];
    $scope.data = [[]];
    RequestService.getShipTypes(nelat, swlat, nelng, swlng, date, analyse)
            .success(function(ships) {

to this

    RequestService.getShipTypes(nelat, swlat, nelng, swlng, date, analyse)
            .success(function(ships) {
    var allData = [];
    $scope.series = [];
    $scope.labels = [];
    $scope.data = [[]];