Tidy up decimal grids intervals a little.

This commit is contained in:
Olly Smith 2012-06-08 21:47:43 +01:00
parent 380cc5217a
commit 0015b28ef2
3 changed files with 17 additions and 18 deletions

View File

@ -127,6 +127,12 @@ class Morris.Line
else
@options.ymin = ymin
@yInterval = (@options.ymax - @options.ymin) / (@options.numLines - 1)
if @yInterval > 0 and @yInterval < 1
@precision = -Math.floor(Math.log(@yInterval) / Math.log(10))
else
@precision = 0
# Some instance variables for later
@pointGrow = Raphael.animation r: @options.pointSize + 3, 25, 'linear'
@pointShrink = Raphael.animation r: @options.pointSize, 25, 'linear'
@ -153,13 +159,6 @@ class Morris.Line
w = @el.width()
h = @el.height()
@yInterval = (@options.ymax - @options.ymin) / (@options.numLines - 1)
if (@yInterval < 1)
@precision = -parseInt(@yInterval.toExponential().split('e')[1])
else
@precision = 0
if @elementWidth != w or @elementHeight != h
# calculate grid dimensions
@maxYLabelWidth = Math.max(
@ -219,7 +218,7 @@ class Morris.Line
for lineY in [firstY..lastY] by @yInterval
v = lineY
v = parseFloat(lineY.toFixed(@precision))
y = @transY(v)
@r.text(@left - @options.marginLeft/2, y, @yLabelFormat(v))
.attr('font-size', @options.gridTextSize)
@ -395,7 +394,7 @@ class Morris.Line
return ret
yLabelFormat: (label) ->
"#{@options.preUnits}#{Morris.commas(label.toFixed(@precision || 0))}#{@options.postUnits}"
"#{@options.preUnits}#{Morris.commas(label)}#{@options.postUnits}"
# parse a date into a javascript timestamp
#

View File

@ -152,6 +152,12 @@
this.options.ymin = ymin;
}
}
this.yInterval = (this.options.ymax - this.options.ymin) / (this.options.numLines - 1);
if (this.yInterval > 0 && this.yInterval < 1) {
this.precision = -Math.floor(Math.log(this.yInterval) / Math.log(10));
} else {
this.precision = 0;
}
this.pointGrow = Raphael.animation({
r: this.options.pointSize + 3
}, 25, 'linear');
@ -185,12 +191,6 @@
_this = this;
w = this.el.width();
h = this.el.height();
this.yInterval = (this.options.ymax - this.options.ymin) / (this.options.numLines - 1);
if (this.yInterval < 1) {
this.precision = -parseInt(this.yInterval.toExponential().split('e')[1]);
} else {
this.precision = 0;
}
if (this.elementWidth !== w || this.elementHeight !== h) {
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.left = this.maxYLabelWidth + this.options.marginLeft;
@ -259,7 +259,7 @@
firstY = this.options.ymin;
lastY = this.options.ymax;
for (lineY = _i = firstY, _ref = this.yInterval; firstY <= lastY ? _i <= lastY : _i >= lastY; lineY = _i += _ref) {
v = lineY;
v = parseFloat(lineY.toFixed(this.precision));
y = this.transY(v);
this.r.text(this.left - this.options.marginLeft / 2, y, this.yLabelFormat(v)).attr('font-size', this.options.gridTextSize).attr('fill', this.options.gridTextColor).attr('text-anchor', 'end');
this.r.path("M" + this.left + "," + y + "H" + (this.left + this.width)).attr('stroke', this.options.gridLineColor).attr('stroke-width', this.options.gridStrokeWidth);
@ -482,7 +482,7 @@
};
Line.prototype.yLabelFormat = function(label) {
return "" + this.options.preUnits + (Morris.commas(label.toFixed(this.precision || 0))) + this.options.postUnits;
return "" + this.options.preUnits + (Morris.commas(label)) + this.options.postUnits;
};
return Line;

2
morris.min.js vendored

File diff suppressed because one or more lines are too long