Skip to content

Commit

Permalink
fixing charts with mixed (negative/positive) values
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Dec 5, 2016
1 parent 628408d commit 682ab0b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
10 changes: 10 additions & 0 deletions src/ui/public/vislib/lib/axis/axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ export default function AxisFactory(Private) {
return d.y;
})
.offset(this.axisConfig.get('scale.offset', 'zero'));

const stackedMode = ['normal', 'grouped'].includes(this.axisConfig.get('scale.mode'));
if (stackedMode) {
this.stack.out((d, y0, y) => {
return this._stackNegAndPosVals(d, y0, y);
});
}
}

/**
Expand Down Expand Up @@ -77,6 +84,7 @@ export default function AxisFactory(Private) {
* Calculates the d.y0 value for stacked data in D3.
*/
_calcYZero(y, arr) {
if (y === 0 && this._lastY0) return this._sumYs(arr, this._lastY0 > 0 ? this._isPositive : this._isNegative);
if (y >= 0) return this._sumYs(arr, this._isPositive);
return this._sumYs(arr, this._isNegative);
};
Expand Down Expand Up @@ -121,6 +129,8 @@ export default function AxisFactory(Private) {
}

d.y0 = this._calcYZero(y, this._cache.yValsArr);
if (d.y0 > 0) this._lastY0 = 1;
if (d.y0 < 0) this._lastY0 = -1;
++this._cache.index.stack;


Expand Down
3 changes: 3 additions & 0 deletions src/ui/public/vislib/lib/axis/axis_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ export default function AxisConfigFactory() {
offset = 'zero';
stacked = false;
break;
case SCALE_MODES.GROUPED:
offset = 'group';
break;
case SCALE_MODES.PERCENTAGE:
offset = 'expand';
break;
Expand Down
1 change: 1 addition & 0 deletions src/ui/public/vislib/lib/axis/scale_modes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const SCALE_MODES = {
PERCENTAGE: 'percentage',
WIGGLE: 'wiggle',
SILHOUETTE: 'silhouette',
GROUPED: 'grouped', // this should not be a scale mode but it is at this point to make it compatible with old charts
ALL: ['normal', 'percentage', 'wiggle', 'silhouette']
};

Expand Down
19 changes: 6 additions & 13 deletions src/ui/public/vislib/visualizations/point_series/column_chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export default function ColumnChartFactory(Private) {
const isTooltip = this.handler.visConfig.get('tooltip.show');

const layer = svg.append('g')
.attr('class', function (d, i) {
return `series series-${i}`;
})
.attr('class', 'series')
.attr('clip-path', 'url(#' + this.baseChart.clipPathId + ')');

const bars = layer.selectAll('rect')
Expand Down Expand Up @@ -110,25 +108,20 @@ export default function ColumnChartFactory(Private) {
}

function x(d) {
if (isTimeScale) {
return xScale(d.x) + barWidth * groupNum;
}
return xScale(d.x) + xScale.rangeBand() / groupCount * groupNum;
const groupPosition = isTimeScale ? barWidth * groupNum : xScale.rangeBand() / groupCount * groupNum;
return xScale(d.x) + groupPosition;
}

function y(d) {
if ((isHorizontal && d.y < 0) || (!isHorizontal && d.y > 0)) {
return yScale(0);
return yScale(d.y0);
}
if (!isHorizontal && d.y < 0) return yScale(d.y);
/*if (!isHorizontal && d.y < 0) return yScale(d.y);*/
return yScale(d.y0 + d.y);
}

function widthFunc() {
if (isTimeScale) {
return barWidth;
}
return xScale.rangeBand() / groupCount;
return isTimeScale ? barWidth : xScale.rangeBand() / groupCount;
}

function heightFunc(d) {
Expand Down

0 comments on commit 682ab0b

Please sign in to comment.