From d7beab6bdb34e8d1ab012e093ccdf005f474e52e Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Wed, 11 May 2011 23:46:31 -0400 Subject: [PATCH] fix labels issue when loading CSV multiple times and add a test --- auto_tests/tests/multi_csv.js | 62 +++++++++++++++++++++++++++++++++++++++++++ dygraph.js | 10 +++---- 2 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 auto_tests/tests/multi_csv.js diff --git a/auto_tests/tests/multi_csv.js b/auto_tests/tests/multi_csv.js new file mode 100644 index 0000000..92dcc55 --- /dev/null +++ b/auto_tests/tests/multi_csv.js @@ -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 = "
"; +}; + +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()); +}; diff --git a/dygraph.js b/dygraph.js index 5cbf72e..2e8a38e 100644 --- a/dygraph.js +++ b/dygraph.js @@ -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_(); -- 2.7.4