forked from earlonrails/angular-chartjs-directive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chartjs-directive.js
43 lines (38 loc) · 1.26 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
'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 = element.find('canvas')[0],
context = canvas.getContext('2d'),
chart;
var options = {
type: attrs.type || "Line",
width: attrs.width || baseWidth,
height: attrs.height || baseHeight
};
canvas.width = options.width;
canvas.height = options.height;
chart = new Chart(context);
scope.$watch(function(){ return element.attr('type'); }, function(value){
if(!value) return;
options.type = value;
var chartType = options.type;
chart[chartType](scope.chartObject.data, scope.chartObject.options);
});
//Update when charts data changes
scope.$watch(function() { return scope.chartObject; }, function(value) {
if(!value) return;
var chartType = options.type;
chart[chartType](scope.chartObject.data, scope.chartObject.options);
});
}
}
});