@antv/g2plot 2.4.1 → 2.4.2

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 (55) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/g2plot.min.js +4 -4
  3. package/dist/g2plot.min.js.map +1 -1
  4. package/esm/index.d.ts +9 -1
  5. package/esm/index.js +9 -1
  6. package/esm/index.js.map +1 -1
  7. package/esm/plots/area/adaptor.js +15 -5
  8. package/esm/plots/area/adaptor.js.map +1 -1
  9. package/esm/plots/area/constants.d.ts +2 -0
  10. package/esm/plots/area/constants.js +2 -0
  11. package/esm/plots/area/constants.js.map +1 -1
  12. package/esm/plots/area/index.js +11 -4
  13. package/esm/plots/area/index.js.map +1 -1
  14. package/esm/plots/line/adaptor.js +30 -15
  15. package/esm/plots/line/adaptor.js.map +1 -1
  16. package/esm/plots/line/constants.d.ts +2 -0
  17. package/esm/plots/line/constants.js +3 -0
  18. package/esm/plots/line/constants.js.map +1 -1
  19. package/esm/plots/line/index.js +8 -1
  20. package/esm/plots/line/index.js.map +1 -1
  21. package/esm/plots/line/interactions/marker-active.js +8 -11
  22. package/esm/plots/line/interactions/marker-active.js.map +1 -1
  23. package/esm/utils/geometry.js +2 -1
  24. package/esm/utils/geometry.js.map +1 -1
  25. package/lib/index.d.ts +9 -1
  26. package/lib/index.js +13 -5
  27. package/lib/index.js.map +1 -1
  28. package/lib/plots/area/adaptor.js +13 -3
  29. package/lib/plots/area/adaptor.js.map +1 -1
  30. package/lib/plots/area/constants.d.ts +2 -0
  31. package/lib/plots/area/constants.js +3 -1
  32. package/lib/plots/area/constants.js.map +1 -1
  33. package/lib/plots/area/index.js +10 -3
  34. package/lib/plots/area/index.js.map +1 -1
  35. package/lib/plots/line/adaptor.js +29 -14
  36. package/lib/plots/line/adaptor.js.map +1 -1
  37. package/lib/plots/line/constants.d.ts +2 -0
  38. package/lib/plots/line/constants.js +4 -1
  39. package/lib/plots/line/constants.js.map +1 -1
  40. package/lib/plots/line/index.js +7 -0
  41. package/lib/plots/line/index.js.map +1 -1
  42. package/lib/plots/line/interactions/marker-active.js +8 -11
  43. package/lib/plots/line/interactions/marker-active.js.map +1 -1
  44. package/lib/utils/geometry.js +2 -1
  45. package/lib/utils/geometry.js.map +1 -1
  46. package/package.json +1 -1
  47. package/src/index.ts +9 -1
  48. package/src/plots/area/adaptor.ts +16 -5
  49. package/src/plots/area/constants.ts +3 -0
  50. package/src/plots/area/index.ts +10 -3
  51. package/src/plots/line/adaptor.ts +35 -14
  52. package/src/plots/line/constants.ts +4 -0
  53. package/src/plots/line/index.ts +8 -1
  54. package/src/plots/line/interactions/marker-active.ts +9 -12
  55. package/src/utils/geometry.ts +2 -1
@@ -1,13 +1,14 @@
1
1
  import { Geometry } from '@antv/g2';
2
- import { each } from '@antv/util';
2
+ import { each, isNil } from '@antv/util';
3
3
  import { tooltip, slider, interaction, animation, theme, annotation, limitInPlot, pattern } from '../../adaptor/common';
4
- import { findGeometry } from '../../utils';
4
+ import { findGeometry, getAllGeometriesRecursively } from '../../utils';
5
5
  import { Params } from '../../core/adaptor';
6
6
  import { area, point, line } from '../../adaptor/geometries';
7
7
  import { flow, transformLabel, deepAssign } from '../../utils';
8
8
  import { getDataWhetherPecentage } from '../../utils/transform/percent';
9
9
  import { Datum } from '../../types';
