Move the formatting into yLabelFormat.

In order to let the user to get rid of the precision, she can provide a custom
yLabelFormat funciton.
This commit is contained in:
Thomas Pelletier 2012-05-14 11:10:50 +01:00
parent 7def73fbf6
commit e329928bdc
3 changed files with 8 additions and 8 deletions

View File

@ -160,8 +160,8 @@ class Morris.Line
if @elementWidth != w or @elementHeight != h if @elementWidth != w or @elementHeight != h
# calculate grid dimensions # calculate grid dimensions
@maxYLabelWidth = Math.max( @maxYLabelWidth = Math.max(
@measureText(@yLabelFormat(@options.ymin.toFixed(@precision)), @options.gridTextSize).width, @measureText(@yLabelFormat(@options.ymin), @options.gridTextSize).width,
@measureText(@yLabelFormat(@options.ymax.toFixed(@precision)), @options.gridTextSize).width) @measureText(@yLabelFormat(@options.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
@ -218,7 +218,7 @@ class Morris.Line
for lineY in [firstY..lastY] by @yInterval for lineY in [firstY..lastY] by @yInterval
v = lineY v = lineY
y = @transY(v) y = @transY(v)
@r.text(@left - @options.marginLeft/2, y, @yLabelFormat(v.toFixed(@precision))) @r.text(@left - @options.marginLeft/2, y, @yLabelFormat(v))
.attr('font-size', @options.gridTextSize) .attr('font-size', @options.gridTextSize)
.attr('fill', @options.gridTextColor) .attr('fill', @options.gridTextColor)
.attr('text-anchor', 'end') .attr('text-anchor', 'end')
@ -392,7 +392,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,7 +185,7 @@
this.precision = 0; 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.toFixed(this.precision)), this.options.gridTextSize).width, this.measureText(this.yLabelFormat(this.options.ymax.toFixed(this.precision)), 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;
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;
@ -254,7 +254,7 @@
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 = lineY; v = lineY;
y = this.transY(v); y = this.transY(v);
this.r.text(this.left - this.options.marginLeft / 2, y, this.yLabelFormat(v.toFixed(this.precision))).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);
} }
ypos = this.options.marginTop + this.height + this.options.marginBottom / 2; ypos = this.options.marginTop + this.height + this.options.marginBottom / 2;
@ -475,7 +475,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