From 3f55b813316f73082795fe52f6cf26e9f1592f12 Mon Sep 17 00:00:00 2001 From: Klaus Weidner Date: Tue, 31 Jul 2012 12:07:34 -0700 Subject: [PATCH] Export .isSeriesLocked() API function This is a followup to https://github.com/danvk/dygraphs/pull/159, exposing the .lockedSet_ internal attribute through the API for use in callbacks, and also with an eye for future extraction of the mouseover logic into a plugin. Includes a fix for the auto_test name, it was erroneously replacing the existing testClosestPointCallbackCss1 test due to a name clash. --- auto_tests/tests/callback.js | 2 +- dygraph.js | 10 +++++++++- gallery/highlighted-series.js | 22 ++++++++-------------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/auto_tests/tests/callback.js b/auto_tests/tests/callback.js index 6e59787..0e6b4fa 100644 --- a/auto_tests/tests/callback.js +++ b/auto_tests/tests/callback.js @@ -304,7 +304,7 @@ CallbackTestCase.prototype.testClosestPointCallbackCss2 = function() { /** * Closest-point highlighting with locked series. */ -CallbackTestCase.prototype.testClosestPointCallbackCss1 = function() { +CallbackTestCase.prototype.testSetSelectionLocking = function() { var g = runClosestTest(false, 2, 4); // Default behavior, 'b' is closest diff --git a/dygraph.js b/dygraph.js index ba6889e..2d8797b 100644 --- a/dygraph.js +++ b/dygraph.js @@ -1734,7 +1734,7 @@ Dygraph.prototype.mouseMove_ = function(event) { var highlightSeriesOpts = this.attr_("highlightSeriesOpts"); var selectionChanged = false; - if (highlightSeriesOpts && !this.lockedSet_) { + if (highlightSeriesOpts && !this.isSeriesLocked()) { var closest; if (this.attr_("stackedGraph")) { closest = this.findStackedPoint(canvasx, canvasy); @@ -2025,6 +2025,14 @@ Dygraph.prototype.getHighlightSeries = function() { }; /** + * Returns true if the currently-highlighted series was locked + * via setSelection(..., seriesName, true). + */ +Dygraph.prototype.isSeriesLocked = function() { + return this.lockedSet_; +}; + +/** * Fires when there's data available to be graphed. * @param {String} data Raw CSV data to be plotted * @private diff --git a/gallery/highlighted-series.js b/gallery/highlighted-series.js index 27a6458..2fdd1cf 100644 --- a/gallery/highlighted-series.js +++ b/gallery/highlighted-series.js @@ -27,19 +27,6 @@ var getData = function(numSeries, numRows, isStacked) { return data; }; -var makeClickCallback = function(graph) { - var isLocked = false; - return function(ev) { - if (isLocked) { - graph.clearSelection(); - isLocked = false; - } else { - graph.setSelection(graph.getSelection(), graph.getHighlightSeries(), true); - isLocked = true; - } - }; -}; - var makeGraph = function(className, numSeries, numRows, isStacked) { var demo = document.getElementById('demo'); var div = document.createElement('div'); @@ -73,7 +60,14 @@ var makeGraph = function(className, numSeries, numRows, isStacked) { highlightCircleSize: 5, }, }); - g.updateOptions({clickCallback: makeClickCallback(g)}, true); + var onclick = function(ev) { + if (g.isSeriesLocked()) { + g.clearSelection(); + } else { + g.setSelection(g.getSelection(), g.getHighlightSeries(), true); + } + }; + g.updateOptions({clickCallback: onclick}, true); g.setSelection(false, 's005'); //console.log(g); }; -- 2.7.4