Merge branch 'decimal' of https://github.com/pelletier/morris.js into pelletier-decimal

Conflicts:
	morris.min.js
This commit is contained in:
Olly Smith 2012-06-08 21:42:10 +01:00
commit 380cc5217a
3 changed files with 32 additions and 18 deletions

View File

@ -152,6 +152,14 @@ class Morris.Line
calc: -> calc: ->
w = @el.width() w = @el.width()
h = @el.height() 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 if @elementWidth != w or @elementHeight != h
# calculate grid dimensions # calculate grid dimensions
@maxYLabelWidth = Math.max( @maxYLabelWidth = Math.max(
@ -206,11 +214,12 @@ class Morris.Line
# #
drawGrid: -> drawGrid: ->
# draw y axis labels, horizontal lines # draw y axis labels, horizontal lines
yInterval = (@options.ymax - @options.ymin) / (@options.numLines - 1) firstY = @options.ymin
firstY = Math.ceil(@options.ymin / yInterval) * yInterval lastY = @options.ymax
lastY = Math.floor(@options.ymax / yInterval) * yInterval
for lineY in [firstY..lastY] by yInterval
v = Math.floor(lineY) for lineY in [firstY..lastY] by @yInterval
v = lineY
y = @transY(v) y = @transY(v)
@r.text(@left - @options.marginLeft/2, y, @yLabelFormat(v)) @r.text(@left - @options.marginLeft/2, y, @yLabelFormat(v))
.attr('font-size', @options.gridTextSize) .attr('font-size', @options.gridTextSize)
@ -386,7 +395,7 @@ class Morris.Line
return ret return ret
yLabelFormat: (label) -> yLabelFormat: (label) ->
"#{@options.preUnits}#{Morris.commas(label)}#{@options.postUnits}" "#{@options.preUnits}#{Morris.commas(label.toFixed(@precision || 0))}#{@options.postUnits}"
# parse a date into a javascript timestamp # parse a date into a javascript timestamp
# #

View File

@ -185,6 +185,12 @@
_this = this; _this = this;
w = this.el.width(); w = this.el.width();
h = this.el.height(); 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) { 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.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; this.left = this.maxYLabelWidth + this.options.marginLeft;
@ -248,13 +254,12 @@
}; };
Line.prototype.drawGrid = function() { Line.prototype.drawGrid = function() {
var drawLabel, firstY, i, l, labelText, lastY, lineY, prevLabelMargin, v, xLabelMargin, y, yInterval, ypos, _i, _j, _k, _len, _ref, _ref1, _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;
yInterval = (this.options.ymax - this.options.ymin) / (this.options.numLines - 1); firstY = this.options.ymin;
firstY = Math.ceil(this.options.ymin / yInterval) * yInterval; lastY = this.options.ymax;
lastY = Math.floor(this.options.ymax / yInterval) * yInterval; for (lineY = _i = firstY, _ref = this.yInterval; firstY <= lastY ? _i <= lastY : _i >= lastY; lineY = _i += _ref) {
for (lineY = _i = firstY; firstY <= lastY ? _i <= lastY : _i >= lastY; lineY = _i += yInterval) { v = lineY;
v = Math.floor(lineY);
y = this.transY(v); 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.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); this.r.path("M" + this.left + "," + y + "H" + (this.left + this.width)).attr('stroke', this.options.gridLineColor).attr('stroke-width', this.options.gridStrokeWidth);
@ -276,17 +281,17 @@
if (this.columnLabels.length === 1 && this.options.xLabels === 'auto') { if (this.columnLabels.length === 1 && this.options.xLabels === 'auto') {
return drawLabel(this.columnLabels[0], this.xvals[0]); return drawLabel(this.columnLabels[0], this.xvals[0]);
} else { } else {
_ref = Morris.labelSeries(this.xmin, this.xmax, this.width, this.options.xLabels, this.options.xLabelFormat); _ref1 = Morris.labelSeries(this.xmin, this.xmax, this.width, this.options.xLabels, this.options.xLabelFormat);
_results = []; _results = [];
for (_j = 0, _len = _ref.length; _j < _len; _j++) { for (_j = 0, _len = _ref1.length; _j < _len; _j++) {
l = _ref[_j]; l = _ref1[_j];
_results.push(drawLabel(l[0], l[1])); _results.push(drawLabel(l[0], l[1]));
} }
return _results; return _results;
} }
} else { } else {
_results1 = []; _results1 = [];
for (i = _k = 0, _ref1 = this.columnLabels.length; 0 <= _ref1 ? _k <= _ref1 : _k >= _ref1; i = 0 <= _ref1 ? ++_k : --_k) { for (i = _k = 0, _ref2 = this.columnLabels.length; 0 <= _ref2 ? _k <= _ref2 : _k >= _ref2; i = 0 <= _ref2 ? ++_k : --_k) {
labelText = this.columnLabels[this.columnLabels.length - i - 1]; labelText = this.columnLabels[this.columnLabels.length - i - 1];
_results1.push(drawLabel(labelText, i)); _results1.push(drawLabel(labelText, i));
} }
@ -477,7 +482,7 @@
}; };
Line.prototype.yLabelFormat = function(label) { Line.prototype.yLabelFormat = function(label) {
return "" + this.options.preUnits + (Morris.commas(label)) + this.options.postUnits; return "" + this.options.preUnits + (Morris.commas(label.toFixed(this.precision || 0))) + this.options.postUnits;
}; };
return Line; return Line;

2
morris.min.js vendored

File diff suppressed because one or more lines are too long