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