X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=auto_tests%2Ftests%2Fper_series.js;h=8a7e081f66d55670a38d6a889ff15398fcf65d98;hb=07270a6d3c41dbff993fa891d7d48ec03ad94641;hp=3b6ee540e57f52ba18c6d8284f4dfc2a268328ec;hpb=e2d8db3a5de3343e44e92d087e5cc3d2a56da1a6;p=dygraphs.git diff --git a/auto_tests/tests/per_series.js b/auto_tests/tests/per_series.js index 3b6ee54..8a7e081 100644 --- a/auto_tests/tests/per_series.js +++ b/auto_tests/tests/per_series.js @@ -1,5 +1,5 @@ /** - * @fileoverview FILL THIS IN + * @fileoverview Tests for per-series options. * * @author danvk@google.com (Dan Vanderkam) */ @@ -12,44 +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 = { width: 480, @@ -58,7 +20,9 @@ perSeriesTestCase.prototype.testPerSeriesFill = function() { drawYGrid: false, drawXAxis: false, drawYAxis: false, - Y: { fillGraph: true }, + series: { + Y: { fillGraph: true }, + }, colors: [ '#FF0000', '#0000FF' ], fillAlpha: 0.15 }; @@ -85,3 +49,130 @@ perSeriesTestCase.prototype.testPerSeriesFill = function() { assertEquals([255,0,0,38], sampler.colorAtCoordinate(6.5, 0.5)); }; +perSeriesTestCase.prototype.testOldStyleSeries = function() { + var opts = { + pointSize : 5, + Y: { pointSize : 4 }, + }; + var graph = document.getElementById("graph"); + var data = "X,Y,Z\n1,0,0\n"; + g = new Dygraph(graph, data, opts); + + assertEquals(5, g.getOption("pointSize")); + assertEquals(4, g.getOption("pointSize", "Y")); + assertEquals(5, g.getOption("pointSize", "Z")); +}; + +perSeriesTestCase.prototype.testNewStyleSeries = function() { + var opts = { + pointSize : 5, + series : { + Y: { pointSize : 4 } + }, + }; + var graph = document.getElementById("graph"); + var data = "X,Y,Z\n1,0,0\n"; + g = new Dygraph(graph, data, opts); + + assertEquals(5, g.getOption("pointSize")); + assertEquals(4, g.getOption("pointSize", "Y")); + assertEquals(5, g.getOption("pointSize", "Z")); +}; + +perSeriesTestCase.prototype.testNewStyleSeriesTrumpsOldStyle = function() { + var opts = { + pointSize : 5, + Z : { pointSize : 6 }, + series : { + Y: { pointSize : 4 } + }, + }; + var graph = document.getElementById("graph"); + var data = "X,Y,Z\n1,0,0\n"; + g = new Dygraph(graph, data, opts); + + assertEquals(5, g.getOption("pointSize")); + assertEquals(4, g.getOption("pointSize", "Y")); + assertEquals(5, g.getOption("pointSize", "Z")); + + // Erase the series object, and Z will become visible again. + g.updateOptions({ series : undefined }); + assertEquals(5, g.getOption("pointSize")); + assertEquals(6, g.getOption("pointSize", "Z")); + assertEquals(5, g.getOption("pointSize", "Y")); +}; + +// TODO(konigsberg): move to multiple_axes.js +perSeriesTestCase.prototype.testAxisInNewSeries = function() { + var opts = { + series : { + D : { axis : 'y2' }, + C : { axis : 1 }, + B : { axis : 0 }, + E : { axis : 'y' } + } + }; + var graph = document.getElementById("graph"); + var data = "X,A,B,C,D,E\n0,1,2,3,4,5\n"; + g = new Dygraph(graph, data, opts); + + assertEquals(["A", "B", "E"], g.attributes_.seriesForAxis(0)); + assertEquals(["C", "D"], g.attributes_.seriesForAxis(1)); +}; + +// TODO(konigsberg): move to multiple_axes.js +perSeriesTestCase.prototype.testAxisInNewSeries_withAxes = function() { + var opts = { + series : { + D : { axis : 'y2' }, + C : { axis : 1 }, + B : { axis : 0 }, + E : { axis : 'y' } + }, + axes : { + y : { pointSize : 7 }, + y2 : { pointSize : 6 } + } + }; + var graph = document.getElementById("graph"); + var data = "X,A,B,C,D,E\n0,1,2,3,4,5\n"; + g = new Dygraph(graph, data, opts); + + assertEquals(["A", "B", "E"], g.attributes_.seriesForAxis(0)); + assertEquals(["C", "D"], g.attributes_.seriesForAxis(1)); + + assertEquals(1.5, g.getOption("pointSize")); + assertEquals(7, g.getOption("pointSize", "A")); + assertEquals(7, g.getOption("pointSize", "B")); + assertEquals(6, g.getOption("pointSize", "C")); + assertEquals(6, g.getOption("pointSize", "D")); + assertEquals(7, g.getOption("pointSize", "E")); +}; + +// TODO(konigsberg): move to multiple_axes.js +perSeriesTestCase.prototype.testOldAxisSpecInNewSeriesThrows = function() { + var opts = { + series : { + D : { axis : {} }, + }, + }; + var graph = document.getElementById("graph"); + var data = "X,A,B,C,D,E\n0,1,2,3,4,5\n"; + var threw = false; + try { + new Dygraph(graph, data, opts); + } catch(e) { + threw = true; + } + + assertTrue(threw); +} + +perSeriesTestCase.prototype.testColorOption = function() { + var graph = document.getElementById("graph"); + var data = "X,A,B,C\n0,1,2,3\n"; + var g = new Dygraph(graph, data, {}); + assertEquals(['rgb(64,128,0)', 'rgb(64,0,128)', 'rgb(0,128,128)'], g.getColors()); + g.updateOptions({series : { B : { color : 'purple' }}}); + assertEquals(['rgb(64,128,0)', 'purple', 'rgb(0,128,128)'], g.getColors()); +}