-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-chartjs-doughnut-directive.js
76 lines (67 loc) · 3.04 KB
/
angular-chartjs-doughnut-directive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*!
* AngularJS Chart.js Doughnut Directive
*
* Copyright 2013 Stephane Begaudeau
* Released under the MIT license
*/
angular.module('angular.directives-chartjs-doughnut', []).directive('angChartjsDoughnut', [function () {
var compilationFunction = function (templateElement, templateAttributes, transclude) {
if (templateElement.length === 1) {
var node = templateElement[0];
var width = node.getAttribute('data-chartjs-width') || '400';
var height = node.getAttribute('data-chartjs-height') || '400';
var canvas = document.createElement('canvas');
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
canvas.setAttribute('data-chartjs-model', node.getAttribute('data-chartjs-model'));
var options = {};
var potentialOptions = [
{key:'data-chartjs-segment-show-stroke', value:'segmentShowStroke', isBoolean: true},
{key:'data-chartjs-segment-stroke-color', value:'segmentStrokeColor'},
{key:'data-chartjs-segment-stroke-width', value:'segmentStrokeWidth', isNumber: true},
{key:'data-chartjs-percentage-inner-cutout', value:'percentageInnerCutout', isNumber: true},
{key:'data-chartjs-animation', value:'animation', isBoolean: true},
{key:'data-chartjs-animation-steps', value:'animationSteps', isNumber: true},
{key:'data-chartjs-animation-easing', value:'animationEasing'},
{key:'data-chartjs-animate-rotate', value:'animateRotate', isBoolean: true},
{key:'data-chartjs-animate-scale', value:'animateScale', isBoolean: true}
];
for (var i = 0; i < potentialOptions.length; i++) {
var aKey = node.getAttribute(potentialOptions[i].key);
if (aKey && potentialOptions[i].isBoolean) {
if ('true' === aKey) {
options[potentialOptions[i].value] = true;
} else if ('false' === aKey) {
options[potentialOptions[i].value] = false;
}
} else if (aKey && potentialOptions[i].isNumber) {
options[potentialOptions[i].value] = parseInt(aKey);
}else if (aKey) {
options[potentialOptions[i].value] = aKey;
}
}
var chart = new Chart(canvas.getContext('2d'));
node.parentNode.replaceChild(canvas, node);
return {
pre: function preLink(scope, instanceElement, instanceAttributes, controller) {
var expression = canvas.getAttribute('data-chartjs-model');
scope.$watch(expression, function (newValue, oldValue) {
if (angular.isArray(newValue)){
var callback = scope[node.getAttribute('data-chartjs-on-animation-complete')];
if (callback !== undefined) {
options.onAnimationComplete = callback;
}
chart.Doughnut(newValue, options);
}
}, true);
},
post: function postLink(scope, instanceElement, instanceAttributes, controller) {}
};
}
};
var chartjsDoughnut = {
compile: compilationFunction,
replace: true
};
return chartjsDoughnut;
}]);