Fix echart zoom reset on data update by using getOption() API#5822
Merged
falkoschindler merged 1 commit intomainfrom Feb 19, 2026
Merged
Fix echart zoom reset on data update by using getOption() API#5822falkoschindler merged 1 commit intomainfrom
getOption() API#5822falkoschindler merged 1 commit intomainfrom
Conversation
4 tasks
evnchn
approved these changes
Feb 19, 2026
Collaborator
evnchn
left a comment
There was a problem hiding this comment.
MRE fails on main works on this PR.
getOption is not in docs but in the source code.
Contributor
Author
|
See link in the PR description: https://echarts.apache.org/en/api.html#echartsInstance.getOption |
Collaborator
|
Haha, I was searching in https://echarts.apache.org/en/option.html instead, so came up empty. Anyways... |
Collaborator
|
@falkoschindler I was asking Claude Code to review this as well, and it came up with this: """Test for PR #5822: echart zoom should not reset on data update.
Run with: pytest tests/test_pr5822.py -xvs
"""
from nicegui import ui
from nicegui.testing import Screen
def test_datazoom_preserved_on_data_update(screen: Screen):
"""Verify that dataZoom slider position is NOT reset when chart data is updated via timer."""
@ui.page('/')
def page():
chart = ui.echart({
'xAxis': {'type': 'value'},
'yAxis': {},
'dataZoom': [{'type': 'slider', 'yAxisIndex': 0}],
'series': [{'type': 'line', 'data': [[i, i * 2] for i in range(20)]}],
})
# Button to set zoom to a custom range
async def set_zoom():
chart.run_chart_method(
'dispatchAction',
{'type': 'dataZoom', 'dataZoomIndex': 0, 'start': 20, 'end': 80},
)
ui.button('Set Zoom', on_click=set_zoom)
# Single label to show current zoom state (overwritten each time)
zoom_label = ui.label('ZoomState: unread')
async def read_zoom():
result = await chart.run_chart_method(':getOption')
dz = result.get('dataZoom', [{}])[0]
start = dz.get('start', -1)
end = dz.get('end', -1)
zoom_label.set_text(f'ZoomState: {start:.0f}-{end:.0f}')
ui.button('Read Zoom', on_click=read_zoom)
# Button to add data (simulates what the timer does in the MRE)
def add_data():
n = len(chart.options['series'][0]['data'])
for i in range(5):
chart.options['series'][0]['data'].append([n + i, (n + i) * 2])
chart.update()
ui.button('Add Data', on_click=add_data)
screen.open('/')
screen.wait(1)
# Step 1: Set zoom to 20-80
screen.click('Set Zoom')
screen.wait(0.5)
# Step 2: Read zoom to confirm it was set
screen.click('Read Zoom')
screen.should_contain('ZoomState: 20-80')
# Step 3: Add data multiple times (this triggers chart.update -> setOption)
screen.click('Add Data')
screen.wait(0.3)
screen.click('Add Data')
screen.wait(0.3)
screen.click('Add Data')
screen.wait(0.5)
# Step 4: Read zoom again — should still be ~20-80, NOT 0-100
screen.click('Read Zoom')
screen.wait(0.5)
screen.should_contain('ZoomState: 20-80')
screen.should_not_contain('ZoomState: 0-100')But I don't think we want to go to that extreme of a test. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Fixes #5819.
Updating echart data (e.g. appending points via a timer) always resets interactive state like dataZoom slider positions. This is because
this.chart.optionsis not a valid ECharts API — it's alwaysundefined— so thenotMergeflag evaluates totrueon every update, causing a full replacement of chart state instead of a merge.MRE:
Implementation
Replace
this.chart.options?.series?.lengthwiththis.chart.getOption()?.series?.lengthinechart.js.getOption()is the correct ECharts API to retrieve the current chart configuration. This waynotMergeis onlytruewhen the number of series actually changes (requiring a clean replacement), andfalsefor normal data updates (preserving zoom, slider positions, and other interactive state).Progress