Skip to content

fix(echarts): keep zoom, legend and brush over the theme switch; statistic labels by line height - #236

Merged
eizedev merged 5 commits into
mainfrom
fix/echarts-theme-zoom-marklines
Nov 25, 2026
Merged

eizedev merged 5 commits into
mainfrom
fix/echarts-theme-zoom-marklines

Conversation

@eizedev

@eizedev eizedev commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Summary

Two bugs in the ECharts module that I measured on PSU main during #234.

1. Zoom, legend selection and brush were lost on PSU's light/dark switch. The comment in EChart.jsx said they stay. They did not, for two reasons:

  • ECharts 6.1's setTheme calls GlobalModel.setTheme, which runs _resetOption('recreate'). The chart is built again from its option, and the state that actions set is dropped. I reproduced this with ECharts' server-side rendering: 40–60 becomes 0–100, and the legend selection is empty.
  • On the first switch after a page loads, PSU mounts the component again: a new element and a new ECharts instance with the same id. The old chart is disposed while data-theme still has the old value. PSU sets the new value in the same task.

The new Components/viewState.js reads the view (zoom per dataZoom, the selection of all legends, the brush areas) and sets it again with silent actions, so -OnZoom, -OnSelect and -OnLegendChange do not fire:

  • around setTheme (setThemeKeepingView), and
  • across the remount: the unmounted chart hands its view over by id, and a chart with the same id mounted in the same task takes it after its first setOption. The view is handed over only when the component unmounts (useUnmounting). A chart made again for another renderer or locale starts as before.

2. -MarkLine statistic labels overlapped. markLineOf placed series 0 at insideEndTop and series 1 at insideEndBottom. When the second line was higher, the labels faced each other. The side now follows where the lines are drawn. The highest line's label goes above, the lowest one's below, and one in between faces away from its nearer neighbour. Each statistic is ranked on its own, among the series on the same value axis. A stacked series counts with its stacked values, as ECharts' markerHelper.js computes them. A line with no other line of its statistic on its axis keeps the series' position, as before.

The showcase's workaround from #234 (insideStartBottom for the previous period) stays for now. The demo requires ECharts 0.3.1, which is already released and does not have this fix. With the new module, the item-level position wins and the workaround has no effect. With 0.3.1 it still keeps the labels apart. It can go once the demo requires the next ECharts release.

Whole-change check

I read each touched file and function whole.

File / function Its job now Extracted or reused Why the rest fits
viewState.js (new) Read and restore the view of a chart (readView, applyView, setThemeKeepingView); hand it over across a remount (handOverView, takeHandedOverView). Plain JS without an echarts import, so it can be tested with a fake chart and with the real ECharts. One job: the view over a theme change.
EChart.jsx, EChart Same component. The theme effect uses setThemeKeepingView. The init effect takes and hands over the view. The option effect applies a handed-over view once. Two jobs moved out into hooks next to toggleFullscreen: useFullscreen (the full-screen listener, unchanged) and useUnmounting (the new flag). EChart is 3 lines shorter than its baseline (251 against 254). The comment that promised zoom and legend over a theme change is now true and names what survives.
optionParts.js, markLineOf Builds the mark lines. Labels at insideEndTop. The index-based side and its seriesIndex parameter are gone. Polar's call never passed an index.
optionParts.js, addStatisticLines, placeStatisticLabels, labelSide, drawnValues, pointOf Statistic lines for every series of a cartesian chart, and their label side by drawn value. Extracted from cartesian(), which is 4 lines shorter. Reuses AGGREGATES, quantile and num. Small functions, each with one job.
cartesian.js, cartesian Same; one call instead of the loop. – –
Docs themes.md, lines-and-time.md One sentence each on what the reader sees now. – Placed where the reader of theme or mark lines looks.

Growth guard: javascript reports EChart and cartesian smaller than the baseline. I could not lower it: node build/growth.js --update was blocked in my session. That CI step stays red until someone runs it on this branch.

Not changed, worth a follow-up (it existed before, but now it also affects the label ranking): with -Band, the band series are put in front of the series list. The series.slice(1) loop therefore gives statistic lines to band and trend helper series and overwrites a trend's own mark line. Those helper series now also take part in the ranking.

Neutral review

