Skip to content

Commit de389d3

Browse files
authored
feat(coordinate): 支持 coordinate 坐标转换配置 (#3172)
1 parent b046c23 commit de389d3

File tree

30 files changed

+271
-40
lines changed

30 files changed

+271
-40
lines changed

__tests__/bugs/bar-axis-label-spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('bar yaxis formatter', () => {
2424
yField: 'type',
2525
xAxis: {
2626
label: {
27-
formatter: (v) => 'hello',
27+
formatter: () => 'hello',
2828
},
2929
},
3030
});

__tests__/unit/plots/area/change-data-spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Area } from '../../../../src';
2-
import { getDataWhetherPecentage } from '../../../../src/utils/transform/percent';
2+
import { getDataWhetherPercentage } from '../../../../src/utils/transform/percent';
33
import { partySupport } from '../../../data/party-support';
44
import { createDiv } from '../../../utils/dom';
55

@@ -69,7 +69,7 @@ describe('area', () => {
6969
expect(area.chart.geometries[1].elements.length).toBe(2);
7070
expect(area.options.data).toEqual(partySupport.filter((o) => ['FF', 'Lab'].includes(o.type)));
7171
const { data, isPercent, xField, yField } = area.options;
72-
expect(area.chart.getData()).toEqual(getDataWhetherPecentage(data, yField, xField, yField, isPercent));
72+
expect(area.chart.getData()).toEqual(getDataWhetherPercentage(data, yField, xField, yField, isPercent));
7373

7474
area.destroy();
7575
});

__tests__/unit/plots/word-cloud/legend-spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { groupBy, keys } from '@antv/util';
21
import { WordCloud } from '../../../../src';
32
import { CountryEconomy } from '../../../data/country-economy';
43
import { createDiv } from '../../../utils/dom';

docs/api/plots/area.en.md

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Grouping field. For example, when we need to analyze the transaction volume tren
2929

3030
### Graphic Style
3131

32+
`markdown:docs/common/coordinate.en.md`
33+
3234
#### smooth
3335

3436
<description>**optional** _boolean_ _default:_ `false`</description>

docs/api/plots/area.zh.md

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ order: 1
2929

3030
### 图形样式
3131

32+
`markdown:docs/common/coordinate.zh.md`
33+
3234
#### smooth
3335

3436
<description>**optional** _boolean_ _default:_ `false`</description>

