@antv/g2 5.0.5 → 5.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/api/chart.ts CHANGED
@@ -130,7 +130,7 @@ export type ChartOptions = ViewComposition & {
130
130
  autoFit?: boolean;
131
131
  renderer?: CanvasRenderer;
132
132
  plugins?: RendererPlugin[];
133
- theme: string;
133
+ theme?: string;
134
134
  };
135
135
 
136
136
  type ChartProps = Concrete<ViewComposition>;
@@ -178,6 +178,8 @@ export class Chart extends View<ChartOptions> {
178
178
  private _options: G2ViewTree;
179
179
  private _width: number;
180
180
  private _height: number;
181
+ // Identifies whether bindAutoFit.
182
+ private _hasBindAutoFit = false;
181
183
 
182
184
  constructor(options: ChartOptions) {
183
185
  const { container, canvas, ...rest } = options || {};
@@ -185,10 +187,11 @@ export class Chart extends View<ChartOptions> {
185
187
  this._container = normalizeContainer(container);
186
188
  this._emitter = new EventEmitter();
187
189
  this._context = { library, emitter: this._emitter, canvas };
188
- this._bindAutoFit();
189
190
  }
190
191
 
191
192
  render(): Promise<Chart> {
193
+ this._bindAutoFit();
194
+
192
195
  if (!this._context.canvas) {
193
196
  // Init width and height.
194
197
  const { renderer, plugins } = this.options();
@@ -253,6 +256,13 @@ export class Chart extends View<ChartOptions> {
253
256
  return this;
254
257
  }
255
258
 
259
+ // @todo Remove it when implement updateRoot.
260
+ changeData(data: any): Promise<Chart> {
261
+ // Update options data.
262
+ this.options({ data });
263
+ return super.changeData(data);
264
+ }
265
+
256
266
  getContainer(): HTMLElement {
257
267
  return this._container;
258
268
  }
@@ -332,15 +342,22 @@ export class Chart extends View<ChartOptions> {
332
342
  private _bindAutoFit() {
333
343
  const options = this.options();
334
344
  const { autoFit } = options;
345
+
346
+ if (this._hasBindAutoFit) {
347
+ // If it was bind before, unbind it now.
348
+ if (!autoFit) this._unbindAutoFit();
349
+ return;
350
+ }
351
+
335
352
  if (autoFit) {
353
+ this._hasBindAutoFit = true;
336
354
  window.addEventListener('resize', this._onResize);
337
355
  }
338
356
  }
339
357
 
340
358
  private _unbindAutoFit() {
341
- const options = this.options();
342
- const { autoFit } = options;
343
- if (autoFit) {
359
+ if (this._hasBindAutoFit) {
360
+ this._hasBindAutoFit = false;
344
361
  window.removeEventListener('resize', this._onResize);
345
362
  }
346
363
  }
@@ -1,6 +1,6 @@
1
1
  import { deepMix } from '@antv/util';
2
2
  import { subObject } from '../utils/helper';
3
- import { invert, domainOf } from '../utils/scale';
3
+ import { selectionOf } from '../utils/scale';
4
4
  import { brush as createBrush } from './brushHighlight';
5
5
  import { selectPlotArea } from './utils';
6
6
 
@@ -26,6 +26,10 @@ export function brushFilter(
26
26
  brushRegion,
27
27
  extent: optionalExtent,
28
28
  reverse,
29
+ emitter,
30
+ scale,
31
+ coordinate,
32
+ selection,
29
33
  series = false,
30
34
  ...rest
31
35
  },
@@ -49,17 +53,29 @@ export function brushFilter(
49
53
 
50
54
  // Filter when brush created.
51
55
  function brushcreated(x, y, x1, y1, event) {
52
- filter(x, y, x1, y1, event);
56
+ event.nativeEvent = true;
57
+ filter(selection(x, y, x1, y1), event);
53
58
  brush.remove();
54
59
  }
55
60
 
56
61
  // Reset when dblclick.
57
62
  function click(e) {
58
- if (isDblclick(e)) reset(e);
63
+ if (isDblclick(e)) {
64
+ e.nativeEvent = true;
65
+ reset(e);
66
+ }
59
67
  }
60
68
 
69
+ const onFilter = ({ nativeEvent, data }) => {
70
+ if (nativeEvent) return;
71
+ const { selection } = data;
72
+ filter(selection, { nativeEvent: false });
73
+ };
74
+ emitter.on('brush:filter', onFilter);
75
+
61
76
  return () => {
62
77
  brush.destroy();
78
+ emitter.off('brush:filter', onFilter);
63
79
  root.removeEventListener('click', click);
64
80
  };
65
81
  }
@@ -80,27 +96,21 @@ export function BrushFilter({ hideX = true, hideY = true, ...rest }) {
80
96
  let filtering = false;
81
97
  let newView = view;
82
98
 
99
+ const { scale, coordinate } = view;
83
100
  return brushFilter(plotArea, {
84
101
  brushRegion: (x, y, x1, y1) => [x, y, x1, y1],
85
- filter: async (x, y, x1, y1, event) => {
102
+ selection: (x, y, x1, y1) => {
103
+ const { scale, coordinate } = newView;
104
+ return selectionOf(x, y, x1, y1, scale, coordinate);
105
+ },
106
+ filter: async (selection, event) => {
86
107
  // Avoid redundant filter.
87
108
  if (filtering) return;
88
109
  filtering = true;
89
110
 
90
- // Invert visual range to abstract domain.
91
- const { scale, coordinate } = newView;
92
- const { x: scaleX, y: scaleY } = scale;
93
- const abstractDomain = (point, start) => {
94
- const [x, y] = coordinate.invert(point);
95
- return [invert(scaleX, x, start), invert(scaleY, y, start)];
96
- };
97
- const p0 = abstractDomain([x, y], true);
98
- const p1 = abstractDomain([x1, y1], false);
99
-
100
111
  // Update the domain of x and y scale to filter data.
112
+ const [domainX, domainY] = selection;
101
113
  const { marks } = viewOptions;
102
- const domainX = domainOf(scaleX, [p0[0], p1[0]]);
103
- const domainY = domainOf(scaleY, [p0[1], p1[1]]);
104
114
  const newMarks = marks.map((mark) =>
105
115
  deepMix(
106
116
  {
@@ -153,6 +163,9 @@ export function BrushFilter({ hideX = true, hideY = true, ...rest }) {
153
163
  update(viewOptions);
154
164
  },
155
165
  extent: undefined,
166
+ emitter,
167
+ scale,
168
+ coordinate,
156
169
  ...defaultOptions,
157
170
  ...rest,
158
171
  });
@@ -1,5 +1,6 @@
1
1
  import { DisplayObject, Rect, Path } from '@antv/g';
2
2
  import { subObject } from '../utils/helper';
3
+ import { selectionOf, pixelsOf } from '../utils/scale';
3
4
  import {
4
5
  selectG2Elements,
5
6
  selectPlotArea,
@@ -240,11 +241,16 @@ export function brush(
240
241
 
241
242
  return {
242
243
  mask,
244
+ move(x, y, x1, y1) {
245
+ if (!mask) initMask(x, y);
246
+ start = [x, y];
247
+ end = [x1, y1];
248
+ updateMask([x, y], [x1, y1]);
249
+ },
243
250
  remove() {
244
251
  if (mask) removeMask();
245
252
  },
246
253
  destroy() {
247
- if (mask) removeMask();
248
254
  root.removeEventListener('dragstart', dragstart);
249
255
  root.removeEventListener('drag', drag);
250
256
  root.removeEventListener('dragend', dragend);
@@ -290,6 +296,8 @@ export function brushHighlight(
290
296
  brushRegion,
291
297
  extent: optionalExtent,
292
298
  reverse,
299
+ scale,
300
+ coordinate,
293
301
  series = false,
294
302
  key = (d) => d,
295
303
  bboxOf = (root) => {
@@ -297,6 +305,7 @@ export function brushHighlight(
297
305
  return { x, y, width, height };
298
306
  },
299
307
  state = {},
308
+ emitter,
300
309
  ...rest
301
310
  },
302
311
  ) {
@@ -379,18 +388,51 @@ export function brushHighlight(
379
388
  }
380
389
  };
381
390
 
382
- return brush(root, {
391
+ const brushHandler = brush(root, {
383
392
  ...brushStyle,
384
393
  extent,
385
394
  brushRegion,
386
395
  reverse,
387
- brushended: series ? seriesBrushend : brushended,
388
- brushed: series ? seriesBrushed : brushed,
396
+ brushended: () => {
397
+ const handler = series ? seriesBrushend : brushended;
398
+ emitter.emit('brush:end', { nativeEvent: true });
399
+ handler();
400
+ },
401
+ brushed: (x, y, x1, y1) => {
402
+ const selection = selectionOf(x, y, x1, y1, scale, coordinate);
403
+ emitter.emit('brush:highlight', {
404
+ nativeEvent: true,
405
+ data: { selection },
406
+ });
407
+ const handler = series ? seriesBrushed : brushed;
408
+ handler(x, y, x1, y1);
409
+ },
389
410
  });
411
+
412
+ // Move brush and highlight data.
413
+ const onHighlight = ({ nativeEvent, data }) => {
414
+ if (nativeEvent) return;
415
+ const { selection } = data;
416
+ const [x, y, x1, y1] = pixelsOf(selection, scale, coordinate);
417
+ brushHandler.move(x, y, x1, y1);
418
+ };
419
+ emitter.on('brush:highlight', onHighlight);
420
+
421
+ // Remove brush and reset data.
422
+ const onRemove = () => brushHandler.remove();
423
+ emitter.on('brush:remove', onRemove);
424
+
425
+ // Remove event handlers.
426
+ brushHandler.destroy = () => {
427
+ emitter.off('brush:highlight', onHighlight);
428
+ emitter.off('brush:remove', onRemove);
429
+ };
430
+
431
+ return brushHandler;
390
432
  }
391
433
 
392
434
  export function BrushHighlight({ facet, brushKey, ...rest }) {
393
- return (target, viewInstances) => {
435
+ return (target, viewInstances, emitter) => {
394
436
  const { container, view, options } = target;
395
437
  const plotArea = selectPlotArea(container);
396
438
  const defaultOptions = {
@@ -400,6 +442,7 @@ export function BrushHighlight({ facet, brushKey, ...rest }) {
400
442
  reverse: false,
401
443
  };
402
444
  const defaultStates = ['active', ['inactive', { opacity: 0.5 }]];
445
+ const { scale, coordinate } = view;
403
446
 
404
447
  if (facet) {
405
448
  const bbox = plotArea.getBounds();
@@ -418,6 +461,9 @@ export function BrushHighlight({ facet, brushKey, ...rest }) {
418
461
  selectFacetViews(target, viewInstances).map((d) => d.options),
419
462
  defaultStates,
420
463
  ),
464
+ emitter,
465
+ scale,
466
+ coordinate,
421
467
  ...defaultOptions,
422
468
  ...rest,
423
469
  });
@@ -439,6 +485,9 @@ export function BrushHighlight({ facet, brushKey, ...rest }) {
439
485
  [options, ...selectSiblingOptions(target, viewInstances, brushKey)],
440
486
  defaultStates,
441
487
  ),
488
+ emitter,
489
+ scale,
490
+ coordinate,
442
491
  ...defaultOptions,
443
492
  ...rest,
444
493
  });
@@ -29,3 +29,26 @@ export function domainOf(scale, values) {
29
29
  const end = domain.indexOf(v2);
30
30
  return domain.slice(start, end + 1);
31
31
  }
32
+
33
+ export function selectionOf(x, y, x1, y1, scale, coordinate) {
34
+ const { x: scaleX, y: scaleY } = scale;
35
+ const abstractDomain = (point, start) => {
36
+ const [x, y] = coordinate.invert(point);
37
+ return [invert(scaleX, x, start), invert(scaleY, y, start)];
38
+ };
39
+ const p0 = abstractDomain([x, y], true);
40
+ const p1 = abstractDomain([x1, y1], false);
41
+ const domainX = domainOf(scaleX, [p0[0], p1[0]]);
42
+ const domainY = domainOf(scaleY, [p0[1], p1[1]]);
43
+ return [domainX, domainY];
44
+ }
45
+
46
+ export function pixelsOf(selection, scale, coordinate) {
47
+ const [[minX, maxX], [minY, maxY]] = selection;
48
+ const { x: scaleX, y: scaleY } = scale;
49
+ const p0 = [scaleX.map(minX), scaleY.map(minY)];
50
+ const p1 = [scaleX.map(maxX), scaleY.map(maxY)];
51
+ const [x, y] = coordinate.map(p0);
52
+ const [x1, y1] = coordinate.map(p1);
53
+ return [x, y, x1, y1];
54
+ }