-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-dygraphs.js
81 lines (72 loc) · 2.91 KB
/
angular-dygraphs.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
angular.module("angular-dygraphs", [])
.directive("dygraphs", ['$timeout', function ($timeout) {
return {
restrict: "A",
scope: {
data: "=",
options: "=?",
synchronize: "=?"
},
require: '?^^dygraphsSynchronize',
link: function (scope, element, attr, ctrl) {
var graph = null;
function fromCssToNum(val) {
return +val.match(/^([0-9]+).*$/)[1];
}
function resize() {
var parent = element.parent()[0];
var compStyle = window.getComputedStyle(parent);
var width = fromCssToNum(compStyle.width);
var height = fromCssToNum(compStyle.height);
var vertPadding = fromCssToNum(compStyle.paddingTop) + fromCssToNum(compStyle.paddingBottom);
var horPadding = fromCssToNum(compStyle.paddingLeft) + fromCssToNum(compStyle.paddingRight);
graph && graph.resize(width - horPadding, height - vertPadding);
}
scope.$watch(function () {
var parent = element.parent()[0];
return [parent.offsetWidth, parent.offsetHeight].join('x');
}, function () {
resize();
})
scope.$watch('data', function () {
graph && graph.destroy();
if (scope.data && scope.data.length > 0) {
graph = new Dygraph(element[0], scope.data, scope.options);
ctrl && ctrl.toggleDygraph(attr.id, graph);
resize();
}
}, true);
scope.$watch('options', function () {
graph && graph.updateOptions(scope.options);
}, true);
}
};
}])
.directive("dygraphsSynchronize", function () {
return {
restrict: "A",
controller: ['$scope', function ($scope) {
var ctrl = this;
ctrl.sync = null;
ctrl.syncMap = {};
ctrl.toggleDygraph = function (id, d) {
if (ctrl.sync) {
try {
ctrl.sync.detach();
} catch (e) { };
}
ctrl.syncMap[id] = d;
var syncList = Object.keys(ctrl.syncMap).map(function (el) {
return ctrl.syncMap[el];
});
if (syncList.length > 1) {
ctrl.sync = Dygraph.synchronize(syncList, {
zoom: true,
range: false
});
}
}
}]
};
})
;