diff --git a/x-pack/plugins/lens/public/visualizations/xy/visualization.test.tsx b/x-pack/plugins/lens/public/visualizations/xy/visualization.test.tsx index 012823831b8eb..e2c6ce25bd2e3 100644 --- a/x-pack/plugins/lens/public/visualizations/xy/visualization.test.tsx +++ b/x-pack/plugins/lens/public/visualizations/xy/visualization.test.tsx @@ -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'); + }); + }); }); diff --git a/x-pack/plugins/lens/public/visualizations/xy/visualization.tsx b/x-pack/plugins/lens/public/visualizations/xy/visualization.tsx index 64a2ad4fc2754..6f17a2253a35e 100644 --- a/x-pack/plugins/lens/public/visualizations/xy/visualization.tsx +++ b/x-pack/plugins/lens/public/visualizations/xy/visualization.tsx @@ -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) );