-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathQuarterSimpleCard.tsx
More file actions
196 lines (181 loc) · 4.15 KB
/
Copy pathQuarterSimpleCard.tsx
File metadata and controls
196 lines (181 loc) · 4.15 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/** @jsxImportSource @antv/infographic-jsx */
import {
ComponentType,
Group,
Path,
getElementBounds,
} from '@antv/infographic-jsx';
import { ItemDesc, ItemIcon, ItemLabel } from '../components';
import { getItemProps } from '../utils';
import { registerItem } from './registry';
import type { BaseItemProps } from './types';
export interface QuarterSimpleCardProps extends BaseItemProps {
width?: number;
height?: number;
iconSize?: number;
padding?: number;
borderRadius?: number;
}
export const QuarterSimpleCard: ComponentType<QuarterSimpleCardProps> = (
props,
) => {
const [
{
datum,
indexes,
width = 150,
height = 150,
iconSize = 30,
padding = 20,
borderRadius = 16,
positionH = 'normal',
positionV = 'normal',
themeColors,
},
restProps,
] = getItemProps(props, [
'width',
'height',
'iconSize',
'padding',
'borderRadius',
]);
// 计算内容区域
const contentWidth = width - padding * 2;
const contentX = padding;
const contentY = padding;
// 图标位置
const iconX =
positionH === 'flipped'
? width - padding - iconSize
: positionH === 'center'
? (width - iconSize) / 2
: contentX;
const iconY = contentY;
// 标签位置(图标下方)
const labelY = iconY + iconSize + 8;
const labelBounds = getElementBounds(
<ItemLabel indexes={indexes} width={contentWidth}>
{datum.label}
</ItemLabel>,
);
const labelX =
positionH === 'flipped'
? width - padding - contentWidth
: positionH === 'center'
? padding
: contentX;
// 描述位置(标签下方)
const descY = labelY + labelBounds.height + 4;
const descX = labelX;
// 根据 positionH 和 positionV 决定直角位置
const r = borderRadius;
let cardPath = '';
if (positionH === 'flipped' && positionV === 'flipped') {
// 直角在左下角
cardPath = `
M ${r} 0
L ${width - r} 0
Q ${width} 0 ${width} ${r}
L ${width} ${height - r}
Q ${width} ${height} ${width - r} ${height}
L ${r} ${height}
Q 0 ${height} 0 ${height - r}
L 0 0
L ${r} 0
Z
`;
} else if (positionH === 'normal' && positionV === 'flipped') {
// 右上角为直角,其它角为圆角
cardPath = `
M 0 0
L ${width} 0
L ${width} ${height - r}
Q ${width} ${height} ${width - r} ${height}
L ${r} ${height}
Q 0 ${height} 0 ${height - r}
L 0 ${r}
Q 0 0 ${r} 0
Z
`;
} else if (positionH === 'flipped') {
// 直角在左上角
cardPath = `
M ${r} 0
L ${width - r} 0
Q ${width} 0 ${width} ${r}
L ${width} ${height - r}
Q ${width} ${height} ${width - r} ${height}
L 0 ${height}
L 0 ${r}
Q 0 0 ${r} 0
Z
`;
} else {
// 默认逻辑:直角在右下角
cardPath = `
M ${r} 0
L ${width - r} 0
Q ${width} 0 ${width} ${r}
L ${width} ${height}
L ${r} ${height}
Q 0 ${height} 0 ${height - r}
L 0 ${r}
Q 0 0 ${r} 0
Z
`;
}
return (
<Group {...restProps}>
{/* 背景卡片 - 三个圆角 */}
<Path
d={cardPath}
x={0}
y={0}
width={width}
height={height}
fill={themeColors.colorPrimary}
/>
{/* 图标 */}
<ItemIcon
indexes={indexes}
x={iconX}
y={iconY}
size={iconSize}
fill={themeColors.colorBg}
/>
{/* 标签 */}
<ItemLabel
indexes={indexes}
x={labelX}
y={labelY}
width={contentWidth}
fontSize={14}
fontWeight="bold"
fill={themeColors.colorBg}
alignHorizontal={positionH === 'flipped' ? 'right' : 'left'}
>
{datum.label}
</ItemLabel>
{/* 描述 */}
{datum.desc && (
<ItemDesc
indexes={indexes}
x={descX}
y={descY}
width={contentWidth}
fontSize={11}
wordWrap={true}
fill={themeColors.colorBg}
alignHorizontal={positionH === 'flipped' ? 'right' : 'left'}
>
{datum.desc}
</ItemDesc>
)}
</Group>
);
};
registerItem('quarter-simple-card', {
component: QuarterSimpleCard,
composites: ['icon', 'label', 'desc'],
});