10
10
  import { meta, legend, axis } from '../line/adaptor';
11
+ import { POINT_VIEW_ID } from './constants';
11
12
  import { AreaOptions } from './types';
12
13
 
13
14
  export { meta };
@@ -70,12 +71,21 @@ function geometry(params: Params<AreaOptions>): Params<AreaOptions> {
70
71
  const lineParams = deepAssign({ options: { line: { size: 2 } } }, primary, {
71
72
  options: { sizeField: seriesField, tooltip: false },
72
73
  });
73
- const pointParams = deepAssign({}, primary, { options: { tooltip: false, state: pointState } });
74
74
 
75
75
  // area geometry 处理
76
76
  area(primary);
77
77
  line(lineParams);
78
- point(pointParams);
78
+
79
+ const pointParams = deepAssign({}, primary, { options: { tooltip: false, state: pointState } });
80
+ if (pointMapping) {
81
+ const pointView = chart.createView({ id: POINT_VIEW_ID });
82
+ pointView.axis(false);
83
+ pointView.tooltip(false);
84
+ pointView.legend(false);
85
+ // [PERFORMANCE] 🚀 数据为空的 point 标注点都不渲染(不包括:数据为 0)
86
+ pointView.data(data.filter((d) => !isNil(d[yField])));
87
+ point({ ...pointParams, chart: pointView });
88
+ }
79
89
 
80
90
  return params;
81
91
  }
