Skip to content

Commit 4663b95

Browse files
authored
refactor(statistic): 统一中心文本结构 (#1587)
1 parent 067f3c5 commit 4663b95

File tree

8 files changed

+60
-42
lines changed

8 files changed

+60
-42
lines changed

examples/liquid/basic/demo/style.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ const liquidPlot = new Liquid(document.getElementById('container'), {
66
autoFit: false,
77
percent: 0.75,
88
statistic: {
9-
formatter: (v) => {
10-
return `占比${v * 100}%`;
9+
content: {
10+
formatter: ({ percent }) => {
11+
return `占比${percent * 100}%`;
12+
},
1113
},
1214
},
13-
liquidStyle: (v) => {
15+
liquidStyle: ({ percent }) => {
1416
return {
15-
fill: v > 0.75 ? 'red' : '#acc9ff',
17+
fill: percent > 0.75 ? 'red' : '#acc9ff',
1618
};
1719
},
1820
color: () => '#acc9ff',

src/plots/liquid/adaptor.ts

+18-10
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,25 @@ function statistic(params: Params<LiquidOptions>): Params<LiquidOptions> {
6565
const { chart, options } = params;
6666
const { statistic, percent } = options;
6767

68-
const { style, formatter } = statistic;
68+
const { title, content } = statistic;
6969

70-
// annotation
71-
chart.annotation().text({
72-
top: true,
73-
position: {
74-
type: CAT_VALUE,
75-
percent: 0.5,
76-
},
77-
content: isFunction(formatter) ? formatter({ percent }) : `${percent}`,
78-
style: isFunction(style) ? style({ percent }) : style,
70+
// annotation title 和 content 分别使用一个 text
71+
[title, content].forEach((annotation) => {
72+
if (annotation) {
73+
const { formatter, style, offsetX, offsetY, rotate } = annotation;
74+
chart.annotation().text({
75+
top: true,
76+
position: {
77+
type: CAT_VALUE,
78+
percent: 0.5,
79+
},
80+
content: isFunction(formatter) ? formatter({ percent }) : `${percent}`,
81+
style: isFunction(style) ? style({ percent }) : style,
82+
offsetX,
83+
offsetY,
84+
rotate,
85+
});
86+
}
7987
});
8088

8189
return params;

src/plots/liquid/index.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ export class Liquid extends Plot<LiquidOptions> {
1919
color: '#6a99f9',
2020
radius: 0.9,
2121
statistic: {
22-
formatter: ({ percent }) => `${(percent * 100).toFixed(2)}%`,
23-
style: {
24-
opacity: 0.75,
25-
fontSize: 30,
26-
textAlign: 'center',
22+
title: false,
23+
content: {
24+
formatter: ({ percent }) => `${(percent * 100).toFixed(2)}%`,
25+
style: {
26+
opacity: 0.75,
27+
fontSize: 30,
28+
textAlign: 'center',
29+
},
2730
},
2831
},
2932
};

src/plots/liquid/types.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import { Datum } from '@antv/g2/lib/interface';
2-
import { Options, StyleAttr, ColorAttr } from '../../types';
3-
4-
type Statistic = {
5-
/** 统计文本的样式 */
6-
readonly style?: StyleAttr;
7-
/** 文本的格式化 */
8-
readonly formatter?: (datum: Datum) => string;
9-
};
1+
import { Options, StyleAttr, ColorAttr, Statistic } from '../../types';
102

113
/** 配置类型定义 */
124
export interface LiquidOptions extends Omit<Options, 'data'> {

src/plots/ring-progress/adaptor.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ function statistic(params: Params<RingProgressOptions>): Params<RingProgressOpti
2929
const { chart, options } = params;
3030
const { statistic, percent } = options;
3131

32+
// 先不处理 title 了,mini 应该没有 title 的需求了。
3233
const { content } = statistic;
3334

3435
if (content) {
35-
const { style, formatter } = content;
36+
const { style, formatter, offsetX, offsetY, rotate } = content;
3637
chart.annotation().text({
3738
position: ['50%', '50%'],
3839
content: formatter ? formatter({ percent }) : percent,
3940
style,
41+
offsetX,
42+
offsetY,
43+
rotate,
4044
});
4145
}
4246

src/plots/ring-progress/types.ts

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
import { Datum } from '@antv/g2/lib/interface';
2-
import { Options, StyleAttr, ColorAttr } from '../../types';
3-
4-
type StatisticText = {
5-
/** 统计文本的样式 */
6-
readonly style?: StyleAttr;
7-
/** 文本的格式化 */
8-
readonly formatter?: (datum: Datum) => string;
9-
};
10-
11-
type Statistic = {
12-
readonly content?: false | StatisticText;
13-
};
1+
import { Options, StyleAttr, ColorAttr, Statistic } from '../../types';
142

153
/** mini 图类型定义需要 omit 很多的 G2 Options 配置 */
164
export interface RingProgressOptions extends Omit<Options, 'data' | 'tooltip' | 'legend' | 'label' | 'color'> {

src/types/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './tooltip';
33
export * from './tuple';
44
export * from './state';
55
export * from './attr';
6+
export * from './statistic';

src/types/statistic.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { StyleAttr } from './attr';
2+
import { Datum } from './common';
3+
4+
type StatisticText = {
5+
/** 统计文本的样式 */
6+
readonly style?: StyleAttr;
7+
/** 文本的格式化 */
8+
readonly formatter?: (datum: Datum) => string;
9+
readonly rotate?: number;
10+
readonly offsetX?: number;
11+
readonly offsetY?: number;
12+
};
13+
14+
/**
15+
* 中心文本的统计信息,统一一个数据结构
16+
*/
17+
export type Statistic = {
18+
readonly title?: false | StatisticText;
19+
readonly content?: false | StatisticText;
20+
};

0 commit comments

Comments
 (0)