A read-only reviewer (its own worktree, brief: "behaviour must not change except the two fixes") reviewed all commits up to a38a3c0.

  1. Medium: a lone statistic line got an item label, so a series-level markLine.label.position from -Options stopped applying even for one series. Fixed: groups with one line are left alone. A test covers this.
  2. Medium: without the mode check, any remount of the same id in the same task gets the view, possibly also a New-UDDynamic refresh of a chart with a fixed -Id. Kept and open: the mode check had to go, because PSU remounts before it changes data-theme. A chart without -Id gets a new id on every server render, so it is not affected. Whether a refresh of a chart with a fixed -Id remounts it is not checked.
  3. Low/medium: the unmount flag was never reset, so after React's trial unmount in StrictMode every re-creation would hand over. Fixed (useUnmounting sets it to false on mount).
  4. Low: the viewState.js header still described the theme-only case. Fixed.
  5. Low: ECharts rounds the drawn statistic to the data's precision, so two averages a fraction apart can be drawn on one line, in either order. The reviewer also confirmed by rendering that the ranking matches the drawn order for two series, stacked series with negatives, nulls and horizontal bars. Kept.
  6. Low: the tests compared the ranking with hand-computed values only. Fixed: a test draws two close averages with the module's ECharts (vertical and horizontal) and checks that the label boxes do not overlap. It fails on main with "line: labels overlap".
  7. Note: the tests that use the module's ECharts are skipped in CI's javascript job, which installs only the root packages (same pattern as the pivot tests). A CI step that runs them in the ECharts build job would be a follow-up.

Checked on PowerShell Universal

PSU 2026.3.4. The ECharts module built from this branch at a38a3c0 (index.9d997e19…), the three apps that use it restarted, Chromium. /echarts-demo is the module's app.

Point Result
Cause, on main First switch after load: new element and instance, zoom 0–100. Later switches: same instance, still 0–100 (setTheme).
Zoom 40–60, then three theme switches (first one remounts) 40-60 40-60 after each switch on "Opened per day" (time axis), and 40-60 on the period chart.
Legend deselection {"P1":false,"Opened":true} and {"This period":false,…} after each switch.
Brush The range [1793491200000,1793836800000] after each switch.
Events during the switches 0 POSTs.
Ticket report averages, second line higher The period chart: "Previous period" (16.3) at insideEndTop, "This period" (16.17) at insideEndBottom. Screenshots light at 1400 px and dark at 390 px: both labels readable, apart.
psu.spec against /echarts-demo 16 passed (Chromium).
A first build at 1eb0849 Every chart on the ticket page failed ("Cannot read properties of undefined (reading 'getComponent')"): the theme effect runs before the first setOption. Fixed in 7eea5e9, with a test.

Not checked on PSU: the last commit 524015f (lone lines keep their position, the StrictMode reset, the hook extraction). The test window on the instance had ended. Its effects are covered by the unit tests and the rendering test above.

Afterwards I deployed the build of main (index.7ffb21e4…) again and restarted the three apps. /echarts-demo/tickets, /echarts and /echarts-smoke load their charts without render errors.

Checklist

  • Linked issue
  • A fix comes with a test that fails without it; no test weakened, skipped or deleted
  • The pages in docs/ and the module's CHANGELOG.md describe the new state
  • A visible change: checked on a PSU instance in light and dark theme, narrow and wide window, with the events it fires (PSU version: 2026.3.4)

Test plan

  • npm run lint (0 problems), npm test (236 tests, 230 pass, 0 fail, 6 skipped), npm run growth (only "smaller than the baseline", see above), Invoke-Pester -Path tests/powershell (376 passed, 0 failed, 11 skipped), Invoke-ScriptAnalysis.ps1 (exit 0), Build-Module.ps1 for ECharts and Test-BuiltModule.ps1, Build-Site.ps1 and the Zensical build ("No issues found"), npm run test:browser -- --project=chromium (29 passed, 71 skipped)
  • PSU checks as above

🤖 Generated with Claude Code

eizedev and others added 5 commits November 25, 2026 23:52
…statistic labels by height

ECharts' setTheme builds the chart from its option again and drops the
zoom, the legend selection and the brush; PSU also mounts the component
again on the first light/dark switch after a page loads. viewState.js
reads the view before and sets it again with silent actions, around
setTheme and across that remount (same id, other mode, same task).

markLine statistic labels were placed above or below by series index,
so close lines overlapped when the second was higher. They now follow
the drawn values: the highest line's label above, the lowest's below,
one in between away from its nearer neighbour, per statistic and value
axis, stacked series by their stacked values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The theme effect runs before the option effect on mount; ECharts has no
model then, and readView threw, so the chart did not render on PSU.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r the mode

On PSU the remount comes before data-theme changes, so the new chart
started in the old mode and the mode check dropped the view. The view is
now handed over only when the component unmounts (an effect declared
before the chart's sets the flag), not when the chart is made again for
another renderer or locale.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ion, unmount flag, real render test

- A statistic line without another series' line of the same statistic
  on its axis gets no label of its own, so a position from -Options for
  the series' markLine applies as before.
- The unmount flag lives in useUnmounting and is false again after
  React's trial unmount in StrictMode.
- A test draws two close averages with the module's ECharts and checks
  that the labels do not overlap (fails on main).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s change made smaller

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@eizedev
eizedev merged commit d41349e into main Nov 25, 2026
12 checks passed
@eizedev
eizedev deleted the fix/echarts-theme-zoom-marklines branch November 25, 2026 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant