Skip to content

Commit 54342e6

Browse files
authored
feat(area): 面积图支持line独立设置state,避免填充颜色出现问题 (#3053)
* feat(area): 面积图支持line独立设置state,避免填充颜色出现问题 * docs(area): 面积图增加 line state 文档 api 说明 * fix(area): 修复 ci 单测问题
1 parent 216c036 commit 54342e6

File tree

7 files changed

+137
-26
lines changed

7 files changed

+137
-26
lines changed
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { Area } from '../../../../src';
2+
import { partySupport } from '../../../data/party-support';
3+
import { createDiv } from '../../../utils/dom';
4+
5+
describe('area state', () => {
6+
const data = partySupport.filter((o) => ['FF', 'Lab'].includes(o.type));
7+
const options = {
8+
width: 400,
9+
height: 300,
10+
data,
11+
xField: 'date',
12+
yField: 'value',
13+
seriesField: 'type',
14+
line: {
15+
style: {
16+
lineWidth: 1,
17+
},
18+
},
19+
appendPadding: 10,
20+
};
21+
22+
it('set statesStyle', () => {
23+
const plot = new Area(createDiv(), {
24+
...options,
25+
animation: false,
26+
state: {
27+
selected: {
28+
style: {
29+
lineWidth: 4,
30+
fill: 'red',
31+
},
32+
},
33+
inactive: {
34+
style: {
35+
fill: 'blue',
36+
},
37+
},
38+
},
39+
line: {
40+
state: {
41+
selected: {
42+
style: {
43+
lineWidth: 4,
44+
},
45+
},
46+
},
47+
},
48+
});
49+
50+
plot.render();
51+
52+
plot.setState('selected', (d: any) => (Array.isArray(d) ? d[0].type : d.type) === data[0].type);
53+
const shape = plot.chart.geometries[0].elements[0].shape;
54+
const lineShape = plot.chart.geometries[1].elements[0].shape;
55+
56+
expect(lineShape.attr('lineWidth')).toBe(4);
57+
expect(lineShape.attr('fill')).not.toBe('red');
58+
expect(shape.attr('fill')).toBe('red');
59+
60+
// // 取消 selected
61+
plot.setState('selected', (d: any) => (Array.isArray(d) ? d[0].type : d.type) === data[0].type, false);
62+
plot.setState('inactive', (d: any) => (Array.isArray(d) ? d[0].type : d.type) === data[0].type);
63+
expect(shape.attr('fill')).toBe('blue');
64+
expect(plot.getStates()[0].state).toBe('inactive');
65+
66+
plot.destroy();
67+
});
68+
69+
it('set statesStyle by theme', () => {
70+
const plot = new Area(createDiv(), {
71+
...options,
72+
animation: false,
73+
theme: {
74+
geometries: {
75+
area: {
76+
area: {
77+
active: {
78+
style: {
79+
fill: 'yellow',
80+
fillOpacity: 0.65,
81+
},
82+
},
83+
},
84+
},
85+
},
86+
},
87+
});
88+
89+
plot.render();
90+
91+
plot.setState('active', (d: any) => (Array.isArray(d) ? d[0].type : d.type) === data[0].type);
92+
const shape = plot.chart.geometries[0].elements[0].shape;
93+
94+
expect(shape.attr('fill')).toBe('yellow');
95+
expect(shape.attr('fillOpacity')).toBe(0.65);
96+
expect(plot.getStates()[0].state).toBe('active');
97+
98+
plot.destroy();
99+
});
100+
});

docs/api/plots/area.en.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ Area graphic style.
7171

7272
Line graphic style in the Area.
7373

74-
| Properties | Type | Description |
75-
| ---------- | ---------------------------------------- | ------------------ |
76-
| color | _string \| string[] \| Function_ | Line color |
77-
| style | _ShapeStyle \| Function_ | Line graphic style |
78-
| size | _number \| [number, number] \| Function_ | Line width |
74+
| Properties | Type | Description |
75+
| ---------- | ---------------------------------------- | ------------------------------------------ |
76+
| color | _string \| string[] \| Function_ | Line color |
77+
| style | _ShapeStyle \| Function_ | Line graphic style |
78+
| size | _number \| [number, number] \| Function_ | Line width |
79+
| state | _object_ | State style of line,details to see: [#state] |
7980

8081
#### point
8182

@@ -126,4 +127,3 @@ Point graphic style in the Area.
126127
<description>**optional** _any_</description>
127128

128129
通过 `customInfo` 属性,可以向 shape 中传入自定义的数据。目前可能仅仅可能用于在 `registerShape` 的时候,像自定义 shape 中传入自定义的数据,方便实现自定义 shape 的配置能力。
129-

docs/api/plots/area.zh.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ order: 1
7171

7272
面积中折线的样式。
7373

74-
| 细分配置项名称 | 类型 | 功能描述 |
75-
| -------------- | ---------------------------------------- | -------- |
76-
| color | _string \| string[] \| Function_ | 颜色映射 |
77-
| style | _ShapeStyle \| Function_ | 样式映射 |
78-
| size | _number \| [number, number] \| Function_ | 折线宽度 |
74+
| 细分配置项名称 | 类型 | 功能描述 |
75+
| -------------- | ---------------------------------------- | ------------------------------------------ |
76+
| color | _string \| string[] \| Function_ | 颜色映射 |
77+
| style | _ShapeStyle \| Function_ | 样式映射 |
78+
| size | _number \| [number, number] \| Function_ | 折线宽度 |
79+
| state | _object_ | 面积图的描边折线的状态样式,类型同[#state] |
7980

8081
#### point
8182

@@ -126,4 +127,3 @@ order: 1
126127
<description>**可选** _any_</description>
127128

128129
通过 `customInfo` 属性,可以向 shape 中传入自定义的数据。目前可能仅仅可能用于在 `registerShape` 的时候,像自定义 shape 中传入自定义的数据,方便实现自定义 shape 的配置能力。
129-

src/plots/area/adaptor.ts

+21-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Geometry } from '@antv/g2';
2-
import { each } from '@antv/util';
2+
import { each, omit } from '@antv/util';
33
import { tooltip, slider, interaction, animation, theme, annotation, limitInPlot, pattern } from '../../adaptor/common';
44
import { findGeometry } from '../../utils';
55
import { Params } from '../../core/adaptor';
@@ -48,12 +48,6 @@ function geometry(params: Params<AreaOptions>): Params<AreaOptions> {
4848
const primary = deepAssign({}, params, {
4949
options: {
5050
area: { color, style: areaStyle },
51-
// 颜色保持一致,因为如果颜色不一致,会导致 tooltip 中元素重复。
52-
// 如果存在,才设置,否则为空
53-
line: lineMapping && {
54-
color,
55-
...lineMapping,
56-
},
5751
point: pointMapping && {
5852
color,
5953
...pointMapping,
@@ -66,10 +60,26 @@ function geometry(params: Params<AreaOptions>): Params<AreaOptions> {
6660
},
6761
},
6862
});
69-
// 线默认 2px
70-
const lineParams = deepAssign({ options: { line: { size: 2 } } }, primary, {
71-
options: { sizeField: seriesField, tooltip: false },
72-
});
63+
// 线默认 2px (折线不能复用面积图的 state,因为 fill 和 stroke 不匹配)
64+
const lineParams = {
65+
chart,
66+
options: deepAssign({ line: { size: 2 } }, omit(options as any, ['state']), {
67+
// 颜色保持一致,因为如果颜色不一致,会导致 tooltip 中元素重复。
68+
// 如果存在,才设置,否则为空
69+
line: lineMapping && {
70+
color,
71+
...lineMapping,
72+
},
73+
sizeField: seriesField,
74+
state: lineMapping?.state,
75+
tooltip: false,
76+
// label 不传递给各个 geometry adaptor,由 label adaptor 处理
77+
label: undefined,
78+
args: {
79+
startOnZero,
80+
},
81+
}),
82+
};
7383
const pointParams = deepAssign({}, primary, { options: { tooltip: false, state: pointState } });
7484

7585
// area geometry 处理

src/plots/area/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface AreaOptions extends Options, Pick<GeometryOptions, 'customInfo'
1818
/** 面积图形样式 */
1919
readonly areaStyle?: StyleAttr;
2020
/** 面积中折线的样式 */
21-
readonly line?: LineGeometryOptions['line'];
21+
readonly line?: LineGeometryOptions['line'] & Pick<PointGeometryOptions, 'state'>;
2222
/** 面积图数据点图形样式 */
2323
readonly point?: PointGeometryOptions['point'] & Pick<PointGeometryOptions, 'state'>;
2424
/** 积图是否从 0 基准线开始填充 */

src/plots/line/adaptor.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function geometry(params: Params<LineOptions>): Params<LineOptions> {
1616
const { chart, options } = params;
1717
const { data, color, lineStyle, lineShape, point: pointMapping, area: areaMapping, seriesField } = options;
1818
const pointState = pointMapping?.state;
19+
const areaState = areaMapping?.state;
1920

2021
chart.data(data);
2122

@@ -45,7 +46,7 @@ function geometry(params: Params<LineOptions>): Params<LineOptions> {
4546
},
4647
});
4748
const second = deepAssign({}, primary, { options: { tooltip: false, state: pointState } });
48-
const areaParams = deepAssign({}, primary, { options: { tooltip: false, state: pointState } });
49+
const areaParams = deepAssign({}, primary, { options: { tooltip: false, state: areaState } });
4950

5051
line(primary);
5152
point(second);

src/plots/line/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface LineOptions extends Options, Pick<GeometryOptions, 'customInfo'
2828
/** 折线数据点:1、图形映射属性 2、状态样式 */
2929
readonly point?: PointGeometryOptions['point'] & Pick<PointGeometryOptions, 'state'>;
3030
/** 折线趋势填充色:1、图形映射属性 */
31-
readonly area?: AreaGeometryOptions['area'];
31+
readonly area?: AreaGeometryOptions['area'] & Pick<PointGeometryOptions, 'state'>;
3232

3333
// 其他
3434
/** 坐标轴反转配置 */

0 commit comments

Comments
 (0)