From 23db4e0795017c84f7ae1c081a914ff46b719c42 Mon Sep 17 00:00:00 2001 From: Robert Konigsberg Date: Sun, 16 Dec 2012 19:52:23 -0500 Subject: [PATCH] Remove axis.logscale; add test that verifies that custom bars are drawn correctly in log scale. --- auto_tests/tests/custom_bars.js | 45 +++++++++++++++++++++++++++++++++++++++++ dygraph-layout.js | 15 +++++++++----- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/auto_tests/tests/custom_bars.js b/auto_tests/tests/custom_bars.js index 22ffd20..493e86c 100644 --- a/auto_tests/tests/custom_bars.js +++ b/auto_tests/tests/custom_bars.js @@ -6,11 +6,16 @@ */ var CustomBarsTestCase = TestCase("custom-bars"); +var _origFunc = Dygraph.getContext; CustomBarsTestCase.prototype.setUp = function() { document.body.innerHTML = "
"; + Dygraph.getContext = function(canvas) { + return new Proxy(_origFunc(canvas)); + } }; CustomBarsTestCase.prototype.tearDown = function() { + Dygraph.getContext = _origFunc; }; // This test used to reliably produce an infinite loop. @@ -106,3 +111,43 @@ CustomBarsTestCase.prototype.testCustomBarsAtTop = function() { var sampler = new PixelSampler(g); assertEquals([0, 255, 0, 38], sampler.colorAtCoordinate(5, 60)); }; + +// Tests that custom bars work with log scale. +CustomBarsTestCase.prototype.testCustomBarsLogScale = function() { + var g = new Dygraph(document.getElementById("graph"), + [ + [1, [10, 10, 100]], + [5, [15,120, 80]], + [9, [10, 50, 100]] + ], { + width: 500, height: 350, + customBars: true, + errorBars: true, + valueRange: [1, 120], + drawXGrid: false, + drawYGrid: false, + drawXAxis: false, + drawYAxis: false, + fillAlpha: 1.0, + logscale: true, + colors: [ '#00FF00' ] + }); + + // The following assertions describe the sides of the custom bars, which are + // drawn in two halves. + CanvasAssertions.assertConsecutiveLinesDrawn( + g.hidden_ctx_, + [[0, 13.329014086362069], + [247.5, 29.64240889852502], + [247.5, 152.02209814465604], + [0, 181.66450704318103]], + { fillStyle: "#00ff00" }); + + CanvasAssertions.assertConsecutiveLinesDrawn( + g.hidden_ctx_, + [[247.5, 29.64240889852502], + [495, 13.329014086362069], + [495, 181.66450704318103], + [247.5, 152.02209814465604]], + { fillStyle: "#00ff00" }); +}; diff --git a/dygraph-layout.js b/dygraph-layout.js index 9a0ac3d..900db68 100644 --- a/dygraph-layout.js +++ b/dygraph-layout.js @@ -210,8 +210,8 @@ DygraphLayout.prototype._evaluateLimits = function() { } }; -DygraphLayout._calcYNormal = function(axis, value) { - if (axis.logscale) { +DygraphLayout._calcYNormal = function(axis, value, logscale) { + if (logscale) { return 1.0 - ((Dygraph.log10(value) - Dygraph.log10(axis.minyval)) * axis.ylogscale); } else { return 1.0 - ((value - axis.minyval) * axis.yscale); @@ -232,6 +232,8 @@ DygraphLayout.prototype._evaluateLineCharts = function() { var dataset = this.datasets[setIdx]; var setName = this.setNames[setIdx]; var axis = this.dygraph_.axisPropertiesForSeries(setName); + // TODO (konigsberg): use optionsForAxis instead. + var logscale = this.dygraph_.attributes_.getForSeries("logscale", setIdx); // Preallocating the size of points reduces reallocations, and therefore, // calls to collect garbage. @@ -245,7 +247,7 @@ DygraphLayout.prototype._evaluateLineCharts = function() { // Range from 0-1 where 0 represents left and 1 represents right. var xNormal = (xValue - this.minxval) * this.xscale; // Range from 0-1 where 0 represents top and 1 represents bottom - var yNormal = DygraphLayout._calcYNormal(axis, yValue); + var yNormal = DygraphLayout._calcYNormal(axis, yValue, logscale); // TODO(danvk): drop the point in this case, don't null it. // The nulls create complexity in DygraphCanvasRenderer._drawSeries. @@ -322,6 +324,9 @@ DygraphLayout.prototype.evaluateWithError = function() { var dataset = this.datasets[setIdx]; var setName = this.setNames[setIdx]; var axis = this.dygraph_.axisPropertiesForSeries(setName); + // TODO (konigsberg): use optionsForAxis instead. + var logscale = this.dygraph_.attributes_.getForSeries("logscale", setIdx); + for (j = 0; j < dataset.length; j++, i++) { var item = dataset[j]; var xv = DygraphLayout.parseFloat_(item[0]); @@ -334,8 +339,8 @@ DygraphLayout.prototype.evaluateWithError = function() { var yv_minus = yv - errorMinus; var yv_plus = yv + errorPlus; - points[j].y_top = DygraphLayout._calcYNormal(axis, yv_minus); - points[j].y_bottom = DygraphLayout._calcYNormal(axis, yv_plus); + points[j].y_top = DygraphLayout._calcYNormal(axis, yv_minus, logscale); + points[j].y_bottom = DygraphLayout._calcYNormal(axis, yv_plus, logscale); } } } -- 2.7.4