-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-chartjs.js
93 lines (83 loc) · 2.25 KB
/
angular-chartjs.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
85
86
87
88
89
90
91
92
93
(function () {
'use strict';
var chartjs = angular.module('chartjs', []),
chartTypes = {
line: 'Line',
bar: 'Bar',
radar: 'Radar',
polar: 'PolarArea',
pie: 'Pie',
doughnut: 'Doughnut'
},
makeChartDirective = function (chartType) {
var upper = chartType.charAt(0).toUpperCase() + chartType.slice(1);
chartjs.directive('cjs' + upper, ['ChartFactory', function (ChartFactory) {
return new ChartFactory(chartType);
}]);
},
sizeChart = function (width, height, canvas) {
var oW = canvas.width,
oH = canvas.height;
if (oW !== width || oH !== height) {
canvas.width = width;
canvas.height = height;
return true;
}
return false;
},
fitChart = function (canvas, element) {
var w = element.parent().prop('offsetWidth'),
h = element.parent().prop('offsetHeight');
return sizeChart(w, h, canvas);
};
for (var c in chartTypes) {
makeChartDirective(c);
}
chartjs.factory('ChartFactory', function () {
return 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: '<canvas></canvas>',
replace: true,
scope: {
dataset: '=',
options: '='
},
link: function postLink(scope, element, attrs) {
var ctx = element[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);
}
};
};
});
})();