|
| 1 | +import { Flex, theme } from "antd"; |
| 2 | +import Link from "next/link"; |
| 3 | +import { useCallback, useState } from "react"; |
| 4 | +import { Cell, Legend, type LegendType, Pie, PieChart } from "recharts"; |
| 5 | +import type { Payload } from "recharts/types/component/DefaultLegendContent"; |
| 6 | +import { renderActiveShapeCompact } from "../Utilities/renderShape"; |
| 7 | +import styles from "./index.module.css"; |
| 8 | +import { themeColor } from "./utils"; |
| 9 | + |
| 10 | +// The `Legend` component uses `value`, `color`, |
| 11 | +// and `type` by default to render the data. |
| 12 | +export interface SummaryChartItem { |
| 13 | + key: React.Key; |
| 14 | + value: string; |
| 15 | + percent: string; |
| 16 | + color?: string; |
| 17 | + count: number; |
| 18 | + type?: LegendType; |
| 19 | + href?: string; |
| 20 | +} |
| 21 | + |
| 22 | +const { useToken } = theme; |
| 23 | + |
| 24 | +interface Props { |
| 25 | + items: SummaryChartItem[]; |
| 26 | + chartWidth?: number; |
| 27 | +} |
| 28 | + |
| 29 | +const INNER_RADIUS = 30; |
| 30 | +const OUTER_RADIUS = 50; |
| 31 | + |
| 32 | +const SummaryPieChart: React.FC<Props> = ({ |
| 33 | + items, |
| 34 | + chartWidth = 600, |
| 35 | +}: Props) => { |
| 36 | + const { token } = useToken(); |
| 37 | + |
| 38 | + const renderLegendText = (value: string) => { |
| 39 | + const item = coloredItems.find((i) => i.value === value); |
| 40 | + if (value === coloredItems[activeIndexRunner].value) { |
| 41 | + return ( |
| 42 | + <span> |
| 43 | + <u> |
| 44 | + <b>{item?.count ?? 0}</b>{" "} |
| 45 | + <span style={{ color: token.colorText }}> |
| 46 | + {item?.href ? <Link href={item.href}>{value}</Link> : value} ( |
| 47 | + {item?.percent}) |
| 48 | + </span> |
| 49 | + </u> |
| 50 | + </span> |
| 51 | + ); |
| 52 | + } |
| 53 | + return ( |
| 54 | + <span> |
| 55 | + <b>{item?.count ?? 0}</b>{" "} |
| 56 | + <span style={{ color: token.colorText }}> |
| 57 | + {item?.href ? <Link href={item.href}>{value}</Link> : value} ( |
| 58 | + {item?.percent}) |
| 59 | + </span> |
| 60 | + </span> |
| 61 | + ); |
| 62 | + }; |
| 63 | + |
| 64 | + const [activeIndexRunner, setActiveIndexRunner] = useState<number>(0); |
| 65 | + const onRunnerPieEnter = useCallback((_: Payload, index: number) => { |
| 66 | + setActiveIndexRunner(index); |
| 67 | + }, []); |
| 68 | + |
| 69 | + // Items are sorted to display the highest count first in the legend |
| 70 | + items.sort((a, b) => { |
| 71 | + return b.count - a.count; |
| 72 | + }); |
| 73 | + |
| 74 | + const coloredItems = items.map((item, index) => ({ |
| 75 | + ...item, |
| 76 | + color: item?.color || themeColor(token, index), |
| 77 | + })); |
| 78 | + |
| 79 | + return ( |
| 80 | + <Flex vertical gap="middle" style={{ width: chartWidth }}> |
| 81 | + <PieChart height={OUTER_RADIUS * 3} width={chartWidth}> |
| 82 | + <Pie |
| 83 | + activeIndex={activeIndexRunner} |
| 84 | + activeShape={renderActiveShapeCompact} |
| 85 | + dataKey="count" |
| 86 | + data={coloredItems} |
| 87 | + innerRadius={INNER_RADIUS} |
| 88 | + outerRadius={OUTER_RADIUS} |
| 89 | + onMouseEnter={onRunnerPieEnter} |
| 90 | + > |
| 91 | + {coloredItems.map((value: SummaryChartItem) => { |
| 92 | + return <Cell key={value.key} fill={value.color} />; |
| 93 | + })} |
| 94 | + </Pie> |
| 95 | + </PieChart> |
| 96 | + <div className={styles.summaryPieChartsWrapper}> |
| 97 | + <Legend |
| 98 | + payload={coloredItems} |
| 99 | + chartWidth={chartWidth} |
| 100 | + layout="vertical" |
| 101 | + align="center" |
| 102 | + wrapperStyle={{ |
| 103 | + position: "static", |
| 104 | + columns: 2, |
| 105 | + }} |
| 106 | + formatter={renderLegendText} |
| 107 | + onMouseEnter={onRunnerPieEnter} |
| 108 | + /> |
| 109 | + </div> |
| 110 | + </Flex> |
| 111 | + ); |
| 112 | +}; |
| 113 | + |
| 114 | +export default SummaryPieChart; |
0 commit comments