forked from develersrl/angular-flot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-flot.js
More file actions
128 lines (106 loc) · 2.93 KB
/
angular-flot.js
File metadata and controls
128 lines (106 loc) · 2.93 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* global $ */
/* global angular */
/* global jQuery */
angular.module('angular-flot', []).directive('flot', function () {
return {
restrict: 'EA',
template: '<div></div>',
scope: {
dataset: '=',
options: '=',
callback: '=',
onPlotClick: '&',
onPlotHover: '&'
},
link: function (scope, element, attributes) {
var plot = null;
var width = attributes.width || '100%';
var height = attributes.height || '100%';
// Bug: Passing a jQuery object causes an infinite loop within Angular. Fail hard telling
// users that they should pass us a jQuery expression as string instead.
if ((((scope.options || {}).legend || {}).container) instanceof jQuery) {
throw new Error('Please use a jQuery expression string with the "legend.container" option.')
}
if (!scope.dataset) {
scope.dataset = [];
}
if (!scope.options) {
scope.options = {
legend: {
show: false
}
};
}
var plotArea = $(element.children()[0]);
plotArea.css({
width: width,
height: height
});
var init = function () {
var plotObj = $.plot(plotArea, scope.dataset, scope.options);
if (scope.callback) {
scope.callback(plotObj);
}
return plotObj;
};
//
// Events
//
plotArea.on('plotclick', function onPlotClick (event, pos, item) {
scope.$apply(function onApplyPlotClick () {
scope.onPlotClick({
event: event,
pos: pos,
item: item
});
});
});
plotArea.on('plothover', function onPlotHover (event, pos, item) {
scope.$apply(function onApplyPlotHover () {
scope.onPlotHover({
event: event,
pos: pos,
item: item
});
});
});
//
// Watches
//
var onOptionsChanged = function () {
plot = init();
};
var unwatchOptions = scope.$watch('options', onOptionsChanged, true);
var onDatasetChanged = function (dataset) {
if (plot) {
plot.setData(dataset);
plot.setupGrid();
return plot.draw();
} else {
plot = init();
}
};
var unwatchDataset = scope.$watch('dataset', onDatasetChanged, true);
attributes.$observe('width', function(value) {
if (!value) return;
width = value;
plotArea.css('width', value);
});
attributes.$observe('height', function(value) {
if (!value) return;
height = value;
plotArea.css('height', value);
});
//
// Tear Down
//
element.on('$destroy', function onDestroy () {
plotArea.off('plotclick');
plotArea.off('plothover');
plot.shutdown();
unwatchDataset();
unwatchOptions();
});
}
};
});