-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChartjs.ng.js
84 lines (76 loc) · 2.05 KB
/
Chartjs.ng.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
77
78
79
80
81
82
83
84
define( [
"angular",
"qvangular",
'//cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.1-beta.2/Chart.min.js'
],
// inspired from here : https://github.com/petermelias/angular-chartjs
function (
angular,
qvangular
) {
var chartTypes = {
line: 'Line',
bar: 'Bar',
radar: 'Radar',
polar: 'PolarArea',
pie: 'Pie',
doughnut: 'Doughnut'
};
var makeChartDirective = function (chartType) {
var upper = chartType.charAt(0).toUpperCase() + chartType.slice(1);
qvangular.directive('qChart' + upper, [ function () {
return ChartFact(chartType);
}]);
};
for (var c in chartTypes) { makeChartDirective(c);}
var ChartFact = function (chartType) {
chartType = chartTypes[chartType];
var extractSpecOpts = function (opts, attrs) {
var extracted = {}, k, c;
for (k in opts) {
c = attrs[k];
if (typeof(c) !== 'undefined') { extracted[k] = c; }
}
return extracted;
};
if (typeof(chartType) === 'undefined') { return; }
return {
restrict: 'EAC',
template: '<div><canvas class="cjs-chart"></canvas><legend class="cjs-legend"></legend></div>',
replace: true,
scope: {
dataset: '=',
options: '='
},
link: function postLink(scope, element, attrs) {
var ctx = element[0].children[0].getContext('2d'),
chart = new Chart(ctx),
chartOpts = {};
angular.extend(
chartOpts,
Chart.defaults.global,
Chart.defaults[chartType]
);
angular.extend(
chartOpts,
scope.options,
extractSpecOpts(
chartOpts,
attrs
)
);
chart = chart[chartType](scope.dataset, chartOpts);
element[0].children[1].innerHTML = chart.generateLegend();
scope.$watch('dataset', function (newData, oldData) {
chart.initialize(newData);
}, true);
scope.$watch('options', function (newData, oldData) {
angular.extend(
chart.options,
scope.options
);
}, true);
}
};
};
} );