Skip to content

Commit f83a60c

Browse files
authored
[Lens] Fixes the partition legend actions header format problem (#149114)
## Summary Closes #149019 This PR fixes 2 bugs that are related. - The first one is unreleased and is described on the issue. It happens only if you try to open the legend actions with a partition chart with custom interval breakdown. The problem was that the title we were giving on the ContextMenu was not a string but an Object - The second one exists on 8.6 (haven't checked the prior versions) and has to do with the title of the legend action popover. It was not formatted correctly so you can see a title like that: <img width="658" alt="image" src="https://user-images.githubusercontent.com/17003240/213172686-b8f1a4d1-c245-4211-bc2e-534d7ed6b473.png"> or <img width="400" alt="image" src="https://user-images.githubusercontent.com/17003240/213172759-015998fc-38d1-46fd-a6f8-294ca113442a.png"> This PR fixes both problems: <img width="824" alt="image" src="https://user-images.githubusercontent.com/17003240/213172863-d088359e-89ab-4d7e-9bdd-5d9d8181e482.png"> ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
1 parent e50da6f commit f83a60c

File tree

3 files changed

+60
-17
lines changed

3 files changed

+60
-17
lines changed

src/plugins/chart_expressions/expression_partition_vis/public/utils/filter_helpers.test.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
* Side Public License, v 1.
77
*/
88
import { Datatable, DatatableColumn } from '@kbn/expressions-plugin/public';
9-
import { getFilterClickData, getFilterEventData } from './filter_helpers';
10-
import { createMockBucketColumns, createMockVisData } from '../mocks';
9+
import { fieldFormatsMock } from '@kbn/field-formats-plugin/common/mocks';
10+
import { getFilterClickData, getFilterEventData, getFilterPopoverTitle } from './filter_helpers';
11+
import { createMockBucketColumns, createMockVisData, createMockPieParams } from '../mocks';
1112
import { consolidateMetricColumns } from '../../common/utils';
1213
import { LayerValue } from '@elastic/charts';
1314
import faker from 'faker';
1415

1516
const bucketColumns = createMockBucketColumns();
1617
const visData = createMockVisData();
18+
const visParams = createMockPieParams();
1719

1820
describe('getFilterClickData', () => {
1921
it('returns the correct filter data for the specific layer', () => {
@@ -264,3 +266,31 @@ describe('getFilterEventData', () => {
264266
expect(data[0].column).toEqual(0);
265267
});
266268
});
269+
270+
describe('getFilterPopoverTitle', () => {
271+
it('returns the series key if no buckets', () => {
272+
const series = {
273+
key: 'Kibana Airlines',
274+
specId: 'pie',
275+
};
276+
const newVisParams = {
277+
...visParams,
278+
buckets: [],
279+
};
280+
const defaultFormatter = jest.fn((...args) => fieldFormatsMock.deserialize(...args));
281+
282+
const title = getFilterPopoverTitle(newVisParams, visData, 0, defaultFormatter, series.key);
283+
expect(title).toBe('Kibana Airlines');
284+
});
285+
286+
it('calls the formatter if buckets given', () => {
287+
const series = {
288+
key: '0',
289+
specId: 'pie',
290+
};
291+
const defaultFormatter = jest.fn((...args) => fieldFormatsMock.deserialize(...args));
292+
293+
getFilterPopoverTitle(visParams, visData, 1, defaultFormatter, series.key);
294+
expect(defaultFormatter).toHaveBeenCalled();
295+
});
296+
});

src/plugins/chart_expressions/expression_partition_vis/public/utils/filter_helpers.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import { LayerValue, SeriesIdentifier } from '@elastic/charts';
1010
import { Datatable, DatatableColumn } from '@kbn/expressions-plugin/public';
1111
import { DataPublicPluginStart } from '@kbn/data-plugin/public';
1212
import { ValueClickContext } from '@kbn/embeddable-plugin/public';
13-
import type { FieldFormat } from '@kbn/field-formats-plugin/common';
14-
import { BucketColumns } from '../../common/types';
13+
import { getFormatByAccessor } from '@kbn/visualizations-plugin/common/utils';
14+
import type { FieldFormat, FormatFactory } from '@kbn/field-formats-plugin/common';
15+
import { BucketColumns, PartitionVisParams } from '../../common/types';
1516
import { FilterEvent } from '../types';
1617

1718
export const canFilter = async (
@@ -129,3 +130,20 @@ export const getFilterEventData = (
129130
export const getSeriesValueColumnIndex = (value: string, visData: Datatable): number => {
130131
return visData.columns.findIndex(({ id }) => !!visData.rows.find((r) => r[id] === value));
131132
};
133+
134+
export const getFilterPopoverTitle = (
135+
visParams: PartitionVisParams,
136+
visData: Datatable,
137+
columnIndex: number,
138+
formatter: FormatFactory,
139+
seriesKey: string
140+
) => {
141+
let formattedTitle = '';
142+
if (visParams.dimensions.buckets) {
143+
const accessor = visParams.dimensions.buckets[columnIndex];
144+
formattedTitle = accessor
145+
? formatter(getFormatByAccessor(accessor, visData.columns)).convert(seriesKey)
146+
: '';
147+
}
148+
return formattedTitle || seriesKey;
149+
};

src/plugins/chart_expressions/expression_partition_vis/public/utils/get_legend_actions.tsx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ import { EuiContextMenuPanelDescriptor, EuiIcon, EuiPopover, EuiContextMenu } fr
1313
import { LegendAction, SeriesIdentifier, useLegendAction } from '@elastic/charts';
1414
import { DataPublicPluginStart } from '@kbn/data-plugin/public';
1515
import { Datatable } from '@kbn/expressions-plugin/public';
16-
import { getFormatByAccessor, getAccessor } from '@kbn/visualizations-plugin/common/utils';
1716
import { FieldFormatsStart } from '@kbn/field-formats-plugin/public';
1817
import { PartitionVisParams } from '../../common/types';
1918
import { ColumnCellValueActions, FilterEvent } from '../types';
20-
import { getSeriesValueColumnIndex } from './filter_helpers';
19+
import { getSeriesValueColumnIndex, getFilterPopoverTitle } from './filter_helpers';
2120

2221
export const getLegendActions = (
2322
canFilter: (
@@ -50,17 +49,13 @@ export const getLegendActions = (
5049
return null;
5150
}
5251

53-
let formattedTitle = '';
54-
if (visParams.dimensions.buckets) {
55-
const accessor = visParams.dimensions.buckets.find(
56-
(bucket) => getAccessor(bucket) === columnIndex
57-
);
58-
formattedTitle =
59-
formatter
60-
.deserialize(accessor ? getFormatByAccessor(accessor, visData.columns) : undefined)
61-
.convert(pieSeries.key) ?? '';
62-
}
63-
const title = formattedTitle || pieSeries.key;
52+
const title = getFilterPopoverTitle(
53+
visParams,
54+
visData,
55+
columnIndex,
56+
formatter.deserialize,
57+
pieSeries.key
58+
);
6459

6560
const panelItems: EuiContextMenuPanelDescriptor['items'] = [];
6661

0 commit comments

Comments
 (0)