From 7219edb31f2695199aaa6fc4ce6d74c7d62be9f0 Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Wed, 2 Mar 2011 23:32:36 -0600 Subject: [PATCH] more warnings on CSV parsing --- dygraph.js | 19 ++++++++++++++++--- tests/independent-series.html | 1 + 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/dygraph.js b/dygraph.js index 3756702..e343548 100644 --- a/dygraph.js +++ b/dygraph.js @@ -2970,14 +2970,27 @@ Dygraph.prototype.parseCSV_ = function(data) { for (var j = 1; j < inFields.length; j++) { // TODO(danvk): figure out an appropriate way to flag parse errors. var vals = inFields[j].split("/"); - fields[j] = [this.parseFloat_(vals[0], i, line), - this.parseFloat_(vals[1], i, line)]; + if (vals.length != 2) { + this.error('Expected fractional "num/den" values in CSV data ' + + "but found a value '" + inFields[j] + "' on line " + + (1 + i) + " ('" + line + "') which is not of this form."); + fields[j] = [0, 0]; + } else { + fields[j] = [this.parseFloat_(vals[0], i, line), + this.parseFloat_(vals[1], i, line)]; + } } } else if (this.attr_("errorBars")) { // If there are error bars, values are (value, stddev) pairs - for (var j = 1; j < inFields.length; j += 2) + if (inFields.length % 2 != 1) { + this.error('Expected alternating (value, stdev.) pairs in CSV data ' + + 'but line ' + (1 + i) + ' has an odd number of values (' + + (inFields.length - 1) + "): '" + line + "'"); + } + for (var j = 1; j < inFields.length; j += 2) { fields[(j + 1) / 2] = [this.parseFloat_(inFields[j], i, line), this.parseFloat_(inFields[j + 1], i, line)]; + } } else if (this.attr_("customBars")) { // Bars are a low;center;high tuple for (var j = 1; j < inFields.length; j++) { diff --git a/tests/independent-series.html b/tests/independent-series.html index 37cf0e0..543f51f 100644 --- a/tests/independent-series.html +++ b/tests/independent-series.html @@ -27,6 +27,7 @@ +

Independent Series

By using the connectSeparated attribute, it's possible to display a chart of several time series with completely independent x-values.

The trick is to specify values for the series at the union of the x-values of all series. For one series' x values, specify null for each of the other series.

-- 2.7.4