2 * Synchronize zooming and/or selections between a set of dygraphs.
6 * var g1 = new Dygraph(...),
7 * g2 = new Dygraph(...),
9 * var sync = Dygraph.synchronize(g1, g2, ...);
10 * // charts are now synchronized
12 * // charts are no longer synchronized
14 * You can set options using the last parameter, for example:
16 * var sync = Dygraph.synchronize(g1, g2, g3, {
21 * The default is to synchronize both of these.
23 * Instead of passing one Dygraph object as each parameter, you may also pass an
26 * var sync = Dygraph.synchronize([g1, g2, g3], {
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).
34 * SPDX-License-Identifier: MIT
35 * Original source: https://github.com/danvk/dygraphs/blob/master/src/extras/synchronizer.js
36 * at commit b55a71d768d2f8de62877c32b3aec9e9975ac389
38 * Copyright (c) 2009 Dan Vanderkam
40 * Permission is hereby granted, free of charge, to any person
41 * obtaining a copy of this software and associated documentation
42 * files (the "Software"), to deal in the Software without
43 * restriction, including without limitation the rights to use,
44 * copy, modify, merge, publish, distribute, sublicense, and/or sell
45 * copies of the Software, and to permit persons to whom the
46 * Software is furnished to do so, subject to the following
49 * The above copyright notice and this permission notice shall be
50 * included in all copies or substantial portions of the Software.
52 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
53 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
54 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
55 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
56 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
57 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
58 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
59 * OTHER DEALINGS IN THE SOFTWARE.
62 /* global Dygraph:false */
67 Dygraph = window.Dygraph;
68 } else if (typeof(module) !== 'undefined') {
69 Dygraph = require('../dygraph');
72 var synchronize = function(/* dygraphs..., opts */) {
73 if (arguments.length === 0) {
74 throw 'Invalid invocation of Dygraph.synchronize(). Need >= 1 argument.';
77 var OPTIONS = ['selection', 'zoom', 'range'];
84 var prevCallbacks = [];
86 var parseOpts = function(obj) {
87 if (!(obj instanceof Object)) {
88 throw 'Last argument must be either Dygraph or Object.';
90 for (var i = 0; i < OPTIONS.length; i++) {
91 var optName = OPTIONS[i];
92 if (obj.hasOwnProperty(optName)) opts[optName] = obj[optName];
97 if (arguments[0] instanceof Dygraph) {
98 // Arguments are Dygraph objects.
99 for (var i = 0; i < arguments.length; i++) {
100 if (arguments[i] instanceof Dygraph) {
101 dygraphs.push(arguments[i]);
106 if (i < arguments.length - 1) {
107 throw 'Invalid invocation of Dygraph.synchronize(). ' +
108 'All but the last argument must be Dygraph objects.';
109 } else if (i == arguments.length - 1) {
110 parseOpts(arguments[arguments.length - 1]);
112 } else if (arguments[0].length) {
113 // Invoked w/ list of dygraphs, options
114 for (var i = 0; i < arguments[0].length; i++) {
115 dygraphs.push(arguments[0][i]);
117 if (arguments.length == 2) {
118 parseOpts(arguments[1]);
119 } else if (arguments.length > 2) {
120 throw 'Invalid invocation of Dygraph.synchronize(). ' +
121 'Expected two arguments: array and optional options argument.';
122 } // otherwise arguments.length == 1, which is fine.
124 throw 'Invalid invocation of Dygraph.synchronize(). ' +
125 'First parameter must be either Dygraph or list of Dygraphs.';
128 if (dygraphs.length < 2) {
129 throw 'Invalid invocation of Dygraph.synchronize(). ' +
130 'Need two or more dygraphs to synchronize.';
133 var readycount = dygraphs.length;
134 for (var i = 0; i < dygraphs.length; i++) {
136 g.ready( function() {
137 if (--readycount == 0) {
138 // store original callbacks
139 var callBackTypes = ['drawCallback', 'highlightCallback', 'unhighlightCallback'];
140 for (var j = 0; j < dygraphs.length; j++) {
141 if (!prevCallbacks[j]) {
142 prevCallbacks[j] = {};
144 for (var k = callBackTypes.length - 1; k >= 0; k--) {
145 prevCallbacks[j][callBackTypes[k]] = dygraphs[j].getFunctionOption(callBackTypes[k]);
149 // Listen for draw, highlight, unhighlight callbacks.
151 attachZoomHandlers(dygraphs, opts, prevCallbacks);
154 if (opts.selection) {
155 attachSelectionHandlers(dygraphs, prevCallbacks);
163 for (var i = 0; i < dygraphs.length; i++) {
166 g.updateOptions({drawCallback: prevCallbacks[i].drawCallback});
168 if (opts.selection) {
170 highlightCallback: prevCallbacks[i].highlightCallback,
171 unhighlightCallback: prevCallbacks[i].unhighlightCallback
175 // release references & make subsequent calls throw.
178 prevCallbacks = null;
183 function arraysAreEqual(a, b) {
184 if (!Array.isArray(a) || !Array.isArray(b)) return false;
186 if (i !== b.length) return false;
188 if (a[i] !== b[i]) return false;
193 function attachZoomHandlers(gs, syncOpts, prevCallbacks) {
195 for (var i = 0; i < gs.length; i++) {
198 drawCallback: function(me, initial) {
199 if (block || initial) return;
202 dateWindow: me.xAxisRange()
204 if (syncOpts.range) opts.valueRange = me.yAxisRange();
206 for (var j = 0; j < gs.length; j++) {
208 if (prevCallbacks[j] && prevCallbacks[j].drawCallback) {
209 prevCallbacks[j].drawCallback.apply(this, arguments);
214 // Only redraw if there are new options
215 if (arraysAreEqual(opts.dateWindow, gs[j].getOption('dateWindow')) &&
216 arraysAreEqual(opts.valueRange, gs[j].getOption('valueRange'))) {
220 gs[j].updateOptions(opts);
224 }, true /* no need to redraw */);
228 function attachSelectionHandlers(gs, prevCallbacks) {
230 for (var i = 0; i < gs.length; i++) {
234 highlightCallback: function(event, x, points, row, seriesName) {
238 for (var i = 0; i < gs.length; i++) {
240 if (prevCallbacks[i] && prevCallbacks[i].highlightCallback) {
241 prevCallbacks[i].highlightCallback.apply(this, arguments);
245 var idx = gs[i].getRowForX(x);
247 gs[i].setSelection(idx, seriesName);
252 unhighlightCallback: function(event) {
256 for (var i = 0; i < gs.length; i++) {
258 if (prevCallbacks[i] && prevCallbacks[i].unhighlightCallback) {
259 prevCallbacks[i].unhighlightCallback.apply(this, arguments);
263 gs[i].clearSelection();
267 }, true /* no need to redraw */);
271 Dygraph.synchronize = synchronize;