From 9f636500f08868182ecd88288636e7f8718e28de Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Thu, 16 Aug 2012 17:19:19 -0400 Subject: [PATCH] Fix Issue 328:Thick data lines shown breaks --- auto_tests/misc/local.html | 1 + auto_tests/tests/PixelSampler.js | 46 ++++++++++++++++++++++++++++++++++++++ auto_tests/tests/per_series.js | 37 ------------------------------ auto_tests/tests/simple_drawing.js | 36 +++++++++++++++++++++++++++++ dygraph-canvas.js | 5 ++--- jsTestDriver.conf | 5 +++++ 6 files changed, 90 insertions(+), 40 deletions(-) create mode 100644 auto_tests/tests/PixelSampler.js diff --git a/auto_tests/misc/local.html b/auto_tests/misc/local.html index 2104d31..d1c6cd9 100644 --- a/auto_tests/misc/local.html +++ b/auto_tests/misc/local.html @@ -15,6 +15,7 @@ + diff --git a/auto_tests/tests/PixelSampler.js b/auto_tests/tests/PixelSampler.js new file mode 100644 index 0000000..89c520f --- /dev/null +++ b/auto_tests/tests/PixelSampler.js @@ -0,0 +1,46 @@ +// Copyright 2012 Google Inc. All Rights Reserved. + +/** + * @fileoverview A class to facilitate sampling colors at particular pixels on a + * dygraph. + * @author danvk@google.com (Dan Vanderkam) + */ + +'use strict'; + +/** + * @constructor + */ +var PixelSampler = function(dygraph) { + this.dygraph_ = dygraph; + + var canvas = dygraph.hidden_; + var ctx = canvas.getContext("2d"); + this.imageData_ = ctx.getImageData(0, 0, canvas.width, canvas.height); +}; + +/** + * @param {number} x The screen x-coordinate at which to sample. + * @param {number} y The screen y-coordinate at which to sample. + * @return {Array.} a 4D array: [R, G, B, alpha]. All four values + * are in [0, 255]. A pixel which has never been touched will be [0,0,0,0]. + */ +PixelSampler.prototype.colorAtPixel = function(x, y) { + var i = 4 * (x + this.imageData_.width * y); + var d = this.imageData_.data; + return [d[i], d[i+1], d[i+2], d[i+3]]; +}; + +/** + * The method samples a color using data coordinates (not screen coordinates). + * This will round your data coordinates to the nearest screen pixel before + * sampling. + * @param {number} x The data x-coordinate at which to sample. + * @param {number} y The data y-coordinate at which to sample. + * @return {Array.} a 4D array: [R, G, B, alpha]. All four values + * are in [0, 255]. A pixel which has never been touched will be [0,0,0,0]. + */ +PixelSampler.prototype.colorAtCoordinate = function(x, y) { + var dom_xy = this.dygraph_.toDomCoords(x, y); + return this.colorAtPixel(Math.round(dom_xy[0]), Math.round(dom_xy[1])); +}; diff --git a/auto_tests/tests/per_series.js b/auto_tests/tests/per_series.js index 3b6ee54..4831ded 100644 --- a/auto_tests/tests/per_series.js +++ b/auto_tests/tests/per_series.js @@ -12,43 +12,6 @@ perSeriesTestCase.prototype.setUp = function() { perSeriesTestCase.prototype.tearDown = function() { }; -/** - * @constructor - */ -var PixelSampler = function(dygraph) { - this.dygraph_ = dygraph; - - var canvas = dygraph.hidden_; - var ctx = canvas.getContext("2d"); - this.imageData_ = ctx.getImageData(0, 0, canvas.width, canvas.height); -}; - -/** - * @param {number} x The screen x-coordinate at which to sample. - * @param {number} y The screen y-coordinate at which to sample. - * @return {Array.} a 4D array: [R, G, B, alpha]. All four values - * are in [0, 255]. A pixel which has never been touched will be [0,0,0,0]. - */ -PixelSampler.prototype.colorAtPixel = function(x, y) { - var i = 4 * (x + this.imageData_.width * y); - var d = this.imageData_.data; - return [d[i], d[i+1], d[i+2], d[i+3]]; -}; - -/** - * The method samples a color using data coordinates (not screen coordinates). - * This will round your data coordinates to the nearest screen pixel before - * sampling. - * @param {number} x The data x-coordinate at which to sample. - * @param {number} y The data y-coordinate at which to sample. - * @return {Array.} a 4D array: [R, G, B, alpha]. All four values - * are in [0, 255]. A pixel which has never been touched will be [0,0,0,0]. - */ -PixelSampler.prototype.colorAtCoordinate = function(x, y) { - var dom_xy = this.dygraph_.toDomCoords(x, y); - return this.colorAtPixel(Math.round(dom_xy[0]), Math.round(dom_xy[1])); -}; - perSeriesTestCase.prototype.testPerSeriesFill = function() { var opts = { diff --git a/auto_tests/tests/simple_drawing.js b/auto_tests/tests/simple_drawing.js index 0767d83..c7c7f9b 100644 --- a/auto_tests/tests/simple_drawing.js +++ b/auto_tests/tests/simple_drawing.js @@ -91,3 +91,39 @@ SimpleDrawingTestCase.prototype.testDrawSimpleDash = function() { // assertEquals(29, CanvasAssertions.numLinesDrawn(htx, "#ff0000")); CanvasAssertions.assertBalancedSaveRestore(htx); }; + +/** + * Tests that thick lines are drawn continuously. + * Regression test for http://code.google.com/p/dygraphs/issues/detail?id=328 + */ +SimpleDrawingTestCase.prototype.testDrawThickLine = function() { + var opts = { + drawXGrid: false, + drawYGrid: false, + drawXAxis: false, + drawYAxis: false, + strokeWidth: 15, + colors: ['#ff0000'] + }; + + var graph = document.getElementById("graph"); + // Set the dims so we pass if default changes. + graph.style.width='480px'; + graph.style.height='320px'; + var g = new Dygraph(graph, [[1, 2], [2, 5], [3, 2], [4, 7], [5, 0]], opts); + htx = g.hidden_ctx_; + + // There's a big gap in the line at (2, 5) + // If the bug is fixed, then there should be some red going up from here. + var xy = g.toDomCoords(2, 5); + var x = Math.round(xy[0]), y = Math.round(xy[1]); + + var sampler = new PixelSampler(g); + assertEquals([255,0,0,255], sampler.colorAtPixel(x, y)); + assertEquals([255,0,0,255], sampler.colorAtPixel(x, y - 1)); + assertEquals([255,0,0,255], sampler.colorAtPixel(x, y - 2)); + + // TODO(danvk): figure out a good way to restore this test. + // assertEquals(29, CanvasAssertions.numLinesDrawn(htx, "#ff0000")); + CanvasAssertions.assertBalancedSaveRestore(htx); +}; diff --git a/dygraph-canvas.js b/dygraph-canvas.js index c79e1b8..d9ef742 100644 --- a/dygraph-canvas.js +++ b/dygraph-canvas.js @@ -364,13 +364,12 @@ DygraphCanvasRenderer._drawSeries = function(e, if (stepPlot) { ctx.moveTo(prevCanvasX, prevCanvasY); ctx.lineTo(point.canvasx, prevCanvasY); - prevCanvasX = point.canvasx; } - // TODO(danvk): this moveTo is rarely necessary - ctx.moveTo(prevCanvasX, prevCanvasY); ctx.lineTo(point.canvasx, point.canvasy); } + } else { + ctx.moveTo(point.canvasx, point.canvasy); } if (drawPoints || isIsolated) { pointsOnLine.push([point.canvasx, point.canvasy]); diff --git a/jsTestDriver.conf b/jsTestDriver.conf index e038acc..756ca07 100644 --- a/jsTestDriver.conf +++ b/jsTestDriver.conf @@ -17,7 +17,12 @@ load: - dygraph-tickers.js - dygraph-dev.js - excanvas.js + # NOTE: we can't do plugins/*.js because the order is important. - plugins/base.js + - plugins/annotations.js + - plugins/axes.js + - plugins/chart-labels.js + - plugins/grid.js - plugins/legend.js - plugins/install.js - auto_tests/tests/*.js -- 2.7.4