Using tc-angular-chartjs I’m able to get pie chart to function, but haven’t been able to figure out how to sum the label values to display correctly. Also, I’m able to get bar chart to work using their example, but not with external data via ajax call.
Note: I’m new to both angularjs and javascript so i maybe missing something simple here, but I’ve been trying to figure this out for days with no luck.
Pie Chart Example: I’m using a factory and restangular to get the external data and that works.
PROBLEM #1
To calculate the element values I’ve tried several references I’ve found, but with no luck trying to incorporate into chart. This is one of the references i’ve tried. most of them are variances of this that i’ve seen: occurrence of array elements
Code snippet for Chart:
services.forEach(function(service) {
data.push({
'value': incident.service.name.length, // Need to calculate sum of each 'label: service' element. [Used .length to get test value]
'color': colors.getRandom(), // This works, but need colors to be same for ea. service name
'highlight': 'brown',
'label': incident.service.name
});
$scope.data = data;
//console.log($scope.data);
});
Data Output Example:
Array[65]
0: Object
color: "orange"
highlight: "brown"
label: "Service1"
value: 36
__proto__: Object
1: Object
color: "navy"
highlight: "brown"
label: "Service1"
value: 40
__proto__: Object
2: Object
color: "green"
highlight: "brown"
label: "Service2"
value: 40
3: Object
color: "blue"
highlight: "brown"
label: "Service3"
value: 20
etc ..
HTML:
<div class="chart" ng-controller="chartController" style="margin-top:20px;">
<canvas tc-chartjs-pie chart-options="options" chart-data="data" auto-legend></canvas>
</div>
Bar Chart only works using example on their site.
PROBLEM #2 - I'll investigate this one further
Trying below I get: TypeError: Cannot read property 'length' of undefined
Code snippet:
// Chart.js Data
services.forEach(function(service) {
data.push({
label: incident.service.name,
fillColor: 'rgba(220,220,220,0.5)',
strokeColor: 'rgba(220,220,220,0.8)',
highlightFill: 'rgba(220,220,220,0.75)',
highlightStroke: 'rgba(220,220,220,1)',
data: incident.service.name.length
});
$scope.data = data;
//console.log($scope.data);
});