Fixed performance issue with data ranges with a large span of values

This commit is contained in:
Mark Abbott 2012-03-13 22:06:49 +00:00
parent 5ee41e16e2
commit bfb1e71193
3 changed files with 29 additions and 19 deletions

View file

@ -148,12 +148,17 @@ class Morris.Line
# draw x axis labels # draw x axis labels
prevLabelMargin = null prevLabelMargin = null
xLabelMargin = 50 # make this an option? xLabelMargin = 50 # make this an option?
xstep = width / @xvals.length
next_x = null
for i in [Math.ceil(@xmin)..Math.floor(@xmax)] for i in [Math.ceil(@xmin)..Math.floor(@xmax)]
if xstep < 1 or null == next_x or i >= next_x
labelText = if @options.parseTime then i else @columnLabels[@columnLabels.length-i-1] labelText = if @options.parseTime then i else @columnLabels[@columnLabels.length-i-1]
label = @r.text(transX(i), @options.marginTop + height + @options.marginBottom / 2, labelText) label = @r.text(transX(i), @options.marginTop + height + @options.marginBottom / 2, labelText)
.attr('font-size', @options.gridTextSize) .attr('font-size', @options.gridTextSize)
.attr('fill', @options.gridTextColor) .attr('fill', @options.gridTextColor)
labelBox = label.getBBox() labelBox = label.getBBox()
# Calculate the next value of i to use
next_x = (labelBox.x + labelBox.width + xLabelMargin) * xstep
# ensure a minimum of `xLabelMargin` pixels between labels # ensure a minimum of `xLabelMargin` pixels between labels
if prevLabelMargin is null or prevLabelMargin <= labelBox.x if prevLabelMargin is null or prevLabelMargin <= labelBox.x
prevLabelMargin = labelBox.x + labelBox.width + xLabelMargin prevLabelMargin = labelBox.x + labelBox.width + xLabelMargin

View file

@ -105,7 +105,7 @@
}; };
Line.prototype.redraw = function() { Line.prototype.redraw = function() {
var c, circle, columns, coords, dx, dy, firstY, height, hideHover, hilight, hover, hoverHeight, hoverMargins, hoverSet, i, label, labelBox, labelText, lastY, left, lineY, maxYLabelWidth, path, pointGrow, pointShrink, prevHilight, prevLabelMargin, s, seriesCoords, seriesPoints, touchHandler, transX, transY, updateHilight, updateHover, v, width, x, xLabel, xLabelMargin, y, yInterval, yLabel, yLabels, _i, _j, _len, _len2, _ref, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, var c, circle, columns, coords, dx, dy, firstY, height, hideHover, hilight, hover, hoverHeight, hoverMargins, hoverSet, i, label, labelBox, labelText, lastY, left, lineY, maxYLabelWidth, next_x, path, pointGrow, pointShrink, prevHilight, prevLabelMargin, s, seriesCoords, seriesPoints, touchHandler, transX, transY, updateHilight, updateHover, v, width, x, xLabel, xLabelMargin, xstep, y, yInterval, yLabel, yLabels, _i, _j, _len, _len2, _ref, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7,
_this = this; _this = this;
this.el.empty(); this.el.empty();
this.r = new Raphael(this.el[0]); this.r = new Raphael(this.el[0]);
@ -136,16 +136,21 @@
} }
prevLabelMargin = null; prevLabelMargin = null;
xLabelMargin = 50; xLabelMargin = 50;
xstep = width / this.xvals.length;
next_x = null;
for (i = _ref = Math.ceil(this.xmin), _ref2 = Math.floor(this.xmax); _ref <= _ref2 ? i <= _ref2 : i >= _ref2; _ref <= _ref2 ? i++ : i--) { for (i = _ref = Math.ceil(this.xmin), _ref2 = Math.floor(this.xmax); _ref <= _ref2 ? i <= _ref2 : i >= _ref2; _ref <= _ref2 ? i++ : i--) {
if (xstep < 1 || null === next_x || i >= next_x) {
labelText = this.options.parseTime ? i : this.columnLabels[this.columnLabels.length - i - 1]; labelText = this.options.parseTime ? i : this.columnLabels[this.columnLabels.length - i - 1];
label = this.r.text(transX(i), this.options.marginTop + height + this.options.marginBottom / 2, labelText).attr('font-size', this.options.gridTextSize).attr('fill', this.options.gridTextColor); label = this.r.text(transX(i), this.options.marginTop + height + this.options.marginBottom / 2, labelText).attr('font-size', this.options.gridTextSize).attr('fill', this.options.gridTextColor);
labelBox = label.getBBox(); labelBox = label.getBBox();
next_x = (labelBox.x + labelBox.width + xLabelMargin) * xstep;
if (prevLabelMargin === null || prevLabelMargin <= labelBox.x) { if (prevLabelMargin === null || prevLabelMargin <= labelBox.x) {
prevLabelMargin = labelBox.x + labelBox.width + xLabelMargin; prevLabelMargin = labelBox.x + labelBox.width + xLabelMargin;
} else { } else {
label.remove(); label.remove();
} }
} }
}
columns = (function() { columns = (function() {
var _i, _len, _ref3, _results; var _i, _len, _ref3, _results;
_ref3 = this.xvals; _ref3 = this.xvals;

2
morris.min.js vendored

File diff suppressed because one or more lines are too long