From: Dan Vanderkam Date: Sat, 24 Nov 2012 00:52:16 +0000 (-0800) Subject: Merge pull request #178 from kberg/tests X-Git-Tag: v1.0.0~168 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=3adf4a6f00d3577b925338891a0b021ebc5ae325;hp=57154831f03ec3c7d43d398c9c748935122ca344;p=dygraphs.git Merge pull request #178 from kberg/tests Add test for xValueParser --- 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/auto_tests/tests/pathological_cases.js b/auto_tests/tests/pathological_cases.js index 21ff30a..83cba83 100644 --- a/auto_tests/tests/pathological_cases.js +++ b/auto_tests/tests/pathological_cases.js @@ -48,3 +48,10 @@ pathologicalCasesTestCase.prototype.testNullLegend = function() { var graph = document.getElementById("graph"); var g = new Dygraph(graph, data, opts); }; + +pathologicalCasesTestCase.prototype.testDivAsString = function() { + var data = "X,Y\n" + + "1,2\n"; + + var g = new Dygraph('graph', data, {}); +} diff --git a/dygraph-range-selector.js b/dygraph-range-selector.js index e2973b0..b777470 100644 --- a/dygraph-range-selector.js +++ b/dygraph-range-selector.js @@ -480,6 +480,8 @@ DygraphRangeSelector.prototype.drawMiniPlot_ = function() { return; } + var stepPlot = this.attr_('stepPlot'); + var combinedSeriesData = this.computeCombinedSeriesAndLimits_(); var yRange = combinedSeriesData.yMax - combinedSeriesData.yMin; @@ -494,14 +496,36 @@ DygraphRangeSelector.prototype.drawMiniPlot_ = function() { var canvasWidth = this.canvasRect_.w - margin; var canvasHeight = this.canvasRect_.h - margin; + var prevX = null, prevY = null; + ctx.beginPath(); ctx.moveTo(margin, canvasHeight); for (var i = 0; i < combinedSeriesData.data.length; i++) { var dataPoint = combinedSeriesData.data[i]; - var x = (dataPoint[0] - xExtremes[0])*xFact; - var y = canvasHeight - (dataPoint[1] - combinedSeriesData.yMin)*yFact; + var x = ((dataPoint[0] !== null) ? ((dataPoint[0] - xExtremes[0])*xFact) : NaN); + var y = ((dataPoint[1] !== null) ? (canvasHeight - (dataPoint[1] - combinedSeriesData.yMin)*yFact) : NaN); if (isFinite(x) && isFinite(y)) { + if(prevX === null) { + ctx.lineTo(x, canvasHeight); + } + else if (stepPlot) { + ctx.lineTo(x, prevY); + } ctx.lineTo(x, y); + prevX = x; + prevY = y; + } + else { + if(prevX !== null) { + if (stepPlot) { + ctx.lineTo(x, prevY); + ctx.lineTo(x, canvasHeight); + } + else { + ctx.lineTo(prevX, canvasHeight); + } + } + prevX = prevY = null; } } ctx.lineTo(canvasWidth, canvasHeight); diff --git a/dygraph.js b/dygraph.js index 3a8c862..42e87c8 100644 --- a/dygraph.js +++ b/dygraph.js @@ -355,6 +355,10 @@ Dygraph.prototype.__init__ = function(div, file, attrs) { attrs = Dygraph.mapLegacyOptions_(attrs); + if (typeof(div) == 'string') { + div = document.getElementById(div); + } + if (!div) { Dygraph.error("Constructing dygraph with a non-existent div!"); return; @@ -1687,7 +1691,7 @@ Dygraph.prototype.findClosestPoint = function(domX, domY) { var minDist = Infinity; var idx = -1; var dist, dx, dy, point, closestPoint, closestSeries; - for (var setIdx = 0; setIdx < this.layout_.datasets.length; ++setIdx) { + for ( var setIdx = this.layout_.datasets.length - 1 ; setIdx >= 0 ; --setIdx ) { var points = this.layout_.points[setIdx]; for (var i = 0; i < points.length; ++i) { var point = points[i]; @@ -1788,7 +1792,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); @@ -2081,6 +2085,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); };