From c34d8c681cdc6a10d6a2cb8b6559b86f679aeaf4 Mon Sep 17 00:00:00 2001 From: David Miller Date: Thu, 1 Aug 2013 13:06:28 +0100 Subject: [PATCH 1/2] 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); From 49d89cc0ea4979249967843ae9171990c2d6d809 Mon Sep 17 00:00:00 2001 From: David Miller Date: Tue, 6 Aug 2013 18:26:12 +0100 Subject: [PATCH 2/2] Only create variables if we need them. --- src/view.flot.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/view.flot.js b/src/view.flot.js index 4e111fe5..1b50f063 100644 --- a/src/view.flot.js +++ b/src/view.flot.js @@ -313,8 +313,6 @@ my.Flot = Backbone.View.extend({ var points = []; var fieldLabel = self.model.fields.get(field).get('label'); - 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)); @@ -323,10 +321,17 @@ my.Flot = Backbone.View.extend({ } return x } - } else if (_.all(raw, function(x){ return !isNaN(parseFloat(x)) })){ - var cast = function(x){ return parseFloat(x) } } else { - self.xvaluesAreIndex = true + var raw = _.map(self.model.records.models, + function(doc, index){ + return doc.getFieldValueUnrendered(xfield) + }); + + 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) {