Merge pull request #7 from thedjinn/ymax-option

Added option to set maximum y-value
This commit is contained in:
Olly Smith 2012-02-27 23:00:58 -08:00
commit 7d9d0b811a
3 changed files with 20 additions and 14 deletions

View File

@ -31,6 +31,7 @@ class Morris.Line
'#cb4b4b' '#cb4b4b'
'#9440ed' '#9440ed'
] ]
ymax: 'auto'
marginTop: 25 marginTop: 25
marginRight: 25 marginRight: 25
marginBottom: 30 marginBottom: 30
@ -71,9 +72,11 @@ class Morris.Line
@xmin -= 1 @xmin -= 1
@xmax += 1 @xmax += 1
# use $.map to flatten arrays and find the max y value # Compute the vertical range of the graph if desired
all_y_vals = $.map @series, (x) -> Math.max.apply null, x if @options.ymax == 'auto'
@ymax = Math.max(20, Math.max.apply(null, all_y_vals)) # use $.map to flatten arrays and find the max y value
all_y_vals = $.map @series, (x) -> Math.max.apply null, x
@options.ymax = Math.max(20, Math.max.apply(null, all_y_vals))
# Clear and redraw the graph # Clear and redraw the graph
# #
@ -85,11 +88,11 @@ class Morris.Line
@r = new Raphael(@el[0]) @r = new Raphael(@el[0])
# calculate grid dimensions # calculate grid dimensions
left = @measureText(@ymax, @options.gridTextSize).width + @options.marginLeft left = @measureText(@options.ymax, @options.gridTextSize).width + @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 / @ymax dy = height / @options.ymax
# quick translation helpers # quick translation helpers
transX = (x) => transX = (x) =>
@ -104,7 +107,7 @@ class Morris.Line
lineInterval = height / (@options.numLines - 1) lineInterval = height / (@options.numLines - 1)
for i in [0..@options.numLines-1] for i in [0..@options.numLines-1]
y = @options.marginTop + i * lineInterval y = @options.marginTop + i * lineInterval
v = Math.round((@options.numLines - 1 - i) * @ymax / (@options.numLines - 1)) v = Math.round((@options.numLines - 1 - i) * @options.ymax / (@options.numLines - 1))
@r.text(left - @options.marginLeft/2, y, v) @r.text(left - @options.marginLeft/2, y, v)
.attr('font-size', @options.gridTextSize) .attr('font-size', @options.gridTextSize)
.attr('fill', @options.gridTextColor) .attr('fill', @options.gridTextColor)

View File

@ -19,6 +19,7 @@
lineWidth: 3, lineWidth: 3,
pointSize: 4, pointSize: 4,
lineColors: ['#0b62a4', '#7A92A3', '#4da74d', '#afd8f8', '#edc240', '#cb4b4b', '#9440ed'], lineColors: ['#0b62a4', '#7A92A3', '#4da74d', '#afd8f8', '#edc240', '#cb4b4b', '#9440ed'],
ymax: 'auto',
marginTop: 25, marginTop: 25,
marginRight: 25, marginRight: 25,
marginBottom: 30, marginBottom: 30,
@ -63,10 +64,12 @@
this.xmin -= 1; this.xmin -= 1;
this.xmax += 1; this.xmax += 1;
} }
all_y_vals = $.map(this.series, function(x) { if (this.options.ymax === 'auto') {
return Math.max.apply(null, x); all_y_vals = $.map(this.series, function(x) {
}); return Math.max.apply(null, x);
return this.ymax = Math.max(20, Math.max.apply(null, all_y_vals)); });
return this.options.ymax = Math.max(20, Math.max.apply(null, all_y_vals));
}
}; };
Line.prototype.redraw = function() { Line.prototype.redraw = function() {
@ -74,11 +77,11 @@
_this = this; _this = this;
this.el.empty(); this.el.empty();
this.r = new Raphael(this.el[0]); this.r = new Raphael(this.el[0]);
left = this.measureText(this.ymax, this.options.gridTextSize).width + this.options.marginLeft; left = this.measureText(this.options.ymax, this.options.gridTextSize).width + this.options.marginLeft;
width = this.el.width() - left - this.options.marginRight; width = this.el.width() - left - this.options.marginRight;
height = this.el.height() - this.options.marginTop - this.options.marginBottom; height = this.el.height() - this.options.marginTop - this.options.marginBottom;
dx = width / (this.xmax - this.xmin); dx = width / (this.xmax - this.xmin);
dy = height / this.ymax; dy = height / this.options.ymax;
transX = function(x) { transX = function(x) {
if (_this.xvals.length === 1) { if (_this.xvals.length === 1) {
return left + width / 2; return left + width / 2;
@ -92,7 +95,7 @@
lineInterval = height / (this.options.numLines - 1); lineInterval = height / (this.options.numLines - 1);
for (i = 0, _ref = this.options.numLines - 1; 0 <= _ref ? i <= _ref : i >= _ref; 0 <= _ref ? i++ : i--) { for (i = 0, _ref = this.options.numLines - 1; 0 <= _ref ? i <= _ref : i >= _ref; 0 <= _ref ? i++ : i--) {
y = this.options.marginTop + i * lineInterval; y = this.options.marginTop + i * lineInterval;
v = Math.round((this.options.numLines - 1 - i) * this.ymax / (this.options.numLines - 1)); v = Math.round((this.options.numLines - 1 - i) * this.options.ymax / (this.options.numLines - 1));
this.r.text(left - this.options.marginLeft / 2, y, v).attr('font-size', this.options.gridTextSize).attr('fill', this.options.gridTextColor).attr('text-anchor', 'end'); this.r.text(left - this.options.marginLeft / 2, y, v).attr('font-size', this.options.gridTextSize).attr('fill', this.options.gridTextColor).attr('text-anchor', 'end');
this.r.path("M" + left + "," + y + 'H' + (left + width)).attr('stroke', this.options.gridLineColor).attr('stroke-width', this.options.gridStrokeWidth); this.r.path("M" + left + "," + y + 'H' + (left + width)).attr('stroke', this.options.gridLineColor).attr('stroke-width', this.options.gridStrokeWidth);
} }

2
morris.min.js vendored

File diff suppressed because one or more lines are too long