jesperct commented on code in PR #40173:
URL: https://github.com/apache/superset/pull/40173#discussion_r3274138262


##########
superset-frontend/plugins/plugin-chart-echarts/src/components/Echart.tsx:
##########
@@ -280,12 +280,37 @@ function Echart(
 
       const notMerge = !isDashboardRefreshing;
       chartRef.current?.dispatchAction({ type: 'hideTip' });
+      // setOption(notMerge:true) replaces the dataZoom config, dropping any
+      // range the user has engaged. Preserve it across the call.
+      const previousZoom = notMerge
+        ? (chartRef.current?.getOption() as any)?.dataZoom
+        : undefined;
       chartRef.current?.setOption(themedEchartOptions, {
         notMerge,
         replaceMerge: notMerge ? undefined : ['series'],
         // lazyUpdate defers render, causing tooltip crashes on stale shapes 
(#39247)
         lazyUpdate: false,
       });
+      if (previousZoom?.length) {
+        const batch = previousZoom
+          .map((dz: any, dataZoomIndex: number) => ({
+            dataZoomIndex,
+            start: dz.start,
+            end: dz.end,
+            startValue: dz.startValue,
+            endValue: dz.endValue,
+          }))

Review Comment:
   Plugins in this repo don't set `id` on dataZoom configs, so id-based 
addressing would mean changes across every plugin. Added a length-equality 
guard instead in 222533f: if the new option reshapes dataZoom, we skip restore. 
When shapes match, index-based restore is safe, which holds for the bug being 
fixed here (width-driven re-render keeps the dataZoom shape identical).



##########
superset-frontend/plugins/plugin-chart-echarts/src/components/Echart.tsx:
##########
@@ -280,12 +280,37 @@ function Echart(
 
       const notMerge = !isDashboardRefreshing;
       chartRef.current?.dispatchAction({ type: 'hideTip' });
+      // setOption(notMerge:true) replaces the dataZoom config, dropping any
+      // range the user has engaged. Preserve it across the call.
+      const previousZoom = notMerge
+        ? (chartRef.current?.getOption() as any)?.dataZoom
+        : undefined;
       chartRef.current?.setOption(themedEchartOptions, {
         notMerge,
         replaceMerge: notMerge ? undefined : ['series'],
         // lazyUpdate defers render, causing tooltip crashes on stale shapes 
(#39247)
         lazyUpdate: false,
       });
+      if (previousZoom?.length) {
+        const batch = previousZoom
+          .map((dz: any, dataZoomIndex: number) => ({
+            dataZoomIndex,
+            start: dz.start,
+            end: dz.end,
+            startValue: dz.startValue,
+            endValue: dz.endValue,
+          }))
+          .filter(
+            (b: any) =>
+              b.start !== undefined ||
+              b.end !== undefined ||
+              b.startValue !== undefined ||
+              b.endValue !== undefined,
+          );

Review Comment:
   Done in 222533f. Skip restore when the captured range is `start:0/end:100` 
with no value bounds.



##########
superset-frontend/plugins/plugin-chart-echarts/test/components/Echart.test.tsx:
##########
@@ -0,0 +1,132 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import type { EChartsCoreOption } from 'echarts/core';
+import { render, waitFor } from 'spec/helpers/testing-library';
+
+const setOption = jest.fn();
+const on = jest.fn();
+const off = jest.fn();
+const resize = jest.fn();
+const dispose = jest.fn();
+const dispatchAction = jest.fn();
+const getOption = jest.fn();
+
+const mockInstance = {
+  setOption,
+  on,
+  off,
+  resize,
+  dispose,
+  dispatchAction,
+  getOption,
+  getZr: () => ({ on: jest.fn(), off: jest.fn() }),
+};
+
+jest.mock('echarts/core', () => ({
+  __esModule: true,
+  use: jest.fn(),
+  init: jest.fn(() => mockInstance),
+  registerLocale: jest.fn(),
+}));
+jest.mock('echarts/charts', () => ({}));
+jest.mock('echarts/renderers', () => ({}));
+jest.mock('echarts/components', () => ({}));
+jest.mock('echarts/features', () => ({}));
+
+// eslint-disable-next-line import/first
+import Echart from '../../src/components/Echart';
+
+const renderEchart = (echartOptions: EChartsCoreOption) => {
+  const refs = { divRef: undefined };
+  return render(
+    <Echart
+      width={400}
+      height={300}
+      echartOptions={echartOptions}
+      refs={refs}
+    />,
+    { useRedux: true, useTheme: true },
+  );
+};
+
+beforeEach(() => {
+  setOption.mockClear();
+  on.mockClear();
+  off.mockClear();
+  resize.mockClear();
+  dispatchAction.mockClear();
+  getOption.mockReset();
+});
+
+test('preserves user dataZoom range across setOption(notMerge)', async () => {
+  // After the user has zoomed, ECharts reports the current dataZoom range
+  // via getOption().dataZoom. We expect Echart to capture this before
+  // setOption replaces the option payload, then restore it via dispatchAction.
+  getOption.mockReturnValue({
+    dataZoom: [{ start: 12, end: 48 }],
+  });
+
+  const { rerender } = renderEchart({ xAxis: {}, series: [] });
+
+  // Trigger another setOption call by changing the echartOptions reference
+  rerender(
+    <Echart
+      width={400}
+      height={300}
+      echartOptions={{ xAxis: {}, series: [{ type: 'line' }] }}
+      refs={{ divRef: undefined }}
+    />,
+  );
+
+  await waitFor(() =>
+    expect(dispatchAction).toHaveBeenCalledWith(
+      expect.objectContaining({
+        type: 'dataZoom',
+        batch: [
+          expect.objectContaining({ dataZoomIndex: 0, start: 12, end: 48 }),
+        ],
+      }),
+    ),
+  );
+});
+
+test('does not restore when no prior zoom range exists', async () => {
+  // Fresh chart with no engaged zoom: dataZoom config has no start/end.
+  getOption.mockReturnValue({
+    dataZoom: [{ type: 'slider', show: true }],
+  });

Review Comment:
   Added a dedicated test in 222533f proving we don't restore `start:0/end:100` 
defaults.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to