Skip to content

Commit 5854ea4

Browse files
[Viz] legend duplicates percentile options when chart has both left & right Y axes (#113073) (#113169)
* [Viz] legend duplicates percentile options when chart has both left & right Y axes * Update comment for isPercentileIdEqualToSeriesId * Remove Dimension interface * Replace partial aspect with whole aspect value Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
1 parent 61b76ab commit 5854ea4

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

src/plugins/vis_types/xy/public/utils/accessors.test.ts

+39-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
* Side Public License, v 1.
77
*/
88

9-
import { COMPLEX_SPLIT_ACCESSOR, getComplexAccessor } from './accessors';
9+
import {
10+
COMPLEX_SPLIT_ACCESSOR,
11+
getComplexAccessor,
12+
isPercentileIdEqualToSeriesId,
13+
} from './accessors';
1014
import { BUCKET_TYPES } from '../../../../data/common';
1115
import { AccessorFn, Datum } from '@elastic/charts';
1216

@@ -99,3 +103,37 @@ describe('XY chart datum accessors', () => {
99103
expect(accessor).toBeUndefined();
100104
});
101105
});
106+
107+
describe('isPercentileIdEqualToSeriesId', () => {
108+
it('should be equal for plain column ids', () => {
109+
const seriesColumnId = 'col-0-1';
110+
const columnId = `${seriesColumnId}`;
111+
112+
const isEqual = isPercentileIdEqualToSeriesId(columnId, seriesColumnId);
113+
expect(isEqual).toBeTruthy();
114+
});
115+
116+
it('should be equal for column with percentile', () => {
117+
const seriesColumnId = '1';
118+
const columnId = `${seriesColumnId}.95`;
119+
120+
const isEqual = isPercentileIdEqualToSeriesId(columnId, seriesColumnId);
121+
expect(isEqual).toBeTruthy();
122+
});
123+
124+
it('should not be equal for column with percentile equal to seriesColumnId', () => {
125+
const seriesColumnId = '1';
126+
const columnId = `2.1`;
127+
128+
const isEqual = isPercentileIdEqualToSeriesId(columnId, seriesColumnId);
129+
expect(isEqual).toBeFalsy();
130+
});
131+
132+
it('should not be equal for column with percentile, where columnId contains seriesColumnId', () => {
133+
const seriesColumnId = '1';
134+
const columnId = `${seriesColumnId}2.1`;
135+
136+
const isEqual = isPercentileIdEqualToSeriesId(columnId, seriesColumnId);
137+
expect(isEqual).toBeFalsy();
138+
});
139+
});

src/plugins/vis_types/xy/public/utils/accessors.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import { AccessorFn, Accessor } from '@elastic/charts';
1010
import { BUCKET_TYPES } from '../../../../data/public';
1111
import { FakeParams } from '../../../../visualizations/public';
12-
import { Aspect } from '../types';
12+
import type { Aspect } from '../types';
1313

1414
export const COMPLEX_X_ACCESSOR = '__customXAccessor__';
1515
export const COMPLEX_SPLIT_ACCESSOR = '__complexSplitAccessor__';
@@ -77,3 +77,11 @@ export const getSplitSeriesAccessorFnMap = (
7777

7878
return m;
7979
};
80+
81+
// For percentile, the aggregation id is coming in the form %s.%d, where %s is agg_id and %d - percents
82+
export const isPercentileIdEqualToSeriesId = (columnId: number | string, seriesColumnId: string) =>
83+
columnId.toString().split('.')[0] === seriesColumnId;
84+
85+
export const isValidSeriesForDimension = (seriesColumnId: string, { aggId, accessor }: Aspect) =>
86+
(aggId === seriesColumnId || isPercentileIdEqualToSeriesId(aggId ?? '', seriesColumnId)) &&
87+
accessor !== null;

src/plugins/vis_types/xy/public/utils/render_all_series.tsx

+2-12
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import {
2222
} from '@elastic/charts';
2323

2424
import { DatatableRow } from '../../../../expressions/public';
25-
import { METRIC_TYPES } from '../../../../data/public';
2625

2726
import { ChartType } from '../../common';
2827
import { SeriesParam, VisConfig } from '../types';
28+
import { isValidSeriesForDimension } from './accessors';
2929

3030
/**
3131
* Matches vislib curve to elastic charts
@@ -82,17 +82,7 @@ export const renderAllSeries = (
8282
interpolate,
8383
type,
8484
}) => {
85-
const yAspects = aspects.y.filter(({ aggId, aggType, accessor }) => {
86-
if (
87-
aggType === METRIC_TYPES.PERCENTILES ||
88-
aggType === METRIC_TYPES.PERCENTILE_RANKS ||
89-
aggType === METRIC_TYPES.STD_DEV
90-
) {
91-
return aggId?.includes(paramId) && accessor !== null;
92-
} else {
93-
return aggId === paramId && accessor !== null;
94-
}
95-
});
85+
const yAspects = aspects.y.filter((aspect) => isValidSeriesForDimension(paramId, aspect));
9686
if (!show || !yAspects.length) {
9787
return null;
9888
}

0 commit comments

Comments
 (0)