fix labels issue when loading CSV multiple times and add a test
authorDan Vanderkam <dan@dygraphs.com>
Thu, 12 May 2011 03:46:31 +0000 (23:46 -0400)
committerDan Vanderkam <dan@dygraphs.com>
Thu, 12 May 2011 03:46:31 +0000 (23:46 -0400)
auto_tests/tests/multi_csv.js [new file with mode: 0644]
dygraph.js

diff --git a/auto_tests/tests/multi_csv.js b/auto_tests/tests/multi_csv.js
new file mode 100644 (file)
index 0000000..92dcc55
--- /dev/null
@@ -0,0 +1,62 @@
+/** 
+ * @fileoverview Test cases for how axis labels are chosen and formatted.
+ *
+ * @author dan@dygraphs.com (Dan Vanderkam)
+ */
+var MultiCsvTestCase = TestCase("axis-labels");
+
+MultiCsvTestCase.prototype.setUp = function() {
+  document.body.innerHTML = "<div id='graph'></div>";
+};
+
+MultiCsvTestCase.prototype.tearDown = function() {
+};
+
+function getXLabels() {
+  var x_labels = document.getElementsByClassName("dygraph-axis-label-x");
+  var ary = [];
+  for (var i = 0; i < x_labels.length; i++) {
+    ary.push(x_labels[i].innerHTML);
+  }
+  return ary;
+}
+
+MultiCsvTestCase.prototype.testOneCSV = 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);
+
+  assertEquals(['0','0.5','1','1.5','2','2.5'], getXLabels());
+};
+
+MultiCsvTestCase.prototype.testTwoCSV = 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);
+
+  assertEquals(['0','0.5','1','1.5','2','2.5'], getXLabels());
+
+  g.updateOptions({file: data});
+
+  assertEquals(['0','0.5','1','1.5','2','2.5'], getXLabels());
+};
index 5cbf72e..2e8a38e 100644 (file)
@@ -304,9 +304,6 @@ Dygraph.prototype.__init__ = function(div, file, attrs) {
 
   this.boundaryIds_ = [];
 
-  // Make a note of whether labels will be pulled from the CSV file.
-  this.labelsFromCSV_ = (this.attr_("labels") == null);
-
   // Create the containing DIV and other interactive elements
   this.createInterface_();
 
@@ -3320,9 +3317,10 @@ Dygraph.prototype.parseCSV_ = function(data) {
   }
 
   var start = 0;
-  if (this.labelsFromCSV_) {
+  if (!('labels' in this.user_attrs_)) {
+    // User hasn't explicitly set labels, so they're (presumably) in the CSV.
     start = 1;
-    this.attrs_.labels = lines[0].split(delim);
+    this.attrs_.labels = lines[0].split(delim);  // NOTE: _not_ user_attrs_.
   }
   var line_no = 0;
 
@@ -3774,8 +3772,6 @@ Dygraph.prototype.updateOptions = function(attrs, block_redraw) {
 
   Dygraph.update(this.user_attrs_, attrs);
 
-  this.labelsFromCSV_ = (this.attr_("labels") == null);
-
   if (attrs['file']) {
     this.file_ = attrs['file'];
     if (!block_redraw) this.start_();