Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 99 additions & 22 deletions src/layout/barGrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ import {
import { BaseBarSeriesSubType, getStartValue, requireAxisStatisticsForBaseBar } from './barCommon';
import { COORD_SYS_TYPE_CARTESIAN_2D } from '../coord/cartesian/GridModel';
import { createBandWidthBasedAxisContainShapeHandler, makeAxisStatKey2 } from '../chart/helper/axisSnippets';
import { findStackedOnItem } from '../processor/dataStack';
import type SeriesData from '../data/SeriesData';
import type SeriesModel from '../model/Series';


const callOnlyOnce = makeCallOnlyOnce();
Expand Down Expand Up @@ -405,6 +408,7 @@ export function createProgressiveLayout(seriesType: string): StageHandler {

const isLarge = isInLargeMode(seriesModel);
const barMinHeight = seriesModel.get('barMinHeight') || 0;
const stackedOnBarMinHeight = stacked && hasStackedOnBarMinHeight(seriesType, data);

const stackedDimIdx = stackResultDim && data.getDimensionIndex(stackResultDim);

Expand All @@ -431,11 +435,17 @@ export function createProgressiveLayout(seriesType: string): StageHandler {
const baseValue = store.get(baseDimIdx, dataIndex) as number;
let baseCoord = valueAxisStart;
let stackStartValue;
let barMinHeightOffset = 0;

// Because of the barMinHeight, we can not use the value in
// stackResultDimension directly.
if (stacked) {
stackStartValue = +value - (store.get(valueDimIdx, dataIndex) as number);
if (stackedOnBarMinHeight) {
barMinHeightOffset = getStackedBarMinHeightOffset(
seriesType, data, data.getRawIndex(dataIndex)
);
}
}

let x;
Expand All @@ -446,38 +456,26 @@ export function createProgressiveLayout(seriesType: string): StageHandler {
if (isValueAxisH) {
const coord = cartesian.dataToPoint([value, baseValue]);
if (stacked) {
const stackStartCoord = cartesian.dataToPoint([stackStartValue, baseValue])[0];
// On a log axis, a `stackStartValue` that is out of the log domain
// (e.g., 0, resulting from stacking over a null value) maps to a
// non-finite coordinate. Draw the bar from the axis start in that
// case, as if it were the first bar in the stack.
baseCoord = isFinite(stackStartCoord) ? stackStartCoord : valueAxisStart;
baseCoord = getStackStartCoord(
cartesian, isValueAxisH, valueAxisStart, stackStartValue, baseValue
);
}
x = baseCoord;
x = baseCoord + barMinHeightOffset;
y = coord[1] + columnOffset;
width = coord[0] - baseCoord;
width = applyBarMinHeight(coord[0] - baseCoord, barMinHeight, isValueAxisH);
height = columnWidth;

if (mathAbs(width) < barMinHeight) {
width = (width < 0 ? -1 : 1) * barMinHeight;
}
}
else {
const coord = cartesian.dataToPoint([baseValue, value]);
if (stacked) {
const stackStartCoord = cartesian.dataToPoint([baseValue, stackStartValue])[1];
// See the comment in the horizontal case above.
baseCoord = isFinite(stackStartCoord) ? stackStartCoord : valueAxisStart;
baseCoord = getStackStartCoord(
cartesian, isValueAxisH, valueAxisStart, stackStartValue, baseValue
);
}
x = coord[0] + columnOffset;
y = baseCoord;
y = baseCoord + barMinHeightOffset;
width = columnWidth;
height = coord[1] - baseCoord;

if (mathAbs(height) < barMinHeight) {
// Include zero to has a positive bar
height = (height <= 0 ? -1 : 1) * barMinHeight;
}
height = applyBarMinHeight(coord[1] - baseCoord, barMinHeight, isValueAxisH);
}

if (!isLarge) {
Expand Down Expand Up @@ -518,6 +516,85 @@ function isInLargeMode(seriesModel: BaseBarSeriesModel) {
return seriesModel.pipelineContext && seriesModel.pipelineContext.large;
}

function getStackStartCoord(
cartesian: Cartesian2D,
isValueAxisH: boolean,
valueAxisStart: number,
stackStartValue: number,
baseValue: number
): number {
const stackStartCoord = isValueAxisH
? cartesian.dataToPoint([stackStartValue, baseValue])[0]
: cartesian.dataToPoint([baseValue, stackStartValue])[1];
// On a log axis, a `stackStartValue` that is out of the log domain
// (e.g., 0, resulting from stacking over a null value) maps to a
// non-finite coordinate. Draw the bar from the axis start in that
// case, as if it were the first bar in the stack.
return isFinite(stackStartCoord) ? stackStartCoord : valueAxisStart;
}

function applyBarMinHeight(size: number, barMinHeight: number, isValueAxisH: boolean): number {
if (mathAbs(size) < barMinHeight) {
// Include zero to has a positive bar
return (isValueAxisH ? (size < 0 ? -1 : 1) : (size <= 0 ? -1 : 1)) * barMinHeight;
}
return size;
}

function hasStackedOnBarMinHeight(seriesType: string, data: SeriesData): boolean {
let stackedOnSeries = data.getCalculationInfo('stackedOnSeries') as SeriesModel;
while (stackedOnSeries) {
if (stackedOnSeries.subType === seriesType && (stackedOnSeries as BaseBarSeriesModel).get('barMinHeight')) {
return true;
}
stackedOnSeries = stackedOnSeries.getData().getCalculationInfo('stackedOnSeries');
}
return false;
}

function getStackedBarMinHeightOffset(seriesType: string, data: SeriesData, rawIndex: number): number {
let offset = 0;
let stackedOnItem = findStackedOnItem(data, rawIndex);
while (stackedOnItem) {
const stackedOnSeries = stackedOnItem.data.hostModel as BaseBarSeriesModel;
if (stackedOnSeries.subType !== seriesType || !isCartesian2DInjectedAsDataCoordSys(stackedOnSeries)) {
break;
}
offset += getBarMinHeightExtension(stackedOnSeries, stackedOnItem.rawIndex);
stackedOnItem = findStackedOnItem(stackedOnItem.data, stackedOnItem.rawIndex);
}
return offset;
}

function getBarMinHeightExtension(seriesModel: BaseBarSeriesModel, rawIndex: number): number {
const barMinHeight = seriesModel.get('barMinHeight') || 0;
if (!barMinHeight) {
return 0;
}
const data = seriesModel.getData();
const cartesian = seriesModel.coordinateSystem as Cartesian2D;
const baseAxis = cartesian.getBaseAxis();
const valueAxis = cartesian.getOtherAxis(baseAxis);
const isValueAxisH = valueAxis.isHorizontal();
const valueDim = data.mapDimension(valueAxis.dim);
const valueAxisStart = valueAxis.toGlobalCoord(valueAxis.dataToCoord(getStartValue(valueAxis)));
const baseValue = data.getByRawIndex(data.mapDimension(baseAxis.dim), rawIndex) as number;
let value = data.getByRawIndex(valueDim, rawIndex) as number;
let baseCoord = valueAxisStart;

if (isDimensionStacked(data, valueDim) && data.getCalculationInfo('stackedOnSeries')) {
const stackResult = data.getByRawIndex(data.getCalculationInfo('stackResultDimension'), rawIndex) as number;
baseCoord = getStackStartCoord(cartesian, isValueAxisH, valueAxisStart, stackResult - value, baseValue);
value = stackResult;
}

const size = (isValueAxisH
? cartesian.dataToPoint([value, baseValue])[0]
: cartesian.dataToPoint([baseValue, value])[1]
) - baseCoord;
return applyBarMinHeight(size, barMinHeight, isValueAxisH) - size;
}

function barGridCreateAxisContainShapeHandler(seriesType: BaseBarSeriesSubType): AxisContainShapeHandler {
// See also #6728, #4862, `test/bar-overflow-time-plot.html` `test/bar-overflow-plot2.html`
// NOTE:
Expand Down
65 changes: 54 additions & 11 deletions src/processor/dataStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,7 @@ function calculateStack(stackInfoList: StackInfo[]) {
) as number;

// Considering positive stack, negative stack and empty data
if (
// A null/NaN value in a lower series must be excluded from the
// stack, otherwise it corrupts the sum. The other strategies
// already reject it via their `val > 0`/`val < 0` checks, but
// `all` needs an explicit guard. See #21685.
(stackStrategy === 'all' && !isNaN(val)) // single stack group
|| (stackStrategy === 'positive' && val > 0)
|| (stackStrategy === 'negative' && val < 0)
|| (stackStrategy === 'samesign' && sum >= 0 && val > 0) // All positive stack
|| (stackStrategy === 'samesign' && sum <= 0 && val < 0) // All negative stack
) {
if (isStackedOn(stackStrategy, sum, val)) {
// The sum has to be very small to be affected by the
// floating arithmetic problem. An incorrect result will probably
// cause axis min/max to be filtered incorrectly.
Expand All @@ -177,3 +167,56 @@ function calculateStack(stackInfoList: StackInfo[]) {
});
});
}

function isStackedOn(
stackStrategy: SeriesStackOptionMixin['stackStrategy'],
sum: number,
val: number
): boolean {
return (
// A null/NaN value in a lower series must be excluded from the
// stack, otherwise it corrupts the sum. The other strategies
// already reject it via their `val > 0`/`val < 0` checks, but
// `all` needs an explicit guard. See #21685.
(stackStrategy === 'all' && !isNaN(val)) // single stack group
|| (stackStrategy === 'positive' && val > 0)
|| (stackStrategy === 'negative' && val < 0)
|| (stackStrategy === 'samesign' && sum >= 0 && val > 0) // All positive stack
|| (stackStrategy === 'samesign' && sum <= 0 && val < 0) // All negative stack
);
}

export function findStackedOnItem(
data: SeriesData,
rawIndex: number
): { data: SeriesData, rawIndex: number } | null {
const sum = data.getByRawIndex(data.getCalculationInfo('stackedDimension'), rawIndex) as number;
if (isNaN(sum)) {
return null;
}
const isStackedByIndex = data.getCalculationInfo('isStackedByIndex');
const byValue = isStackedByIndex
? null
: data.getByRawIndex(data.getCalculationInfo('stackedByDimension'), rawIndex) as number;
const stackStrategy = (data.hostModel as SeriesModel<SeriesOption & SeriesStackOptionMixin>)
.get('stackStrategy') || 'samesign';

let stackedOnSeries = data.getCalculationInfo('stackedOnSeries') as SeriesModel;
while (stackedOnSeries) {
const stackedOnData = stackedOnSeries.getData();
const stackedOnRawIndex = isStackedByIndex
? rawIndex
: stackedOnData.rawIndexOf(stackedOnData.getCalculationInfo('stackedByDimension'), byValue);
if (stackedOnRawIndex >= 0 && isStackedOn(
stackStrategy,
sum,
stackedOnData.getByRawIndex(
stackedOnData.getCalculationInfo('stackResultDimension'), stackedOnRawIndex
) as number
)) {
return { data: stackedOnData, rawIndex: stackedOnRawIndex };
}
stackedOnSeries = stackedOnData.getCalculationInfo('stackedOnSeries');
}
return null;
}
Loading