diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/chart_switch.test.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/chart_switch.test.tsx index 0a540334f2cad..1b60098fd45ad 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/chart_switch.test.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/chart_switch.test.tsx @@ -369,7 +369,7 @@ describe('chart_switch', () => { ); }); - it('should not remove layers if the visualization is not changing', () => { + it('should not remove layers when switching between subtypes', () => { const dispatch = jest.fn(); const frame = mockFrame(['a', 'b', 'c']); const visualizations = mockVisualizations(); @@ -397,6 +397,7 @@ describe('chart_switch', () => { initialState: 'therebedragons', }) ); + expect(frame.removeLayers).not.toHaveBeenCalled(); }); it('should switch to the updated datasource state', () => { diff --git a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/chart_switch.tsx b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/chart_switch.tsx index 8b67646edded9..dca6b3e7616d6 100644 --- a/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/chart_switch.tsx +++ b/x-pack/legacy/plugins/lens/public/editor_frame_plugin/editor_frame/chart_switch.tsx @@ -28,6 +28,7 @@ interface VisualizationSelection { dataLoss: 'nothing' | 'layers' | 'everything' | 'columns'; datasourceId?: string; datasourceState?: unknown; + sameDatasources?: boolean; } interface Props { @@ -89,7 +90,10 @@ export function ChartSwitch(props: Props) { 'SWITCH_VISUALIZATION' ); - if (!selection.datasourceId || selection.dataLoss === 'everything') { + if ( + (!selection.datasourceId && !selection.sameDatasources) || + selection.dataLoss === 'everything' + ) { props.framePublicAPI.removeLayers(Object.keys(props.framePublicAPI.datasourceLayers)); } }; @@ -109,6 +113,7 @@ export function ChartSwitch(props: Props) { dataLoss: 'nothing', keptLayerIds: Object.keys(props.framePublicAPI.datasourceLayers), getVisualizationState: () => switchVisType(subVisualizationId, props.visualizationState), + sameDatasources: true, }; }