Fix issue 139:set fill option by series rather than for entire graph
[dygraphs.git] / auto_tests / tests / per_series.js
CommitLineData
e2d8db3a
DV
1/**
2 * @fileoverview FILL THIS IN
3 *
4 * @author danvk@google.com (Dan Vanderkam)
5 */
6var perSeriesTestCase = TestCase("per-series");
7
8perSeriesTestCase.prototype.setUp = function() {
9 document.body.innerHTML = "<div id='graph'></div>";
10};
11
12perSeriesTestCase.prototype.tearDown = function() {
13};
14
15/**
16 * @constructor
17 */
18var PixelSampler = function(dygraph) {
19 this.dygraph_ = dygraph;
20
21 var canvas = dygraph.hidden_;
22 var ctx = canvas.getContext("2d");
23 this.imageData_ = ctx.getImageData(0, 0, canvas.width, canvas.height);
24};
25
26/**
27 * @param {number} x The screen x-coordinate at which to sample.
28 * @param {number} y The screen y-coordinate at which to sample.
29 * @return {Array.<number>} a 4D array: [R, G, B, alpha]. All four values
30 * are in [0, 255]. A pixel which has never been touched will be [0,0,0,0].
31 */
32PixelSampler.prototype.colorAtPixel = function(x, y) {
33 var i = 4 * (x + this.imageData_.width * y);
34 var d = this.imageData_.data;
35 return [d[i], d[i+1], d[i+2], d[i+3]];
36};
37
38/**
39 * The method samples a color using data coordinates (not screen coordinates).
40 * This will round your data coordinates to the nearest screen pixel before
41 * sampling.
42 * @param {number} x The data x-coordinate at which to sample.
43 * @param {number} y The data y-coordinate at which to sample.
44 * @return {Array.<number>} a 4D array: [R, G, B, alpha]. All four values
45 * are in [0, 255]. A pixel which has never been touched will be [0,0,0,0].
46 */
47PixelSampler.prototype.colorAtCoordinate = function(x, y) {
48 var dom_xy = this.dygraph_.toDomCoords(x, y);
49 return this.colorAtPixel(Math.round(dom_xy[0]), Math.round(dom_xy[1]));
50};
51
52
53perSeriesTestCase.prototype.testPerSeriesFill = function() {
54 var opts = {
55 width: 480,
56 height: 320,
57 drawXGrid: false,
58 drawYGrid: false,
59 drawXAxis: false,
60 drawYAxis: false,
61 Y: { fillGraph: true },
62 colors: [ '#FF0000', '#0000FF' ],
63 fillAlpha: 0.15
64 };
65 var data = "X,Y,Z\n" +
66 "1,0,0\n" +
67 "2,0,1\n" +
68 "3,0,1\n" +
69 "4,0,0\n" +
70 "5,0,0\n" +
71 "6,1,0\n" +
72 "7,1,0\n" +
73 "8,0,0\n"
74 ;
75
76 var graph = document.getElementById("graph");
77 g = new Dygraph(graph, data, opts);
78
79 var sampler = new PixelSampler(g);
80
81 // Inside of the "Z" bump -- no fill.
82 assertEquals([0,0,0,0], sampler.colorAtCoordinate(2.5, 0.5));
83
84 // Inside of the "Y" bump -- filled in.
85 assertEquals([255,0,0,38], sampler.colorAtCoordinate(6.5, 0.5));
86};
87