| 1 | /** |
| 2 | * @fileoverview Test cases for how axis labels are chosen and formatted. |
| 3 | * |
| 4 | * @author dan@dygraphs.com (Dan Vanderkam) |
| 5 | */ |
| 6 | var AxisLabelsTestCase = TestCase("axis-labels"); |
| 7 | |
| 8 | AxisLabelsTestCase.prototype.setUp = function() { |
| 9 | document.body.innerHTML = "<div id='graph'></div>"; |
| 10 | }; |
| 11 | |
| 12 | AxisLabelsTestCase.prototype.tearDown = function() { |
| 13 | }; |
| 14 | |
| 15 | function getYLabels() { |
| 16 | var y_labels = document.getElementsByClassName("dygraph-axis-label-y"); |
| 17 | var ary = []; |
| 18 | for (var i = 0; i < y_labels.length; i++) { |
| 19 | ary.push(y_labels[i].innerHTML); |
| 20 | } |
| 21 | return ary; |
| 22 | } |
| 23 | |
| 24 | function getXLabels() { |
| 25 | var x_labels = document.getElementsByClassName("dygraph-axis-label-x"); |
| 26 | var ary = []; |
| 27 | for (var i = 0; i < x_labels.length; i++) { |
| 28 | ary.push(x_labels[i].innerHTML); |
| 29 | } |
| 30 | return ary; |
| 31 | } |
| 32 | |
| 33 | function makeNumbers(ary) { |
| 34 | var ret = []; |
| 35 | for (var i = 0; i < ary.length; i++) { |
| 36 | ret.push(parseFloat(ary[i])); |
| 37 | } |
| 38 | return ret; |
| 39 | } |
| 40 | |
| 41 | function getLegend() { |
| 42 | var legend = document.getElementsByClassName("dygraph-legend")[0]; |
| 43 | return legend.textContent; |
| 44 | } |
| 45 | |
| 46 | AxisLabelsTestCase.prototype.kCloseFloat = 1.0e-10; |
| 47 | |
| 48 | AxisLabelsTestCase.prototype.testMinusOneToOne = function() { |
| 49 | var opts = { |
| 50 | width: 480, |
| 51 | height: 320 |
| 52 | }; |
| 53 | var data = "X,Y\n" + |
| 54 | "0,-1\n" + |
| 55 | "1,0\n" + |
| 56 | "2,1\n" + |
| 57 | "3,0\n" |
| 58 | ; |
| 59 | |
| 60 | var graph = document.getElementById("graph"); |
| 61 | var g = new Dygraph(graph, data, opts); |
| 62 | |
| 63 | // TODO(danvk): would ['-1.0','-0.5','0.0','0.5','1.0'] be better? |
| 64 | assertEquals(['-1','-0.5','0','0.5','1'], getYLabels()); |
| 65 | |
| 66 | // Go up to 2 |
| 67 | data += "4,2\n"; |
| 68 | g.updateOptions({file: data}); |
| 69 | assertEquals(['-1','-0.5','0','0.5','1','1.5','2'], getYLabels()); |
| 70 | |
| 71 | // Now 10 |
| 72 | data += "5,10\n"; |
| 73 | g.updateOptions({file: data}); |
| 74 | assertEquals(['-2','0','2','4','6','8','10'], getYLabels()); |
| 75 | |
| 76 | // Now 100 |
| 77 | data += "6,100\n"; |
| 78 | g.updateOptions({file: data}); |
| 79 | assertEquals(['0','20','40','60','80','100'], getYLabels()); |
| 80 | |
| 81 | g.setSelection(0); |
| 82 | assertEquals('0: Y:-1', getLegend()); |
| 83 | }; |
| 84 | |
| 85 | AxisLabelsTestCase.prototype.testSmallRangeNearZero = function() { |
| 86 | var opts = { |
| 87 | width: 480, |
| 88 | height: 320 |
| 89 | }; |
| 90 | var data = "X,Y\n" + |
| 91 | "0,-1\n" + |
| 92 | "1,0\n" + |
| 93 | "2,1\n" + |
| 94 | "3,0\n" |
| 95 | ; |
| 96 | opts.valueRange = [-0.1, 0.1]; |
| 97 | |
| 98 | var graph = document.getElementById("graph"); |
| 99 | var g = new Dygraph(graph, data, opts); |
| 100 | assertEqualsDelta(makeNumbers(["-0.1","-0.08","-0.06","-0.04","-0.02","0","0.02","0.04","0.06","0.08"]), |
| 101 | makeNumbers(getYLabels()), this.kCloseFloat); |
| 102 | |
| 103 | opts.valueRange = [-0.05, 0.05]; |
| 104 | g.updateOptions(opts); |
| 105 | // TODO(danvk): why '1.00e-2' and not '0.01'? |
| 106 | assertEquals(makeNumbers(["-0.05","-0.04","-0.03","-0.02","-0.01","0","1.00e-2","0.02","0.03","0.04"]), |
| 107 | makeNumbers(getYLabels())); |
| 108 | |
| 109 | opts.valueRange = [-0.01, 0.01]; |
| 110 | g.updateOptions(opts); |
| 111 | assertEquals(makeNumbers(["-0.01","-8.00e-3","-6.00e-3","-4.00e-3","-2.00e-3","0","2.00e-3","4.00e-3","6.00e-3","8.00e-3"]), makeNumbers(getYLabels())); |
| 112 | |
| 113 | g.setSelection(1); |
| 114 | assertEquals('1: Y:0', getLegend()); |
| 115 | }; |
| 116 | |
| 117 | AxisLabelsTestCase.prototype.testSmallRangeAwayFromZero = function() { |
| 118 | var opts = { |
| 119 | width: 480, |
| 120 | height: 320 |
| 121 | }; |
| 122 | var data = "X,Y\n" + |
| 123 | "0,-1\n" + |
| 124 | "1,0\n" + |
| 125 | "2,1\n" + |
| 126 | "3,0\n" |
| 127 | ; |
| 128 | var graph = document.getElementById("graph"); |
| 129 | |
| 130 | opts.valueRange = [9.9, 10.1]; |
| 131 | var g = new Dygraph(graph, data, opts); |
| 132 | assertEquals(["9.9","9.92","9.94","9.96","9.98","10","10.02","10.04","10.06","10.08"], getYLabels()); |
| 133 | |
| 134 | opts.valueRange = [9.99, 10.01]; |
| 135 | g.updateOptions(opts); |
| 136 | // TODO(danvk): this is bad |
| 137 | assertEquals(["9.99","9.99","9.99","10","10","10","10","10","10.01","10.01"], getYLabels()); |
| 138 | |
| 139 | opts.valueRange = [9.999, 10.001]; |
| 140 | g.updateOptions(opts); |
| 141 | // TODO(danvk): this is even worse! |
| 142 | assertEquals(["10","10","10","10","10","10","10","10","10","10"], getYLabels()); |
| 143 | |
| 144 | g.setSelection(1); |
| 145 | assertEquals('1: Y:0', getLegend()); |
| 146 | }; |
| 147 | |
| 148 | AxisLabelsTestCase.prototype.testXAxisTimeLabelFormatter = function() { |
| 149 | var opts = { |
| 150 | width: 480, |
| 151 | height: 320 |
| 152 | }; |
| 153 | var data = [[5.0,0],[5.1,1],[5.2,2],[5.3,3],[5.4,4],[5.5,5],[5.6,6],[5.7,7],[5.8,8],[5.9,9]]; |
| 154 | var graph = document.getElementById("graph"); |
| 155 | var g = new Dygraph(graph, data, opts); |
| 156 | g.updateOptions({ |
| 157 | xAxisLabelFormatter: function (totalMinutes) { |
| 158 | var hours = Math.floor( totalMinutes / 60); |
| 159 | var minutes = Math.floor((totalMinutes - (hours * 60))); |
| 160 | var seconds = Math.round((totalMinutes * 60) - (hours * 3600) - (minutes * 60)); |
| 161 | |
| 162 | if (hours < 10) hours = "0" + hours; |
| 163 | if (minutes < 10) minutes = "0" + minutes; |
| 164 | if (seconds < 10) seconds = "0" + seconds; |
| 165 | |
| 166 | return hours + ':' + minutes + ':' + seconds; |
| 167 | } |
| 168 | }); |
| 169 | |
| 170 | assertEquals(["00:05:00","00:05:12","00:05:24","00:05:36","00:05:48"], getXLabels()); |
| 171 | |
| 172 | // The legend does not use the xAxisLabelFormatter: |
| 173 | g.setSelection(1); |
| 174 | assertEquals('5.1: Y1:1', getLegend()); |
| 175 | }; |
| 176 | |
| 177 | AxisLabelsTestCase.prototype.testAxisLabelFormatter = function () { |
| 178 | var opts = { |
| 179 | width: 480, |
| 180 | height: 320, |
| 181 | xAxisLabelFormatter: function(x, granularity, opts, dg) { |
| 182 | assertEquals('number', typeof(x)); |
| 183 | assertEquals('number', typeof(granularity)); |
| 184 | assertEquals('function', typeof(opts)); |
| 185 | assertEquals('[Dygraph graph]', dg.toString()); |
| 186 | return 'x' + x; |
| 187 | }, |
| 188 | yAxisLabelFormatter: function(y, granularity, opts, dg) { |
| 189 | assertEquals('number', typeof(y)); |
| 190 | assertEquals('number', typeof(granularity)); |
| 191 | assertEquals('function', typeof(opts)); |
| 192 | assertEquals('[Dygraph graph]', dg.toString()); |
| 193 | return 'y' + y; |
| 194 | }, |
| 195 | labels: ['x', 'y'] |
| 196 | }; |
| 197 | var data = []; |
| 198 | for (var i = 0; i < 10; i++) { |
| 199 | data.push([i, 2 * i]); |
| 200 | } |
| 201 | var graph = document.getElementById("graph"); |
| 202 | var g = new Dygraph(graph, data, opts); |
| 203 | |
| 204 | assertEquals(['x0','x2','x4','x6','x8'], getXLabels()); |
| 205 | assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], getYLabels()); |
| 206 | |
| 207 | g.setSelection(2); |
| 208 | assertEquals("2: y:4", getLegend()); |
| 209 | }; |
| 210 | |
| 211 | AxisLabelsTestCase.prototype.testDateAxisLabelFormatter = function () { |
| 212 | var opts = { |
| 213 | width: 480, |
| 214 | height: 320, |
| 215 | xAxisLabelFormatter: function(x, granularity, opts, dg) { |
| 216 | assertTrue(Dygraph.isDateLike(x)); |
| 217 | assertEquals('number', typeof(granularity)); |
| 218 | assertEquals('function', typeof(opts)); |
| 219 | assertEquals('[Dygraph graph]', dg.toString()); |
| 220 | return 'x' + x.strftime('%Y/%m/%d'); |
| 221 | }, |
| 222 | yAxisLabelFormatter: function(y, granularity, opts, dg) { |
| 223 | assertEquals('number', typeof(y)); |
| 224 | assertEquals('number', typeof(granularity)); |
| 225 | assertEquals('function', typeof(opts)); |
| 226 | assertEquals('[Dygraph graph]', dg.toString()); |
| 227 | return 'y' + y; |
| 228 | }, |
| 229 | labels: ['x', 'y'] |
| 230 | }; |
| 231 | var data = []; |
| 232 | for (var i = 1; i < 10; i++) { |
| 233 | data.push([new Date("2011/01/0" + i), 2 * i]); |
| 234 | } |
| 235 | var graph = document.getElementById("graph"); |
| 236 | var g = new Dygraph(graph, data, opts); |
| 237 | |
| 238 | assertEquals(["x2011/01/01", "x2011/01/02", "x2011/01/03", "x2011/01/04", "x2011/01/05", "x2011/01/06", "x2011/01/07", "x2011/01/08", "x2011/01/09"], getXLabels()); |
| 239 | assertEquals(['y2','y4','y6','y8','y10','y12','y14','y16','y18'], getYLabels()); |
| 240 | |
| 241 | g.setSelection(0); |
| 242 | assertEquals("2011/01/01: y:2", getLegend()); |
| 243 | }; |
| 244 | |
| 245 | // This test verifies that when a valueFormatter is set (but not an |
| 246 | // axisLabelFormatter), then the valueFormatter is used to format the axis |
| 247 | // labels. |
| 248 | AxisLabelsTestCase.prototype.testValueFormatter = function () { |
| 249 | var opts = { |
| 250 | width: 480, |
| 251 | height: 320, |
| 252 | xValueFormatter: function(x, opts, series_name, dg) { |
| 253 | assertEquals('number', typeof(x)); |
| 254 | assertEquals('function', typeof(opts)); |
| 255 | assertEquals('string', typeof(series_name)); |
| 256 | assertEquals('[Dygraph graph]', dg.toString()); |
| 257 | return 'x' + x; |
| 258 | }, |
| 259 | yValueFormatter: function(y, opts, series_name, dg) { |
| 260 | assertEquals('number', typeof(y)); |
| 261 | assertEquals('function', typeof(opts)); |
| 262 | assertEquals('string', typeof(series_name)); |
| 263 | assertEquals('[Dygraph graph]', dg.toString()); |
| 264 | return 'y' + y; |
| 265 | }, |
| 266 | labels: ['x', 'y'] |
| 267 | }; |
| 268 | var data = []; |
| 269 | for (var i = 0; i < 10; i++) { |
| 270 | data.push([i, 2 * i]); |
| 271 | } |
| 272 | var graph = document.getElementById("graph"); |
| 273 | var g = new Dygraph(graph, data, opts); |
| 274 | |
| 275 | // the valueFormatter options do not affect the ticks. |
| 276 | assertEquals(['0','2','4','6','8'], getXLabels()); |
| 277 | assertEquals(['0','2','4','6','8','10','12','14','16','18'], |
| 278 | getYLabels()); |
| 279 | |
| 280 | // they do affect the legend, however. |
| 281 | g.setSelection(2); |
| 282 | assertEquals("x2: y:y4", getLegend()); |
| 283 | }; |
| 284 | |
| 285 | AxisLabelsTestCase.prototype.testDateValueFormatter = function () { |
| 286 | var opts = { |
| 287 | width: 480, |
| 288 | height: 320, |
| 289 | xValueFormatter: function(x, opts, series_name, dg) { |
| 290 | assertEquals('number', typeof(x)); |
| 291 | assertEquals('function', typeof(opts)); |
| 292 | assertEquals('string', typeof(series_name)); |
| 293 | assertEquals('[Dygraph graph]', dg.toString()); |
| 294 | return 'x' + new Date(x).strftime('%Y/%m/%d'); |
| 295 | }, |
| 296 | yValueFormatter: function(y, opts, series_name, dg) { |
| 297 | assertEquals('number', typeof(y)); |
| 298 | assertEquals('function', typeof(opts)); |
| 299 | assertEquals('string', typeof(series_name)); |
| 300 | assertEquals('[Dygraph graph]', dg.toString()); |
| 301 | return 'y' + y; |
| 302 | }, |
| 303 | labels: ['x', 'y'] |
| 304 | }; |
| 305 | |
| 306 | var data = []; |
| 307 | for (var i = 1; i < 10; i++) { |
| 308 | data.push([new Date("2011/01/0" + i), 2 * i]); |
| 309 | } |
| 310 | var graph = document.getElementById("graph"); |
| 311 | var g = new Dygraph(graph, data, opts); |
| 312 | |
| 313 | // valueFormatters do not affect ticks. |
| 314 | assertEquals(['01Jan','02Jan','03Jan','04Jan','05Jan','06Jan','07Jan','08Jan','09Jan'], getXLabels()); |
| 315 | assertEquals(['2','4','6','8','10','12','14','16','18'], getYLabels()); |
| 316 | |
| 317 | // the valueFormatter options also affect the legend. |
| 318 | g.setSelection(2); |
| 319 | assertEquals('x2011/01/03: y:y6', getLegend()); |
| 320 | }; |
| 321 | |
| 322 | // This test verifies that when both a valueFormatter and an axisLabelFormatter |
| 323 | // are specified, the axisLabelFormatter takes precedence. |
| 324 | AxisLabelsTestCase.prototype.testAxisLabelFormatterPrecedence = function () { |
| 325 | var opts = { |
| 326 | width: 480, |
| 327 | height: 320, |
| 328 | xValueFormatter: function(x) { |
| 329 | return 'xvf' + x; |
| 330 | }, |
| 331 | yValueFormatter: function(y) { |
| 332 | return 'yvf' + y; |
| 333 | }, |
| 334 | xAxisLabelFormatter: function(x, granularity) { |
| 335 | return 'x' + x; |
| 336 | }, |
| 337 | yAxisLabelFormatter: function(y) { |
| 338 | return 'y' + y; |
| 339 | }, |
| 340 | labels: ['x', 'y'] |
| 341 | }; |
| 342 | var data = []; |
| 343 | for (var i = 0; i < 10; i++) { |
| 344 | data.push([i, 2 * i]); |
| 345 | } |
| 346 | var graph = document.getElementById("graph"); |
| 347 | var g = new Dygraph(graph, data, opts); |
| 348 | |
| 349 | assertEquals(['x0','x2','x4','x6','x8'], getXLabels()); |
| 350 | assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], getYLabels()); |
| 351 | |
| 352 | g.setSelection(9); |
| 353 | assertEquals("xvf9: y:yvf18", getLegend()); |
| 354 | }; |
| 355 | |
| 356 | // This is the same as the previous test, except that options are added |
| 357 | // one-by-one. |
| 358 | AxisLabelsTestCase.prototype.testAxisLabelFormatterIncremental = function () { |
| 359 | var opts = { |
| 360 | width: 480, |
| 361 | height: 320, |
| 362 | labels: ['x', 'y'] |
| 363 | }; |
| 364 | var data = []; |
| 365 | for (var i = 0; i < 10; i++) { |
| 366 | data.push([i, 2 * i]); |
| 367 | } |
| 368 | var graph = document.getElementById("graph"); |
| 369 | var g = new Dygraph(graph, data, opts); |
| 370 | g.updateOptions({ |
| 371 | xValueFormatter: function(x) { |
| 372 | return 'xvf' + x; |
| 373 | } |
| 374 | }); |
| 375 | g.updateOptions({ |
| 376 | yValueFormatter: function(y) { |
| 377 | return 'yvf' + y; |
| 378 | } |
| 379 | }); |
| 380 | g.updateOptions({ |
| 381 | xAxisLabelFormatter: function(x, granularity) { |
| 382 | return 'x' + x; |
| 383 | } |
| 384 | }); |
| 385 | g.updateOptions({ |
| 386 | yAxisLabelFormatter: function(y) { |
| 387 | return 'y' + y; |
| 388 | } |
| 389 | }); |
| 390 | |
| 391 | assertEquals(["x0","x2","x4","x6","x8"], getXLabels()); |
| 392 | assertEquals(['y0','y2','y4','y6','y8','y10','y12','y14','y16','y18'], getYLabels()); |
| 393 | |
| 394 | g.setSelection(9); |
| 395 | assertEquals("xvf9: y:yvf18", getLegend()); |
| 396 | }; |
| 397 | |
| 398 | AxisLabelsTestCase.prototype.testGlobalFormatters = function() { |
| 399 | var opts = { |
| 400 | width: 480, |
| 401 | height: 320, |
| 402 | labels: ['x', 'y'], |
| 403 | valueFormatter: function(x) { |
| 404 | return 'vf' + x; |
| 405 | }, |
| 406 | axisLabelFormatter: function(x) { |
| 407 | return 'alf' + x; |
| 408 | } |
| 409 | }; |
| 410 | var data = []; |
| 411 | for (var i = 0; i < 10; i++) { |
| 412 | data.push([i, 2 * i]); |
| 413 | } |
| 414 | var graph = document.getElementById("graph"); |
| 415 | var g = new Dygraph(graph, data, opts); |
| 416 | |
| 417 | assertEquals(['alf0','alf2','alf4','alf6','alf8'], getXLabels()); |
| 418 | assertEquals(['alf0','alf2','alf4','alf6','alf8','alf10','alf12','alf14','alf16','alf18'], getYLabels()); |
| 419 | |
| 420 | g.setSelection(9); |
| 421 | assertEquals("vf9: y:vf18", getLegend()); |
| 422 | }; |
| 423 | |
| 424 | AxisLabelsTestCase.prototype.testSeriesOrder = function() { |
| 425 | var opts = { |
| 426 | width: 480, |
| 427 | height: 320 |
| 428 | }; |
| 429 | var data = "x,00,01,10,11\n" + |
| 430 | "0,101,201,301,401\n" + |
| 431 | "1,102,202,302,402\n" + |
| 432 | "2,103,203,303,403\n" + |
| 433 | "3,104,204,304,404\n" |
| 434 | ; |
| 435 | |
| 436 | var graph = document.getElementById("graph"); |
| 437 | var g = new Dygraph(graph, data, opts); |
| 438 | |
| 439 | g.setSelection(2); |
| 440 | assertEquals('2: 00:103 01:203 10:303 11:403', getLegend()); |
| 441 | |
| 442 | // Sanity checks for indexFromSetName |
| 443 | assertEquals(0, g.indexFromSetName("x")); |
| 444 | assertEquals(1, g.indexFromSetName("00")); |
| 445 | assertEquals(null, g.indexFromSetName("abcde")); |
| 446 | |
| 447 | // Verify that we get the label list back in the right order |
| 448 | assertEquals(["x", "00", "01", "10", "11"], g.getLabels()); |
| 449 | }; |