-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathPyramid.tsx
More file actions
189 lines (171 loc) · 4.93 KB
/
Copy pathPyramid.tsx
File metadata and controls
189 lines (171 loc) · 4.93 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
/** @jsxImportSource @antv/infographic-jsx */
import {
ComponentType,
Defs,
getElementBounds,
Group,
Point,
Polygon,
Rect,
} from '@antv/infographic-jsx';
import roundPolygon, { getSegments } from 'round-polygon';
import tinycolor from 'tinycolor2';
import { ItemDesc, ItemIcon, ItemLabel } from '../components';
import { getItemId, getItemProps } from '../utils';
import { registerItem } from './registry';
import type { BaseItemProps } from './types';
export interface PyramidProps extends BaseItemProps {
gap?: number;
width?: number;
height?: number;
iconSize?: number;
pyramidWidth?: number;
}
export const Pyramid: ComponentType<PyramidProps> = (props) => {
const [
{
datum,
data,
indexes,
gap = 5,
width = 300,
height = 60,
iconSize = 30,
pyramidWidth = width * 0.6,
themeColors,
},
restProps,
] = getItemProps(props, [
'gap',
'width',
'height',
'iconSize',
'pyramidWidth',
]);
const radius = 5;
const { points, topWidth, bottomWidth } = calculateTriangleSegment(
pyramidWidth,
height,
gap,
data.items.length,
indexes[0],
);
const rounded = roundPolygon(points, radius);
const segments = getSegments(rounded, 'AMOUNT', 10);
const isFirst = indexes[0] === 0;
const pyramidCenterX = pyramidWidth / 2;
const rightTopX = pyramidCenterX + topWidth / 2;
const rightCenterX = rightTopX + (bottomWidth - topWidth) / 4;
const rightBottomX = pyramidCenterX + bottomWidth / 2;
const overlapWidth = radius;
const backgroundX = rightTopX - overlapWidth; // radius is overlap
const backgroundY = radius;
const backgroundWidth = width - pyramidWidth + radius;
const backgroundHeight = height - overlapWidth * 2;
const iconX = pyramidCenterX - iconSize / 2;
const iconY = height / 2 - iconSize / 2 + (isFirst ? 10 : 0);
const labelX = rightCenterX;
const descX = rightBottomX;
const textWidth = width - pyramidWidth - 40;
const itemLabelContent = (
<ItemLabel
indexes={indexes}
x={labelX}
y={10}
width={textWidth}
fontSize={14}
fill={themeColors.colorPrimary}
>
{datum.desc}
</ItemLabel>
);
const itemLabelBounds = getElementBounds(itemLabelContent);
const descY = itemLabelBounds.y + itemLabelBounds.height + 5;
const pyramidColorId = `${themeColors.colorPrimary}-white`;
return (
<Group width={width} height={height} {...restProps}>
<Defs>
<linearGradient id={pyramidColorId} x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0" stop-color={themeColors.colorPrimary} />
<stop
offset="100%"
stop-color={tinycolor
.mix(themeColors.colorPrimary, '#fff', 40)
.toHexString()}
/>
</linearGradient>
</Defs>
<Group
width={width}
height={height}
id={getItemId(indexes, 'shapes-group')}
>
<Rect
id={getItemId(indexes, 'static', 'background')}
x={backgroundX}
width={backgroundWidth}
y={backgroundY}
height={backgroundHeight}
ry="10"
fill={themeColors.colorPrimaryBg}
/>
<Polygon
id={getItemId(indexes, 'static', 'pyramid')}
points={segments}
fill={`url(#${pyramidColorId})`}
/>
<ItemIcon
indexes={indexes}
x={iconX}
y={iconY}
size={iconSize}
fill="#fff"
/>
{itemLabelContent}
<ItemDesc
indexes={indexes}
x={descX}
y={descY}
width={textWidth}
lineHeight={1}
lineNumber={1}
fill={themeColors.colorTextSecondary}
>
{datum.desc}
</ItemDesc>
</Group>
</Group>
);
};
function calculateTriangleSegment(
width: number,
height: number,
gap: number,
counts: number,
index: number,
) {
const centerX = width / 2;
const triangleHeight = counts * height + (counts - 1) * gap;
const rectTop = index * (height + gap);
const rectBottom = rectTop + height;
const topWidth = (rectTop / triangleHeight) * width;
const bottomWidth = (rectBottom / triangleHeight) * width;
let points: Point[];
if (index === 0) {
const p1: Point = { x: centerX, y: 0 }; // 三角形顶点
const p2: Point = { x: centerX + bottomWidth / 2, y: height }; // 右下
const p3: Point = { x: centerX - bottomWidth / 2, y: height }; // 左下
points = [p1, p2, p3];
} else {
const p1: Point = { x: centerX + topWidth / 2, y: 0 }; // 右上
const p2: Point = { x: centerX + bottomWidth / 2, y: height }; // 右下
const p3: Point = { x: centerX - bottomWidth / 2, y: height }; // 左下
const p4: Point = { x: centerX - topWidth / 2, y: 0 }; // 左上
points = [p1, p2, p3, p4];
}
return { points, topWidth, bottomWidth };
}
registerItem('pyramid', {
component: Pyramid,
composites: ['icon', 'label', 'desc'],
});