2 * @fileoverview Test cases for the per-axis grid options, including the new
3 * option "gridLinePattern".
5 * @author david.eberlein@ch.sauter-bc.com (Fr. Sauter AG)
8 import Dygraph from
'../../src/dygraph';
9 import * as utils from
'../../src/dygraph-utils';
11 import Proxy from
'./Proxy';
12 import CanvasAssertions from
'./CanvasAssertions';
13 import Util from
'./Util';
14 import PixelSampler from
'./PixelSampler';
16 describe("grid-per-axis", function() {
19 useProxyCanvas(utils
, Proxy
);
21 it('testIndependentGrids', function() {
26 labels
: [ "X", "Left", "Right" ],
38 independentTicks
: true
43 var data
= [ [ 1, 0, 0 ], [ 2, 12, 88 ], [ 3, 88, 122 ], [ 4, 63, 273 ],
45 var graph
= document
.getElementById("graph");
46 var g
= new Dygraph(graph
, data
, opts
);
48 var htx
= g
.hidden_ctx_
;
50 // The expected gridlines
51 var yGridlines
= [ 0, 20, 40, 60, 80, 100, 120 ];
52 var y2Gridlines
= [ 0, 50, 100, 150, 200, 250, 300, 350 ];
53 var gridlines
= [ yGridlines
, y2Gridlines
];
56 return Math
.round(x
) + 0.5;
58 function halfDown(y
) {
59 return Math
.round(y
) - 0.5;
63 x
= halfUp(g
.plotter_
.area
.x
);
64 // Step through y(0) and y2(1) axis
65 for ( var axis
= 0; axis
< 2; axis
++) {
66 // Step through all gridlines of the axis
67 for ( var i
= 0; i
< gridlines
[axis
].length
; i
++) {
69 var labels
= Util
.getYLabels(axis
+ 1);
70 assert
.equal(gridlines
[axis
][i
], labels
[i
], "Expected label not found.");
72 // Check that the grid was drawn.
73 y
= halfDown(g
.toDomYCoord(gridlines
[axis
][i
], axis
));
75 var p2
= [ x
+ g
.plotter_
.area
.w
, y
];
76 CanvasAssertions
.assertLineDrawn(htx
, p1
, p2
, attrs
);
81 it('testPerAxisGridColors', function() {
86 labels
: [ "X", "Left", "Right" ],
97 gridLineColor
: "#0000ff",
102 independentTicks
: true,
103 gridLineColor
: "#ff0000",
108 var data
= [ [ 1, 0, 0 ], [ 2, 12, 88 ], [ 3, 88, 122 ], [ 4, 63, 273 ],
110 var graph
= document
.getElementById("graph");
111 var g
= new Dygraph(graph
, data
, opts
);
112 var htx
= g
.hidden_ctx_
;
114 // The expected gridlines
115 var yGridlines
= [ 20, 40, 60, 80, 100, 120 ];
116 var y2Gridlines
= [ 50, 100, 150, 200, 250, 300, 350 ];
117 var gridlines
= [ yGridlines
, y2Gridlines
];
118 var gridColors
= [ [ 0, 0, 255, 255 ], [ 255, 0, 0, 255 ] ];
121 return Math
.round(x
) + 1;
123 function halfDown(y
) {
124 return Math
.round(y
) - 1;
127 x
= halfUp(g
.plotter_
.area
.x
);
128 var sampler
= new PixelSampler(g
);
129 // Step through y(0) and y2(1) axis
130 for ( var axis
= 0; axis
< 2; axis
++) {
131 // Step through all gridlines of the axis
132 for ( var i
= 0; i
< gridlines
[axis
].length
; i
++) {
133 y
= halfDown(g
.toDomYCoord(gridlines
[axis
][i
], axis
));
134 // Check the grid colors.
135 assert
.deepEqual(gridColors
[axis
], sampler
.colorAtPixel(x
, y
),
136 "Unexpected grid color found at pixel: x: " + x
+ "y: " + y
);
141 it('testPerAxisGridWidth', function() {
146 gridLineColor
: "#ff0000",
147 labels
: [ "X", "Left", "Right" ],
165 independentTicks
: true,
170 var data
= [ [ 1, 0, 0 ], [ 2, 12, 88 ], [ 3, 88, 122 ], [ 4, 63, 273 ],
172 var graph
= document
.getElementById("graph");
173 var g
= new Dygraph(graph
, data
, opts
);
174 var htx
= g
.hidden_ctx_
;
176 // The expected gridlines
177 var yGridlines
= [ 20, 40, 60, 80 ];
178 var y2Gridlines
= [ 50, 100, 150, 200, 250, 350 ];
179 var gridlines
= [ yGridlines
, y2Gridlines
];
180 var xGridlines
= [ 2, 3, 4 ];
181 var gridColor
= [ 255, 0, 0 ];
182 var emptyColor
= [ 0, 0, 0 ];
185 return Math
.round(x
) + 1;
187 function halfDown(y
) {
188 return Math
.round(y
) - 1;
191 x
= halfUp(g
.plotter_
.area
.x
+ 10);
193 var sampler
= new PixelSampler(g
);
194 // Step through y(0) and y2(1) axis
195 for ( var axis
= 0; axis
< 2; axis
++) {
196 // Step through all gridlines of the axis
197 for ( var i
= 0; i
< gridlines
[axis
].length
; i
++) {
198 y
= halfDown(g
.toDomYCoord(gridlines
[axis
][i
], axis
));
199 // Ignore the alpha value
201 // FIXME(pholden): this test fails with a context pixel ratio of 2.
202 var drawnPixeldown2
= sampler
.rgbAtPixel(x
, y
- 2);
203 var drawnPixeldown1
= sampler
.rgbAtPixel(x
, y
- 1);
204 var drawnPixel
= sampler
.rgbAtPixel(x
, y
);
205 var drawnPixelup1
= sampler
.rgbAtPixel(x
, y
+ 1);
206 var drawnPixelup2
= sampler
.rgbAtPixel(x
, y
+ 2);
207 // Check the grid width.
209 case 0: // y with 2 pixels width
210 assert
.deepEqual(emptyColor
, drawnPixeldown2
, "Unexpected y-grid color found at pixel: x: " + x
+ "y: " + y
);
211 assert
.deepEqual(gridColor
, drawnPixeldown1
, "Unexpected y-grid color found at pixel: x: " + x
+ "y: " + y
);
212 assert
.deepEqual(gridColor
, drawnPixel
, "Unexpected y-grid color found at pixel: x: " + x
+ "y: " + y
);
213 assert
.deepEqual(gridColor
, drawnPixelup1
, "Unexpected y-grid color found at pixel: x: " + x
+ "y: " + y
);
214 assert
.deepEqual(emptyColor
, drawnPixelup2
, "Unexpected y-grid color found at pixel: x: " + x
+ "y: " + y
);
216 case 1: // y2 with 1 pixel width
217 assert
.deepEqual(emptyColor
, drawnPixeldown1
, "Unexpected y2-grid color found at pixel: x: " + x
+ "y: " + y
);
218 assert
.deepEqual(gridColor
, drawnPixel
, "Unexpected y2-grid color found at pixel: x: " + x
+ "y: " + y
);
219 assert
.deepEqual(emptyColor
, drawnPixelup1
, "Unexpected y2-grid color found at pixel: x: " + x
+ "y: " + y
);
225 // Check the x axis grid
226 y
= halfDown(g
.plotter_
.area
.y
) + 10;
227 for ( var i
= 0; i
< xGridlines
.length
; i
++) {
228 x
= halfUp(g
.toDomXCoord(xGridlines
[i
]));
229 assert
.deepEqual(emptyColor
, sampler
.rgbAtPixel(x
- 4, y
),
230 "Unexpected x-grid color found at pixel: x: " + x
+ "y: " + y
);
231 assert
.deepEqual(gridColor
, sampler
.rgbAtPixel(x
- 3, y
),
232 "Unexpected x-grid color found at pixel: x: " + x
+ "y: " + y
);
233 assert
.deepEqual(gridColor
, sampler
.rgbAtPixel(x
- 2, y
),
234 "Unexpected x-grid color found at pixel: x: " + x
+ "y: " + y
);
235 assert
.deepEqual(gridColor
, sampler
.rgbAtPixel(x
- 1, y
),
236 "Unexpected x-grid color found at pixel: x: " + x
+ "y: " + y
);
237 assert
.deepEqual(gridColor
, sampler
.rgbAtPixel(x
, y
),
238 "Unexpected x-grid color found at pixel: x: " + x
+ "y: " + y
);
239 assert
.deepEqual(gridColor
, sampler
.rgbAtPixel(x
+ 1, y
),
240 "Unexpected x-grid color found at pixel: x: " + x
+ "y: " + y
);
241 assert
.deepEqual(emptyColor
, sampler
.rgbAtPixel(x
+ 2, y
),
242 "Unexpected x-grid color found at pixel: x: " + x
+ "y: " + y
);
246 // PhantomJS 1.9.x does not support setLineDash
247 // When Travis-CI updates to Phantom2, this can be re-enabled.
248 // See https://github.com/ariya/phantomjs/issues/12948
249 if (!navigator
.userAgent
.match(/PhantomJS\/1.9/)) {
250 it('testGridLinePattern', function() {
255 labels
: [ "X", "Left", "Right" ],
256 colors
: [ "rgba(0,0,0,0)", "rgba(0,0,0,0)" ],
272 gridLineColor
: "#0000ff",
273 gridLinePattern
: [ 10, 10 ]
277 var data
= [ [ 1, 0, 0 ], [ 2, 12, 88 ], [ 3, 88, 122 ], [ 4, 63, 273 ],
279 var graph
= document
.getElementById("graph");
280 var g
= new Dygraph(graph
, data
, opts
);
281 var htx
= g
.hidden_ctx_
;
283 // The expected gridlines
284 var yGridlines
= [ 0, 20, 40, 60, 80, 100, 120 ];
287 return Math
.round(x
) + 1;
289 function halfDown(y
) {
290 return Math
.round(y
) - 1;
293 var sampler
= new PixelSampler(g
);
294 // Step through all gridlines of the axis
295 for (var i
= 0; i
< yGridlines
.length
; i
++) {
296 y
= halfDown(g
.toDomYCoord(yGridlines
[i
], 0));
297 // Step through the pixels of the line and test the pattern.
298 for (x
= halfUp(g
.plotter_
.area
.x
); x
< g
.plotter_
.area
.w
; x
++) {
299 // avoid checking the edge pixels since they differ depending on the OS.
300 var pixelpos
= x
% 10;
301 if(pixelpos
< 1 || pixelpos
> 8) continue;
303 // XXX: check what this looks like at master
306 var drawnPixel
= sampler
.rgbAtPixel(x
, y
);
307 var pattern
= (Math
.floor((x
) / 10)) % 2;
310 assert
.deepEqual([ 0, 0, 255 ], drawnPixel
,
311 "Unexpected filled grid-pattern color found at pixel: x: " + x
+ " y: " + y
);
314 assert
.deepEqual([ 0, 0, 0 ], drawnPixel
,
315 "Unexpected empty grid-pattern color found at pixel: x: " + x
+ " y: " + y
);