Skip to content

Commit a328e7b

Browse files
authored
fix: 自定义图形颜色和主题切换 (#1989)
* docs: fix custom plot demo * docs: fix custom plot demo
1 parent 66d8341 commit a328e7b

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

examples/plugin/basic/demo/hill-column.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { isObject, deepMix } from '@antv/util';
12
import { P, G2 } from '@antv/g2plot';
23

34
// 自定义图形
45
G2.registerShape('interval', 'hill', {
56
draw(info, container) {
6-
const { points, style } = info;
7+
const { points, style, defaultStyle = {} } = info;
78

89
let path = [
910
['M', points[0].x, points[0].y],
@@ -17,6 +18,7 @@ G2.registerShape('interval', 'hill', {
1718
return container.addShape('path', {
1819
attrs: {
1920
path,
21+
...defaultStyle,
2022
...style,
2123
},
2224
});
@@ -31,7 +33,7 @@ const defaultOptions = {
3133
// 2. adaptor 实现
3234
function adaptor(params) {
3335
const { chart, options } = params;
34-
const { data, xField, yField, columnWidthRatio, columnStyle, seriesField } = options;
36+
const { data, xField, yField, columnWidthRatio, columnStyle, theme } = options;
3537

3638
// 数据
3739
chart.data(data);
@@ -46,9 +48,11 @@ function adaptor(params) {
4648
});
4749

4850
// 设置重叠比率
49-
chart.theme({
50-
columnWidthRatio: columnWidthRatio,
51-
});
51+
chart.theme(
52+
deepMix({}, isObject(theme) ? theme : G2.getTheme(theme), {
53+
columnWidthRatio: columnWidthRatio,
54+
})
55+
);
5256

5357
const gap = (1 / data.length / 2) * columnWidthRatio; // 左右预留
5458
chart.scale({
@@ -86,7 +90,6 @@ const hill = new P(
8690
yField: 'sold',
8791
columnStyle: {
8892
fillOpacity: 0.3,
89-
fill: 'red',
9093
},
9194
},
9295
adaptor,

examples/plugin/basic/demo/meta.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"zh": "自定义柱图",
1111
"en": "Custom hill column"
1212
},
13-
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*QcBKQYfdoKUAAAAAAAAAAAAAARQnAQ"
13+
"screenshot": "https://gw.alipayobjects.com/mdn/rms_d314dd/afts/img/A*zBrDQJhu1BwAAAAAAAAAAAAAARQnAQ"
1414
}
1515
]
1616
}

0 commit comments

Comments
 (0)