Skip to content

Commit 3f6f94d

Browse files
chore: use calculateSegmentAngles() to keep code dry and fix comment
1 parent 115b7f6 commit 3f6f94d

2 files changed

Lines changed: 31 additions & 25 deletions

File tree

scripts/buildData.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ function postProcessItems(items: Item[]): {
259259
}
260260

261261
async function main() {
262-
// check segment length between 2 and 6
262+
// check segment length between 1 and 6
263263
if (!segments.length || segments.length > 6) {
264264
errorHandler.processBuildErrors(
265265
ErrorType.InvalidSegmentLength,

src/components/Radar/Chart.tsx

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,50 +36,48 @@ const _Chart: FC<ChartProps> = ({
3636
};
3737
};
3838

39-
// Function to generate the path for a ring segment
39+
// Helper function to calculate angles
40+
const calculateSegmentAngles = (position: number, numSegments: number) => {
41+
const angleIncrement = 360 / numSegments;
42+
const startAngle = (position - 1) * angleIncrement;
43+
const endAngle = startAngle + angleIncrement;
44+
return { startAngle, endAngle, angleIncrement };
45+
};
46+
47+
// Optimized function to generate the path for a ring segment
4048
const describeArc = (
4149
radiusPercentage: number,
4250
position: number,
4351
numSegments: number,
4452
): string => {
45-
// Calculate the start and end angles based on the number of segments
46-
const angleIncrement = 360 / numSegments;
47-
const startAngle = (position - 1) * angleIncrement;
48-
const endAngle = startAngle + angleIncrement;
49-
5053
const radius = radiusPercentage * center; // Convert percentage to actual radius
51-
const start = polarToCartesian(radius, endAngle);
52-
const end = polarToCartesian(radius, startAngle);
5354

5455
// If there's only one segment, draw a full circle
55-
// prettier-ignore
5656
if (numSegments === 1) {
57+
// prettier-ignore
5758
return [
5859
"M", center, center - radius,
5960
"A", radius, radius, 0, 1, 0, center, center + radius,
6061
"A", radius, radius, 0, 1, 0, center, center - radius,
6162
].join(" ");
6263
}
6364

65+
const { startAngle, endAngle } = calculateSegmentAngles(
66+
position,
67+
numSegments,
68+
);
69+
const start = polarToCartesian(radius, endAngle);
70+
const end = polarToCartesian(radius, startAngle);
71+
6472
// prettier-ignore
6573
return [
66-
"M", start.x, start.y,
67-
"A", radius, radius, 0, 0, 0, end.x, end.y,
74+
"M", start.x, start.y,
75+
"A", radius, radius, 0, 0, 0, end.x, end.y,
6876
].join(" ");
6977
};
7078

7179
const renderGlow = (position: number, color: string, numSegments: number) => {
72-
const angleIncrement = 360 / numSegments;
73-
const startAngle = (position - 1) * angleIncrement;
74-
const endAngle = startAngle + angleIncrement;
75-
76-
const cx = (startAngle + endAngle) / 2 > 180 ? 1 : 0;
77-
const cy =
78-
(startAngle + endAngle) / 2 > 90 && (startAngle + endAngle) / 2 < 270
79-
? 1
80-
: 0;
81-
82-
if (numSegments == 1)
80+
if (numSegments === 1) {
8381
return (
8482
<circle
8583
cx={center}
@@ -89,8 +87,9 @@ const _Chart: FC<ChartProps> = ({
8987
mask="url(#glow-mask)"
9088
/>
9189
);
90+
}
9291

93-
if (numSegments == 2) {
92+
if (numSegments === 2) {
9493
return (
9594
<rect
9695
x={position === 1 ? center : 0}
@@ -103,9 +102,16 @@ const _Chart: FC<ChartProps> = ({
103102
);
104103
}
105104

105+
const { startAngle, endAngle } = calculateSegmentAngles(
106+
position,
107+
numSegments,
108+
);
109+
const start = polarToCartesian(size, startAngle);
110+
const end = polarToCartesian(size, endAngle);
111+
106112
return (
107113
<polygon
108-
points={`${center},${center} ${polarToCartesian(size, startAngle).x},${polarToCartesian(size, startAngle).y} ${polarToCartesian(size, endAngle).x},${polarToCartesian(size, endAngle).y}`}
114+
points={`${center},${center} ${start.x},${start.y} ${end.x},${end.y}`}
109115
fill={color}
110116
mask="url(#glow-mask)"
111117
/>

0 commit comments

Comments
 (0)