X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=extras%2Fsynchronizer.js;h=16e4d96bc36926a8a0a502bc7d20c294c705a3a5;hb=8db6393ec8cac592f323724e43a2a7122a84614f;hp=f39209be2c4723375976f5b76cb87eb00a8abb06;hpb=12a4344926f6c51212450323d8cd43a9cf5f49ad;p=dygraphs.git diff --git a/extras/synchronizer.js b/extras/synchronizer.js index f39209b..16e4d96 100644 --- a/extras/synchronizer.js +++ b/extras/synchronizer.js @@ -100,16 +100,24 @@ Dygraph.synchronize = function(/* dygraphs..., opts */) { throw 'Invalid invocation of Dygraph.synchronize(). ' + 'Need two or more dygraphs to synchronize.'; } + + var readycount = dygraphs.length; + for (var i = 0; i < dygraphs.length; i++) { + var g = dygraphs[i]; + g.ready( function() { + if (--readycount == 0) { + // Listen for draw, highlight, unhighlight callbacks. + if (opts.zoom) { + attachZoomHandlers(dygraphs, opts, prevCallbacks); + } - // Listen for draw, highlight, unhighlight callbacks. - if (opts.zoom) { - attachZoomHandlers(dygraphs, opts, prevCallbacks); - } - - if (opts.selection) { - attachSelectionHandlers(dygraphs, prevCallbacks); + if (opts.selection) { + attachSelectionHandlers(dygraphs, prevCallbacks); + } + } + }); } - + return { detach: function() { for (var i = 0; i < dygraphs.length; i++) { @@ -173,7 +181,7 @@ function attachSelectionHandlers(gs, prevCallbacks) { var me = this; for (var i = 0; i < gs.length; i++) { if (me == gs[i]) continue; - var idx = dygraphsBinarySearch(gs[i], x); + var idx = gs[i].getRowForX(x); if (idx !== null) { gs[i].setSelection(idx, seriesName); } @@ -195,25 +203,4 @@ function attachSelectionHandlers(gs, prevCallbacks) { } } -// Returns the index corresponding to xVal, or null if there is none. -function dygraphsBinarySearch(g, xVal) { - var low = 0, - high = g.numRows() - 1; - - while (low < high) { - var idx = (high + low) >> 1; - var x = g.getValue(idx, 0); - if (x < xVal) { - low = idx + 1; - } else if (x > xVal) { - high = idx - 1; - } else { - return idx; - } - } - - // TODO: give an option to find the closest point, i.e. not demand an exact match. - return null; -} - })();