From c34d8c681cdc6a10d6a2cb8b6559b86f679aeaf4 Mon Sep 17 00:00:00 2001 From: David Miller Date: Thu, 1 Aug 2013 13:06:28 +0100 Subject: [PATCH] Check that all elements of a series are convertable to floats before casting individual elements. --- src/view.flot.js | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/view.flot.js b/src/view.flot.js index f20a3ab3..4e111fe5 100644 --- a/src/view.flot.js +++ b/src/view.flot.js @@ -312,21 +312,28 @@ my.Flot = Backbone.View.extend({ _.each(this.state.attributes.series, function(field) { var points = []; var fieldLabel = self.model.fields.get(field).get('label'); - _.each(self.model.records.models, function(doc, index) { - var x = doc.getFieldValueUnrendered(xfield); - if (isDateTime) { - // cast to string as Date(1990) produces 1970 date but Date('1990') produces 1/1/1990 - var _date = moment(String(x)); - if (_date.isValid()) { - x = _date.toDate().getTime(); - } - } else if (typeof x === 'string') { - x = parseFloat(x); - if (isNaN(x)) { // assume this is a string label - x = index; - self.xvaluesAreIndex = true; - } + var raw = _.map(self.model.records.models, function(doc, index){ return doc.getFieldValueUnrendered(xfield) }); + + if (isDateTime){ + var cast = function(x){ + var _date = moment(String(x)); + if (_date.isValid()) { + x = _date.toDate().getTime(); + } + return x + } + } else if (_.all(raw, function(x){ return !isNaN(parseFloat(x)) })){ + var cast = function(x){ return parseFloat(x) } + } else { + self.xvaluesAreIndex = true + } + + _.each(self.model.records.models, function(doc, index) { + if(self.xvaluesAreIndex){ + var x = index; + }else{ + var x = cast(doc.getFieldValueUnrendered(xfield)); } var yfield = self.model.fields.get(field);