This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathjquery.flot.hiddengraphs.js
229 lines (195 loc) · 6.73 KB
/
jquery.flot.hiddengraphs.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* Plugin to hide series in flot graphs.
*
* To activate, set legend.hideable to true in the flot options object.
* To hide one or more series by default, set legend.hidden to an array of
* label strings.
*
* At the moment, this only works with line and point graphs.
*
* Example:
*
* var plotdata = [
* {
* data: [[1, 1], [2, 1], [3, 3], [4, 2], [5, 5]],
* label: "graph 1"
* },
* {
* data: [[1, 0], [2, 1], [3, 0], [4, 4], [5, 3]],
* label: "graph 2"
* }
* ];
*
* plot = $.plot($("#placeholder"), plotdata, {
* series: {
* points: { show: true },
* lines: { show: true }
* },
* legend: {
* hideable: true,
* hidden: ["graph 1", "graph 2"]
* }
* });
*
*/
(function ($) {
var options = { };
function init(plot) {
var drawnOnce = false;
function findPlotSeries(label) {
var plotdata = plot.getData();
for (var i = 0; i < plotdata.length; i++) {
if (plotdata[i].label == label) {
return plotdata[i];
}
}
return null;
}
function plotLabelClicked(label, mouseOut) {
var series = findPlotSeries(label);
if (!series) {
return;
}
var options = plot.getOptions();
var switchedOff = false;
if (typeof series.points.oldShow === "undefined") {
series.points.oldShow = false;
}
if (typeof series.lines.oldShow === "undefined") {
series.lines.oldShow = false;
}
if (series.points.show && !series.points.oldShow) {
series.points.show = false;
series.points.oldShow = true;
switchedOff = true;
}
if (series.lines.show && !series.lines.oldShow) {
series.lines.show = false;
series.lines.oldShow = true;
switchedOff = true;
}
if (switchedOff) {
series.oldColor = series.color;
series.color = "#fff";
setHidden(options, label, true);
} else {
var switchedOn = false;
if (!series.points.show && series.points.oldShow) {
series.points.show = true;
series.points.oldShow = false;
switchedOn = true;
}
if (!series.lines.show && series.lines.oldShow) {
series.lines.show = true;
series.lines.oldShow = false;
switchedOn = true;
}
if (switchedOn) {
series.color = series.oldColor;
setHidden(options, label, false);
}
}
}
function setSetupRedraw () {
// HACK: Reset the data, triggering recalculation of graph bounds
plot.setData(plot.getData());
plot.setupGrid();
plot.draw();
}
function setHidden(options, label, hide) {
// Record state to a new variable in the legend option object.
if (!options.legend.hidden) {
options.legend.hidden = [];
}
var pos = options.legend.hidden.indexOf(label);
if (hide) {
if (pos < 0) {
options.legend.hidden.push(label);
}
} else {
if (pos > -1) {
options.legend.hidden.splice(pos, 1);
}
}
}
function setHideAction(elem) {
elem.mouseenter(function() { $(this).css("cursor", "pointer"); })
.mouseleave(function() { $(this).css("cursor", "default"); })
.unbind("click").click(function() {
if ($(this).is(".legendColorBox")) {
plotLabelClicked($(this).next('.legendLabel').text());
} else {
plotLabelClicked($(this).parent().text());
}
setSetupRedraw();
});
}
function plotLabelHandlers(plot) {
var options = plot.getOptions();
if (!options.legend.hideable) {
return;
}
var p = plot.getPlaceholder();
setHideAction(p.find(".graphlabel"));
setHideAction(p.find(".legendColorBox"));
if (!drawnOnce) {
drawnOnce = true;
if (options.legend.hidden) {
for (var i = 0; i < options.legend.hidden.length; i++) {
plotLabelClicked(options.legend.hidden[i], true);
}
setSetupRedraw();
}
}
}
function checkOptions(plot, options) {
if (!options.legend.hideable) {
return;
}
options.legend.labelFormatter = function(label, series) {
return '<span class="graphlabel">' + label + '</span>';
};
}
function hideDatapointsIfNecessary(plot, s, datapoints) {
var options = plot.getOptions();
if (!options.legend.hideable) {
return;
}
if (options.legend.hidden &&
options.legend.hidden.indexOf(s.label) > -1) {
var off = false;
if (s.points.show) {
s.points.show = false;
s.points.oldShow = true;
off = true;
}
if (s.lines.show) {
s.lines.show = false;
s.lines.oldShow = true;
off = true;
}
if (off) {
s.oldColor = s.color;
s.color = "#fff";
}
}
if (!s.points.show && !s.lines.show) {
s.datapoints.format = [ null, null ];
}
}
plot.hooks.processOptions.push(checkOptions);
plot.hooks.draw.push(function (plot, ctx) {
plotLabelHandlers(plot);
});
plot.hooks.processDatapoints.push(hideDatapointsIfNecessary);
}
$.plot.plugins.push({
init: init,
options: options,
name: 'hiddenGraphs',
version: '1.1'
});
})(jQuery);