1e71aad03a6b1817c9c65b9452d3e2d87b181201
2 * @fileoverview Test cases for the option "stepPlot" especially for the
3 * scenario where the option is not set for the whole graph but
6 * @author david.eberlein@ch.sauter-bc.com (Fr. Sauter AG)
8 var StepTestCase
= TestCase("grid_per_axis");
10 StepTestCase
.prototype.setUp
= function() {
11 document
.body
.innerHTML
= "<div id='graph'></div>";
14 StepTestCase
.origFunc
= Dygraph
.getContext
;
16 StepTestCase
.prototype.setUp
= function() {
17 document
.body
.innerHTML
= "<div id='graph'></div>";
18 Dygraph
.getContext
= function(canvas
) {
19 return new Proxy(StepTestCase
.origFunc(canvas
));
23 StepTestCase
.prototype.tearDown
= function() {
24 Dygraph
.getContext
= StepTestCase
.origFunc
;
27 StepTestCase
.prototype.testIndependantGrids
= function() {
32 labels
: [ "X", "Left", "Right" ],
44 independentTicks
: true
49 var data
= [ [ 1, 0, 0 ], [ 2, 12, 88 ], [ 3, 88, 122 ], [ 4, 63, 273 ],
51 var graph
= document
.getElementById("graph");
52 var g
= new Dygraph(graph
, data
, opts
);
56 // The expected gridlines
57 var yGridlines
= [ 0, 20, 40, 60, 80, 100, 120 ];
58 var y2Gridlines
= [ 0, 50, 100, 150, 200, 250, 300, 350 ];
59 var gridlines
= [ yGridlines
, y2Gridlines
];
62 return Math
.round(x
) + 0.5;
64 function halfDown(y
) {
65 return Math
.round(y
) - 0.5;
69 x
= halfUp(g
.plotter_
.area
.x
);
70 // Step through y(0) and y2(1) axis
71 for ( var axis
= 0; axis
< 2; axis
++) {
72 // Step through all gridlines of the axis
73 for ( var i
= 0; i
< gridlines
[axis
].length
; i
++) {
75 var labels
= Util
.getYLabels(axis
+ 1);
76 assertEquals("Expected label not found.", gridlines
[axis
][i
], labels
[i
]);
78 // Check that the grid was drawn.
79 y
= halfDown(g
.toDomYCoord(gridlines
[axis
][i
], axis
));
81 var p2
= [ x
+ g
.plotter_
.area
.w
, y
];
82 CanvasAssertions
.assertLineDrawn(htx
, p1
, p2
, attrs
);
87 StepTestCase
.prototype.testPerAxisGridColors
= function() {
92 labels
: [ "X", "Left", "Right" ],
103 gridLineColor
: "#0000ff",
108 independentTicks
: true,
109 gridLineColor
: "#ff0000",
114 var data
= [ [ 1, 0, 0 ], [ 2, 12, 88 ], [ 3, 88, 122 ], [ 4, 63, 273 ],
116 var graph
= document
.getElementById("graph");
117 var g
= new Dygraph(graph
, data
, opts
);
120 // The expected gridlines
121 var yGridlines
= [ 20, 40, 60, 80, 100, 120 ];
122 var y2Gridlines
= [ 50, 100, 150, 200, 250, 300, 350 ];
123 var gridlines
= [ yGridlines
, y2Gridlines
];
124 var gridColors
= [ [ 0, 0, 255, 255 ], [ 255, 0, 0, 255 ] ];
127 return Math
.round(x
) + 1;
129 function halfDown(y
) {
130 return Math
.round(y
) - 1;
133 x
= halfUp(g
.plotter_
.area
.x
);
134 // Step through y(0) and y2(1) axis
135 for ( var axis
= 0; axis
< 2; axis
++) {
136 // Step through all gridlines of the axis
137 for ( var i
= 0; i
< gridlines
[axis
].length
; i
++) {
138 y
= halfDown(g
.toDomYCoord(gridlines
[axis
][i
], axis
));
139 // Check the grid colors.
140 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: " + y
,
141 gridColors
[axis
], Util
.samplePixel(g
.hidden_
, x
, y
));
145 StepTestCase
.prototype.testPerAxisGridWidth
= function() {
150 gridLineColor
: "#ff0000",
151 labels
: [ "X", "Left", "Right" ],
169 independentTicks
: true,
174 var data
= [ [ 1, 0, 0 ], [ 2, 12, 88 ], [ 3, 88, 122 ], [ 4, 63, 273 ],
176 var graph
= document
.getElementById("graph");
177 var g
= new Dygraph(graph
, data
, opts
);
180 // The expected gridlines
181 var yGridlines
= [ 20, 40, 60, 80 ];
182 var y2Gridlines
= [ 50, 100, 150, 200, 250, 350 ];
183 var gridlines
= [ yGridlines
, y2Gridlines
];
184 var xGridlines
= [ 2, 3, 4 ];
187 return Math
.round(x
) + 1;
189 function halfDown(y
) {
190 return Math
.round(y
) - 1;
193 x
= halfUp(g
.plotter_
.area
.x
+ 10);
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 var drawnPixeldown2
= Util
.samplePixel(g
.hidden_
, x
, y
- 2);
200 var drawnPixeldown1
= Util
.samplePixel(g
.hidden_
, x
, y
- 1);
201 var drawnPixel
= Util
.samplePixel(g
.hidden_
, x
, y
);
202 var drawnPixelup1
= Util
.samplePixel(g
.hidden_
, x
, y
+ 1);
203 var drawnPixelup2
= Util
.samplePixel(g
.hidden_
, x
, y
+ 2);
204 // Check the grid width.
206 case 0: // y with 2 pixels width
207 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: "
208 + y
, [ 0, 0, 0, 0 ], drawnPixeldown2
);
209 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: "
210 + y
, [ 255, 0, 0, 127 ], drawnPixeldown1
);
211 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: "
212 + y
, [ 255, 0, 0, 255 ], drawnPixel
);
213 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: "
214 + y
, [ 255, 0, 0, 128 ], drawnPixelup1
);
215 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: "
216 + y
, [ 0, 0, 0, 0 ], drawnPixelup2
);
218 case 1: // y2 with 1 pixel width
219 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: "
220 + y
, [ 0, 0, 0, 0 ], drawnPixeldown1
);
221 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: "
222 + y
, [ 255, 0, 0, 255 ], drawnPixel
);
223 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: "
224 + y
, [ 0, 0, 0, 0 ], drawnPixelup1
);
230 // Check the x axis grid
231 y
= halfDown(g
.plotter_
.area
.y
) + 10;
232 for ( var i
= 0; i
< xGridlines
.length
; i
++) {
233 x
= halfUp(g
.toDomXCoord(xGridlines
[i
]));
234 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: " + y
, [
235 0, 0, 0, 0 ], Util
.samplePixel(g
.hidden_
, x
- 4, y
));
236 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: " + y
, [
237 255, 0, 0, 128 ], Util
.samplePixel(g
.hidden_
, x
- 3, y
));
238 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: " + y
, [
239 255, 0, 0, 255 ], Util
.samplePixel(g
.hidden_
, x
- 2, y
));
240 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: " + y
, [
241 255, 0, 0, 255 ], Util
.samplePixel(g
.hidden_
, x
- 1, y
));
242 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: " + y
, [
243 255, 0, 0, 255 ], Util
.samplePixel(g
.hidden_
, x
, y
));
244 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: " + y
, [
245 255, 0, 0, 128 ], Util
.samplePixel(g
.hidden_
, x
+ 1, y
));
246 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: " + y
, [
247 0, 0, 0, 0 ], Util
.samplePixel(g
.hidden_
, x
+ 2, y
));
250 StepTestCase
.prototype.testGridLinePattern
= function() {
258 labels
: [ "X", "Left", "Right" ],
259 colors
: [ "rgba(0,0,0,0)", "rgba(0,0,0,0)" ],
270 gridLineColor
: "#0000ff",
271 gridLinePattern
: [ 4, 4 ]
275 var data
= [ [ 1, 0, 0 ], [ 2, 12, 88 ], [ 3, 88, 122 ], [ 4, 63, 273 ],
277 var graph
= document
.getElementById("graph");
278 var g
= new Dygraph(graph
, data
, opts
);
281 // The expected gridlines
282 var yGridlines
= [ 0, 20, 40, 60, 80, 100, 120 ];
285 return Math
.round(x
) + 1;
287 function halfDown(y
) {
288 return Math
.round(y
) - 1;
291 x
= halfUp(g
.plotter_
.area
.x
);
292 // Step through all gridlines of the axis
293 for ( var i
= 0; i
< yGridlines
.length
; i
++) {
294 y
= halfDown(g
.toDomYCoord(yGridlines
[i
], 0));
295 // Step through the pixels of the line and test the pattern.
296 for (x
; x
< g
.plotter_
.area
.w
; x
++) {
297 var drawnPixel
= Util
.samplePixel(g
.hidden_
, x
, y
);
298 var pattern
= (Math
.floor((x
) / 4)) % 2;
301 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: "
302 + y
, [ 0, 0, 255, 77 ], drawnPixel
);
305 assertEquals("Unexpected grid color found at pixel: x: " + x
+ "y: "
306 + y
, [ 0, 0, 0, 0 ], drawnPixel
);