mirror of
https://github.com/morrisjs/morris.js.git
synced 2024-11-10 21:36:34 +01:00
A small refactor.
This commit is contained in:
parent
0079d307f3
commit
fa8f8c60e5
@ -14,10 +14,10 @@ class Morris.Line
|
|||||||
@el = $ document.getElementById(options.element)
|
@el = $ document.getElementById(options.element)
|
||||||
else
|
else
|
||||||
@el = $ options.element
|
@el = $ options.element
|
||||||
#backwards compatibility for units -> postunits
|
|
||||||
if typeof options.units is 'string'
|
|
||||||
options.postunits = options.units
|
|
||||||
@options = $.extend {}, @defaults, options
|
@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
|
# bail if there's no data
|
||||||
if @options.data is undefined or @options.data.length is 0
|
if @options.data is undefined or @options.data.length is 0
|
||||||
return
|
return
|
||||||
@ -62,8 +62,8 @@ class Morris.Line
|
|||||||
smooth: true
|
smooth: true
|
||||||
hideHover: false
|
hideHover: false
|
||||||
parseTime: true
|
parseTime: true
|
||||||
preunits: ''
|
preUnits: ''
|
||||||
postunits: ''
|
postUnits: ''
|
||||||
dateFormat: (x) -> new Date(x).toString()
|
dateFormat: (x) -> new Date(x).toString()
|
||||||
xLabels: 'auto'
|
xLabels: 'auto'
|
||||||
xLabelFormat: null
|
xLabelFormat: null
|
||||||
@ -147,8 +147,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(@options.preunits + @options.ymin + @options.postunits, @options.gridTextSize).width,
|
@measureText(@yLabelFormat(@options.ymin), @options.gridTextSize).width,
|
||||||
@measureText(@options.preunits + @options.ymax + @options.postunits, @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
|
||||||
@ -204,7 +204,7 @@ class Morris.Line
|
|||||||
for lineY in [firstY..lastY] by yInterval
|
for lineY in [firstY..lastY] by yInterval
|
||||||
v = Math.floor(lineY)
|
v = Math.floor(lineY)
|
||||||
y = @transY(v)
|
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('font-size', @options.gridTextSize)
|
||||||
.attr('fill', @options.gridTextColor)
|
.attr('fill', @options.gridTextColor)
|
||||||
.attr('text-anchor', 'end')
|
.attr('text-anchor', 'end')
|
||||||
@ -327,7 +327,7 @@ class Morris.Line
|
|||||||
@hoverSet.show()
|
@hoverSet.show()
|
||||||
@xLabel.attr('text', @columnLabels[index])
|
@xLabel.attr('text', @columnLabels[index])
|
||||||
for i in [0..@series.length-1]
|
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
|
# recalculate hover box width
|
||||||
maxLabelWidth = Math.max.apply null, $.map @yLabels, (l) ->
|
maxLabelWidth = Math.max.apply null, $.map @yLabels, (l) ->
|
||||||
l.getBBox().width
|
l.getBBox().width
|
||||||
@ -377,6 +377,9 @@ class Morris.Line
|
|||||||
tt.remove()
|
tt.remove()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
yLabelFormat: (label) ->
|
||||||
|
"#{@options.preUnits}#{Morris.commas(label)}#{@options.postUnits}"
|
||||||
|
|
||||||
# parse a date into a javascript timestamp
|
# parse a date into a javascript timestamp
|
||||||
#
|
#
|
||||||
Morris.parseDate = (date) ->
|
Morris.parseDate = (date) ->
|
||||||
|
18
morris.js
18
morris.js
@ -19,8 +19,10 @@
|
|||||||
} else {
|
} else {
|
||||||
this.el = $(options.element);
|
this.el = $(options.element);
|
||||||
}
|
}
|
||||||
if (typeof options.units === 'string') options.postunits = options.units;
|
|
||||||
this.options = $.extend({}, this.defaults, options);
|
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;
|
if (this.options.data === void 0 || this.options.data.length === 0) return;
|
||||||
this.el.addClass('graph-initialised');
|
this.el.addClass('graph-initialised');
|
||||||
this.precalc();
|
this.precalc();
|
||||||
@ -54,8 +56,8 @@
|
|||||||
smooth: true,
|
smooth: true,
|
||||||
hideHover: false,
|
hideHover: false,
|
||||||
parseTime: true,
|
parseTime: true,
|
||||||
preunits: '',
|
preUnits: '',
|
||||||
postunits: '',
|
postUnits: '',
|
||||||
dateFormat: function(x) {
|
dateFormat: function(x) {
|
||||||
return new Date(x).toString();
|
return new Date(x).toString();
|
||||||
},
|
},
|
||||||
@ -161,7 +163,7 @@
|
|||||||
w = this.el.width();
|
w = this.el.width();
|
||||||
h = this.el.height();
|
h = this.el.height();
|
||||||
if (this.elementWidth !== w || this.elementHeight !== h) {
|
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.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;
|
||||||
@ -231,7 +233,7 @@
|
|||||||
for (lineY = firstY; firstY <= lastY ? lineY <= lastY : lineY >= lastY; lineY += yInterval) {
|
for (lineY = firstY; firstY <= lastY ? lineY <= lastY : lineY >= lastY; lineY += yInterval) {
|
||||||
v = Math.floor(lineY);
|
v = Math.floor(lineY);
|
||||||
y = this.transY(v);
|
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);
|
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;
|
||||||
@ -375,7 +377,7 @@
|
|||||||
this.hoverSet.show();
|
this.hoverSet.show();
|
||||||
this.xLabel.attr('text', this.columnLabels[index]);
|
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--) {
|
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) {
|
maxLabelWidth = Math.max.apply(null, $.map(this.yLabels, function(l) {
|
||||||
return l.getBBox().width;
|
return l.getBBox().width;
|
||||||
@ -447,6 +449,10 @@
|
|||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Line.prototype.yLabelFormat = function(label) {
|
||||||
|
return "" + this.options.preUnits + (Morris.commas(label)) + this.options.postUnits;
|
||||||
|
};
|
||||||
|
|
||||||
return Line;
|
return Line;
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
2
morris.min.js
vendored
2
morris.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user