fix(chart): harden interaction and axis caching

This commit is contained in:
李启源
2026-07-21 09:32:57 +08:00
parent c63ea2855e
commit a15df36e18
8 changed files with 770 additions and 22 deletions

View File

@@ -52,18 +52,39 @@ function resolveAxisSides(axisCount: number): Array<'left' | 'right'> {
return ['left']
}
// 缓存 axis groups 计算结果,避免重复计算
const yAxisGroupsCache = new WeakMap<DisplayTrack, Map<WaveformOverlayMode, YAxisSeriesGroup[]>>()
// Cache across recreated track objects without reusing groups whose axis-relevant data changed.
const yAxisGroupsCache = new Map<string, Map<WaveformOverlayMode, YAxisSeriesGroup[]>>()
const MAX_CACHE_SIZE = 100
function getCacheKey(track: DisplayTrack): string {
return JSON.stringify([
track.id,
track.yDomain,
track.visibleSeries.map((series) => [
series.id,
series.name,
series.unit,
series.color,
series.yDomain,
]),
])
}
export function buildYAxisSeriesGroups(
track: DisplayTrack,
overlayMode: WaveformOverlayMode,
): YAxisSeriesGroup[] {
// 检查缓存
let trackCache = yAxisGroupsCache.get(track)
const cacheKey = getCacheKey(track)
let trackCache = yAxisGroupsCache.get(cacheKey)
if (!trackCache) {
trackCache = new Map()
yAxisGroupsCache.set(track, trackCache)
yAxisGroupsCache.set(cacheKey, trackCache)
if (yAxisGroupsCache.size > MAX_CACHE_SIZE) {
const firstKey = yAxisGroupsCache.keys().next().value
if (firstKey !== undefined) {
yAxisGroupsCache.delete(firstKey)
}
}
}
const cached = trackCache.get(overlayMode)