Skip to content

Commit de6bb8f

Browse files
committed
fix(ui): add dynamic rounded corners to stacked bar chart
1 parent d92e003 commit de6bb8f

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

src/server/ui/app/dashboard/page.tsx

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { useEffect, useMemo, useState, type ReactElement } from "react"
44
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
55
import { ChartContainer, ChartTooltip, ChartTooltipContent } from "@/components/ui/chart"
6-
import { BarChart, Bar, LineChart, Line, XAxis, YAxis, CartesianGrid } from "recharts"
6+
import { BarChart, Bar, LineChart, Line, XAxis, YAxis, CartesianGrid, Rectangle } from "recharts"
77
import { useTranslations } from "next-intl"
88
import {
99
Select,
@@ -207,6 +207,39 @@ export default function DashboardPage() {
207207
</div>
208208
)
209209

210+
// Create a Bar shape function with dynamic rounded corners
211+
// Determine the topmost field with a value based on the data point, and set rounded corners for the corresponding Bar
212+
const createStackedBarShape = (dataKey: string, fill: string) => {
213+
// Bar order from bottom to top: completed -> inProgress -> pending -> failed
214+
const order = ["completed", "inProgress", "pending", "failed"]
215+
216+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
217+
const ShapeComponent = (props: any) => {
218+
const { payload, ...rest } = props
219+
220+
// Check the order from bottom to top, find the last field with a value
221+
let topDataKey: string | null = null
222+
if (payload) {
223+
for (let i = order.length - 1; i >= 0; i--) {
224+
const key = order[i]
225+
const value = payload[key as keyof typeof payload]
226+
if (typeof value === "number" && value > 0) {
227+
topDataKey = key
228+
break
229+
}
230+
}
231+
}
232+
233+
// If the current Bar is the topmost field with a value, set rounded corners
234+
const radius: [number, number, number, number] = topDataKey === dataKey ? [4, 4, 0, 0] : [0, 0, 0, 0]
235+
236+
return <Rectangle {...rest} fill={fill} radius={radius} />
237+
}
238+
239+
ShapeComponent.displayName = `StackedBarShape-${dataKey}`
240+
return ShapeComponent
241+
}
242+
210243
return (
211244
<div className="container mx-auto p-6 space-y-6">
212245
{/* Page header with title and time range selector */}
@@ -313,28 +346,28 @@ export default function DashboardPage() {
313346
stackId="a"
314347
fill="#10b981"
315348
name={t("completed")}
316-
radius={[0, 0, 0, 0]}
349+
shape={createStackedBarShape("completed", "#10b981")}
317350
/>
318351
<Bar
319352
dataKey="inProgress"
320353
stackId="a"
321354
fill="#3b82f6"
322355
name={t("inProgress")}
323-
radius={[0, 0, 0, 0]}
356+
shape={createStackedBarShape("inProgress", "#3b82f6")}
324357
/>
325358
<Bar
326359
dataKey="pending"
327360
stackId="a"
328361
fill="#f59e0b"
329362
name={t("pending")}
330-
radius={[0, 0, 0, 0]}
363+
shape={createStackedBarShape("pending", "#f59e0b")}
331364
/>
332365
<Bar
333366
dataKey="failed"
334367
stackId="a"
335368
fill="#ef4444"
336369
name={t("failed")}
337-
radius={[4, 4, 0, 0]}
370+
shape={createStackedBarShape("failed", "#ef4444")}
338371
/>
339372
</BarChart>
340373
)

0 commit comments

Comments
 (0)