add tests for axis labels to pin down the current behavior
authorDan Vanderkam <dan@dygraphs.com>
Thu, 12 May 2011 03:57:24 +0000 (23:57 -0400)
committerDan Vanderkam <dan@dygraphs.com>
Thu, 12 May 2011 03:57:24 +0000 (23:57 -0400)
auto_tests/misc/local.html
auto_tests/tests/axis_labels.js [new file with mode: 0644]

index 111d07a..046a11e 100644 (file)
@@ -22,6 +22,8 @@
   <script type="text/javascript" src="../tests/sanity.js"></script>
   <script type="text/javascript" src="../tests/simple_drawing.js"></script>
   <script type="text/javascript" src="../tests/range_tests.js"></script>
+  <script type="text/javascript" src="../tests/axis_labels.js"></script>
+  <script type="text/javascript" src="../tests/multi_csv.js"></script>
 </head>
 <body>
   <div id='graph'></div>
diff --git a/auto_tests/tests/axis_labels.js b/auto_tests/tests/axis_labels.js
new file mode 100644 (file)
index 0000000..f1c06d6
--- /dev/null
@@ -0,0 +1,111 @@
+/** 
+ * @fileoverview Test cases for how axis labels are chosen and formatted.
+ *
+ * @author dan@dygraphs.com (Dan Vanderkam)
+ */
+var AxisLabelsTestCase = TestCase("axis-labels");
+
+AxisLabelsTestCase.prototype.setUp = function() {
+  document.body.innerHTML = "<div id='graph'></div>";
+};
+
+AxisLabelsTestCase.prototype.tearDown = function() {
+};
+
+function getYLabels() {
+  var y_labels = document.getElementsByClassName("dygraph-axis-label-y");
+  var ary = [];
+  for (var i = 0; i < y_labels.length; i++) {
+    ary.push(y_labels[i].innerHTML);
+  }
+  return ary;
+}
+
+AxisLabelsTestCase.prototype.testMinusOneToOne = function() {
+  var opts = {
+    width: 480,
+    height: 320
+  };
+  var data = "X,Y\n" +
+      "0,-1\n" +
+      "1,0\n" +
+      "2,1\n" +
+      "3,0\n"
+  ;
+
+  var graph = document.getElementById("graph");
+  var g = new Dygraph(graph, data, opts);
+
+  // TODO(danvk): would ['-1.0','-0.5','0.0','0.5','1.0'] be better?
+  assertEquals(['-1','-0.5','0','0.5','1'], getYLabels());
+
+  // Go up to 2
+  data += "4,2\n";
+  g.updateOptions({file: data});
+  assertEquals(['-1','-0.5','0','0.5','1','1.5','2'], getYLabels());
+
+  // Now 10
+  data += "5,10\n";
+  g.updateOptions({file: data});
+  assertEquals(['-2','0','2','4','6','8','10'], getYLabels());
+
+  // Now 100
+  data += "6,100\n";
+  g.updateOptions({file: data});
+  assertEquals(['0','20','40','60','80','100'], getYLabels());
+};
+
+AxisLabelsTestCase.prototype.testSmallRangeNearZero = function() {
+  var opts = {
+    width: 480,
+    height: 320
+  };
+  var data = "X,Y\n" +
+      "0,-1\n" +
+      "1,0\n" +
+      "2,1\n" +
+      "3,0\n"
+  ;
+  opts.valueRange = [-0.1, 0.1];
+
+  var graph = document.getElementById("graph");
+  var g = new Dygraph(graph, data, opts);
+  assertEquals(["-0.1","-0.08","-0.06","-0.04","-0.02","0","0.02","0.04","0.06","0.08"], getYLabels());
+
+  opts.valueRange = [-0.05, 0.05];
+  g.updateOptions(opts);
+  // TODO(danvk): why '1.00e-2' and not '0.01'?
+  assertEquals(["-0.05","-0.04","-0.03","-0.02","-0.01","0","1.00e-2","0.02","0.03","0.04"], getYLabels());
+
+  opts.valueRange = [-0.01, 0.01];
+  g.updateOptions(opts);
+  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());
+};
+
+AxisLabelsTestCase.prototype.testSmallRangeAwayFromZero = function() {
+  var opts = {
+    width: 480,
+    height: 320
+  };
+  var data = "X,Y\n" +
+      "0,-1\n" +
+      "1,0\n" +
+      "2,1\n" +
+      "3,0\n"
+  ;
+  var graph = document.getElementById("graph");
+
+  opts.valueRange = [9.9, 10.1];
+  var g = new Dygraph(graph, data, opts);
+  assertEquals(["9.9","9.92","9.94","9.96","9.98","10","10.02","10.04","10.06","10.08"], getYLabels());
+
+  opts.valueRange = [9.99, 10.01];
+  g.updateOptions(opts);
+  // TODO(danvk): this is bad
+  assertEquals(["9.99","9.99","9.99","10","10","10","10","10","10.01","10.01"], getYLabels());
+
+  opts.valueRange = [9.999, 10.001];
+  g.updateOptions(opts);
+  // TODO(danvk): this is even worse!
+  assertEquals(["10","10","10","10","10","10","10","10","10","10"], getYLabels());
+};