docs/api/plots/bar.en.md

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Whether the plot is Percent Bar. When isPercent is `true`, isStack must be `true
5959

6060
### Graphic Style
6161

62+
`markdown:docs/common/coordinate.en.md`
63+
6264
`markdown:docs/common/color.en.md`
6365

6466
#### pattern ✨

docs/api/plots/bar.zh.md

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ order: 3
5959

6060
### 图形样式
6161

62+
`markdown:docs/common/coordinate.zh.md`
63+
6264
`markdown:docs/common/color.zh.md`
6365

6466
#### pattern ✨

docs/api/plots/column.en.md

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Whether to percent columns, if isPercent is true, isStack also needs to be true.
5959

6060
### Geometry Style
6161

62+
`markdown:docs/common/coordinate.en.md`
63+
6264
`markdown:docs/common/color.en.md`
6365

6466
#### pattern ✨

docs/api/plots/column.zh.md

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ order: 2
6161

6262
### 图形样式
6363

64+
`markdown:docs/common/coordinate.zh.md`
65+
6466
`markdown:docs/common/color.zh.md`
6567

6668
#### pattern ✨

docs/common/coordinate.en.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#### coordinate
2+
3+
<description>**optional** _Transformation[] _</description>
4+
5+
Transformations of coordinate;
6+
7+
Types of _Transformation_ are as follows:
8+
9+
```ts
10+
type Transformation =
11+
| {
12+
type: 'reflectX'; // send (x, y) to (-x, y)
13+
}
14+
| {
15+
type: 'reflectY'; // send (x, y) to (x, -y)
16+
}
17+
| {
18+
type: 'transpose'; // send (x, y) to (y, x)
19+
};
20+
```

docs/common/coordinate.zh.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#### coordinate
2+
3+
<description>**optional** _Transformation[] _</description>
4+
5+
坐标转换配置。
6+
7+
__Transformation_ 类型定义如下:
8+
9+
```ts
10+
type Transformation =
11+
| {
12+
type: 'reflectX'; // send (x, y) to (-x, y)
13+
}
14+
| {
15+
type: 'reflectY'; // send (x, y) to (x, -y)
16+
}
17+
| {
18+
type: 'transpose'; // send (x, y) to (y, x)
19+
};
20+
```

examples/bar/basic/demo/coordinate.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Bar } from '@antv/g2plot';
2+
3+
const data = [
4+
{ year: '1951 年', value: 38 },
5+
{ year: '1952 年', value: 52 },
6+
{ year: '1956 年', value: 61 },
7+
{ year: '1957 年', value: 145 },
8+
{ year: '1958 年', value: 48 },
9+
];
10+
11+
const bar = new Bar('container', {
12+
data,
13+
xField: 'value',
14+
yField: 'year',
15+
seriesField: 'year',
16+
legend: {
17+
position: 'top-left',
18+
},
19+
coordinate: [{ type: 'reflectX' }, { type: 'reflectY' }],
20+
});
21+
22+
bar.render();

examples/bar/basic/demo/meta.json

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
},
1313
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/rWp4Sq5ofk/10256978-7f35-4a4c-b67c-39aaad269bc2.png"
1414
},
15+
{
16+
"filename": "coordinate.ts",
17+
"title": {
18+
"zh": "条形图设置坐标转换",
19+
"en": "Bar plot with coordinate transformations"
20+
},
21+
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/l41r3at5bs/becb13e0-3dc7-41e2-9458-651f6da6cdc3.png"
22+
},
1523
{
1624
"filename": "color.ts",
1725
"title": {
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { Column } from '@antv/g2plot';
2+
3+
const data = [
4+
{
5+
type: '家具家电',
6+
sales: 38,
7+
},
8+
{
9+
type: '粮油副食',
10+
sales: 52,
11+
},
12+
{
13+
type: '生鲜水果',
14+
sales: 61,
15+
},
16+
{
17+
type: '美容洗护',
18+
sales: 145,
19+
},
20+
{
21+
type: '母婴用品',
22+
sales: 48,
23+
},
24+
{
25+
type: '进口食品',
26+
sales: 38,
27+
},
28+
{
29+
type: '食品饮料',
30+
sales: 38,
31+
},
32+
{
33+
type: '家庭清洁',
34+
sales: 38,
35+
},
36+
];
37+
38+
const columnPlot = new Column('container', {
39+
data,
40+
xField: 'type',
41+
yField: 'sales',
42+
label: {
43+
// 可手动配置 label 数据标签位置
44+
position: 'middle', // 'top', 'bottom', 'middle',
45+
// 配置样式
46+
style: {
47+
fill: '#FFFFFF',
48+
opacity: 0.6,
49+
},
50+
},
51+
xAxis: {
52+
label: {
53+
autoHide: true,
54+
autoRotate: false,
55+
},
56+
},
57+
meta: {
58+
type: {
59+
alias: '类别',
60+
},
61+
sales: {
62+
alias: '销售额',
63+
},
64+
},
65+
coordinate: [{ type: 'reflectY' }],
66+
});
67+
68+
columnPlot.render();

examples/column/basic/demo/meta.json

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
},
1313
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*KAg2TY-oYRUAAAAAAAAAAAAAARQnAQ"
1414
},
15+
{
16+
"filename": "coordinate.ts",
17+
"title": {
18+
"zh": "倒转柱状图",
19+
"en": "Column plot reflectY"
20+
},
21+
"new": true,
22+
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/pgIqf6yZuq/510565ac-e2d6-4869-b71e-da8c5c12bd87.png"
23+
},
1524
{
1625
"filename": "color.ts",
1726
"title": {

examples/plugin/multi-view/demo/meta.json

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"zh": "定制股票图",
7575
"en": "Customized stock"
7676
},
77+
"new": true,
7778
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/%26P96Yg5fKh/78f5836e-efde-409d-a5b6-7dfe91cd2d04.png"
7879
}
7980
]

gatsby-config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ module.exports = {
320320
indexName: 'antv_g2plot',
321321
},
322322
announcement: {
323-
zh: '✨欢迎订阅图表指引,不定时会更新: https://www.yuque.com/antv/g2plot/plot-guide',
324-
en: '✨欢迎订阅图表指引,不定时会更新: https://www.yuque.com/antv/g2plot/plot-guide',
323+
zh: '1. ✨欢迎订阅图表指引,不定时会更新: https://www.yuque.com/antv/g2plot/plot-guide 2. 柱状图、条形图、面积图支持配置坐标系转换',
324+
en: '1. ✨欢迎订阅图表指引,不定时会更新: https://www.yuque.com/antv/g2plot/plot-guide 2. 柱状图、条形图、面积图支持配置坐标系转换',
325325
}
326326
},
327327
};

src/adaptor/common.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Geometry } from '@antv/g2';
1+
import { Geometry, Types } from '@antv/g2';
22
import { each, isNil, isObject } from '@antv/util';
33
import { Params } from '../core/adaptor';
44
import { Options } from '../types';
55
import { Interaction } from '../types/interaction';
6+
import { Transformations } from '../types/coordinate';
67
import { Axis } from '../types/axis';
78
import { AXIS_META_CONFIG_KEYS } from '../constant';
89
import { pick, deepAssign } from '../utils';
@@ -207,4 +208,30 @@ export function limitInPlot(params: Params<Options>): Params<Options> {
207208
return params;
208209
}
209210

211+
/**
212+
* 坐标系转换
213+
*/
214+
export function transformations(coordinateType: Types.CoordinateOption['type'] = 'rect') {
215+
return (params: Params<Options & { coordinate: Transformations }>) => {
216+
const { chart, options } = params;
217+
const { coordinate } = options;
218+
219+
const actions: Types.CoordinateActions[] = Array.from(coordinate || [])
220+
.map((cfg) => {
221+
if (cfg.type === 'reflectX') return ['reflect', 'x'] as Types.CoordinateActions;
222+
if (cfg.type === 'reflectY') return ['reflect', 'y'] as Types.CoordinateActions;
223+
if (cfg.type === 'transpose') return ['transpose'] as Types.CoordinateActions;
224+
225+
return null;
226+
})
227+
.filter((d) => !!d);
228+
229+
if (actions.length !== 0) {
230+
chart.coordinate({ type: coordinateType, actions });
231+
}
232+
233+
return params;
234+
};
235+
}
236+
210237
export { pattern } from './pattern';

src/plots/area/adaptor.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import { Geometry } from '@antv/g2';
22
import { each, omit } from '@antv/util';
3-
import { tooltip, slider, interaction, animation, theme, annotation, limitInPlot, pattern } from '../../adaptor/common';
3+
import {
4+
tooltip,
5+
slider,
6+
interaction,
7+
animation,
8+
theme,
9+
annotation,
10+
limitInPlot,
11+
pattern,
12+
transformations,
13+
} from '../../adaptor/common';
414
import { findGeometry } from '../../utils';
515
import { Params } from '../../core/adaptor';
616
import { area, point, line } from '../../adaptor/geometries';
717
import { flow, transformLabel, deepAssign } from '../../utils';
8-
import { getDataWhetherPecentage } from '../../utils/transform/percent';
18+
import { getDataWhetherPercentage } from '../../utils/transform/percent';
919
import { Datum } from '../../types';
1020
import { meta, legend, axis } from '../line/adaptor';
1121
import { AreaOptions } from './types';
@@ -33,7 +43,7 @@ function geometry(params: Params<AreaOptions>): Params<AreaOptions> {
3343
} = options;
3444
const pointState = pointMapping?.state;
3545

36-
const chartData = getDataWhetherPecentage(data, yField, xField, yField, isPercent);
46+
const chartData = getDataWhetherPercentage(data, yField, xField, yField, isPercent);
3747
chart.data(chartData);
3848
// 百分比堆积图,默认会给一个 % 格式化逻辑, 用户可自定义
3949
const tooltipOptions = isPercent
@@ -149,6 +159,7 @@ export function adaptor(params: Params<AreaOptions>) {
149159
return flow(
150160
theme,
151161
pattern('areaStyle'),
162+
transformations('rect'),
152163
geometry,
153164
meta,
154165
adjust,

src/plots/area/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Plot } from '../../core/plot';
22
import { Adaptor } from '../../core/adaptor';
3-
import { getDataWhetherPecentage } from '../../utils/transform/percent';
3+
import { getDataWhetherPercentage } from '../../utils/transform/percent';
44
import { AreaOptions } from './types';
55
import { adaptor, meta } from './adaptor';
66
import { DEFAULT_OPTIONS } from './constants';
@@ -35,7 +35,7 @@ export class Area extends Plot<AreaOptions> {
3535
const { isPercent, xField, yField } = this.options;
3636
const { chart, options } = this;
3737
meta({ chart, options });
38-
this.chart.changeData(getDataWhetherPecentage(data, yField, xField, yField, isPercent));
38+
this.chart.changeData(getDataWhetherPercentage(data, yField, xField, yField, isPercent));
3939
}
4040

4141
/**

src/plots/area/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { GeometryOptions, LineGeometryOptions, PointGeometryOptions } from '../../adaptor/geometries';
22
import { Options, StyleAttr } from '../../types';
3+
import { Transformations } from '../../types/coordinate';
34

45
/** 面积图的配置类型定义 */
56
export interface AreaOptions extends Options, Pick<GeometryOptions, 'customInfo'> {
@@ -48,4 +49,10 @@ export interface AreaOptions extends Options, Pick<GeometryOptions, 'customInfo'
4849
* @default false
4950
*/
5051
readonly startOnZero?: boolean;
52+
53+
/**
54+
* @title 坐标转换
55+
* @description 可以对坐标系进行转换,如: reflectX, reflectY, transpose 等
56+
*/
57+
readonly coordinate?: Transformations;
5158
}

src/plots/bar/adaptor.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ function legend(params: Params<BarOptions>): Params<BarOptions> {
103103
* @param params
104104
*/
105105
function coordinate(params: Params<BarOptions>): Params<BarOptions> {
106-
const { chart } = params;
107106
// transpose column to bar 对角变换 & y 方向镜像变换
108-
chart.coordinate({ actions: [['transpose'], ['reflect', 'y']] });
109-
return params;
107+
const { options } = params;
108+
const coordinateOptions = [{ type: 'transpose' }, { type: 'reflectY' }].concat(options.coordinate || []);
109+
return deepAssign({}, params, { options: { coordinate: coordinateOptions } });
110110
}
111111

112112
/**

0 commit comments

Comments
 (0)