deno.land / x / billboardjs@3.6.0 / ChartInternal / interactions / zoom.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/** * Copyright (c) 2017 ~ present NAVER Corp. * billboard.js project is licensed under the MIT license */import {drag as d3Drag} from "d3-drag";import {zoomIdentity as d3ZoomIdentity, zoom as d3Zoom, ZoomTransform as d3ZoomTransform} from "d3-zoom";import {$COMMON, $ZOOM} from "../../config/classes";import {callFn, diffDomain, getPointer, isFunction} from "../../module/util";
export default { /** * Initialize zoom. * @private */ initZoom(): void { const $$ = this;
$$.scale.zoom = null;
$$.generateZoom(); $$.initZoomBehaviour(); },
/** * Bind zoom event * @param {boolean} bind Weather bind or unbound * @private */ bindZoomEvent(bind = true): void { const $$ = this; const {config} = $$; const zoomEnabled = config.zoom_enabled;
if (zoomEnabled && bind) { // Do not bind zoom event when subchart is shown !config.subchart_show && $$.bindZoomOnEventRect(); } else if (bind === false) { $$.api.unzoom(); $$.unbindZoomEvent(); } },
/** * Generate zoom * @private */ generateZoom(): void { const $$ = this; const {config, org, scale} = $$;
const zoom = d3Zoom().duration(0) .on("start", $$.onZoomStart.bind($$)) .on("zoom", $$.onZoom.bind($$)) .on("end", $$.onZoomEnd.bind($$));
// get zoom extent // @ts-ignore zoom.orgScaleExtent = (): [number, number] => { const extent = config.zoom_extent || [1, 10];
return [extent[0], Math.max($$.getMaxDataCount() / extent[1], extent[1])]; };
// @ts-ignore zoom.updateScaleExtent = function() { const ratio = diffDomain($$.scale.x.orgDomain()) / diffDomain($$.getZoomDomain()); const extent = this.orgScaleExtent();
this.scaleExtent([extent[0] * ratio, extent[1] * ratio]);
return this; };
/** * Update scale according zoom transform value * @param {object} transform transform object * @param {boolean} correctTransform if the d3 transform should be updated after rescaling * @private */ // @ts-ignore zoom.updateTransformScale = (transform: d3ZoomTransform, correctTransform: boolean): void => { const isRotated = config.axis_rotated;
// in case of resize, update range of orgXScale org.xScale?.range(scale.x.range());
// rescale from the original scale const newScale = transform[ isRotated ? "rescaleY" : "rescaleX" ](org.xScale || scale.x);
const domain = $$.trimXDomain(newScale.domain()); const rescale = config.zoom_rescale;
newScale.domain(domain, org.xDomain);
// prevent chart from panning off the edge and feeling "stuck" // https://github.com/naver/billboard.js/issues/2588 if (correctTransform) { const t = newScale(scale.x.domain()[0]); const tX = isRotated ? transform.x : t; const tY = isRotated ? t : transform.y;
$$.$el.eventRect.property("__zoom", d3ZoomIdentity.translate(tX, tY).scale(transform.k)); }
if (!$$.state.xTickOffset) { $$.state.xTickOffset = $$.axis.x.tickOffset(); }
scale.zoom = $$.getCustomizedScale(newScale); $$.axis.x.scale(scale.zoom);
if (rescale) { // copy current initial x scale in case of rescale option is used !org.xScale && (org.xScale = scale.x.copy()); scale.x.domain(domain); } };
/** * Get zoom domain * @returns {Array} zoom domain * @private */ // @ts-ignore zoom.getDomain = (): number|Date[] => { const domain = scale[scale.zoom ? "zoom" : "subX"].domain(); const isCategorized = $$.axis.isCategorized();
if (isCategorized) { domain[1] -= 2; }
return domain; }; $$.zoom = zoom; },
/** * 'start' event listener * @param {object} event Event object * @private */ onZoomStart(event): void { const $$ = this; const {sourceEvent} = event;
if (sourceEvent) { $$.zoom.startEvent = sourceEvent; $$.state.zooming = true; callFn($$.config.zoom_onzoomstart, $$.api, event); } },
/** * 'zoom' event listener * @param {object} event Event object * @private */ onZoom(event): void { const $$ = this; const {config, scale, state, org} = $$; const {sourceEvent} = event; const isUnZoom = event?.transform === d3ZoomIdentity;
if ( !config.zoom_enabled || $$.filterTargetsToShow($$.data.targets).length === 0 || (!scale.zoom && sourceEvent?.type.indexOf("touch") > -1 && sourceEvent?.touches.length === 1) ) { return; }
if (event.sourceEvent) { state.zooming = true; }
const isMousemove = sourceEvent?.type === "mousemove"; const isZoomOut = sourceEvent?.wheelDelta < 0; const {transform} = event;
if (!isMousemove && isZoomOut && scale.x.domain().every((v, i) => v !== org.xDomain[i])) { scale.x.domain(org.xDomain); }
$$.zoom.updateTransformScale(transform, config.zoom_type === "wheel" && sourceEvent);
// do zoom transiton when: // - zoom type 'drag' // - when .unzoom() is called (event.transform === d3ZoomIdentity) const doTransition = config.transition_duration > 0 && !config.subchart_show && ( state.dragging || isUnZoom || !event.sourceEvent );
$$.redraw({ withTransition: doTransition, withY: config.zoom_rescale, withSubchart: false, withEventRect: false, withDimension: false });
$$.state.cancelClick = isMousemove;
// do not call event cb when is .unzoom() is called !isUnZoom && callFn(config.zoom_onzoom, $$.api, $$.zoom.getDomain()); },
/** * 'end' event listener * @param {object} event Event object * @private */ onZoomEnd(event): void { const $$ = this; const {config, state} = $$; let {startEvent} = $$.zoom; let e = event?.sourceEvent; const isUnZoom = event?.transform === d3ZoomIdentity;
if (startEvent?.type.indexOf("touch") > -1) { startEvent = startEvent.changedTouches[0]; e = e?.changedTouches?.[0]; }
// if click, do nothing. otherwise, click interaction will be canceled. if (config.zoom_type === "drag" && ( e && startEvent.clientX === e.clientX && startEvent.clientY === e.clientY )) { return; }
$$.redrawEventRect(); $$.updateZoom();
state.zooming = false;
// do not call event cb when is .unzoom() is called !isUnZoom && (e || state.dragging) && callFn(config.zoom_onzoomend, $$.api, $$.zoom.getDomain()); },
/** * Update zoom * @param {boolean} force Force unzoom * @private */ updateZoom(force: boolean): void { const $$ = this; const {subX, x, zoom} = $$.scale;
if (zoom) { const zoomDomain = zoom.domain(); const xDomain = subX.domain(); const delta = 0.015; // arbitrary value
const isfullyShown = (zoomDomain[0] <= xDomain[0] || (zoomDomain[0] - delta) <= xDomain[0]) && (xDomain[1] <= zoomDomain[1] || xDomain[1] <= (zoomDomain[1] - delta));
// check if the zoomed chart is fully shown, then reset scale when zoom is out as initial if (force || isfullyShown) { $$.axis.x.scale(subX); x.domain(subX.orgDomain()); $$.scale.zoom = null; } } },
/** * Attach zoom event on <rect> * @private */ bindZoomOnEventRect(): void { const $$ = this; const {config, $el: {eventRect}} = $$; const behaviour = config.zoom_type === "drag" ? $$.zoomBehaviour : $$.zoom;
// Since Chrome 89, wheel zoom not works properly // Applying the workaround: https://github.com/d3/d3-zoom/issues/231#issuecomment-802305692 $$.$el.svg.on("wheel", () => {});
eventRect .call(behaviour) .on("dblclick.zoom", null); },
/** * Initialize the drag behaviour used for zooming. * @private */ initZoomBehaviour(): void { const $$ = this; const {config, state} = $$; const isRotated = config.axis_rotated; let start = 0; let end = 0; let zoomRect;
const prop = { axis: isRotated ? "y" : "x", attr: isRotated ? "height" : "width", index: isRotated ? 1 : 0 };
$$.zoomBehaviour = d3Drag() .clickDistance(4) .on("start", function(event) { state.event = event; $$.setDragStatus(true); $$.unselectRect();
if (!zoomRect) { zoomRect = $$.$el.main.append("rect") .attr("clip-path", state.clip.path) .attr("class", $ZOOM.zoomBrush) .attr("width", isRotated ? state.width : 0) .attr("height", isRotated ? 0 : state.height); }
start = getPointer(event, this)[prop.index]; end = start;
zoomRect .attr(prop.axis, start) .attr(prop.attr, 0);
$$.onZoomStart(event); }) .on("drag", function(event) { end = getPointer(event, this)[prop.index];
zoomRect .attr(prop.axis, Math.min(start, end)) .attr(prop.attr, Math.abs(end - start)); }) .on("end", event => { const scale = $$.scale.zoom || $$.scale.x;
state.event = event;
zoomRect .attr(prop.axis, 0) .attr(prop.attr, 0);
if (start > end) { [start, end] = [end, start]; }
if (start < 0) { end += Math.abs(start); start = 0; }
if (start !== end) { $$.api.zoom([start, end].map(v => scale.invert(v))); }
$$.setDragStatus(false); }); },
setZoomResetButton(): void { const $$ = this; const {config, $el} = $$; const resetButton = config.zoom_resetButton;
if (resetButton && config.zoom_type === "drag") { if (!$el.zoomResetBtn) { $el.zoomResetBtn = $$.$el.chart.append("div") .classed($COMMON.button, true) .append("span") .on("click", function() { isFunction(resetButton.onclick) && resetButton.onclick.bind($$.api)(this); $$.api.unzoom(); }) .classed($ZOOM.buttonZoomReset, true) .text(resetButton.text || "Reset Zoom"); } else { $el.zoomResetBtn.style("display", null); } } }};
billboardjs

Version Info

Tagged at
a year ago