Skip to content

Commit e27a9c8

Browse files
committed
feat(liquid): add new plot liquid
1 parent 263caf5 commit e27a9c8

File tree

8 files changed

+510
-20
lines changed

8 files changed

+510
-20
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Liquid } from '../../../../src';
2+
import { createDiv } from '../../../utils/dom';
3+
4+
describe('liquid', () => {
5+
it('liquid', () => {
6+
const liquid = new Liquid(createDiv(), {
7+
width: 600,
8+
height: 300,
9+
// autoFit: false,
10+
appendPadding: 10,
11+
percent: 0.75,
12+
// color: (v: number) => {
13+
// return 'yellow';
14+
// },
15+
// color: 'blue',
16+
});
17+
18+
liquid.render();
19+
20+
// @ts-ignore
21+
window.liquid = liquid;
22+
23+
expect(liquid.chart.geometries[0].elements.length).toBe(1);
24+
});
25+
});

src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,6 @@ export { Heatmap, HeatmapOptions } from './plots/heatmap';
5959

6060
// 箱线图及类型定义
6161
export { Box, BoxOptions } from './plots/box';
62+
63+
// 水波图及类型定义
64+
export { Liquid, LiquidOptions } from './plots/liquid';

src/plots/liquid/adaptor.ts

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { isFunction } from '@antv/util';
2+
import { interaction, animation, theme, scale } from '../../adaptor/common';
3+
import { Params } from '../../core/adaptor';
4+
import { flow } from '../../utils';
5+
import { LiquidOptions } from './types';
6+
7+
const CAT_VALUE = 'liquid';
8+
9+
/**
10+
* geometry 处理
11+
* @param params
12+
*/
13+
function geometry(params: Params<LiquidOptions>): Params<LiquidOptions> {
14+
const { chart, options } = params;
15+
const { percent, color, liquidStyle } = options;
16+
17+
const data = [{ percent, type: CAT_VALUE }];
18+
19+
chart.data(data);
20+
21+
chart.scale({
22+
percent: {
23+
min: 0,
24+
max: 1,
25+
},
26+
});
27+
28+
const geometry = chart.interval().position('type*percent').shape('liquid-fill-gauge');
29+
30+
if (color) {
31+
geometry.color('percent', color);
32+
}
33+
34+
if (liquidStyle) {
35+
geometry.style('percent', (v: number) => {
36+
return isFunction(liquidStyle) ? liquidStyle(v) : liquidStyle;
37+
});
38+
}
39+
40+
// 关闭组件
41+
chart.legend(false);
42+
chart.axis(false);
43+
chart.tooltip(false);
44+
45+
return params;
46+
}
47+
48+
/**
49+
* 统计指标文档
50+
* @param params
51+
*/
52+
function statistic(params: Params<LiquidOptions>): Params<LiquidOptions> {
53+
const { chart, options } = params;
54+
const { statistic, percent } = options;
55+
56+
const { style, formatter } = statistic;
57+
58+
// annotation
59+
chart.annotation().text({
60+
top: true,
61+
position: {
62+
type: CAT_VALUE,
63+
percent: 0.5,
64+
},
65+
content: isFunction(formatter) ? formatter(percent) : `${percent}`,
66+
style: isFunction(style) ? style(percent) : style,
67+
});
68+
69+
return params;
70+
}
71+
72+
/**
73+
* 折线图适配器
74+
* @param chart
75+
* @param options
76+
*/
77+
export function adaptor(params: Params<LiquidOptions>) {
78+
// flow 的方式处理所有的配置到 G2 API
79+
return flow(geometry, statistic, scale({}), animation, theme, interaction)(params);
80+
}

src/plots/liquid/index.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Plot } from '../../core/plot';
2+
import { LiquidOptions } from './types';
3+
import { adaptor } from './adaptor';
4+
import { Adaptor } from '../../core/adaptor';
5+
// register liquid shape
6+
import './shapes/liquid';
7+
8+
export { LiquidOptions };
9+
10+
/**
11+
* 传说中的水波图
12+
*/
13+
export class Liquid extends Plot<LiquidOptions> {
14+
/** 图表类型 */
15+
public type: string = 'liquid';
16+
17+
protected getDefaultOptions(): Parital<LiquidOptions> {
18+
return {
19+
color: '#6a99f9',
20+
statistic: {
21+
formatter: (v: number) => `${(v * 100).toFixed(2)}%`,
22+
style: {
23+
opacity: 0.75,
24+
fontSize: 30,
25+
textAlign: 'center',
26+
},
27+
},
28+
};
29+
}
30+
31+
/**
32+
* 获取适配器
33+
*/
34+
protected getSchemaAdaptor(): Adaptor<LiquidOptions> {
35+
return adaptor;
36+
}
37+
}

0 commit comments

Comments
 (0)