[Lens] Reference lines: fix default value for Interval axis (#117847)

* 🐛 Fix the xAccessor computation

*  Add test for fix

* 👌 Integrated feedback

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Marco Liberati 2021-11-10 15:12:23 +01:00 committed by GitHub
parent 36bab568f4
commit 7b6ac0e658
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 6 deletions

View file

@ -459,6 +459,34 @@ describe('reference_line helpers', () => {
).toEqual({ min: 0, max: 375 });
});
it('should compute the correct value for a histogram on stacked chart for the xAccessor', () => {
for (const seriesType of ['bar_stacked', 'bar_horizontal_stacked', 'area_stacked'])
expect(
computeOverallDataDomain(
[
{ layerId: 'id-a', seriesType, accessors: ['c'] },
{ layerId: 'id-b', seriesType, accessors: ['f'] },
] as XYLayerConfig[],
['c', 'f'],
getActiveData([
{
id: 'id-a',
rows: Array(3)
.fill(1)
.map((_, i) => ({ a: 50 * i, b: 100 * i, c: i })),
},
{
id: 'id-b',
rows: Array(3)
.fill(1)
.map((_, i) => ({ d: 25 * (i + 1), e: i % 2 ? 100 : null, f: i })),
},
]),
false // this will avoid the stacking behaviour
)
).toEqual({ min: 0, max: 2 });
});
it('should compute the correct value for a histogram non-stacked chart', () => {
for (const seriesType of ['bar', 'bar_horizontal', 'line', 'area'])
expect(

View file

@ -109,7 +109,8 @@ export function getStaticValue(
filteredLayers,
accessors,
activeData,
groupId !== 'x' // histogram axis should compute the min based on the current data
groupId !== 'x', // histogram axis should compute the min based on the current data
groupId !== 'x'
) || fallbackValue
);
}
@ -152,13 +153,15 @@ function getAccessorCriteriaForGroup(
export function computeOverallDataDomain(
dataLayers: Array<Pick<XYLayerConfig, 'seriesType' | 'accessors' | 'xAccessor' | 'layerId'>>,
accessorIds: string[],
activeData: NonNullable<FramePublicAPI['activeData']>
activeData: NonNullable<FramePublicAPI['activeData']>,
allowStacking: boolean = true
) {
const accessorMap = new Set(accessorIds);
let min: number | undefined;
let max: number | undefined;
const [stacked, unstacked] = partition(dataLayers, ({ seriesType }) =>
isStackedChart(seriesType)
const [stacked, unstacked] = partition(
dataLayers,
({ seriesType }) => isStackedChart(seriesType) && allowStacking
);
for (const { layerId, accessors } of unstacked) {
const table = activeData[layerId];
@ -215,7 +218,8 @@ function computeStaticValueForGroup(
dataLayers: Array<Pick<XYLayerConfig, 'seriesType' | 'accessors' | 'xAccessor' | 'layerId'>>,
accessorIds: string[],
activeData: NonNullable<FramePublicAPI['activeData']>,
minZeroOrNegativeBase: boolean = true
minZeroOrNegativeBase: boolean = true,
allowStacking: boolean = true
) {
const defaultReferenceLineFactor = 3 / 4;
@ -224,7 +228,12 @@ function computeStaticValueForGroup(
return defaultReferenceLineFactor;
}
const { min, max } = computeOverallDataDomain(dataLayers, accessorIds, activeData);
const { min, max } = computeOverallDataDomain(
dataLayers,
accessorIds,
activeData,
allowStacking
);
if (min != null && max != null && isFinite(min) && isFinite(max)) {
// Custom axis bounds can go below 0, so consider also lower values than 0