@antv/g2 5.0.4 → 5.0.5

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 (65) 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 +7 -5
  4. package/esm/api/chart.js +39 -25
  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 +1 -1
  14. package/esm/interaction/brushFilter.js +22 -8
  15. package/esm/interaction/brushFilter.js.map +1 -1
  16. package/esm/interaction/brushHighlight.js +1 -1
  17. package/esm/interaction/brushHighlight.js.map +1 -1
  18. package/esm/interaction/brushXFilter.d.ts +1 -1
  19. package/esm/interaction/brushYFilter.d.ts +1 -1
  20. package/esm/interaction/sliderFilter.js +16 -7
  21. package/esm/interaction/sliderFilter.js.map +1 -1
  22. package/esm/runtime/render.js +3 -1
  23. package/esm/runtime/render.js.map +1 -1
  24. package/esm/transform/sort.js +13 -3
  25. package/esm/transform/sort.js.map +1 -1
  26. package/esm/utils/size.d.ts +0 -10
  27. package/esm/utils/size.js +0 -23
  28. package/esm/utils/size.js.map +1 -1
  29. package/lib/api/chart.d.ts +7 -5
  30. package/lib/api/chart.js +38 -24
  31. package/lib/api/chart.js.map +1 -1
  32. package/lib/api/composition/base.d.ts +1 -0
  33. package/lib/api/composition/base.js +2 -2
  34. package/lib/api/composition/base.js.map +1 -1
  35. package/lib/component/axis.js +9 -7
  36. package/lib/component/axis.js.map +1 -1
  37. package/lib/component/slider.js +8 -5
  38. package/lib/component/slider.js.map +1 -1
  39. package/lib/interaction/brushFilter.d.ts +1 -1
  40. package/lib/interaction/brushFilter.js +22 -8
  41. package/lib/interaction/brushFilter.js.map +1 -1
  42. package/lib/interaction/brushHighlight.js +1 -1
  43. package/lib/interaction/brushHighlight.js.map +1 -1
  44. package/lib/interaction/brushXFilter.d.ts +1 -1
  45. package/lib/interaction/brushYFilter.d.ts +1 -1
  46. package/lib/interaction/sliderFilter.js +16 -7
  47. package/lib/interaction/sliderFilter.js.map +1 -1
  48. package/lib/runtime/render.js +3 -1
  49. package/lib/runtime/render.js.map +1 -1
  50. package/lib/transform/sort.js +13 -3
  51. package/lib/transform/sort.js.map +1 -1
  52. package/lib/utils/size.d.ts +0 -10
  53. package/lib/utils/size.js +1 -25
  54. package/lib/utils/size.js.map +1 -1
  55. package/package.json +2 -2
  56. package/src/api/chart.ts +49 -42
  57. package/src/api/composition/base.ts +4 -2
  58. package/src/component/axis.ts +12 -7
  59. package/src/component/slider.ts +10 -5
  60. package/src/interaction/brushFilter.ts +25 -8
  61. package/src/interaction/brushHighlight.ts +1 -1
  62. package/src/interaction/sliderFilter.ts +21 -7
  63. package/src/runtime/render.ts +3 -1
  64. package/src/transform/sort.ts +12 -3
  65. package/src/utils/size.ts +0 -31
