Skip to content

Commit 304fbf6

Browse files
authored
feat(Events-ZoomDomain.CHANGE): add type ('in' | 'out' | 'reset' | 'manual') (#2035)
1 parent 51224dd commit 304fbf6

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

packages/core/src/components/axes/grid-brush.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,24 @@ export class ChartBrush extends Component {
104104
const xScale = scaleTime().range([0, width]).domain(zoomDomain)
105105

106106
let newDomain = [xScale.invert(startPoint), xScale.invert(endPoint)]
107+
let zoomType: 'in' | 'out' | 'reset' | 'manual' = 'manual'
108+
107109
// if selected start time and end time are the same
108110
// reset to default full range
109111
if (newDomain[0].valueOf() === newDomain[1].valueOf()) {
110112
// same as d3 behavior and zoom bar behavior: set to default full range
111113
newDomain = this.services.zoom.getDefaultZoomBarDomain()
114+
zoomType = 'reset'
115+
} else {
116+
zoomType = 'manual'
112117
}
113118

114119
// only if zoomDomain needs update
115120
if (
116121
zoomDomain[0].valueOf() !== newDomain[0].valueOf() ||
117122
zoomDomain[1].valueOf() !== newDomain[1].valueOf()
118123
) {
119-
this.services.zoom.handleDomainChange(newDomain)
124+
this.services.zoom.handleDomainChange(newDomain, { dispatchEvent: true, type: zoomType })
120125
}
121126
}
122127

packages/core/src/components/axes/zoom-bar.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,21 @@ export class ZoomBar extends Component {
317317
event.sourceEvent.type === 'touchmove' ||
318318
event.sourceEvent.type === 'touchend')
319319
) {
320+
// Determine zoom type for manual zoom bar operations
321+
let zoomType: 'in' | 'out' | 'reset' | 'manual' = 'manual'
322+
if (zoomDomain && zoomDomain[0] && zoomDomain[1]) {
323+
// Check if it's a reset to full range
324+
const defaultDomain = this.services.zoom.getDefaultZoomBarDomain()
325+
if (
326+
newDomain[0].valueOf() === defaultDomain[0].valueOf() &&
327+
newDomain[1].valueOf() === defaultDomain[1].valueOf()
328+
) {
329+
zoomType = 'reset'
330+
} else {
331+
zoomType = 'manual'
332+
}
333+
}
334+
320335
// only if zoomDomain is never set or needs update
321336
if (
322337
zoomDomain === undefined ||
@@ -326,7 +341,8 @@ export class ZoomBar extends Component {
326341
// don't dispatch event for all event types
327342
// let the following code to dispatch necessary events
328343
this.services.zoom.handleDomainChange(newDomain, {
329-
dispatchEvent: false
344+
dispatchEvent: false,
345+
type: zoomType
330346
})
331347
}
332348

@@ -340,7 +356,8 @@ export class ZoomBar extends Component {
340356
zoomBarEventType = Events.ZoomBar.SELECTION_END
341357
// only dispatch zoom domain change event for triggering api call when event type equals to end
342358
this.services.events.dispatchEvent(Events.ZoomDomain.CHANGE, {
343-
newDomain
359+
newDomain,
360+
type: zoomType
344361
})
345362
}
346363
this.services.events.dispatchEvent(zoomBarEventType, {

packages/core/src/services/zoom.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,15 @@ export class Zoom extends Service {
7171
)
7272
}
7373

74-
handleDomainChange(newDomain: any, configs = { dispatchEvent: true }) {
74+
handleDomainChange(
75+
newDomain: any,
76+
configs = { dispatchEvent: true, type: 'manual' as 'in' | 'out' | 'reset' | 'manual' }
77+
) {
7578
this.model.set({ zoomDomain: newDomain }, { animate: false })
7679
if (configs.dispatchEvent) {
7780
this.services.events?.dispatchEvent(Events.ZoomDomain.CHANGE, {
78-
newDomain
81+
newDomain,
82+
type: configs.type
7983
})
8084
}
8185
}
@@ -148,7 +152,7 @@ export class Zoom extends Service {
148152
currentZoomDomain[0].valueOf() !== newDomain[0].valueOf() ||
149153
currentZoomDomain[1].valueOf() !== newDomain[1].valueOf()
150154
) {
151-
this.handleDomainChange(newDomain)
155+
this.handleDomainChange(newDomain, { dispatchEvent: true, type: 'in' })
152156
}
153157
}
154158

@@ -181,7 +185,7 @@ export class Zoom extends Service {
181185
currentZoomDomain[0].valueOf() !== newDomain[0].valueOf() ||
182186
currentZoomDomain[1].valueOf() !== newDomain[1].valueOf()
183187
) {
184-
this.handleDomainChange(newDomain)
188+
this.handleDomainChange(newDomain, { dispatchEvent: true, type: 'out' })
185189
}
186190
}
187191

@@ -195,7 +199,7 @@ export class Zoom extends Service {
195199
currentZoomDomain[0].valueOf() !== newDomain[0].valueOf() ||
196200
currentZoomDomain[1].valueOf() !== newDomain[1].valueOf()
197201
) {
198-
this.handleDomainChange(newDomain)
202+
this.handleDomainChange(newDomain, { dispatchEvent: true, type: 'reset' })
199203
}
200204
}
201205

0 commit comments

Comments
 (0)