@antv/g2 5.0.4 → 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.
Files changed (78) hide show
  1. package/dist/g2-lite.min.js +3 -3
  2. package/dist/g2.min.js +3 -3
  3. package/esm/api/chart.d.ts +10 -6
  4. package/esm/api/chart.js +56 -28
  5. package/esm/api/chart.js.map +1 -1
  6. package/esm/api/composition/base.d.ts +1 -0
  7. package/esm/api/composition/base.js +2 -2
  8. package/esm/api/composition/base.js.map +1 -1
  9. package/esm/component/axis.js +9 -7
  10. package/esm/component/axis.js.map +1 -1
  11. package/esm/component/slider.js +8 -5
  12. package/esm/component/slider.js.map +1 -1
  13. package/esm/interaction/brushFilter.d.ts +6 -2
  14. package/esm/interaction/brushFilter.js +42 -21
  15. package/esm/interaction/brushFilter.js.map +1 -1
  16. package/esm/interaction/brushHighlight.d.ts +8 -2
  17. package/esm/interaction/brushHighlight.js +51 -10
  18. package/esm/interaction/brushHighlight.js.map +1 -1
  19. package/esm/interaction/brushXFilter.d.ts +1 -1
  20. package/esm/interaction/brushXHighlight.d.ts +2 -1
  21. package/esm/interaction/brushYFilter.d.ts +1 -1
  22. package/esm/interaction/brushYHighlight.d.ts +2 -1
  23. package/esm/interaction/sliderFilter.js +16 -7
  24. package/esm/interaction/sliderFilter.js.map +1 -1
  25. package/esm/runtime/render.js +3 -1
  26. package/esm/runtime/render.js.map +1 -1
  27. package/esm/transform/sort.js +13 -3
  28. package/esm/transform/sort.js.map +1 -1
  29. package/esm/utils/scale.d.ts +2 -0
  30. package/esm/utils/scale.js +21 -0
  31. package/esm/utils/scale.js.map +1 -1
  32. package/esm/utils/size.d.ts +0 -10
  33. package/esm/utils/size.js +0 -23
  34. package/esm/utils/size.js.map +1 -1
  35. package/lib/api/chart.d.ts +10 -6
  36. package/lib/api/chart.js +55 -27
  37. package/lib/api/chart.js.map +1 -1
  38. package/lib/api/composition/base.d.ts +1 -0
  39. package/lib/api/composition/base.js +2 -2
  40. package/lib/api/composition/base.js.map +1 -1
  41. package/lib/component/axis.js +9 -7
  42. package/lib/component/axis.js.map +1 -1
  43. package/lib/component/slider.js +8 -5
  44. package/lib/component/slider.js.map +1 -1
  45. package/lib/interaction/brushFilter.d.ts +6 -2
  46. package/lib/interaction/brushFilter.js +41 -20
  47. package/lib/interaction/brushFilter.js.map +1 -1
  48. package/lib/interaction/brushHighlight.d.ts +8 -2
  49. package/lib/interaction/brushHighlight.js +51 -10
  50. package/lib/interaction/brushHighlight.js.map +1 -1
  51. package/lib/interaction/brushXFilter.d.ts +1 -1
  52. package/lib/interaction/brushXHighlight.d.ts +2 -1
  53. package/lib/interaction/brushYFilter.d.ts +1 -1
  54. package/lib/interaction/brushYHighlight.d.ts +2 -1
  55. package/lib/interaction/sliderFilter.js +16 -7
  56. package/lib/interaction/sliderFilter.js.map +1 -1
  57. package/lib/runtime/render.js +3 -1
  58. package/lib/runtime/render.js.map +1 -1
  59. package/lib/transform/sort.js +13 -3
  60. package/lib/transform/sort.js.map +1 -1
  61. package/lib/utils/scale.d.ts +2 -0
  62. package/lib/utils/scale.js +24 -1
  63. package/lib/utils/scale.js.map +1 -1
  64. package/lib/utils/size.d.ts +0 -10
  65. package/lib/utils/size.js +1 -25
  66. package/lib/utils/size.js.map +1 -1
  67. package/package.json +5 -5
  68. package/src/api/chart.ts +70 -46
  69. package/src/api/composition/base.ts +4 -2
  70. package/src/component/axis.ts +12 -7
  71. package/src/component/slider.ts +10 -5
  72. package/src/interaction/brushFilter.ts +49 -19
  73. package/src/interaction/brushHighlight.ts +55 -6
  74. package/src/interaction/sliderFilter.ts +21 -7
  75. package/src/runtime/render.ts +3 -1
  76. package/src/transform/sort.ts +12 -3
  77. package/src/utils/scale.ts +23 -0
  78. package/src/utils/size.ts +0 -31
