From e2d8db3a5de3343e44e92d087e5cc3d2a56da1a6 Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Thu, 16 Aug 2012 16:31:17 -0400 Subject: [PATCH] Fix issue 139:set fill option by series rather than for entire graph --- auto_tests/misc/local.html | 1 + auto_tests/tests/per_series.js | 87 ++++++++++++++++++++++++++++++++++++++++++ dygraph-canvas.js | 30 ++++++++++----- tests/fillGraph.html | 17 +++++++++ 4 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 auto_tests/tests/per_series.js diff --git a/auto_tests/misc/local.html b/auto_tests/misc/local.html index 89c7efc..2104d31 100644 --- a/auto_tests/misc/local.html +++ b/auto_tests/misc/local.html @@ -43,6 +43,7 @@ + diff --git a/auto_tests/tests/per_series.js b/auto_tests/tests/per_series.js new file mode 100644 index 0000000..3b6ee54 --- /dev/null +++ b/auto_tests/tests/per_series.js @@ -0,0 +1,87 @@ +/** + * @fileoverview FILL THIS IN + * + * @author danvk@google.com (Dan Vanderkam) + */ +var perSeriesTestCase = TestCase("per-series"); + +perSeriesTestCase.prototype.setUp = function() { + document.body.innerHTML = "
"; +}; + +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 = { + width: 480, + height: 320, + drawXGrid: false, + drawYGrid: false, + drawXAxis: false, + drawYAxis: false, + Y: { fillGraph: true }, + colors: [ '#FF0000', '#0000FF' ], + fillAlpha: 0.15 + }; + var data = "X,Y,Z\n" + + "1,0,0\n" + + "2,0,1\n" + + "3,0,1\n" + + "4,0,0\n" + + "5,0,0\n" + + "6,1,0\n" + + "7,1,0\n" + + "8,0,0\n" + ; + + var graph = document.getElementById("graph"); + g = new Dygraph(graph, data, opts); + + var sampler = new PixelSampler(g); + + // Inside of the "Z" bump -- no fill. + assertEquals([0,0,0,0], sampler.colorAtCoordinate(2.5, 0.5)); + + // Inside of the "Y" bump -- filled in. + assertEquals([255,0,0,38], sampler.colorAtCoordinate(6.5, 0.5)); +}; + diff --git a/dygraph-canvas.js b/dygraph-canvas.js index 165d885..c79e1b8 100644 --- a/dygraph-canvas.js +++ b/dygraph-canvas.js @@ -583,15 +583,15 @@ DygraphCanvasRenderer._linePlotter = function(e) { */ DygraphCanvasRenderer._errorPlotter = function(e) { var g = e.dygraph; + var setName = e.setName; var errorBars = g.getOption("errorBars") || g.getOption("customBars"); if (!errorBars) return; - var fillGraph = g.getOption("fillGraph"); + var fillGraph = g.getOption("fillGraph", setName); if (fillGraph) { g.warn("Can't use fillGraph option with error bars"); } - var setName = e.setName; var ctx = e.drawingContext; var color = e.color; var fillAlpha = g.getOption('fillAlpha', setName); @@ -661,18 +661,12 @@ DygraphCanvasRenderer._errorPlotter = function(e) { * @private */ DygraphCanvasRenderer._fillPlotter = function(e) { - var g = e.dygraph; - if (!g.getOption("fillGraph")) return; - // We'll handle all the series at once, not one-by-one. if (e.seriesIndex !== 0) return; - var ctx = e.drawingContext; - var area = e.plotArea; - var sets = e.allSeriesPoints; - var setCount = sets.length; - + var g = e.dygraph; var setNames = g.getLabels().slice(1); // remove x-axis + // getLabels() includes names for invisible series, which are not included in // allSeriesPoints. We remove those to make the two match. // TODO(danvk): provide a simpler way to get this information. @@ -680,6 +674,20 @@ DygraphCanvasRenderer._fillPlotter = function(e) { if (!g.visibility()[i]) setNames.splice(i, 1); } + var anySeriesFilled = (function() { + for (var i = 0; i < setNames.length; i++) { + if (g.getOption("fillGraph", setNames[i])) return true; + } + return false; + })(); + + if (!anySeriesFilled) return; + + var ctx = e.drawingContext; + var area = e.plotArea; + var sets = e.allSeriesPoints; + var setCount = sets.length; + var fillAlpha = g.getOption('fillAlpha'); var stepPlot = g.getOption('stepPlot'); var stackedGraph = g.getOption("stackedGraph"); @@ -691,6 +699,8 @@ DygraphCanvasRenderer._fillPlotter = function(e) { // process sets in reverse order (needed for stacked graphs) for (var setIdx = setCount - 1; setIdx >= 0; setIdx--) { var setName = setNames[setIdx]; + if (!g.getOption('fillGraph', setName)) continue; + var color = colors[setIdx]; var axis = g.axisPropertiesForSeries(setName); var axisY = 1.0 + axis.minyval * axis.yscale; diff --git a/tests/fillGraph.html b/tests/fillGraph.html index b6c501d..0188dc4 100644 --- a/tests/fillGraph.html +++ b/tests/fillGraph.html @@ -20,6 +20,9 @@

Filled, negatives:

+

Filled on a per-series basis:

+
+ -- 2.7.4