Kendo + Angular chart data

2019-02-05 07:15发布

I'm trying out Kendo charts with angular, and I have problem displaying data, here is my code:

HTML:

<div  kendo-chart="rchart" data-k-options="chartOptions" data-role="chart" class="k-chart" style="position: relative;"></div>

Javascript:

resultService.getResult().then(function (resultResponse) {
         $scope.data = resultResponse.data;
         $scope.oldReps = _.pluck($scope.data.TreningScores.Item1, 'Item2');
         $scope.newReps = _.pluck($scope.data.TreningScores.Item2, 'Item2');
         $scope.categories = _.pluck($scope.data.TreningScores.Item1, 'Item1');
     });


$scope.chartOptions = {

         legend: {
             position: "bottom"
         },
         seriesDefaults: {
             type: "column"
         },
         series: [{
             name: "Total Visits",
             data: $scope.oldReps
         }, {
             name: "Unique visitors",
             data: $scope.newReps
         }],
         valueAxis: {
             line: {
                 visible: false
             }
         },

         tooltip: {
             visible: true,
             format: "{0}"
         }
     };

The problem is chart isn't updated after data is fetched from server, I've tried refreshing chart like this (but with no luck):

$scope.chart = {        
        refreshChart : function() {
            $scope.rchart.refresh();
        },
    };

And calling this method in:

resultService.getResult().then(function (resultResponse) {});

And I've also tried to define $scope.chartOptions inside same function, but nothing. Is there any way to fix this ?

3条回答
女痞
2楼-- · 2019-02-05 07:44

It's not well documented, but to get a UI control with remote data-binding to update after data has been returned from a server requires both watching the collection for updates from the Angular side and rebinding the data object to its respective UI control from the Kendo side.

In your controller, watch for changes to your data objects using $watchCollection, and update the objects/properties which are bound to those collections:

// API call
$http.get('...').success(function(data){
   $scope.data = data;
});

// KendoUI config object
$scope.chart = {
    dataSource: {
        data: $scope.data
    }
};

// Watch for changes to $scope.data
$scope.$watchCollection('data', function(newData) {

    // Update data bindings with changes
    $scope.chart.dataSource.data = newData;
});

In your view, define the object your UI control should be bound to when changes are made via the k-rebind Angular-Kendo directive:

<div kendo-chart k-options="chart" k-rebind="chart"></div>

Here is an example of a chart bound to remote data:

http://codepen.io/micjamking/pen/4980a5e22cbd4de01264fadae5f25f06

查看更多
迷人小祖宗
3楼-- · 2019-02-05 07:51

I think your problem is that $scope.chartOptions is set before the data of the resultService is retrieved. Angular is returning an empty array in this case and filling in the data later. But $scope.chartOptions not not updated with new data.

You could try with

$scope.$watchCollection('oldReps', function(newData, oldData) {
  $scope.chartOptions.series[0].data = newData;
});
$scope.$watchCollection('newReps', function(newData, oldData) {
  $scope.chartOptions.series[1].data = newData;
});

So chartOptions are updated if oldReps or newReps have changed.

I had a similiar problem and $watchCollection saved my day (Caching data and updating another object for an chart does not work)

查看更多
淡お忘
4楼-- · 2019-02-05 07:53

The use of $watchCollection to track and assign the dataSource.data in the accepted answer and others is a needlessly convoluted approach.

Here's a straightforward implementation:

view:

<div kendo-chart k-theme='metro' k-options="chart" k-rebind="chart"></div>

controller:

$scope.chart = {
  dataSource: new kendo.data.DataSource({
    data: [{ title: "Loading", value: 100, color: '#EFEFEF' }]
  }),
  series: [{ field: 'value', categoryField: 'title', padding: 0, holeSize: 25 }],
  seriesDefaults: { type: 'donut', startAngle: 90 }
};

Using the dataSource.data() method instead of assigning dataSource.data as an array is the key here:

payrollService.load(userId).then(function (result) {
  $scope.chart.dataSource.data(result.items); //where result.items is an array like so:
  //[
  //  { title: "Net Pay", value: 60, color: '#6BAE4B' },
  //  { title: "Taxes", value: 15, color: '#ED6347' },
  //  { title: "Deductions", value: 25, color: '#8161C2' }
  //]
});

Codepen Demo: http://codepen.io/TaeKwonJoe/pen/WGOpEv

查看更多
登录 后发表回答