X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=extras%2Fsynchronizer.js;h=f39209be2c4723375976f5b76cb87eb00a8abb06;hb=12a4344926f6c51212450323d8cd43a9cf5f49ad;hp=66dc2d8220829eabf2bc48edc32f6cf782312f64;hpb=8cadc6c95079212a5fd240616cc64cb474d25fb8;p=dygraphs.git diff --git a/extras/synchronizer.js b/extras/synchronizer.js index 66dc2d8..f39209b 100644 --- a/extras/synchronizer.js +++ b/extras/synchronizer.js @@ -20,13 +20,16 @@ * * The default is to synchronize both of these. * - * Instead of passing one Dygraph objet as each parameter, you may also pass an + * Instead of passing one Dygraph object as each parameter, you may also pass an * array of dygraphs: * * var sync = Dygraph.synchronize([g1, g2, g3], { * selection: false, * zoom: true * }); + * + * You may also set `range: false` if you wish to only sync the x-axis. + * The `range` option has no effect unless `zoom` is true (the default). */ (function() { /* global Dygraph:false */ @@ -37,13 +40,20 @@ Dygraph.synchronize = function(/* dygraphs..., opts */) { throw 'Invalid invocation of Dygraph.synchronize(). Need >= 1 argument.'; } - var OPTIONS = ['selection', 'zoom']; + var OPTIONS = ['selection', 'zoom', 'range']; var opts = { selection: true, - zoom: true + zoom: true, + range: true }; var dygraphs = []; + var prevCallbacks = { + draw: null, + highlight: null, + unhighlight: null + }; + var parseOpts = function(obj) { if (!(obj instanceof Object)) { throw 'Last argument must be either Dygraph or Object.'; @@ -93,11 +103,11 @@ Dygraph.synchronize = function(/* dygraphs..., opts */) { // Listen for draw, highlight, unhighlight callbacks. if (opts.zoom) { - attachZoomHandlers(dygraphs); + attachZoomHandlers(dygraphs, opts, prevCallbacks); } if (opts.selection) { - attachSelectionHandlers(dygraphs); + attachSelectionHandlers(dygraphs, prevCallbacks); } return { @@ -105,39 +115,41 @@ Dygraph.synchronize = function(/* dygraphs..., opts */) { for (var i = 0; i < dygraphs.length; i++) { var g = dygraphs[i]; if (opts.zoom) { - g.updateOptions({drawCallback: null}); + g.updateOptions({drawCallback: prevCallbacks.draw}); } if (opts.selection) { g.updateOptions({ - highlightCallback: null, - unhighlightCallback: null + highlightCallback: prevCallbacks.highlight, + unhighlightCallback: prevCallbacks.unhighlight }); } } // release references & make subsequent calls throw. dygraphs = null; opts = null; + prevCallbacks = null; } }; }; -// TODO: call any `drawCallback`s that were set before this. -function attachZoomHandlers(gs) { +function attachZoomHandlers(gs, syncOpts, prevCallbacks) { var block = false; for (var i = 0; i < gs.length; i++) { var g = gs[i]; + prevCallbacks.draw = g.getFunctionOption('drawCallback'); g.updateOptions({ drawCallback: function(me, initial) { + if (prevCallbacks.draw) prevCallbacks.draw(me, initial); if (block || initial) return; block = true; - var range = me.xAxisRange(); - var yrange = me.yAxisRange(); + var opts = { + dateWindow: me.xAxisRange() + }; + if (syncOpts.range) opts.valueRange = me.yAxisRange(); + for (var j = 0; j < gs.length; j++) { if (gs[j] == me) continue; - gs[j].updateOptions( { - dateWindow: range, - valueRange: yrange - }); + gs[j].updateOptions(opts); } block = false; } @@ -145,12 +157,17 @@ function attachZoomHandlers(gs) { } } -function attachSelectionHandlers(gs) { +function attachSelectionHandlers(gs, prevCallbacks) { var block = false; for (var i = 0; i < gs.length; i++) { var g = gs[i]; + prevCallbacks.highlight = g.getFunctionOption('highlightCallback'); + prevCallbacks.unhighlight = g.getFunctionOption('unhighlightCallback'); g.updateOptions({ highlightCallback: function(event, x, points, row, seriesName) { + if (prevCallbacks.highlight) { + prevCallbacks.highlight(event, x, points, row, seriesName); + } if (block) return; block = true; var me = this; @@ -164,6 +181,7 @@ function attachSelectionHandlers(gs) { block = false; }, unhighlightCallback: function(event) { + if (prevCallbacks.unhighlight) prevCallbacks.unhighlight(event); if (block) return; block = true; var me = this;