@@ -121,7 +131,8 @@ function adjust(params: Params<AreaOptions>): Params<AreaOptions> {
121
131
  const { chart, options } = params;
122
132
  const { isStack, isPercent, seriesField } = options;
123
133
  if ((isPercent || isStack) && seriesField) {
124
- each(chart.geometries, (g: Geometry) => {
134
+ const geometries = getAllGeometriesRecursively(chart);
135
+ each(geometries, (g: Geometry) => {
125
136
  g.adjust('stack');
126
137
  });
127
138
  }
@@ -1,6 +1,9 @@
1
1
  import { Plot } from '../../core/plot';
2
2
  import { deepAssign } from '../../utils';
3
3
 
4
+ /** point 标注点视图 */
5
+ export const POINT_VIEW_ID = 'point-view';
6
+
4
7
  /**
5
8
  * 面积图默认配置项
6
9
  */
@@ -1,9 +1,11 @@
1
+ import { isNil } from '@antv/util';
1
2
  import { Plot } from '../../core/plot';
2
3
  import { Adaptor } from '../../core/adaptor';
3
4
  import { getDataWhetherPecentage } from '../../utils/transform/percent';
5
+ import { findViewById } from '../../utils';
4
6
  import { AreaOptions } from './types';
5
7
  import { adaptor, meta } from './adaptor';
6
- import { DEFAULT_OPTIONS } from './constants';
8
+ import { DEFAULT_OPTIONS, POINT_VIEW_ID } from './constants';
7
9
 
8
10
  export type { AreaOptions };
9
11
 
@@ -32,10 +34,15 @@ export class Area extends Plot<AreaOptions> {
32
34
  */
33
35
  public changeData(data: AreaOptions['data']) {
34
36
  this.updateOption({ data });
35
- const { isPercent, xField, yField } = this.options;
36
37
  const { chart, options } = this;
38
+ const { isPercent, xField, yField } = options;
37
39
  meta({ chart, options });
38
- this.chart.changeData(getDataWhetherPecentage(data, yField, xField, yField, isPercent));
40
+ const chartdata = getDataWhetherPecentage(data, yField, xField, yField, isPercent);
41
+ this.chart.changeData(chartdata);
42
+ const pointView = findViewById(this.chart, POINT_VIEW_ID);
43
+ if (pointView) {
44
+ pointView.changeData(data.filter((d) => !isNil(d[yField])));
45
+ }
39
46
  }
40
47
 
41
48
  /**
@@ -1,11 +1,11 @@
1
1
  import { Geometry } from '@antv/g2';
2
- import { each, isArray } from '@antv/util';
2
+ import { each, isArray, isNil, uniq } from '@antv/util';
3
3
  import { Params } from '../../core/adaptor';
4
4
  import { tooltip, slider, interaction, animation, theme, scale, annotation, limitInPlot } from '../../adaptor/common';
5
- import { findGeometry, transformLabel, deepAssign } from '../../utils';
5
+ import { flow, findGeometry, transformLabel, deepAssign, getAllGeometriesRecursively } from '../../utils';
6
6
  import { point, line, area } from '../../adaptor/geometries';
7
- import { flow } from '../../utils';
8
7
  import { adjustYMetaByZero } from '../../utils/data';
8
+ import { POINT_VIEW_ID } from './constants';
9
9
  import { LineOptions } from './types';
10
10
 
11
11
  /**
@@ -14,7 +14,7 @@ import { LineOptions } from './types';
14
14
  */
15
15
  function geometry(params: Params<LineOptions>): Params<LineOptions> {
16
16
  const { chart, options } = params;
17
- const { data, color, lineStyle, lineShape, point: pointMapping, area: areaMapping, seriesField } = options;
17
+ const { data, color, lineStyle, lineShape, point: pointMapping, area: areaMapping, yField, seriesField } = options;
18
18
  const pointState = pointMapping?.state;
19
19
 
20
20
  chart.data(data);
@@ -44,13 +44,23 @@ function geometry(params: Params<LineOptions>): Params<LineOptions> {
44
44
  label: undefined,
45
45
  },
46
46
  });
47
- const second = deepAssign({}, primary, { options: { tooltip: false, state: pointState } });
47
+
48
48
  const areaParams = deepAssign({}, primary, { options: { tooltip: false, state: pointState } });
49
49
 
50
50
  line(primary);
51
- point(second);
52
51
  area(areaParams);
53
52
 
53
+ const pointParams = deepAssign({}, primary, { options: { tooltip: false, state: pointState } });
54
+ if (pointMapping) {
55
+ const pointView = chart.createView({ id: POINT_VIEW_ID });
56
+ pointView.axis(false);
57
+ pointView.tooltip(false);
58
+ pointView.legend(false);
59
+ // [PERFORMANCE] 🚀 数据为空的 point 标注点都不渲染(不包括:数据为 0)
60
+ pointView.data(data.filter((d) => !isNil(d[yField])));
61
+ point({ ...pointParams, chart: pointView });
62
+ }
63
+
54
64
  return params;
55
65
  }
56
66
 
@@ -60,7 +70,21 @@ function geometry(params: Params<LineOptions>): Params<LineOptions> {
60
70
  */
61
71
  export function meta(params: Params<LineOptions>): Params<LineOptions> {
62
72
  const { options } = params;
63
- const { xAxis, yAxis, xField, yField, data } = options;
73
+ const { xAxis, yAxis, xField, yField, data, seriesField } = options;
74
+ const defaultMeta = {
75
+ [xField]: {
76
+ type: 'cat',
77
+ sync: true,
78
+ values: uniq(data.map((d) => d[xField])),
79
+ },
80
+ [yField]: {
81
+ ...adjustYMetaByZero(data, yField),
82
+ sync: true,
83
+ },
84
+ };
85
+ if (seriesField) {
86
+ defaultMeta[seriesField] = { sync: true };
87
+ }
64
88
 
65
89
  return flow(
66
90
  scale(
@@ -68,12 +92,7 @@ export function meta(params: Params<LineOptions>): Params<LineOptions> {
68
92
  [xField]: xAxis,
69
93
  [yField]: yAxis,
70
94
  },
71
- {
72
- [xField]: {
73
- type: 'cat',
74
- },
75
- [yField]: adjustYMetaByZero(data, yField),
76
- }
95
+ defaultMeta
77
96
  )
78
97
  )(params);
79
98
  }
@@ -180,8 +199,10 @@ export function adjust(params: Params<Pick<LineOptions, 'isStack'>>): Params<any
180
199
  const { chart, options } = params;
181
200
  const { isStack } = options;
182
201
 
202
+ const geometries = getAllGeometriesRecursively(chart);
203
+
183
204
  if (isStack) {
184
- each(chart.geometries, (g: Geometry) => {
205
+ each(geometries, (g: Geometry) => {
185
206
  g.adjust('stack');
186
207
  });
187
208
  }
@@ -1,10 +1,14 @@
1
1
  import { Plot } from '../../core/plot';
2
2
  import { deepAssign } from '../../utils';
3
3
 
4
+ /** point 标注点视图 */
5
+ export const POINT_VIEW_ID = 'point-view';
6
+
4
7
  /**
5
8
  * 折线图默认配置项
6
9
  */
7
10
  export const DEFAULT_OPTIONS = deepAssign({}, Plot.getDefaultOptions(), {
11
+ syncViewPadding: true,
8
12
  tooltip: {
9
13
  shared: true,
10
14
  showMarkers: true,
@@ -1,8 +1,10 @@
1
+ import { isNil } from '@antv/util';
1
2
  import { Plot } from '../../core/plot';
2
3
  import { Adaptor } from '../../core/adaptor';
4
+ import { findViewById } from '../../utils';
3
5
  import { LineOptions } from './types';
4
6
  import { adaptor, meta } from './adaptor';
5
- import { DEFAULT_OPTIONS } from './constants';
7
+ import { DEFAULT_OPTIONS, POINT_VIEW_ID } from './constants';
6
8
  import './interactions';
7
9
 
8
10
  export type { LineOptions };
@@ -26,8 +28,13 @@ export class Line extends Plot<LineOptions> {
26
28
  public changeData(data: LineOptions['data']) {
27
29
  this.updateOption({ data });
28
30
  const { chart, options } = this;
31
+ const { yField } = options;
29
32
  meta({ chart, options });
30
33
  this.chart.changeData(data);
34
+ const pointView = findViewById(this.chart, POINT_VIEW_ID);
35
+ if (pointView) {
36
+ pointView.changeData(data.filter((d) => !isNil(d[yField])));
37
+ }
31
38
  }
32
39
 
33
40
  /**
@@ -1,5 +1,6 @@
1
1
  import { each, findIndex } from '@antv/util';
2
- import { InteractionAction, View, Geometry } from '@antv/g2';
2
+ import { InteractionAction, View } from '@antv/g2';
3
+ import { findGeometry } from '../../../utils';
3
4
 
4
5
  export class MarkerActiveAction extends InteractionAction {
5
6
  public active() {
@@ -8,23 +9,19 @@ export class MarkerActiveAction extends InteractionAction {
8
9
  if (evt.data) {
9
10
  // items: 数组对象,当前 tooltip 显示的每条内容
10
11
  const { items } = evt.data;
11
- const points = view.geometries.filter((geom) => geom.type === 'point');
12
- each(points, (point: Geometry) => {
13
- each(point.elements, (element) => {
14
- const active = findIndex(items, (item) => (item as any).data === element.data) !== -1;
15
- element.setState('active', active);
16
- });
12
+ const point = findGeometry(view, 'point');
13
+ each(point.elements, (element) => {
14
+ const active = findIndex(items, (item) => (item as any).data === element.data) !== -1;
15
+ element.setState('active', active);
17
16
  });
18
17
  }
19
18
  }
20
19
 
21
20
  public reset() {
22
21
  const view = this.getView();
23
- const points = view.geometries.filter((geom) => geom.type === 'point');
24
- each(points, (point: Geometry) => {
25
- each(point.elements, (element) => {
26
- element.setState('active', false);
27
- });
22
+ const point = findGeometry(view, 'point');
23
+ each(point.elements, (element) => {
24
+ element.setState('active', false);
28
25
  });
29
26
  }
30
27
 
@@ -7,7 +7,8 @@ import { reduce, get } from '@antv/util';
7
7
  * @param type
8
8
  */
9
9
  export function findGeometry(view: View, type: string): Geometry {
10
- return view.geometries.find((g: Geometry) => g.type === type);
10
+ const geometries = getAllGeometriesRecursively(view);
11
+ return geometries.find((g: Geometry) => g.type === type);
11
12
  }
12
13
 
13
14
  /**