Skip to content

Commit

Permalink
fix(bar): negative values for stacked charts
Browse files Browse the repository at this point in the history
Fix #16

Inspired from morrisjs#533
  • Loading branch information
Pierre Clavequin committed May 15, 2020
1 parent d9bdefe commit 4ea2bcc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
10 changes: 6 additions & 4 deletions lib/morris.bar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ class Morris.Bar extends Morris.Grid
@data[idx].label_x = []
@data[idx].label_y = []
@seriesBars[idx] = []
lastTop = 0
lastTop = null
lastBottom = null
if @options.rightAxisBar is true
nb = row._y.length
else
Expand Down Expand Up @@ -284,9 +285,10 @@ class Morris.Bar extends Morris.Grid
@drawBar(@yStart, @xStart + idx * groupWidth, @ySize, groupWidth, @options.verticalGridColor, @options.verticalGridOpacity, @options.barRadius)


top -= lastTop if @options.stacked

if not @options.horizontal
lastTop += size
top += lastTop-bottom if @options.stacked and lastTop?
lastTop = top
if size == 0 && @options.showZero then size = 1
@seriesBars[idx][sidx] = @drawBar(left, top, barWidth, size, @colorFor(row, sidx, 'bar'),
@options.barOpacity, @options.barRadius)
Expand All @@ -300,7 +302,7 @@ class Morris.Bar extends Morris.Grid
@data[idx].label_y[sidx] = top+depth;

else
lastTop -= size
lastBottom = bottom
if size == 0 then size = 1
@seriesBars[idx][sidx] = @drawBar(top, left, size, barWidth, @colorFor(row, sidx, 'bar'),
@options.barOpacity, @options.barRadius)
Expand Down
5 changes: 4 additions & 1 deletion lib/morris.grid.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ class Morris.Grid extends Morris.EventEmitter
if idx < @options.ykeys.length - @options.nbYkeys2
if yval? and @hasToShow(idx)
if @cumulative
total += yval
if total < 0 and yval > 0
total = yval
else
total += yval
else
if ymax?
ymax = Math.max(yval, ymax)
Expand Down
21 changes: 13 additions & 8 deletions morris.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,11 @@ Licensed under the BSD-2-Clause License.
if (idx < this.options.ykeys.length - this.options.nbYkeys2) {
if ((yval != null) && this.hasToShow(idx)) {
if (this.cumulative) {
total += yval;
if (total < 0 && yval > 0) {
total = yval;
} else {
total += yval;
}
} else {
if (ymax != null) {
ymax = Math.max(yval, ymax);
Expand Down Expand Up @@ -2753,7 +2757,7 @@ Licensed under the BSD-2-Clause License.
};

Bar.prototype.drawSeries = function() {
var barMiddle, barWidth, bottom, depth, groupWidth, i, idx, lastTop, left, leftPadding, nb, numBars, row, sidx, size, spaceLeft, top, ypos, zeroPos, _i, _ref;
var barMiddle, barWidth, bottom, depth, groupWidth, i, idx, lastBottom, lastTop, left, leftPadding, nb, numBars, row, sidx, size, spaceLeft, top, ypos, zeroPos, _i, _ref;
this.seriesBars = [];
groupWidth = this.xSize / this.options.data.length;
if (this.options.stacked) {
Expand Down Expand Up @@ -2785,7 +2789,8 @@ Licensed under the BSD-2-Clause License.
this.data[idx].label_x = [];
this.data[idx].label_y = [];
this.seriesBars[idx] = [];
lastTop = 0;
lastTop = null;
lastBottom = null;
if (this.options.rightAxisBar === true) {
nb = row._y.length;
} else {
Expand Down Expand Up @@ -2825,11 +2830,11 @@ Licensed under the BSD-2-Clause License.
this.drawBar(this.yStart, this.xStart + idx * groupWidth, this.ySize, groupWidth, this.options.verticalGridColor, this.options.verticalGridOpacity, this.options.barRadius);
}
}
if (this.options.stacked) {
top -= lastTop;
}
if (!this.options.horizontal) {
lastTop += size;
if (this.options.stacked && (lastTop != null)) {
top += lastTop - bottom;
}
lastTop = top;
if (size === 0 && this.options.showZero) {
size = 1;
}
Expand All @@ -2850,7 +2855,7 @@ Licensed under the BSD-2-Clause License.
_results1.push(void 0);
}
} else {
lastTop -= size;
lastBottom = bottom;
if (size === 0) {
size = 1;
}
Expand Down
6 changes: 3 additions & 3 deletions morris.min.js

Large diffs are not rendered by default.

0 comments on commit 4ea2bcc

Please sign in to comment.