| 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 | AxisLabelsTestCase.prototype.testMinusOneToOne = function() { |
| 25 | var opts = { |
| 26 | width: 480, |
| 27 | height: 320 |
| 28 | }; |
| 29 | var data = "X,Y\n" + |
| 30 | "0,-1\n" + |
| 31 | "1,0\n" + |
| 32 | "2,1\n" + |
| 33 | "3,0\n" |
| 34 | ; |
| 35 | |
| 36 | var graph = document.getElementById("graph"); |
| 37 | var g = new Dygraph(graph, data, opts); |
| 38 | |
| 39 | // TODO(danvk): would ['-1.0','-0.5','0.0','0.5','1.0'] be better? |
| 40 | assertEquals(['-1','-0.5','0','0.5','1'], getYLabels()); |
| 41 | |
| 42 | // Go up to 2 |
| 43 | data += "4,2\n"; |
| 44 | g.updateOptions({file: data}); |
| 45 | assertEquals(['-1','-0.5','0','0.5','1','1.5','2'], getYLabels()); |
| 46 | |
| 47 | // Now 10 |
| 48 | data += "5,10\n"; |
| 49 | g.updateOptions({file: data}); |
| 50 | assertEquals(['-2','0','2','4','6','8','10'], getYLabels()); |
| 51 | |
| 52 | // Now 100 |
| 53 | data += "6,100\n"; |
| 54 | g.updateOptions({file: data}); |
| 55 | assertEquals(['0','20','40','60','80','100'], getYLabels()); |
| 56 | }; |
| 57 | |
| 58 | AxisLabelsTestCase.prototype.testSmallRangeNearZero = function() { |
| 59 | var opts = { |
| 60 | width: 480, |
| 61 | height: 320 |
| 62 | }; |
| 63 | var data = "X,Y\n" + |
| 64 | "0,-1\n" + |
| 65 | "1,0\n" + |
| 66 | "2,1\n" + |
| 67 | "3,0\n" |
| 68 | ; |
| 69 | opts.valueRange = [-0.1, 0.1]; |
| 70 | |
| 71 | var graph = document.getElementById("graph"); |
| 72 | var g = new Dygraph(graph, data, opts); |
| 73 | assertEquals(["-0.1","-0.08","-0.06","-0.04","-0.02","0","0.02","0.04","0.06","0.08"], getYLabels()); |
| 74 | |
| 75 | opts.valueRange = [-0.05, 0.05]; |
| 76 | g.updateOptions(opts); |
| 77 | // TODO(danvk): why '1.00e-2' and not '0.01'? |
| 78 | assertEquals(["-0.05","-0.04","-0.03","-0.02","-0.01","0","1.00e-2","0.02","0.03","0.04"], getYLabels()); |
| 79 | |
| 80 | opts.valueRange = [-0.01, 0.01]; |
| 81 | g.updateOptions(opts); |
| 82 | assertEquals(["-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"], getYLabels()); |
| 83 | }; |
| 84 | |
| 85 | AxisLabelsTestCase.prototype.testSmallRangeAwayFromZero = 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 | var graph = document.getElementById("graph"); |
| 97 | |
| 98 | opts.valueRange = [9.9, 10.1]; |
| 99 | var g = new Dygraph(graph, data, opts); |
| 100 | assertEquals(["9.9","9.92","9.94","9.96","9.98","10","10.02","10.04","10.06","10.08"], getYLabels()); |
| 101 | |
| 102 | opts.valueRange = [9.99, 10.01]; |
| 103 | g.updateOptions(opts); |
| 104 | // TODO(danvk): this is bad |
| 105 | assertEquals(["9.99","9.99","9.99","10","10","10","10","10","10.01","10.01"], getYLabels()); |
| 106 | |
| 107 | opts.valueRange = [9.999, 10.001]; |
| 108 | g.updateOptions(opts); |
| 109 | // TODO(danvk): this is even worse! |
| 110 | assertEquals(["10","10","10","10","10","10","10","10","10","10"], getYLabels()); |
| 111 | }; |