@@ -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,
@@ -48,14 +48,14 @@ export function brushFilter(
48
48
  root.addEventListener('click', click);
49
49
 
50
50
  // Filter when brush created.
51
- function brushcreated(x, y, x1, y1) {
52
- filter(x, y, x1, y1);
51
+ function brushcreated(x, y, x1, y1, event) {
52
+ filter(x, y, x1, y1, event);
53
53
  brush.remove();
54
54
  }
55
55
 
56
56
  // Reset when dblclick.
57
57
  function click(e) {
58
- if (isDblclick(e)) reset();
58
+ if (isDblclick(e)) reset(e);
59
59
  }
60
60
 
61
61
  return () => {
@@ -65,7 +65,7 @@ export function brushFilter(
65
65
  }
66
66
 
67
67
  export function BrushFilter({ hideX = true, hideY = true, ...rest }) {
68
- return (target, viewInstances) => {
68
+ return (target, viewInstances, emitter) => {
69
69
  const { container, view, options: viewOptions, update } = target;
70
70
  const plotArea = selectPlotArea(container);
71
71
  const defaultOptions = {
@@ -82,7 +82,7 @@ export function BrushFilter({ hideX = true, hideY = true, ...rest }) {
82
82
 
83
83
  return brushFilter(plotArea, {
84
84
  brushRegion: (x, y, x1, y1) => [x, y, x1, y1],
85
- filter: async (x, y, x1, y1) => {
85
+ filter: async (x, y, x1, y1, event) => {
86
86
  // Avoid redundant filter.
87
87
  if (filtering) return;
88
88
  filtering = true;
@@ -99,6 +99,8 @@ export function BrushFilter({ hideX = true, hideY = true, ...rest }) {
99
99
 
100
100
  // Update the domain of x and y scale to filter data.
101
101
  const { marks } = viewOptions;
102
+ const domainX = domainOf(scaleX, [p0[0], p1[0]]);
103
+ const domainY = domainOf(scaleY, [p0[1], p1[1]]);
102
104
  const newMarks = marks.map((mark) =>
103
105
  deepMix(
104
106
  {
@@ -111,13 +113,18 @@ export function BrushFilter({ hideX = true, hideY = true, ...rest }) {
111
113
  mark,
112
114
  {
113
115
  scale: {
114
- x: { domain: domainOf(scaleX, [p0[0], p1[0]]) },
115
- y: { domain: domainOf(scaleY, [p0[1], p1[1]]) },
116
+ x: { domain: domainX },
117
+ y: { domain: domainY },
116
118
  },
117
119
  },
118
120
  ),
119
121
  );
120
122
 
123
+ // Emit event.
124
+ event.data = event.data || {};
125
+ event.data.selection = [domainX, domainY];
126
+ emitter.emit('brush:filter', event);
127
+
121
128
  // Rerender and update view.
122
129
  const newOptions = {
123
130
  ...viewOptions,
@@ -129,8 +136,18 @@ export function BrushFilter({ hideX = true, hideY = true, ...rest }) {
129
136
  filtering = false;
130
137
  filtered = true;
131
138
  },
132
- reset: () => {
139
+ reset: (event) => {
133
140
  if (filtering || !filtered) return;
141
+
142
+ // Emit event.
143
+ const { scale } = view;
144
+ const { x: scaleX, y: scaleY } = scale;
145
+ const domainX = scaleX.getOptions().domain;
146
+ const domainY = scaleY.getOptions().domain;
147
+ event.data = event.data || {};
148
+ event.data.selection = [domainX, domainY];
149
+ emitter.emit('brush:filter', event);
150
+
134
151
  filtered = false;
135
152
  newView = view;
136
153
  update(viewOptions);
@@ -211,7 +211,7 @@ export function brush(
211
211
  end = brushMousePosition(root, event);
212
212
  const [fx, fy, fx1, fy1] = updateMask(start, end);
213
213
  resizing = false;
214
- brushcreated(fx, fy, fx1, fy1);
214
+ brushcreated(fx, fy, fx1, fy1, event);
215
215
  };
216
216
 
217
217
  // Hide mask.
@@ -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;
package/src/utils/size.ts CHANGED
@@ -31,37 +31,6 @@ export function getContainerSize(container: HTMLElement): Size {
31
31
  };
32
32
  }
33
33
 
34
- /**
35
- *
36
- * @description Calculate the chart size.
37
- * @param container DOM container element.
38
- * @param autoFit Should auto fit.
39
- * @param width Chart width which is set by user.
40
- * @param height Chart height which is set by user.
41
- * @returns chart width and height.
42
- */
43
- export function getChartSize(
44
- container: HTMLElement,
45
- autoFit: boolean,
46
- width: number,
47
- height: number,
48
- ): Size {
49
- if (!autoFit) {
50
- return {
51
- width,
52
- height,
53
- };
54
- }
55
-
56
- // Get size from wrapper container.
57
- const size = getContainerSize(container);
58
-
59
- return {
60
- width: size.width,
61
- height: size.height,
62
- };
63
- }
64
-
65
34
  /**
66
35
  * @description Calculate the real canvas size by view options.
67
36
  */