chart.tsx83 lines, 1.8 KB
| 1 | import { Label, Pie, PieChart } from "recharts"; |
| 2 | import { |
| 3 | Card, |
| 4 | CardContent, |
| 5 | CardDescription, |
| 6 | CardFooter, |
| 7 | CardHeader, |
| 8 | CardTitle, |
| 9 | } from "@/components/ui/card"; |
| 10 | import { |
| 11 | ChartConfig, |
| 12 | ChartContainer, |
| 13 | ChartTooltip, |
| 14 | ChartTooltipContent, |
| 15 | } from "@/components/ui/chart"; |
| 16 | |
| 17 | interface OverallScoreChartProps { |
| 18 | overallScore: number; |
| 19 | } |
| 20 | |
| 21 | export default function OverallScoreChart({ |
| 22 | overallScore, |
| 23 | }: OverallScoreChartProps) { |
| 24 | const pieChartData = [ |
| 25 | { |
| 26 | name: "Risks", |
| 27 | value: 100 - overallScore, |
| 28 | fill: "hsl(var(--chart-1))", |
| 29 | }, |
| 30 | { |
| 31 | name: "Opportunities", |
| 32 | value: overallScore, |
| 33 | fill: "hsl(var(--chart-2))", |
| 34 | }, |
| 35 | ]; |
| 36 | |
| 37 | const chartConfig = { |
| 38 | value: { |
| 39 | label: "value", |
| 40 | }, |
| 41 | Risks: { |
| 42 | label: "Risks", |
| 43 | color: "hsl(var(--chart-1))", |
| 44 | }, |
| 45 | Opportunitiesites: { |
| 46 | label: "Opportunities", |
| 47 | color: "hsl(var(--chart-2))", |
| 48 | }, |
| 49 | } satisfies ChartConfig; |
| 50 | |
| 51 | return ( |
| 52 | <div className="w-full h-48"> |
| 53 | <ChartContainer |
| 54 | config={chartConfig} |
| 55 | className="mx-auto aspect-square max-h-[250px]" |
| 56 | > |
| 57 | <PieChart> |
| 58 | <ChartTooltip cursor content={<ChartTooltipContent />} /> |
| 59 | <Pie |
| 60 | data={pieChartData} |
| 61 | dataKey="value" |
| 62 | nameKey="name" |
| 63 | innerRadius={60} |
| 64 | paddingAngle={5} |
| 65 | > |
| 66 | <Label |
| 67 | content={({ viewBox }) => { |
| 68 | if (viewBox && "cx" in viewBox && "cy" in viewBox) { |
| 69 | return ( |
| 70 | <text> |
| 71 | <tspan>{overallScore}%</tspan> |
| 72 | <tspan>Score</tspan> |
| 73 | </text> |
| 74 | ); |
| 75 | } |
| 76 | }} |
| 77 | /> |
| 78 | </Pie> |
| 79 | </PieChart> |
| 80 | </ChartContainer> |
| 81 | </div> |
| 82 | ); |
| 83 | } |