From df38727182154735230f4b0b852adc650503a0b8 Mon Sep 17 00:00:00 2001 From: Kefan Xie Date: Sun, 4 Mar 2012 17:31:26 -0500 Subject: [PATCH] Added parseTime option to allow non-date parsing line charts --- example.html | 27 +++++++++++++++++++++++++++ morris.js | 20 ++++++++++++++++---- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/example.html b/example.html index 916bb16..d1c91f1 100644 --- a/example.html +++ b/example.html @@ -114,4 +114,31 @@ }); }); + +

Formatting Non-date Arbitrary X-axis

+
+ diff --git a/morris.js b/morris.js index a5c6f32..e123102 100644 --- a/morris.js +++ b/morris.js @@ -44,7 +44,8 @@ hoverOpacity: 0.95, hoverLabelColor: '#444', hoverFontSize: 12, - smooth: true + smooth: true, + parseTime: true }; Line.prototype.precalc = function() { @@ -65,9 +66,16 @@ return d[ykey]; })); } - this.xvals = $.map(this.columnLabels, function(x) { - return _this.parseYear(x); - }); + if (_this.options.parseTime) { + this.xvals = $.map(this.columnLabels, function(x) { + return _this.parseYear(x); + }); + } else { + this.xvals = []; + for (_i = this.columnLabels.length - 1; _i >= 0; _i--) { + this.xvals.push(_i); + } + } this.xmin = Math.min.apply(null, this.xvals); this.xmax = Math.max.apply(null, this.xvals); if (this.xmin === this.xmax) { @@ -115,6 +123,10 @@ xLabelMargin = 50; for (i = _ref2 = Math.ceil(this.xmin), _ref3 = Math.floor(this.xmax); _ref2 <= _ref3 ? i <= _ref3 : i >= _ref3; _ref2 <= _ref3 ? i++ : i--) { label = this.r.text(transX(i), this.options.marginTop + height + this.options.marginBottom / 2, i).attr('font-size', this.options.gridTextSize).attr('fill', this.options.gridTextColor); + if (_this.options.parseTime == false) { + var l = this.columnLabels.length - 1; + label = label.attr('text', this.columnLabels[l - i]); + } labelBox = label.getBBox(); if (prevLabelMargin === null || prevLabelMargin <= labelBox.x) { prevLabelMargin = labelBox.x + labelBox.width + xLabelMargin;