react-code-dataset
/
chakra-ui
/apps
/compositions
/src
/examples
/charts
/bar-chart-with-bar-label.tsx
"use client" | |
import { Chart, useChart } from "@chakra-ui/charts" | |
import { | |
Bar, | |
BarChart, | |
CartesianGrid, | |
LabelList, | |
Legend, | |
Tooltip, | |
XAxis, | |
} from "recharts" | |
export const BarChartWithBarLabel = () => { | |
const chart = useChart({ | |
data: [ | |
{ windows: 186, mac: 80, linux: 120, month: "January" }, | |
{ windows: 165, mac: 95, linux: 110, month: "February" }, | |
{ windows: 190, mac: 87, linux: 125, month: "March" }, | |
{ windows: 195, mac: 88, linux: 130, month: "May" }, | |
], | |
series: [ | |
{ name: "windows", color: "teal.solid" }, | |
{ name: "mac", color: "purple.solid" }, | |
{ name: "linux", color: "blue.solid" }, | |
], | |
}) | |
return ( | |
<Chart.Root maxH="md" chart={chart}> | |
<BarChart data={chart.data}> | |
<CartesianGrid stroke={chart.color("border.muted")} vertical={false} /> | |
<XAxis | |
axisLine={false} | |
tickLine={false} | |
dataKey={chart.key("month")} | |
tickFormatter={(value) => value.slice(0, 3)} | |
/> | |
<Tooltip | |
cursor={{ fill: chart.color("bg.muted") }} | |
animationDuration={100} | |
content={<Chart.Tooltip />} | |
/> | |
<Legend content={<Chart.Legend />} /> | |
{chart.series.map((item) => ( | |
<Bar | |
isAnimationActive={false} | |
key={item.name} | |
dataKey={chart.key(item.name)} | |
fill={chart.color(item.color)} | |
stroke={chart.color(item.color)} | |
stackId={item.stackId} | |
> | |
<LabelList | |
dataKey={chart.key(item.name)} | |
position="top" | |
style={{ fontWeight: "600", fill: chart.color("fg") }} | |
/> | |
</Bar> | |
))} | |
</BarChart> | |
</Chart.Root> | |
) | |
} | |