Cycle lineColors (#78)

Fixes crashes when number of series is larger than number of line colors.
This commit is contained in:
Olly Smith 2012-09-25 21:07:32 +01:00
parent 50833b0ebc
commit a9b0e03b43
3 changed files with 15 additions and 7 deletions

View File

@ -283,7 +283,7 @@ class Morris.Line
if coords.length > 1 if coords.length > 1
path = @createPath coords, @options.marginTop, @left, @options.marginTop + @height, @left + @width path = @createPath coords, @options.marginTop, @left, @options.marginTop + @height, @left + @width
@r.path(path) @r.path(path)
.attr('stroke', @options.lineColors[i]) .attr('stroke', @colorForSeries(i))
.attr('stroke-width', @options.lineWidth) .attr('stroke-width', @options.lineWidth)
@seriesPoints = ([] for i in [0..@seriesCoords.length-1]) @seriesPoints = ([] for i in [0..@seriesCoords.length-1])
for i in [@seriesCoords.length-1..0] for i in [@seriesCoords.length-1..0]
@ -292,7 +292,7 @@ class Morris.Line
circle = null circle = null
else else
circle = @r.circle(c.x, c.y, @options.pointSize) circle = @r.circle(c.x, c.y, @options.pointSize)
.attr('fill', @options.lineColors[i]) .attr('fill', @colorForSeries(i))
.attr('stroke-width', 1) .attr('stroke-width', 1)
.attr('stroke', '#ffffff') .attr('stroke', '#ffffff')
@seriesPoints[i].push(circle) @seriesPoints[i].push(circle)
@ -355,7 +355,7 @@ class Morris.Line
@yLabels = [] @yLabels = []
for i in [0..@series.length-1] for i in [0..@series.length-1]
yLabel = @r.text(0, @options.hoverFontSize * 1.5 * (i + 1.5) - @hoverHeight / 2, '') yLabel = @r.text(0, @options.hoverFontSize * 1.5 * (i + 1.5) - @hoverHeight / 2, '')
.attr('fill', @options.lineColors[i]) .attr('fill', @colorForSeries(i))
.attr('font-size', @options.hoverFontSize) .attr('font-size', @options.hoverFontSize)
@yLabels.push(yLabel) @yLabels.push(yLabel)
@hoverSet.push(yLabel) @hoverSet.push(yLabel)
@ -423,6 +423,10 @@ class Morris.Line
yLabelFormat: (label) -> yLabelFormat: (label) ->
"#{@options.preUnits}#{Morris.commas(label)}#{@options.postUnits}" "#{@options.preUnits}#{Morris.commas(label)}#{@options.postUnits}"
# @private
colorForSeries: (index) ->
@options.lineColors[index % @options.lineColors.length]
# Parse a date into a javascript timestamp # Parse a date into a javascript timestamp
# #

View File

@ -603,7 +603,7 @@
}); });
if (coords.length > 1) { if (coords.length > 1) {
path = this.createPath(coords, this.options.marginTop, this.left, this.options.marginTop + this.height, this.left + this.width); path = this.createPath(coords, this.options.marginTop, this.left, this.options.marginTop + this.height, this.left + this.width);
this.r.path(path).attr('stroke', this.options.lineColors[i]).attr('stroke-width', this.options.lineWidth); this.r.path(path).attr('stroke', this.colorForSeries(i)).attr('stroke-width', this.options.lineWidth);
} }
} }
this.seriesPoints = (function() { this.seriesPoints = (function() {
@ -625,7 +625,7 @@
if (c === null) { if (c === null) {
circle = null; circle = null;
} else { } else {
circle = this.r.circle(c.x, c.y, this.options.pointSize).attr('fill', this.options.lineColors[i]).attr('stroke-width', 1).attr('stroke', '#ffffff'); circle = this.r.circle(c.x, c.y, this.options.pointSize).attr('fill', this.colorForSeries(i)).attr('stroke-width', 1).attr('stroke', '#ffffff');
} }
_results1.push(this.seriesPoints[i].push(circle)); _results1.push(this.seriesPoints[i].push(circle));
} }
@ -687,7 +687,7 @@
this.yLabels = []; this.yLabels = [];
_results = []; _results = [];
for (i = _i = 0, _ref = this.series.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) { for (i = _i = 0, _ref = this.series.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
yLabel = this.r.text(0, this.options.hoverFontSize * 1.5 * (i + 1.5) - this.hoverHeight / 2, '').attr('fill', this.options.lineColors[i]).attr('font-size', this.options.hoverFontSize); yLabel = this.r.text(0, this.options.hoverFontSize * 1.5 * (i + 1.5) - this.hoverHeight / 2, '').attr('fill', this.colorForSeries(i)).attr('font-size', this.options.hoverFontSize);
this.yLabels.push(yLabel); this.yLabels.push(yLabel);
_results.push(this.hoverSet.push(yLabel)); _results.push(this.hoverSet.push(yLabel));
} }
@ -780,6 +780,10 @@
return "" + this.options.preUnits + (Morris.commas(label)) + this.options.postUnits; return "" + this.options.preUnits + (Morris.commas(label)) + this.options.postUnits;
}; };
Line.prototype.colorForSeries = function(index) {
return this.options.lineColors[index % this.options.lineColors.length];
};
return Line; return Line;
})(); })();

2
morris.min.js vendored

File diff suppressed because one or more lines are too long