Skip to content

Commit ac373be

Browse files
committed
refactor: improve PieChartCard label rendering and type safety
- Updated the `renderCustomizedLabel` function in `PieChartCard` to use `PieLabelRenderProps` for better type safety and clarity. - Simplified property extraction with default values for optional properties, enhancing code readability and maintainability. These changes improve the overall quality of the PieChartCard component and ensure more robust label rendering.
1 parent 454e0c7 commit ac373be

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

src/features/analytics/ui/query-results/charts/PieChartCard.tsx

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { FC, useMemo } from 'react';
1+
import { FC, ReactElement, useMemo } from 'react';
22
import { Box, BoxProps, Flex } from '@chakra-ui/react';
33
import { ChartCard } from './ChartCard';
4-
import { Cell, Pie, PieChart, ResponsiveContainer } from 'recharts';
4+
import { Cell, Pie, PieChart, PieLabelRenderProps, ResponsiveContainer } from 'recharts';
55
import { hashString, toColor } from 'src/shared';
66
import { PieChartOptions } from '../../../model';
77

@@ -32,23 +32,16 @@ export const PieChartCard: FC<
3232
return [_data, _colors, true];
3333
}, [dataSource]);
3434

35-
const renderCustomizedLabel = ({
36-
cx,
37-
cy,
38-
midAngle,
39-
innerRadius,
40-
outerRadius,
41-
percent,
42-
index
43-
}: {
44-
cx: number;
45-
cy: number;
46-
midAngle: number;
47-
innerRadius: number;
48-
outerRadius: number;
49-
percent: number;
50-
index: number;
51-
}) => {
35+
const renderCustomizedLabel = (props: PieLabelRenderProps): ReactElement => {
36+
// Extract properties with proper defaults for optional ones
37+
const cx = Number(props.cx);
38+
const cy = Number(props.cy);
39+
const innerRadius = Number(props.innerRadius);
40+
const outerRadius = Number(props.outerRadius);
41+
const index = Number(props.index);
42+
const midAngle = typeof props.midAngle === 'number' ? props.midAngle : 0;
43+
const percent = typeof props.percent === 'number' ? props.percent : 0;
44+
5245
const radius = innerRadius + (outerRadius - innerRadius) * 0.5;
5346
const x = cx + radius * Math.cos(-midAngle * RADIAN) * 2.5;
5447
const y = cy + radius * Math.sin(-midAngle * RADIAN) * 2.5;

0 commit comments

Comments
 (0)