This commit is contained in:
Olly Smith 2012-07-03 07:34:42 +01:00
parent 5f0e65c279
commit eacbc01149
4 changed files with 31 additions and 29 deletions

View File

@ -51,6 +51,7 @@ class Morris.Line
@el.bind 'touchmove', touchHandler @el.bind 'touchmove', touchHandler
@el.bind 'touchend', touchHandler @el.bind 'touchend', touchHandler
@seriesLabels = @options.labels
@setData(@options.data) @setData(@options.data)
# Default configuration # Default configuration
@ -104,7 +105,6 @@ class Morris.Line
@options.data.sort (a, b) => (a[@options.xkey] < b[@options.xkey]) - (b[@options.xkey] < a[@options.xkey]) @options.data.sort (a, b) => (a[@options.xkey] < b[@options.xkey]) - (b[@options.xkey] < a[@options.xkey])
# extract labels # extract labels
@columnLabels = $.map @options.data, (d) => d[@options.xkey] @columnLabels = $.map @options.data, (d) => d[@options.xkey]
@seriesLabels = @options.labels
# extract series data # extract series data
@series = [] @series = []
@ -141,17 +141,20 @@ class Morris.Line
# use Array.concat to flatten arrays and find the max y value # use Array.concat to flatten arrays and find the max y value
ymax = Math.max.apply null, Array.prototype.concat.apply([], @series) ymax = Math.max.apply null, Array.prototype.concat.apply([], @series)
if @options.ymax.length > 5 if @options.ymax.length > 5
@options.ymax = Math.max parseInt(@options.ymax[5..], 10), ymax @ymax = Math.max parseInt(@options.ymax[5..], 10), ymax
else else
@options.ymax = ymax @ymax = ymax
if typeof @options.ymin is 'string' and @options.ymin[0..3] is 'auto' if typeof @options.ymin is 'string' and @options.ymin[0..3] is 'auto'
ymin = Math.min.apply null, Array.prototype.concat.apply([], @series) ymin = Math.min.apply null, Array.prototype.concat.apply([], @series)
if @options.ymin.length > 5 if @options.ymin.length > 5
@options.ymin = Math.min parseInt(@options.ymin[5..], 10), ymin @ymin = Math.min parseInt(@options.ymin[5..], 10), ymin
else else
@options.ymin = ymin @ymin = ymin
if @ymin is @ymax
@ymin -= 1
@ymax += 1
@yInterval = (@options.ymax - @options.ymin) / (@options.numLines - 1) @yInterval = (@ymax - @ymin) / (@options.numLines - 1)
if @yInterval > 0 and @yInterval < 1 if @yInterval > 0 and @yInterval < 1
@precision = -Math.floor(Math.log(@yInterval) / Math.log(10)) @precision = -Math.floor(Math.log(@yInterval) / Math.log(10))
else else
@ -172,13 +175,13 @@ class Morris.Line
@dirty = false @dirty = false
# calculate grid dimensions # calculate grid dimensions
@maxYLabelWidth = Math.max( @maxYLabelWidth = Math.max(
@measureText(@yLabelFormat(@options.ymin), @options.gridTextSize).width, @measureText(@yLabelFormat(@ymin), @options.gridTextSize).width,
@measureText(@yLabelFormat(@options.ymax), @options.gridTextSize).width) @measureText(@yLabelFormat(@ymax), @options.gridTextSize).width)
@left = @maxYLabelWidth + @options.marginLeft @left = @maxYLabelWidth + @options.marginLeft
@width = @el.width() - @left - @options.marginRight @width = @el.width() - @left - @options.marginRight
@height = @el.height() - @options.marginTop - @options.marginBottom @height = @el.height() - @options.marginTop - @options.marginBottom
@dx = @width / (@xmax - @xmin) @dx = @width / (@xmax - @xmin)
@dy = @height / (@options.ymax - @options.ymin) @dy = @height / (@ymax - @ymin)
# calculate series data point coordinates # calculate series data point coordinates
@columns = (@transX(x) for x in @xvals) @columns = (@transX(x) for x in @xvals)
@seriesCoords = [] @seriesCoords = []
@ -202,7 +205,7 @@ class Morris.Line
@left + (x - @xmin) * @dx @left + (x - @xmin) * @dx
transY: (y) => transY: (y) =>
return @options.marginTop + @height - (y - @options.ymin) * @dy return @options.marginTop + @height - (y - @ymin) * @dy
# Clear and redraw the graph # Clear and redraw the graph
# #
@ -218,8 +221,8 @@ class Morris.Line
# #
drawGrid: -> drawGrid: ->
# draw y axis labels, horizontal lines # draw y axis labels, horizontal lines
firstY = @options.ymin firstY = @ymin
lastY = @options.ymax lastY = @ymax
for lineY in [firstY..lastY] by @yInterval for lineY in [firstY..lastY] by @yInterval

View File