package/src/api/chart.ts CHANGED
@@ -5,7 +5,7 @@ import { debounce, deepMix, omit } from '@antv/util';
5
5
  import EventEmitter from '@antv/event-emitter';
6
6
  import { G2Context, render, destroy } from '../runtime';
7
7
  import { ViewComposition } from '../spec';
8
- import { getChartSize } from '../utils/size';
8
+ import { getContainerSize } from '../utils/size';
9
9
  import { ChartEvent } from '../utils/event';
10
10
  import { G2ViewTree } from '../runtime/types/options';
11
11
  import { Node } from './node';
@@ -95,6 +95,13 @@ function Canvas(
95
95
  });
96
96
  }
97
97
 
98
+ function sizeOf(options, container) {
99
+ const { autoFit } = options;
100
+ if (autoFit) return getContainerSize(container);
101
+ const { width = 640, height = 480 } = options;
102
+ return { width, height };
103
+ }
104
+
98
105
  export function optionsOf(node: Node): Record<string, any> {
99
106
  const root = normalizeRoot(node);
100
107
  const discovered: Node[] = [root];
@@ -123,7 +130,7 @@ export type ChartOptions = ViewComposition & {
123
130
  autoFit?: boolean;
124
131
  renderer?: CanvasRenderer;
125
132
  plugins?: RendererPlugin[];
126
- theme: string;
133
+ theme?: string;
127
134
  };
128
135
 
129
136
  type ChartProps = Concrete<ViewComposition>;
@@ -169,36 +176,26 @@ export class Chart extends View<ChartOptions> {
169
176
  private _context: G2Context;
170
177
  private _emitter: EventEmitter;
171
178
  private _options: G2ViewTree;
179
+ private _width: number;
180
+ private _height: number;
181
+ // Identifies whether bindAutoFit.
182
+ private _hasBindAutoFit = false;
172
183
 
173
184
  constructor(options: ChartOptions) {
174
- const { container, canvas, key = G2_CHART_KEY, ...rest } = options || {};
185
+ const { container, canvas, ...rest } = options || {};
175
186
  super(rest, 'view');
176
- this.attr('key', key);
177
187
  this._container = normalizeContainer(container);
178
188
  this._emitter = new EventEmitter();
179
189
  this._context = { library, emitter: this._emitter, canvas };
180
- this.bindAutoFit();
181
190
  }
182
191
 
183
192
  render(): Promise<Chart> {
193
+ this._bindAutoFit();
194
+
184
195
  if (!this._context.canvas) {
185
196
  // Init width and height.
186
- const {
187
- width = 640,
188
- height = 480,
189
- renderer,
190
- plugins,
191
- autoFit,
192
- } = this.options();
193
- const { width: adjustedWidth, height: adjustedHeight } = getChartSize(
194
- this._container,
195
- autoFit,
196
- width,
197
- height,
198
- );
199
- this.width(adjustedWidth);
200
- this.height(adjustedHeight);
201
-
197
+ const { renderer, plugins } = this.options();
198
+ const { width, height } = sizeOf(this.options(), this._container);
202
199
  // Create canvas if it does not exist.
203
200
  this._context.canvas = Canvas(
204
201
  this._container,
@@ -211,12 +208,27 @@ export class Chart extends View<ChartOptions> {
211
208
 
212
209
  return new Promise((resolve, reject) => {
213
210
  try {
214
- render(this.options(), this._context, () => resolve(this), reject);
211
+ const options = this.options();
212
+ const { key = G2_CHART_KEY } = options;
213
+ const { width, height } = sizeOf(options, this._container);
214
+
215
+ // Update actual size and key.
216
+ this._width = width;
217
+ this._height = height;
218
+ this._key = key;
219
+
220
+ render(
221
+ { key: this._key, ...options, width, height },
222
+ this._context,
223
+ () => resolve(this),
224
+ reject,
225
+ );
215
226
  } catch (e) {
216
227
  reject(e);
217
228
  }
218
229
  });
219
230
  }
231
+
220
232
  /**
221
233
  * @overload
222
234
  * @returns {G2ViewTree}
@@ -244,6 +256,13 @@ export class Chart extends View<ChartOptions> {
244
256
  return this;
245
257
  }
246
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
+
247
266
  getContainer(): HTMLElement {
248
267
  return this._container;
249
268
  }
@@ -283,7 +302,7 @@ export class Chart extends View<ChartOptions> {
283
302
  destroy() {
284
303
  const options = this.options();
285
304
  this.emit(ChartEvent.BEFORE_DESTROY);
286
- this.unbindAutoFit();
305
+ this._unbindAutoFit();
287
306
  this._options = {};
288
307
  destroy(options, this._context, true);
289
308
  removeContainer(this._container);
@@ -291,26 +310,24 @@ export class Chart extends View<ChartOptions> {
291
310
  }
292
311
 
293
312
  forceFit() {
294
- const { width, height, autoFit } = this.options();
295
- const { width: adjustedWidth, height: adjustedHeight } = getChartSize(
296
- this._container,
297
- autoFit,
298
- width,
299
- height,
300
- );
301
- if (adjustedHeight && adjustedWidth) {
302
- this.changeSize(adjustedWidth, adjustedHeight);
303
- }
313
+ // Don't call changeSize to prevent update width and height of options.
314
+ // @ts-ignore
315
+ this.options.autoFit = true;
316
+ this.emit(ChartEvent.BEFORE_CHANGE_SIZE);
317
+ const finished = this.render();
318
+ finished.then(() => {
319
+ this.emit(ChartEvent.AFTER_CHANGE_SIZE);
320
+ });
321
+ return finished;
304
322
  }
305
323
 
306
- changeSize(adjustedWidth: number, adjustedHeight: number): Promise<Chart> {
307
- const { width, height, on } = this.options();
308
- if (width === adjustedWidth && height === adjustedHeight) {
324
+ changeSize(width: number, height: number): Promise<Chart> {
325
+ if (width === this._width && height === this._height) {
309
326
  return Promise.resolve(this);
310
327
  }
311
328
  this.emit(ChartEvent.BEFORE_CHANGE_SIZE);
312
- this.width(adjustedWidth);
313
- this.height(adjustedHeight);
329
+ this.width(width);
330
+ this.height(height);
314
331
  const finished = this.render();
315
332
  finished.then(() => {
316
333
  this.emit(ChartEvent.AFTER_CHANGE_SIZE);
@@ -318,23 +335,30 @@ export class Chart extends View<ChartOptions> {
318
335
  return finished;
319
336
  }
320
337
 
321
- private onResize = debounce(() => {
338
+ private _onResize = debounce(() => {
322
339
  this.forceFit();
323
340
  }, 300);
324
341
 
325
- private bindAutoFit() {
342
+ private _bindAutoFit() {
326
343
  const options = this.options();
327
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
+
328
352
  if (autoFit) {
329
- window.addEventListener('resize', this.onResize);
353
+ this._hasBindAutoFit = true;
354
+ window.addEventListener('resize', this._onResize);
330
355
  }
331
356
  }
332
357
 
333
- private unbindAutoFit() {
334
- const options = this.options();
335
- const { autoFit } = options;
336
- if (autoFit) {
337
- window.removeEventListener('resize', this.onResize);
358
+ private _unbindAutoFit() {
359
+ if (this._hasBindAutoFit) {
360
+ this._hasBindAutoFit = false;
361
+ window.removeEventListener('resize', this._onResize);
338
362
  }
339
363
  }
340
364
  }
@@ -9,6 +9,8 @@ export class CompositionNode<
9
9
  ParentValue extends Record<string, any> = Record<string, any>,
10
10
  ChildValue extends Record<string, any> = Record<string, any>,
11
11
  > extends Node<Value, ParentValue, ChildValue> {
12
+ protected _key: string;
13
+
12
14
  /**
13
15
  * Change current node data and its children data.
14
16
  */
@@ -31,7 +33,7 @@ export class CompositionNode<
31
33
  const chart = this.getRoot();
32
34
  const { views } = chart.getContext();
33
35
  if (!views?.length) return undefined;
34
- return views.find((view) => view.key === this.attr('key'));
36
+ return views.find((view) => view.key === this._key);
35
37
  }
36
38
 
37
39
  getScale(): Record<string, Scale> {
@@ -53,7 +55,7 @@ export class CompositionNode<
53
55
  }
54
56
 
55
57
  getGroup(): DisplayObject {
56
- const key = this.attr('key');
58
+ const key = this._key;
57
59
  if (!key) return undefined;
58
60
  const chart = this.getRoot();
59
61
  const chartGroup = chart.getContext().canvas.getRoot();
@@ -153,21 +153,26 @@ function getData(
153
153
  defaultTickFormatter || scale.getFormatter?.() || toString;
154
154
  const applyInset = createInset(position, coordinate);
155
155
  const applyFisheye = createFisheye(position, coordinate);
156
+ const isHorizontal = (position) =>
157
+ ['top', 'bottom', 'center', 'outer'].includes(position);
158
+
159
+ // @todo GUI should consider the overlap problem for the first
160
+ // and label of arc axis.
156
161
  if (isPolar(coordinate) || isTranspose(coordinate)) {
157
- const axisTicks = filteredTicks.map((d, i, array) => {
162
+ return filteredTicks.map((d, i, array) => {
158
163
  const offset = scale.getBandWidth?.(d) / 2 || 0;
159
164
  const tick = applyInset(scale.map(d) + offset);
165
+ const shouldReverse =
166
+ (isRadial(coordinate) && position === 'center') ||
167
+ (isTranspose(coordinate) &&
168
+ scale.getTicks?.() &&
169
+ isHorizontal(position));
160
170
  return {
161
- value: isTranspose(coordinate) && scale.getTicks?.() ? 1 - tick : tick,
171
+ value: shouldReverse ? 1 - tick : tick,
162
172
  label: toString(labelFormatter(prettyNumber(d), i, array)),
163
173
  id: String(i),
164
174
  };
165
175
  });
166
- // @todo GUI should consider the overlap problem for the first
167
- // and label of arc axis.
168
- return isRadial(coordinate) && position === 'center'
169
- ? reverseTicks(axisTicks)
170
- : axisTicks;
171
176
  }
172
177
 
173
178
  return filteredTicks.map((d, i, array) => {
@@ -1,5 +1,6 @@
1
1
  import { Slider as SliderComponent } from '@antv/gui';
2
2
  import { format } from 'd3-format';
3
+ import { isTranspose } from '../utils/coordinate';
3
4
  import { GuideComponentComponent as GCC } from '../runtime';
4
5
  import { invert } from '../utils/scale';
5
6
 
@@ -13,9 +14,10 @@ export type SliderOptions = {
13
14
  */
14
15
  export const Slider: GCC<SliderOptions> = (options) => {
15
16
  // do not pass size.
16
- const { orientation, labelFormatter, size, style, ...rest } = options;
17
+ const { orientation, labelFormatter, size, style, position, ...rest } =
18
+ options;
17
19
 
18
- return ({ scales: [scale], value, theme }) => {
20
+ return ({ scales: [scale], value, theme, coordinate }) => {
19
21
  const { bbox } = value;
20
22
  const { x, y, width, height } = bbox;
21
23
  const { slider: sliderTheme = {} } = theme;
@@ -25,17 +27,20 @@ export const Slider: GCC<SliderOptions> = (options) => {
25
27
  ? format(labelFormatter)
26
28
  : labelFormatter;
27
29
 
30
+ const isHorizontal = orientation === 'horizontal';
31
+ const reverse = isTranspose(coordinate) && isHorizontal;
32
+
28
33
  return new SliderComponent({
29
34
  className: 'slider',
30
35
  style: Object.assign({}, sliderTheme, {
31
36
  x,
32
37
  y,
33
- trackLength: orientation === 'horizontal' ? width : height,
38
+ trackLength: isHorizontal ? width : height,
34
39
  orientation,
35
40
  formatter: (v) => {
36
41
  const f = formatter || defaultFormatter;
37
- // @todo Pass index to distinguish the left and the right value.
38
- const tick = invert(scale, v, true);
42
+ const v1 = reverse ? 1 - v : v;
43
+ const tick = invert(scale, v1, true);
39
44
  return f(tick);
40
45
  },
41
46
  ...style,
@@ -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
  },
@@ -48,24 +52,36 @@ export function brushFilter(
48
52
  root.addEventListener('click', click);
49
53
 
50
54
  // Filter when brush created.
51
- function brushcreated(x, y, x1, y1) {
52
- filter(x, y, x1, y1);
55
+ function brushcreated(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();
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
  }
66
82
 
67
83
  export function BrushFilter({ hideX = true, hideY = true, ...rest }) {
68
- return (target, viewInstances) => {
84
+ return (target, viewInstances, emitter) => {
69
85
  const { container, view, options: viewOptions, update } = target;
70
86
  const plotArea = selectPlotArea(container);
71
87
  const defaultOptions = {
@@ -80,24 +96,20 @@ 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) => {
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
114
  const newMarks = marks.map((mark) =>
103
115
  deepMix(
@@ -111,13 +123,18 @@ export function BrushFilter({ hideX = true, hideY = true, ...rest }) {
111
123
  mark,
112
124
  {
113
125
  scale: {
114
- x: { domain: domainOf(scaleX, [p0[0], p1[0]]) },
115
- y: { domain: domainOf(scaleY, [p0[1], p1[1]]) },
126
+ x: { domain: domainX },
127
+ y: { domain: domainY },
116
128
  },
117
129
  },
118
130
  ),
119
131
  );
120
132
 
133
+ // Emit event.
134
+ event.data = event.data || {};
135
+ event.data.selection = [domainX, domainY];
136
+ emitter.emit('brush:filter', event);
137
+
121
138
  // Rerender and update view.
122
139
  const newOptions = {
123
140
  ...viewOptions,
@@ -129,13 +146,26 @@ export function BrushFilter({ hideX = true, hideY = true, ...rest }) {
129
146
  filtering = false;
130
147
  filtered = true;
131
148
  },
132
- reset: () => {
149
+ reset: (event) => {
133
150
  if (filtering || !filtered) return;
151
+
152
+ // Emit event.
153
+ const { scale } = view;
154
+ const { x: scaleX, y: scaleY } = scale;
155
+ const domainX = scaleX.getOptions().domain;
156
+ const domainY = scaleY.getOptions().domain;
157
+ event.data = event.data || {};
158
+ event.data.selection = [domainX, domainY];
159
+ emitter.emit('brush:filter', event);
160
+
134
161
  filtered = false;
135
162
  newView = view;
136
163
  update(viewOptions);
137
164
  },
138
165
  extent: undefined,
166
+ emitter,
167
+ scale,
168
+ coordinate,
139
169
  ...defaultOptions,
140
170
  ...rest,
141
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,
@@ -211,7 +212,7 @@ export function brush(
211
212
  end = brushMousePosition(root, event);
212
213
  const [fx, fy, fx1, fy1] = updateMask(start, end);
213
214
  resizing = false;
214
- brushcreated(fx, fy, fx1, fy1);
215
+ brushcreated(fx, fy, fx1, fy1, event);
215
216
  };
216
217
 
217
218
  // Hide mask.
@@ -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
  });
@@ -1,4 +1,5 @@
1
1
  import { deepMix, throttle } from '@antv/util';
2
+ import { isTranspose } from '../utils/coordinate';
2
3
  import { invert, domainOf } from '../utils/scale';
3
4
 
4
5
  export const SLIDER_CLASS_NAME = 'slider';
@@ -36,10 +37,11 @@ function filterDataByDomain(options, scaleOptions) {
36
37
  };
37
38
  }
38
39
 
39
- function abstractValue(values, scale) {
40
+ function abstractValue(values, scale, reverse) {
40
41
  const [x, x1] = values;
41
- const d0 = invert(scale, x, true);
42
- const d1 = invert(scale, x1, false);
42
+ const v = reverse ? (d) => 1 - d : (d) => d;
43
+ const d0 = invert(scale, v(x), true);
44
+ const d1 = invert(scale, v(x1), false);
43
45
  return domainOf(scale, [d0, d1]);
44
46
  }
45
47
 
@@ -57,8 +59,16 @@ export function SliderFilter({
57
59
  if (!sliders.length) return () => {};
58
60
 
59
61
  let filtering = false;
60
- const { scale } = view;
62
+ const { scale, coordinate } = view;
61
63
  const { x: scaleX, y: scaleY } = scale;
64
+ const transposed = isTranspose(coordinate);
65
+
66
+ const channelOf = (orientation) => {
67
+ const channel0 = orientation === 'vertical' ? 'y' : 'x';
68
+ const channel1 = orientation === 'vertical' ? 'x' : 'y';
69
+ if (transposed) return [channel1, channel0];
70
+ return [channel0, channel1];
71
+ };
62
72
 
63
73
  const sliderHandler = new Map();
64
74
 
@@ -77,14 +87,18 @@ export function SliderFilter({
77
87
  if (filtering) return;
78
88
  filtering = true;
79
89
 
90
+ const [channel0, channel1] = channelOf(orientation);
91
+
80
92
  // Update domain of the current channel.
81
- const channel0 = orientation === 'vertical' ? 'y' : 'x';
82
93
  const scale0 = scale[channel0];
83
- const domain0 = abstractValue(values, scale0);
94
+ const domain0 = abstractValue(
95
+ values,
96
+ scale0,
97
+ transposed && orientation === 'horizontal',
98
+ );
84
99
  channelDomain[channel0] = domain0;
85
100
 
86
101
  // Get domain of the other channel.
87
- const channel1 = orientation === 'vertical' ? 'x' : 'y';
88
102
  const domain1 = channelDomain[channel1];
89
103
 
90
104
  // Filter data.
@@ -38,7 +38,9 @@ function inferKeys<T extends G2ViewTree = G2ViewTree>(options: T): T {
38
38
  const { children = [] } = node;
39
39
  if (Array.isArray(children)) {
40
40
  for (let i = 0; i < children.length; i++) {
41
- const child = children[i];
41
+ // Clone node as well.
42
+ const child = deepMix({}, children[i]);
43
+ children[i] = child;
42
44
  nodeParent.set(child, node);
43
45
  nodeIndex.set(child, i);
44
46
  discovered.push(child);
@@ -44,7 +44,7 @@ export type SortOptions = {
44
44
  };
45
45
 
46
46
  function sortQuantitative(I, mark, options): [number[], G2Mark] {
47
- const { reverse, slice, channel } = options;
47
+ const { reverse, channel } = options;
48
48
  const { encode } = mark;
49
49
  const [V] = columnOf(encode, channel);
50
50
  const sortedI = sort(I, (i: number) => V[i]);
@@ -53,12 +53,21 @@ function sortQuantitative(I, mark, options): [number[], G2Mark] {
53
53
  return [sortedI, mark];
54
54
  }
55
55
 
56
+ // If domain is specified, only sort data in the domain.
57
+ function filterIndex(I, values, specifiedDomain): number[] {
58
+ if (!Array.isArray(specifiedDomain)) return I;
59
+ const domain = new Set(specifiedDomain);
60
+ return I.filter((i) => domain.has(values[i]));
61
+ }
62
+
56
63
  function sortOrdinal(I, mark, options): [number[], G2Mark] {
57
64
  const { reverse, slice, channel, ...rest } = options;
58
- const { encode } = mark;
65
+ const { encode, scale = {} } = mark;
66
+ const domain = scale[channel]?.domain;
59
67
  const [T] = columnOf(encode, channel);
60
68
  const normalizeReducer = createReducer(channel, rest, encode);
61
- const sortedDomain = groupSort(I, normalizeReducer, (i: number) => T[i]);
69
+ const SI = filterIndex(I, T, domain);
70
+ const sortedDomain = groupSort(SI, normalizeReducer, (i: number) => T[i]);
62
71
  if (reverse) sortedDomain.reverse();
63
72
  const s = typeof slice === 'number' ? [0, slice] : slice;
64
73
  const slicedDomain = slice ? sortedDomain.slice(...s) : sortedDomain;