-
Notifications
You must be signed in to change notification settings - Fork 35
/
chartjs-directive.js
59 lines (52 loc) · 2.01 KB
/
chartjs-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
'use strict';
angular.module('chartjs-directive', []).
.directive('chart', function() {
var baseWidth = 600;
var baseHeight = 400;
return {
restrict: 'E',
template: '<canvas></canvas>',
scope: {
chartObject: "=value"
},
link: function(scope, element, attrs) {
var canvas, context, chart, options;
var init = function(type) {
element.empty();
element.append('<canvas></canvas>');
canvas = element.find("canvas")[0];
context = canvas.getContext("2d");
options = {
type: type || "Line",
width: attrs.width || baseWidth,
height: attrs.height || baseHeight,
last_type: false
};
canvas.height = options.height;
canvas.width = options.width;
chart = new Chart(context);
};
init(attrs.type);
scope.$watch(function() {
return element.attr('type');
}, function(value) {
if (!value) return;
init(value);
var chartType = options.type;
});
//Update when charts data changes
scope.$watch(function() {
return scope.chartObject;
}, function(value) {
if (!value) return;
var chartType = options.type;
if (scope.chartInstance) {
scope.chartInstance.destroy();
}
if (typeof scope.chartObject !== 'undefined') {
scope.chartInstance = chart[chartType](scope.chartObject.data, scope.chartObject.options);
}
}, true);
}
}
});