A small refactor.

This commit is contained in:
Olly Smith 2012-04-05 20:04:19 +01:00
parent 0079d307f3
commit fa8f8c60e5
3 changed files with 25 additions and 16 deletions

View File

@ -14,10 +14,10 @@ class Morris.Line
@el = $ document.getElementById(options.element)
else
@el = $ options.element
#backwards compatibility for units -> postunits
if typeof options.units is 'string'
options.postunits = options.units
@options = $.extend {}, @defaults, options
# backwards compatibility for units -> postUnits
if typeof @options.units is 'string'
@options.postUnits = options.units
# bail if there's no data
if @options.data is undefined or @options.data.length is 0
return
@ -62,8 +62,8 @@ class Morris.Line
smooth: true
hideHover: false
parseTime: true
preunits: ''
postunits: ''
preUnits: ''
postUnits: ''
dateFormat: (x) -> new Date(x).toString()
xLabels: 'auto'
xLabelFormat: null
@ -147,8 +147,8 @@ class Morris.Line
if @elementWidth != w or @elementHeight != h
# calculate grid dimensions
@maxYLabelWidth = Math.max(
@measureText(@options.preunits + @options.ymin + @options.postunits, @options.gridTextSize).width,
@measureText(@options.preunits + @options.ymax + @options.postunits, @options.gridTextSize).width)
@measureText(@yLabelFormat(@options.ymin), @options.gridTextSize).width,
@measureText(@yLabelFormat(@options.ymax), @options.gridTextSize).width)
@left = @maxYLabelWidth + @options.marginLeft
@width = @el.width() - @left - @options.marginRight
@height = @el.height() - @options.marginTop - @options.marginBottom
@ -204,7 +204,7 @@ class Morris.Line
for lineY in [firstY..lastY] by yInterval
v = Math.floor(lineY)
y = @transY(v)
@r.text(@left - @options.marginLeft/2, y, @options.preunits + Morris.commas(v) + @options.postunits)
@r.text(@left - @options.marginLeft/2, y, @yLabelFormat(v))
.attr('font-size', @options.gridTextSize)
.attr('fill', @options.gridTextColor)
.attr('text-anchor', 'end')
@ -327,7 +327,7 @@ class Morris.Line
@hoverSet.show()
@xLabel.attr('text', @columnLabels[index])
for i in [0..@series.length-1]
@yLabels[i].attr('text', "#{@seriesLabels[i]}: #{@options.preunits}#{Morris.commas(@series[i][index])}#{@options.postunits}")
@yLabels[i].attr('text', "#{@seriesLabels[i]}: #{@yLabelFormat(@series[i][index])}")
# recalculate hover box width
maxLabelWidth = Math.max.apply null, $.map @yLabels, (l) ->
l.getBBox().width
@ -377,6 +377,9 @@ class Morris.Line
tt.remove()
return ret
yLabelFormat: (label) ->
"#{@options.preUnits}#{Morris.commas(label)}#{@options.postUnits}"
# parse a date into a javascript timestamp
#
Morris.parseDate = (date) ->

View File

@ -19,8 +19,10 @@
} else {
this.el = $(options.element);
}
if (typeof options.units === 'string') options.postunits = options.units;
this.options = $.extend({}, this.defaults, options);
if (typeof this.options.units === 'string') {
this.options.postUnits = options.units;
}
if (this.options.data === void 0 || this.options.data.length === 0) return;
this.el.addClass('graph-initialised');
this.precalc();
@ -54,8 +56,8 @@
smooth: true,
hideHover: false,
parseTime: true,
preunits: '',
postunits: '',
preUnits: '',
postUnits: '',
dateFormat: function(x) {
return new Date(x).toString();
},
@ -161,7 +163,7 @@
w = this.el.width();
h = this.el.height();
if (this.elementWidth !== w || this.elementHeight !== h) {
this.maxYLabelWidth = Math.max(this.measureText(this.options.preunits + this.options.ymin + this.options.postunits, this.options.gridTextSize).width, this.measureText(this.options.preunits + this.options.ymax + this.options.postunits, 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.width = this.el.width() - this.left - this.options.marginRight;
this.height = this.el.height() - this.options.marginTop - this.options.marginBottom;
@ -231,7 +233,7 @@
for (lineY = firstY; firstY <= lastY ? lineY <= lastY : lineY >= lastY; lineY += yInterval) {
v = Math.floor(lineY);
y = this.transY(v);
this.r.text(this.left - this.options.marginLeft / 2, y, this.options.preunits + Morris.commas(v) + this.options.postunits).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);
}
ypos = this.options.marginTop + this.height + this.options.marginBottom / 2;
@ -375,7 +377,7 @@
this.hoverSet.show();
this.xLabel.attr('text', this.columnLabels[index]);
for (i = 0, _ref = this.series.length - 1; 0 <= _ref ? i <= _ref : i >= _ref; 0 <= _ref ? i++ : i--) {
this.yLabels[i].attr('text', "" + this.seriesLabels[i] + ": " + this.options.preunits + (Morris.commas(this.series[i][index])) + this.options.postunits);
this.yLabels[i].attr('text', "" + this.seriesLabels[i] + ": " + (this.yLabelFormat(this.series[i][index])));
}
maxLabelWidth = Math.max.apply(null, $.map(this.yLabels, function(l) {
return l.getBBox().width;
@ -447,6 +449,10 @@
return ret;
};
Line.prototype.yLabelFormat = function(label) {
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