Fork off Karma file for in-browser debug
[dygraphs.git] / auto_tests / tests / custom_bars.js
1 // Copyright 2011 Google Inc. All Rights Reserved.
2
3 /**
4 * @fileoverview Regression test based on some strange customBars data.
5 * @author danvk@google.com (Dan Vanderkam)
6 */
7 describe("custom-bars", function() {
8
9 var _origFunc = Dygraph.getContext;
10 beforeEach(function() {
11 document.body.innerHTML = "<div id='graph'></div>";
12 Dygraph.getContext = function(canvas) {
13 return new Proxy(_origFunc(canvas));
14 }
15 });
16
17 afterEach(function() {
18 Dygraph.getContext = _origFunc;
19 });
20
21 // This test used to reliably produce an infinite loop.
22 it('testCustomBarsNoHang', function() {
23 var opts = {
24 width: 480,
25 height: 320,
26 customBars: true
27 };
28 var data = "X,Y1,Y2\n" +
29 "1,1178.0;1527.5;1856.6,0;22365658;0\n" +
30 "2,1253.0;1303.3;1327.3,0;22368228;0\n" +
31 "3,878.0;1267.0;1357.1,0;22368895;0\n" +
32 "4,1155.0;1273.1;1303.5,0;22369665;0\n" +
33 "5,1089.0;1294.8;1355.3,0;22370160;0\n" +
34 "6,1088.0;1268.6;1336.1,0;22372346;0\n" +
35 "7,1141.0;1269.1;1301.2,0;22373318;0\n" +
36 "8,1072.0;1255.8;1326.2,0;22374310;0\n" +
37 "9,1209.0;1309.2;1351.8,0;22374924;0\n" +
38 "10,1230.0;1303.9;1332.6,0;22380163;0\n" +
39 "11,1014.0;1263.5;1330.8,0;22381117;0\n" +
40 "12,853.0;1215.6;1330.6,0;22381556;0\n" +
41 "13,1134.0;1581.9;1690.1,0;22384631;0\n" +
42 "14,1113.0;1540.1;1676.5,0;22386933;0\n" +
43 "15,1130.0;1542.7;1678.3,0;22393459;0\n" +
44 "18,1582.0;1644.4;1690.2,0;22395914;0\n" +
45 "19,878.0;1558.3;1708.1,0;22397732;0\n" +
46 "20,1076.0;1598.4;1723.8,0;22397886;0\n" +
47 "21,1077.0;1574.0;1685.3,0;22398659;0\n" +
48 "22,1118.0;1590.4;1697.6,0;22399009;0\n" +
49 "23,1031.0;1473.1;1644.9,0;22401969;0\n" +
50 "24,1090.0;1480.7;1640.0,0;22417989;0\n" +
51 "25,1592.0;1681.7;1714.4,0;22422819;0\n" +
52 "26,1251.0;1657.8;1750.6,0;22423681;0\n" +
53 "27,1144.0;1660.9;1776.2,0;22426947;0\n" +
54 "28,1178.0;1642.4;1745.6,0;22428238;0\n" +
55 "29,1169.0;1649.1;1757.5,0;22429524;0\n" +
56 "30,1150.0;1596.1;1746.7,0;22433472;0\n" +
57 "31,1099.0;1586.5;1732.8,0;22434308;0\n" +
58 "32,1120.0;1456.0;1620.3,0;22434821;0\n" +
59 "33,1640.0;1687.7;1709.0,0;22434882;0\n" +
60 "34,1671.0;1712.1;1733.7,0;22435116;0\n" +
61 "35,,0;22437620;0\n";
62 var graph = document.getElementById("graph");
63 var g = new Dygraph(graph, data, opts);
64 });
65
66 // Regression test for http://code.google.com/p/dygraphs/issues/detail?id=201
67 it('testCustomBarsZero', function() {
68 var opts = {
69 customBars: true
70 };
71 var data = "X,Y1,Y2\n" +
72 "1,1;2;3,0;0;0\n" +
73 "2,2;3;4,0;0;0\n" +
74 "3,1;3;5,0;0;0\n";
75
76 var graph = document.getElementById("graph");
77 var g = new Dygraph(graph, data, opts);
78
79 var range = g.yAxisRange();
80 assert.isTrue(range[0] <= 0, 'y-axis must include 0');
81 assert.isTrue(range[1] >= 5, 'y-axis must include 5');
82 });
83
84 // Regression test for http://code.google.com/p/dygraphs/issues/detail?id=229
85 it('testCustomBarsAtTop', function() {
86 var g = new Dygraph(document.getElementById("graph"),
87 [
88 [1, [10, 10, 100]],
89 [1, [10, 10, 100]],
90 [2, [15, 20, 110]],
91 [3, [10, 30, 100]],
92 [4, [15, 40, 110]],
93 [5, [10,120, 100]],
94 [6, [15, 50, 110]],
95 [7, [10, 70, 100]],
96 [8, [15, 90, 110]],
97 [9, [10, 50, 100]]
98 ], {
99 width: 500, height: 350,
100 customBars: true,
101 errorBars: true,
102 axes: {
103 x: {
104 drawGrid: false,
105 drawAxis: false,
106 },
107 y: {
108 drawGrid: false,
109 drawAxis: false,
110 }
111 },
112 valueRange: [0, 120],
113 fillAlpha: 0.15,
114 colors: ['#00FF00'],
115 labels: ['X', 'Y']
116 });
117
118 var sampler = new PixelSampler(g);
119 assert.deepEqual([0, 255, 0, 38], sampler.colorAtCoordinate(5, 60));
120 });
121
122 // Tests that custom bars work with log scale.
123 it('testCustomBarsLogScale', function() {
124 var g = new Dygraph(document.getElementById("graph"),
125 [
126 [1, [10, 10, 100]],
127 [5, [15,120, 80]],
128 [9, [10, 50, 100]]
129 ], {
130 width: 500, height: 350,
131 customBars: true,
132 errorBars: true,
133 valueRange: [1, 120],
134 axes : {
135 x : {
136 drawGrid: false,
137 drawAxis: false,
138 },
139 y : {
140 drawGrid: false,
141 drawAxis: false,
142 }
143 },
144 fillAlpha: 1.0,
145 logscale: true,
146 colors: ['#00FF00'],
147 labels: ['X', 'Y']
148 });
149
150 // The following assertions describe the sides of the custom bars, which are
151 // drawn in two halves.
152 CanvasAssertions.assertConsecutiveLinesDrawn(
153 g.hidden_ctx_,
154 [[0, 13.329014086362069],
155 [247.5, 29.64240889852502],
156 [247.5, 152.02209814465604],
157 [0, 181.66450704318103]],
158 { fillStyle: "#00ff00" });
159
160 CanvasAssertions.assertConsecutiveLinesDrawn(
161 g.hidden_ctx_,
162 [[247.5, 29.64240889852502],
163 [495, 13.329014086362069],
164 [495, 181.66450704318103],
165 [247.5, 152.02209814465604]],
166 { fillStyle: "#00ff00" });
167 });
168
169 it('testCustomBarsWithNegativeValuesInLogScale', function() {
170 var graph = document.getElementById("graph");
171
172 var count = 0;
173 var drawPointCallback = function() {
174 count++;
175 };
176
177 var g = new Dygraph(graph,
178 [
179 [1, [10, 20,30]],
180 [2, [5, 10, 15]],
181 [3, [-1, 5, 10]]
182 ],
183 {
184 drawPoints: true,
185 drawPointCallback: drawPointCallback,
186 customBars: true,
187 labels: ['X', 'Y']
188 });
189
190 // Normally all three points would be drawn.
191 assert.equal(3, count);
192 count = 0;
193
194 // In log scale, the third point shouldn't be shown.
195 g.updateOptions({ logscale : true });
196 assert.equal(2, count);
197 });
198
199 });