mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
# Backport This will backport the following commits from `main` to `8.x`: - [[Lens] Fix switchVisualizationType to use it without layerId (#196295)](https://github.com/elastic/kibana/pull/196295) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Marta Bondyra","email":"4283304+mbondyra@users.noreply.github.com"},"sourceCommit":{"committedDate":"2024-10-15T21:53:00Z","message":"[Lens] Fix switchVisualizationType to use it without layerId (#196295)\n\nWith [PR #187475](https://github.com/elastic/kibana/pull/187475/files)\r\nwe introduced a bug, affecting the AI assistant's suggestions API when\r\nswitching between different chart types (e.g., from bar to line chart).\r\nThis feature relies on the switchVisualizationType method, which was\r\nchanged to require a `layerId`. However, the suggestions API does not\r\nprovide `layerId`, causing the chart type to not switch as expected.\r\n\r\nSolution:\r\nThe bug can be resolved by modifying the logic in the\r\n`switchVisualizationType` method. We changed:\r\n\r\n```ts\r\nconst dataLayer = state.layers.find((l) => l.layerId === layerId);\r\n```\r\n\r\nto:\r\n\r\n```ts\r\nconst dataLayer = state.layers.find((l) => l.layerId === layerId) ?? state.layers[0];\r\n```\r\n\r\nThis ensures that the suggestions API falls back to the first layer if\r\nno specific layerId is provided.\r\n\r\n---------\r\n\r\nCo-authored-by: Marco Vettorello <vettorello.marco@gmail.com>","sha":"e4762201fdd84f372c78bc2a159061e504b26e78","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Visualizations","release_note:skip","Feature:Lens","v9.0.0","backport:prev-minor"],"title":"[Lens] Fix switchVisualizationType to use it without layerId","number":196295,"url":"https://github.com/elastic/kibana/pull/196295","mergeCommit":{"message":"[Lens] Fix switchVisualizationType to use it without layerId (#196295)\n\nWith [PR #187475](https://github.com/elastic/kibana/pull/187475/files)\r\nwe introduced a bug, affecting the AI assistant's suggestions API when\r\nswitching between different chart types (e.g., from bar to line chart).\r\nThis feature relies on the switchVisualizationType method, which was\r\nchanged to require a `layerId`. However, the suggestions API does not\r\nprovide `layerId`, causing the chart type to not switch as expected.\r\n\r\nSolution:\r\nThe bug can be resolved by modifying the logic in the\r\n`switchVisualizationType` method. We changed:\r\n\r\n```ts\r\nconst dataLayer = state.layers.find((l) => l.layerId === layerId);\r\n```\r\n\r\nto:\r\n\r\n```ts\r\nconst dataLayer = state.layers.find((l) => l.layerId === layerId) ?? state.layers[0];\r\n```\r\n\r\nThis ensures that the suggestions API falls back to the first layer if\r\nno specific layerId is provided.\r\n\r\n---------\r\n\r\nCo-authored-by: Marco Vettorello <vettorello.marco@gmail.com>","sha":"e4762201fdd84f372c78bc2a159061e504b26e78"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/196295","number":196295,"mergeCommit":{"message":"[Lens] Fix switchVisualizationType to use it without layerId (#196295)\n\nWith [PR #187475](https://github.com/elastic/kibana/pull/187475/files)\r\nwe introduced a bug, affecting the AI assistant's suggestions API when\r\nswitching between different chart types (e.g., from bar to line chart).\r\nThis feature relies on the switchVisualizationType method, which was\r\nchanged to require a `layerId`. However, the suggestions API does not\r\nprovide `layerId`, causing the chart type to not switch as expected.\r\n\r\nSolution:\r\nThe bug can be resolved by modifying the logic in the\r\n`switchVisualizationType` method. We changed:\r\n\r\n```ts\r\nconst dataLayer = state.layers.find((l) => l.layerId === layerId);\r\n```\r\n\r\nto:\r\n\r\n```ts\r\nconst dataLayer = state.layers.find((l) => l.layerId === layerId) ?? state.layers[0];\r\n```\r\n\r\nThis ensures that the suggestions API falls back to the first layer if\r\nno specific layerId is provided.\r\n\r\n---------\r\n\r\nCo-authored-by: Marco Vettorello <vettorello.marco@gmail.com>","sha":"e4762201fdd84f372c78bc2a159061e504b26e78"}}]}] BACKPORT--> Co-authored-by: Marta Bondyra <4283304+mbondyra@users.noreply.github.com>
This commit is contained in:
parent
75f078b7f2
commit
9513867b11
2 changed files with 29 additions and 2 deletions
|
@ -4214,4 +4214,30 @@ describe('xy_visualization', () => {
|
|||
`);
|
||||
});
|
||||
});
|
||||
describe('switchVisualizationType', () => {
|
||||
it('should switch all the layers to the new visualization type if layerId is not specified (AI assistant case)', () => {
|
||||
const state = exampleState();
|
||||
state.layers[1] = state.layers[0];
|
||||
state.layers[1].layerId = 'second';
|
||||
state.layers[2] = state.layers[0];
|
||||
state.layers[2].layerId = 'third';
|
||||
const newType = 'bar';
|
||||
const newState = xyVisualization.switchVisualizationType!(newType, state);
|
||||
expect((newState.layers[0] as XYDataLayerConfig).seriesType).toEqual(newType);
|
||||
expect((newState.layers[1] as XYDataLayerConfig).seriesType).toEqual(newType);
|
||||
expect((newState.layers[2] as XYDataLayerConfig).seriesType).toEqual(newType);
|
||||
});
|
||||
it('should switch only the second layer to the new visualization type if layerId is specified (chart switch case)', () => {
|
||||
const state = exampleState();
|
||||
state.layers[1] = { ...state.layers[0] };
|
||||
state.layers[1].layerId = 'second';
|
||||
state.layers[2] = { ...state.layers[0] };
|
||||
state.layers[2].layerId = 'third';
|
||||
const newType = 'bar';
|
||||
const newState = xyVisualization.switchVisualizationType!(newType, state, 'first');
|
||||
expect((newState.layers[0] as XYDataLayerConfig).seriesType).toEqual(newType);
|
||||
expect((newState.layers[1] as XYDataLayerConfig).seriesType).toEqual('area');
|
||||
expect((newState.layers[2] as XYDataLayerConfig).seriesType).toEqual('area');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -263,14 +263,15 @@ export const getXyVisualization = ({
|
|||
getDescription,
|
||||
|
||||
switchVisualizationType(seriesType: string, state: State, layerId?: string) {
|
||||
const dataLayer = state.layers.find((l) => l.layerId === layerId);
|
||||
const dataLayer = layerId
|
||||
? state.layers.find((l) => l.layerId === layerId)
|
||||
: state.layers.at(0);
|
||||
if (dataLayer && !isDataLayer(dataLayer)) {
|
||||
throw new Error('Cannot switch series type for non-data layer');
|
||||
}
|
||||
if (!dataLayer) {
|
||||
return state;
|
||||
}
|
||||
// todo: test how they switch between percentage etc
|
||||
const currentStackingType = stackingTypes.find(({ subtypes }) =>
|
||||
subtypes.includes(dataLayer.seriesType)
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue