Skip to content

Commit 3a583f4

Browse files
authored
Merge pull request #5194 from Harsh3006/fix/reset-zoom-xaxis-labels
fix: x-axis labels not updating after zoom reset
2 parents 9b294da + a78a5d8 commit 3a583f4

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/modules/Toolbar.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,9 @@ export default class Toolbar {
665665
charts.forEach((ch) => {
666666
const w = ch.w
667667

668+
// if user is hitting zoom reset button without zooming in first, then we should not fire zoom reset event
669+
if (!w.interact.zoomed) return
670+
668671
// forget lastXAxis min/max as reset button isn't resetting the x-axis completely if zoomX is called before
669672
w.globals.lastXAxis.min = w.globals.initialConfig.xaxis.min
670673
w.globals.lastXAxis.max = w.globals.initialConfig.xaxis.max
@@ -689,8 +692,6 @@ export default class Toolbar {
689692
})
690693
}
691694

692-
w.interact.zoomed = false
693-
694695
// if user has some series collapsed before hitting zoom reset button,
695696
// those series should stay collapsed
696697
const series = ch.ctx.series.emptyCollapsedSeries(
@@ -701,6 +702,8 @@ export default class Toolbar {
701702
series,
702703
w.config.chart.animations.dynamicAnimation.enabled,
703704
)
705+
706+
w.interact.zoomed = false
704707
})
705708
}
706709

src/modules/helpers/UpdateHelpers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ export default class UpdateHelpers {
186186
* - Series count unchanged (grid column/row counts depend on it)
187187
* - No series currently collapsing (collapsed series changes visible data range)
188188
* - Not a combo chart (combo charts mix types and need coordinated axis recalc)
189+
* - Not currently zoomed (zoomed charts have altered x-labels that need recalculation)
189190
* @param {any[]} newSeries
190191
* @param {number} prevSeriesCount
191192
* @param {import('../../types/internal').ChartStateW} w
@@ -196,6 +197,7 @@ export default class UpdateHelpers {
196197
if (newSeries.length !== prevSeriesCount) return false
197198
if (w.globals.collapsedSeries.length > 0) return false
198199
if (w.globals.comboCharts) return false
200+
if (w.interact.zoomed) return false
199201
return true
200202
}
201203

tests/unit/fast-update.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,22 @@ describe('fastUpdate() — partial updateSeries fast path', () => {
100100
expect(chart.getState().series).toEqual([[7, 8, 9]])
101101
expect(chart.getState().maxY).toBe(9)
102102
})
103+
104+
it('reset zoom should restore axis state using full update', async () => {
105+
const chart = createChart('line', [{ data: [1, 2, 3, 4, 5] }])
106+
107+
const fastUpdateSpy = vi.spyOn(chart, 'fastUpdate')
108+
const updateSpy = vi.spyOn(chart, 'update')
109+
110+
chart.zoomX(2, 4)
111+
fastUpdateSpy.mockClear()
112+
updateSpy.mockClear()
113+
114+
chart.ctx.toolbar.handleZoomReset()
115+
expect(chart.w.interact.zoomed).toBe(false)
116+
expect(chart.w.globals.minX).toBe(1)
117+
expect(chart.w.globals.maxX).toBe(5)
118+
expect(fastUpdateSpy).not.toHaveBeenCalled()
119+
expect(updateSpy).toHaveBeenCalledOnce()
120+
})
103121
})

0 commit comments

Comments
 (0)