Kendo + Angular Chart Data
Solution 1:
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:
<divkendo-chartk-options="chart"k-rebind="chart"></div>
Here is an example of a chart bound to remote data:
http://codepen.io/micjamking/pen/4980a5e22cbd4de01264fadae5f25f06
Solution 2:
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:
<divkendo-chartk-theme='metro'k-options="chart"k-rebind="chart"></div>
controller:
$scope.chart= {
dataSource:newkendo.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
Solution 3:
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)
Post a Comment for "Kendo + Angular Chart Data"