[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:
parent
36bab568f4
commit
7b6ac0e658
|
@ -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(
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue