Code comments and indentation fixes.
[dygraphs.git] / auto_tests / tests / pathological_cases.js
1 /**
2 * @fileoverview Tests zero and one-point charts.
3 * These don't have to render nicely, they just have to not crash.
4 *
5 * @author dan@dygraphs.com (Dan Vanderkam)
6 */
7 var pathologicalCasesTestCase = TestCase("pathological-cases");
8
9 pathologicalCasesTestCase.prototype.setUp = function() {
10 document.body.innerHTML = "<div id='graph'></div>";
11 };
12
13 pathologicalCasesTestCase.prototype.tearDown = function() {
14 };
15
16 pathologicalCasesTestCase.prototype.testZeroPoint = function() {
17 var opts = {
18 width: 480,
19 height: 320
20 };
21 var data = "X,Y\n";
22
23 var graph = document.getElementById("graph");
24 var g = new Dygraph(graph, data, opts);
25 };
26
27 pathologicalCasesTestCase.prototype.testOnePoint = function() {
28 var opts = {
29 width: 480,
30 height: 320
31 };
32 var data = "X,Y\n" +
33 "1,2\n";
34
35 var graph = document.getElementById("graph");
36 var g = new Dygraph(graph, data, opts);
37 };
38
39 pathologicalCasesTestCase.prototype.testCombinations = function() {
40 var dataSets = {
41 empty: [],
42 onePoint: [[10, 2]],
43 nanPoint: [[10, NaN]],
44 nanPoints: [[10, NaN], [20, NaN]],
45 multiNan1: [[10, NaN, 2], [20, 3, NaN]],
46 multiNan2: [[10, NaN, 2], [20, NaN, 4]],
47 multiNan3: [[10, NaN, NaN], [20, 3, 4], [30, NaN, NaN]],
48 atZero: [[0, 0]],
49 atZero2: [[0, 0, 0]],
50 negative: [[-10, -1]],
51 acrossZero: [[-10, 1], [10, 2]],
52 normal: [[0,1,9], [10,3,5], [20,2,7], [30,4,3]]
53 };
54
55 var baseOpts = {
56 lines: {},
57 stacked: {
58 stackedGraph: true
59 }
60 };
61
62 var variantOpts = {
63 none: {},
64 avoidMinZero: {
65 avoidMinZero: true,
66 includeZero: true
67 },
68 padded: {
69 includeZero: true,
70 drawAxesAtZero: true,
71 xRangePad: 2,
72 yRangePad: 4
73 }
74 };
75
76 for (var baseName in baseOpts) {
77 var base = baseOpts[baseName];
78 for (var variantName in variantOpts) {
79 var variant = variantOpts[variantName];
80
81 var opts = {
82 width: 300,
83 height: 150,
84 labelsDivWidth: 100,
85 pointSize: 10
86 };
87 for (var key in base) {
88 if (base.hasOwnProperty(key)) opts[key] = base[key];
89 }
90 for (var key in variant) {
91 if (variant.hasOwnProperty(key)) opts[key] = variant[key];
92 }
93
94 var h = document.createElement('h3');
95 h.appendChild(document.createTextNode(baseName + ' ' + variantName));
96 document.body.appendChild(h);
97 for (var dataName in dataSets) {
98 var data = dataSets[dataName];
99
100 var box = document.createElement('fieldset');
101 box.style.display = 'inline-block';
102 var legend = document.createElement('legend');
103 legend.appendChild(document.createTextNode(dataName));
104 box.appendChild(legend);
105 var gdiv = document.createElement('div');
106 gdiv.style.display = 'inline-block';
107 box.appendChild(gdiv);
108 document.body.appendChild(box);
109
110 var cols = data && data[0] ? data[0].length : 0;
111 opts.labels = ['X', 'A', 'B', 'C'].slice(0, cols);
112
113 var g = new Dygraph(gdiv, data, opts);
114 }
115 }
116 }
117 };
118
119 pathologicalCasesTestCase.prototype.testNullLegend = function() {
120 var opts = {
121 width: 480,
122 height: 320,
123 labelsDiv: null
124 };
125 var data = "X,Y\n" +
126 "1,2\n";
127
128 var graph = document.getElementById("graph");
129 var g = new Dygraph(graph, data, opts);
130 };
131
132 pathologicalCasesTestCase.prototype.testDivAsString = function() {
133 var data = "X,Y\n" +
134 "1,2\n";
135
136 var g = new Dygraph('graph', data, {});
137 };
138
139
140 pathologicalCasesTestCase.prototype.testConstantSeriesNegative = function() {
141 var data = "X,Y\n" +
142 "1,-1\n" +
143 "2,-1\n";
144
145 g = new Dygraph('graph', data, {});
146 // This check could be loosened to
147 // g.yAxisRange()[0] < g.yAxisRange()[1] if it breaks in the future.
148 assertEquals([-1.1, -0.9], g.yAxisRange());
149 };
150
151
152 pathologicalCasesTestCase.prototype.testConstantSeriesNegativeIncludeZero = function() {
153 var data = "X,Y\n" +
154 "1,-1\n" +
155 "2,-1\n";
156
157 g = new Dygraph('graph', data, {includeZero: true});
158 // This check could be loosened to
159 // g.yAxisRange()[0] < g.yAxisRange()[1] if it breaks in the future.
160 assertEquals([-1.1, 0], g.yAxisRange());
161 };