-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathangular-chartjs-pie-directive.js
More file actions
148 lines (113 loc) · 4.43 KB
/
angular-chartjs-pie-directive.js
File metadata and controls
148 lines (113 loc) · 4.43 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*!
* AngularJS Chart.js Pie Directive
*
* Copyright 2013 Stephane Begaudeau
* Released under the MIT license
*/
angular.module('angular.directives-chartjs-pie', []).directive('angChartjsPie', [function () {
var getOptionsFromScope = function (scope) {
var options = {};
var potentialOptions = [
{key:'chartjsSegmentShowStroke', value:'segmentShowStroke', isBoolean: true},
{key:'chartjsSegmentStrokeColor', value:'segmentStrokeColor'},
{key:'chartjsSegmentStrokeWidth', value:'segmentStrokeWidth', isNumber: true},
{key:'chartjsAnimation', value:'animation', isBoolean: true},
{key:'chartjsAnimationSteps', value:'animationSteps', isNumber: true},
{key:'chartjsAnimationEasing', value:'animationEasing'},
{key:'chartjAnimationRotate', value:'animationRotate', isBoolean: true},
{key:'chartjsAnimationScale', value:'animationScale', isBoolean: true}
];
for (var i = 0; i < potentialOptions.length; i++) {
if (scope.hasOwnProperty(potentialOptions[i].key) && scope[potentialOptions[i].key] !== undefined) {
options[potentialOptions[i].value] = scope[potentialOptions[i].key];
}
}
return options;
};
var chartjsPie = {
restrict: 'E',
//compile: compilationFunction,
template: '<canvas class="ang-chartjs-pie"></canvas>',
scope: {
chartjsModel: '=',
chartjsWidth: '=',
chartjsHeight: '=',
chartjsSegmentShowStroke: '=',
chartjsSegmentStrokeColor: '=',
chartjsSegmentStrokeWidth: '=',
chartjsAnimation: '=',
chartjsAnimationSteps: '=',
chartjsAnimationEasing: '=',
chartjAnimationRotate: '=',
chartjsAnimationScale: '='
},
link: function (scope, elements, attributes) {
scope.$watch('chartjsModel', function (newValue) {
if (newValue !== undefined) {
var options = getOptionsFromScope(scope);
if (scope.chart !== undefined) {
scope.chart.Pie(newValue, options);
} else {
var width = scope.chartjsWidth || '400';
var height = scope.chartjsHeight || '400';
var canvas = elements[0].children[0];
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
var chart = new Chart(canvas.getContext('2d'));
chart.Pie(newValue, options);
scope.chart = chart;
}
}
}, true);
}
};
return chartjsPie;
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 = [
];
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) {
var callback = scope[node.getAttribute('data-chartjs-on-animation-complete')];
if (callback !== undefined) {
options.onAnimationComplete = callback;
}
chart.Pie(newValue, options);
}, true);
},
post: function postLink(scope, instanceElement, instanceAttributes, controller) {}
};
}
};
var chartjsBar = {
compile: compilationFunction,
replace: true
};
return chartjsBar;
}]);