@ -68,6 +68,7 @@
this.el.bind('touchstart', touchHandler); this.el.bind('touchstart', touchHandler);
this.el.bind('touchmove', touchHandler); this.el.bind('touchmove', touchHandler);
this.el.bind('touchend', touchHandler); this.el.bind('touchend', touchHandler);
this.seriesLabels = this.options.labels;
this.setData(this.options.data); this.setData(this.options.data);
} }
@ -120,7 +121,6 @@
this.columnLabels = $.map(this.options.data, function(d) { this.columnLabels = $.map(this.options.data, function(d) {
return d[_this.options.xkey]; return d[_this.options.xkey];
}); });
this.seriesLabels = this.options.labels;
this.series = []; this.series = [];
_ref = this.options.ykeys; _ref = this.options.ykeys;
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
@ -171,20 +171,24 @@
if (typeof this.options.ymax === 'string' && this.options.ymax.slice(0, 4) === 'auto') { if (typeof this.options.ymax === 'string' && this.options.ymax.slice(0, 4) === 'auto') {
ymax = Math.max.apply(null, Array.prototype.concat.apply([], this.series)); ymax = Math.max.apply(null, Array.prototype.concat.apply([], this.series));
if (this.options.ymax.length > 5) { if (this.options.ymax.length > 5) {
this.options.ymax = Math.max(parseInt(this.options.ymax.slice(5), 10), ymax); this.ymax = Math.max(parseInt(this.options.ymax.slice(5), 10), ymax);
} else { } else {
this.options.ymax = ymax; this.ymax = ymax;
} }
} }
if (typeof this.options.ymin === 'string' && this.options.ymin.slice(0, 4) === 'auto') { if (typeof this.options.ymin === 'string' && this.options.ymin.slice(0, 4) === 'auto') {
ymin = Math.min.apply(null, Array.prototype.concat.apply([], this.series)); ymin = Math.min.apply(null, Array.prototype.concat.apply([], this.series));
if (this.options.ymin.length > 5) { if (this.options.ymin.length > 5) {
this.options.ymin = Math.min(parseInt(this.options.ymin.slice(5), 10), ymin); this.ymin = Math.min(parseInt(this.options.ymin.slice(5), 10), ymin);
} else { } else {
this.options.ymin = ymin; this.ymin = ymin;
} }
} }
this.yInterval = (this.options.ymax - this.options.ymin) / (this.options.numLines - 1); if (this.ymin === this.ymax) {
this.ymin -= 1;
this.ymax += 1;
}
this.yInterval = (this.ymax - this.ymin) / (this.options.numLines - 1);
if (this.yInterval > 0 && this.yInterval < 1) { if (this.yInterval > 0 && this.yInterval < 1) {
this.precision = -Math.floor(Math.log(this.yInterval) / Math.log(10)); this.precision = -Math.floor(Math.log(this.yInterval) / Math.log(10));
} else { } else {
@ -205,12 +209,12 @@
this.elementWidth = w; this.elementWidth = w;
this.elementHeight = h; this.elementHeight = h;
this.dirty = false; this.dirty = false;
this.maxYLabelWidth = Math.max(this.measureText(this.yLabelFormat(this.options.ymin), this.options.gridTextSize).width, this.measureText(this.yLabelFormat(this.options.ymax), this.options.gridTextSize).width); this.maxYLabelWidth = Math.max(this.measureText(this.yLabelFormat(this.ymin), this.options.gridTextSize).width, this.measureText(this.yLabelFormat(this.ymax), this.options.gridTextSize).width);
this.left = this.maxYLabelWidth + this.options.marginLeft; this.left = this.maxYLabelWidth + this.options.marginLeft;
this.width = this.el.width() - this.left - this.options.marginRight; this.width = this.el.width() - this.left - this.options.marginRight;
this.height = this.el.height() - this.options.marginTop - this.options.marginBottom; this.height = this.el.height() - this.options.marginTop - this.options.marginBottom;
this.dx = this.width / (this.xmax - this.xmin); this.dx = this.width / (this.xmax - this.xmin);
this.dy = this.height / (this.options.ymax - this.options.ymin); this.dy = this.height / (this.ymax - this.ymin);
this.columns = (function() { this.columns = (function() {
var _i, _len, _ref, _results; var _i, _len, _ref, _results;
_ref = this.xvals; _ref = this.xvals;
@ -253,7 +257,7 @@
}; };
Line.prototype.transY = function(y) { Line.prototype.transY = function(y) {
return this.options.marginTop + this.height - (y - this.options.ymin) * this.dy; return this.options.marginTop + this.height - (y - this.ymin) * this.dy;
}; };
Line.prototype.redraw = function() { Line.prototype.redraw = function() {
@ -268,8 +272,8 @@
Line.prototype.drawGrid = function() { Line.prototype.drawGrid = function() {
var drawLabel, firstY, i, l, labelText, lastY, lineY, prevLabelMargin, v, xLabelMargin, y, ypos, _i, _j, _k, _len, _ref, _ref1, _ref2, _results, _results1, var drawLabel, firstY, i, l, labelText, lastY, lineY, prevLabelMargin, v, xLabelMargin, y, ypos, _i, _j, _k, _len, _ref, _ref1, _ref2, _results, _results1,
_this = this; _this = this;
firstY = this.options.ymin; firstY = this.ymin;
lastY = this.options.ymax; lastY = this.ymax;
for (lineY = _i = firstY, _ref = this.yInterval; firstY <= lastY ? _i <= lastY : _i >= lastY; lineY = _i += _ref) { for (lineY = _i = firstY, _ref = this.yInterval; firstY <= lastY ? _i <= lastY : _i >= lastY; lineY = _i += _ref) {
v = parseFloat(lineY.toFixed(this.precision)); v = parseFloat(lineY.toFixed(this.precision));
y = this.transY(v); y = this.transY(v);

2
morris.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -243,8 +243,3 @@ describe 'Morris.line', ->
["1/5/2012", new Date(2012, 0, 5).getTime()], ["1/5/2012", new Date(2012, 0, 5).getTime()],
["1/6/2012", new Date(2012, 0, 6).getTime()] ["1/6/2012", new Date(2012, 0, 6).getTime()]
]) ])