-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathBadgeCard.tsx
More file actions
142 lines (129 loc) · 3.44 KB
/
Copy pathBadgeCard.tsx
File metadata and controls
142 lines (129 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/** @jsxImportSource @antv/infographic-jsx */
import {
ComponentType,
Defs,
Ellipse,
Group,
Rect,
} from '@antv/infographic-jsx';
import tinycolor from 'tinycolor2';
import { ItemDesc, ItemIcon, ItemLabel, ItemValue } from '../components';
import { getItemProps } from '../utils';
import { registerItem } from './registry';
import type { BaseItemProps } from './types';
export interface BadgeCardProps extends BaseItemProps {
width?: number;
height?: number;
iconSize?: number;
badgeSize?: number;
gap?: number;
}
export const BadgeCard: ComponentType<BadgeCardProps> = (props) => {
const [
{
datum,
indexes,
width = 200,
height = 80,
iconSize = 24,
badgeSize = 32,
gap = 8,
positionH = 'normal',
themeColors,
valueFormatter,
},
restProps,
] = getItemProps(props, ['width', 'height', 'iconSize', 'badgeSize', 'gap']);
const value = datum.value;
const gradientId = `${themeColors.colorPrimary}-badge`;
const badgeX = positionH === 'flipped' ? gap : width - gap - badgeSize;
const badgeY = gap;
const contentStartX = positionH === 'flipped' ? badgeSize + 2 * gap : gap;
const contentWidth = width - badgeSize - 3 * gap;
return (
<Group {...restProps}>
<Defs>
<radialGradient id={gradientId} cx="50%" cy="50%" r="50%">
<stop offset="0%" stopColor={themeColors.colorPrimary} />
<stop
offset="100%"
stopColor={tinycolor(themeColors.colorPrimary)
.darken(20)
.toHexString()}
/>
</radialGradient>
</Defs>
{/* 背景卡片 */}
<Rect
x={0}
y={0}
width={width}
height={height}
fill={themeColors.colorPrimaryBg}
rx={8}
ry={8}
/>
{/* 徽章背景 */}
<Ellipse
x={badgeX}
y={badgeY}
width={badgeSize}
height={badgeSize}
fill={`url(#${gradientId})`}
/>
{/* 徽章图标 */}
<ItemIcon
indexes={indexes}
x={badgeX + (badgeSize - iconSize) / 2}
y={badgeY + (badgeSize - iconSize) / 2}
size={iconSize}
fill={themeColors.colorWhite}
/>
{/* 标签 */}
<ItemLabel
indexes={indexes}
x={contentStartX}
y={gap + 2}
width={contentWidth}
alignHorizontal={positionH === 'flipped' ? 'right' : 'left'}
fontSize={14}
fill={themeColors.colorText}
>
{datum.label}
</ItemLabel>
{/* 数值 */}
{value !== undefined && (
<ItemValue
indexes={indexes}
x={contentStartX}
y={gap + 22}
width={contentWidth}
alignHorizontal={positionH === 'flipped' ? 'right' : 'left'}
fontSize={18}
fontWeight="bold"
fill={themeColors.colorPrimary}
value={value}
formatter={valueFormatter}
/>
)}
{/* 描述 */}
<ItemDesc
indexes={indexes}
x={contentStartX}
y={value !== undefined ? gap + 45 : gap + 20}
width={contentWidth}
alignHorizontal={positionH === 'flipped' ? 'right' : 'left'}
fontSize={11}
fill={themeColors.colorTextSecondary}
lineNumber={2}
wordWrap={true}
>
{datum.desc}
</ItemDesc>
</Group>
);
};
registerItem('badge-card', {
component: BadgeCard,
composites: ['icon', 'label', 'value', 'desc'],
});