Commit | Line | Data |
---|---|---|
8cadc6c9 DV |
1 | /** |
2 | * Synchronize zooming and/or selections between a set of dygraphs. | |
3 | * | |
4 | * Usage: | |
5 | * | |
6 | * var g1 = new Dygraph(...), | |
7 | * g2 = new Dygraph(...), | |
8 | * ...; | |
9 | * var sync = Dygraph.synchronize(g1, g2, ...); | |
10 | * // charts are now synchronized | |
11 | * sync.detach(); | |
12 | * // charts are no longer synchronized | |
13 | * | |
14 | * You can set options using the last parameter, for example: | |
15 | * | |
16 | * var sync = Dygraph.synchronize(g1, g2, g3, { | |
17 | * selection: true, | |
18 | * zoom: true | |
19 | * }); | |
20 | * | |
21 | * The default is to synchronize both of these. | |
22 | * | |
b94ba4b5 | 23 | * Instead of passing one Dygraph object as each parameter, you may also pass an |
8cadc6c9 DV |
24 | * array of dygraphs: |
25 | * | |
26 | * var sync = Dygraph.synchronize([g1, g2, g3], { | |
27 | * selection: false, | |
28 | * zoom: true | |
29 | * }); | |
b94ba4b5 DV |
30 | * |
31 | * You may also set `range: false` if you wish to only sync the x-axis. | |
32 | * The `range` option has no effect unless `zoom` is true (the default). | |
8cadc6c9 DV |
33 | */ |
34 | (function() { | |
35 | /* global Dygraph:false */ | |
36 | 'use strict'; | |
37 | ||
38 | Dygraph.synchronize = function(/* dygraphs..., opts */) { | |
39 | if (arguments.length === 0) { | |
40 | throw 'Invalid invocation of Dygraph.synchronize(). Need >= 1 argument.'; | |
41 | } | |
42 | ||
b94ba4b5 | 43 | var OPTIONS = ['selection', 'zoom', 'range']; |
8cadc6c9 DV |
44 | var opts = { |
45 | selection: true, | |
8fd11236 | 46 | zoom: true, |
b94ba4b5 | 47 | range: true |
8cadc6c9 DV |
48 | }; |
49 | var dygraphs = []; | |
87cdddc7 | 50 | var prevCallbacks = []; |
fab3f9b4 | 51 | |
8cadc6c9 DV |
52 | var parseOpts = function(obj) { |
53 | if (!(obj instanceof Object)) { | |
54 | throw 'Last argument must be either Dygraph or Object.'; | |
55 | } else { | |
56 | for (var i = 0; i < OPTIONS.length; i++) { | |
57 | var optName = OPTIONS[i]; | |
58 | if (obj.hasOwnProperty(optName)) opts[optName] = obj[optName]; | |
59 | } | |
60 | } | |
61 | }; | |
62 | ||
63 | if (arguments[0] instanceof Dygraph) { | |
64 | // Arguments are Dygraph objects. | |
65 | for (var i = 0; i < arguments.length; i++) { | |
66 | if (arguments[i] instanceof Dygraph) { | |
67 | dygraphs.push(arguments[i]); | |
68 | } else { | |
69 | break; | |
70 | } | |
71 | } | |
72 | if (i < arguments.length - 1) { | |
73 | throw 'Invalid invocation of Dygraph.synchronize(). ' + | |
74 | 'All but the last argument must be Dygraph objects.'; | |
75 | } else if (i == arguments.length - 1) { | |
76 | parseOpts(arguments[arguments.length - 1]); | |
77 | } | |
78 | } else if (arguments[0].length) { | |
79 | // Invoked w/ list of dygraphs, options | |
80 | for (var i = 0; i < arguments[0].length; i++) { | |
81 | dygraphs.push(arguments[0][i]); | |
82 | } | |
83 | if (arguments.length == 2) { | |
84 | parseOpts(arguments[1]); | |
85 | } else if (arguments.length > 2) { | |
86 | throw 'Invalid invocation of Dygraph.synchronize(). ' + | |
87 | 'Expected two arguments: array and optional options argument.'; | |
88 | } // otherwise arguments.length == 1, which is fine. | |
89 | } else { | |
90 | throw 'Invalid invocation of Dygraph.synchronize(). ' + | |
91 | 'First parameter must be either Dygraph or list of Dygraphs.'; | |
92 | } | |
93 | ||
94 | if (dygraphs.length < 2) { | |
95 | throw 'Invalid invocation of Dygraph.synchronize(). ' + | |
96 | 'Need two or more dygraphs to synchronize.'; | |
97 | } | |
87cdddc7 | 98 | |
23e6849f RN |
99 | var readycount = dygraphs.length; |
100 | for (var i = 0; i < dygraphs.length; i++) { | |
101 | var g = dygraphs[i]; | |
102 | g.ready( function() { | |
103 | if (--readycount == 0) { | |
87cdddc7 MJ |
104 | // store original callbacks |
105 | var callBackTypes = ['drawCallback', 'highlightCallback', 'unhighlightCallback']; | |
106 | for (var j = 0; j < dygraphs.length; j++) { | |
107 | if (!prevCallbacks[j]) { | |
108 | prevCallbacks[j] = {}; | |
109 | } | |
a02a5365 | 110 | for (var k = callBackTypes.length - 1; k >= 0; k--) { |
87cdddc7 MJ |
111 | prevCallbacks[j][callBackTypes[k]] = dygraphs[j].getFunctionOption(callBackTypes[k]); |
112 | } | |
113 | } | |
114 | ||
23e6849f RN |
115 | // Listen for draw, highlight, unhighlight callbacks. |
116 | if (opts.zoom) { | |
117 | attachZoomHandlers(dygraphs, opts, prevCallbacks); | |
118 | } | |
8cadc6c9 | 119 | |
23e6849f RN |
120 | if (opts.selection) { |
121 | attachSelectionHandlers(dygraphs, prevCallbacks); | |
122 | } | |
123 | } | |
124 | }); | |
8cadc6c9 | 125 | } |
87cdddc7 | 126 | |
8cadc6c9 DV |
127 | return { |
128 | detach: function() { | |
129 | for (var i = 0; i < dygraphs.length; i++) { | |
130 | var g = dygraphs[i]; | |
131 | if (opts.zoom) { | |
87cdddc7 | 132 | g.updateOptions({drawCallback: prevCallbacks[i].drawCallback}); |
8cadc6c9 DV |
133 | } |
134 | if (opts.selection) { | |
135 | g.updateOptions({ | |
87cdddc7 MJ |
136 | highlightCallback: prevCallbacks[i].highlightCallback, |
137 | unhighlightCallback: prevCallbacks[i].unhighlightCallback | |
8cadc6c9 DV |
138 | }); |
139 | } | |
140 | } | |
141 | // release references & make subsequent calls throw. | |
142 | dygraphs = null; | |
143 | opts = null; | |
1ca560d2 | 144 | prevCallbacks = null; |
8cadc6c9 DV |
145 | } |
146 | }; | |
147 | }; | |
148 | ||
fab3f9b4 | 149 | function attachZoomHandlers(gs, syncOpts, prevCallbacks) { |
8cadc6c9 DV |
150 | var block = false; |
151 | for (var i = 0; i < gs.length; i++) { | |
152 | var g = gs[i]; | |
153 | g.updateOptions({ | |
154 | drawCallback: function(me, initial) { | |
155 | if (block || initial) return; | |
156 | block = true; | |
b94ba4b5 DV |
157 | var opts = { |
158 | dateWindow: me.xAxisRange() | |
159 | }; | |
160 | if (syncOpts.range) opts.valueRange = me.yAxisRange(); | |
161 | ||
8cadc6c9 | 162 | for (var j = 0; j < gs.length; j++) { |
87cdddc7 MJ |
163 | if (gs[j] == me) { |
164 | if (prevCallbacks[j] && prevCallbacks[j].drawCallback) { | |
165 | prevCallbacks[j].drawCallback(me, initial); | |
166 | } | |
167 | continue; | |
168 | } | |
b94ba4b5 | 169 | gs[j].updateOptions(opts); |
8cadc6c9 DV |
170 | } |
171 | block = false; | |
172 | } | |
173 | }, false /* no need to redraw */); | |
174 | } | |
175 | } | |
176 | ||
fab3f9b4 | 177 | function attachSelectionHandlers(gs, prevCallbacks) { |
8cadc6c9 DV |
178 | var block = false; |
179 | for (var i = 0; i < gs.length; i++) { | |
180 | var g = gs[i]; | |
87cdddc7 | 181 | |
8cadc6c9 DV |
182 | g.updateOptions({ |
183 | highlightCallback: function(event, x, points, row, seriesName) { | |
184 | if (block) return; | |
185 | block = true; | |
186 | var me = this; | |
187 | for (var i = 0; i < gs.length; i++) { | |
87cdddc7 MJ |
188 | if (me == gs[i]) { |
189 | if (prevCallbacks[i] && prevCallbacks[i].highlightCallback) { | |
190 | prevCallbacks[i].highlightCallback(event, x, points, row, seriesName); | |
191 | } | |
192 | continue; | |
193 | } | |
fcf37b29 | 194 | var idx = gs[i].getRowForX(x); |
8cadc6c9 DV |
195 | if (idx !== null) { |
196 | gs[i].setSelection(idx, seriesName); | |
197 | } | |
198 | } | |
199 | block = false; | |
200 | }, | |
201 | unhighlightCallback: function(event) { | |
202 | if (block) return; | |
203 | block = true; | |
204 | var me = this; | |
205 | for (var i = 0; i < gs.length; i++) { | |
87cdddc7 MJ |
206 | if (me == gs[i]) { |
207 | if (prevCallbacks[i] && prevCallbacks[i].unhighlightCallback) { | |
208 | prevCallbacks[i].unhighlightCallback(event); | |
209 | } | |
210 | continue; | |
211 | } | |
8cadc6c9 DV |
212 | gs[i].clearSelection(); |
213 | } | |
214 | block = false; | |
215 | } | |
216 | }); | |
217 | } | |
218 | } | |
219 | ||
8cadc6c9 | 220 | })(); |