Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ export function ChartVisitors() {
<ChartTooltip
cursor={false}
content={<ChartTooltipContent hideLabel />}
defaultIndex={activeIndex}
/>
<Pie
data={desktopData}
dataKey="desktop"
nameKey="month"
innerRadius={60}
strokeWidth={5}
activeIndex={activeIndex}
activeShape={({
outerRadius = 0,
...props
Expand Down
2 changes: 1 addition & 1 deletion apps/v4/app/(internal)/sink/components/chart-bar-mixed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function ChartBarMixed() {
cursor={false}
content={<ChartTooltipContent hideLabel />}
/>
<Bar dataKey="visitors" layout="vertical" radius={5} />
<Bar dataKey="visitors" radius={5} />
</BarChart>
</ChartContainer>
</CardContent>
Expand Down
43 changes: 32 additions & 11 deletions apps/v4/content/docs/components/chart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ description: Beautiful charts. Built using Recharts. Copy and paste into your ap
component: true
---

<Callout>
import { InfoIcon } from "lucide-react"

**Note:** We're working on upgrading to Recharts v3. In the meantime, if you'd like to start testing v3, see the code in the comment [here](https://github.com/shadcn-ui/ui/issues/7669#issuecomment-2998299159). We'll have an official release soon.
<Callout icon={<InfoIcon />}>

**Note:** The `chart` component is now using Recharts v3. See the [Upgrade Guide](/docs/components/chart#upgrade-guide).

</Callout>

Expand Down Expand Up @@ -468,7 +470,7 @@ const chartData = [
#### Tailwind

```tsx
<LabelList className="fill-[--color-desktop]" />
<LabelList className="fill-(--color-desktop)" />
```

## Tooltip
Expand Down Expand Up @@ -527,11 +529,11 @@ const chartConfig = {
},
chrome: {
label: "Chrome",
color: "hsl(var(--chart-1))",
color: "var(--chart-1)",
},
safari: {
label: "Safari",
color: "hsl(var(--chart-2))",
color: "var(--chart-2)",
},
} satisfies ChartConfig
```
Expand Down Expand Up @@ -573,11 +575,11 @@ const chartData = [
const chartConfig = {
chrome: {
label: "Chrome",
color: "hsl(var(--chart-1))",
color: "var(--chart-1)",
},
safari: {
label: "Safari",
color: "hsl(var(--chart-2))",
color: "var(--chart-2)",
},
} satisfies ChartConfig
```
Expand All @@ -590,10 +592,29 @@ This will use `Chrome` and `Safari` for the legend names.

## Accessibility

You can turn on the `accessibilityLayer` prop to add an accessible layer to your chart.
In Recharts v3, the `accessibilityLayer` prop is `true` by default.

This prop adds keyboard access and screen reader support to your charts.
## Upgrade Guide

```tsx
<LineChart accessibilityLayer />
<Callout icon={<InfoIcon />}>

**Before you upgrade:** Note that `recharts` removed [internal props](https://github.com/recharts/recharts/wiki/3.0-migration-guide#removal-of-internal-props). This means the `<Customized />` component no longer receives props. If you depend on those props, you might need to wait for Recharts to release new hooks. You can ask for new hooks by creating an issue on the [recharts](https://github.com/recharts/recharts/issues) repository.

</Callout>

Run the following command to upgrade your `chart.tsx` component:

```bash
npx shadcn@latest add chart --overwrite
```

You can upgrade most charts by following the official upgrade guide: https://github.com/recharts/recharts/wiki/3.0-migration-guide

Here are a few breaking changes that are relevant to `shadcn/charts`:

1. The `layout` property has been removed from `<Bar />`. Remove it from your charts and use `layout` on `<BarChart />`.
2. The `activeIndex` property has been removed. Use `defaultIndex` on `<ChartTooltip />`.
3. The `accessibilityLayer` prop is `true` by default. You can turn it off by setting it to `false`.
4. The `margin` type has been updated. You are now required to provide a value for all margins: `top`, `right`, `bottom`, `left`.

See also this [diff](https://github.com/shadcn-ui/ui/pull/8486/files) where I upgraded the example charts to Recharts v3.
2 changes: 1 addition & 1 deletion apps/v4/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"react-hook-form": "^7.62.0",
"react-resizable-panels": "^2.1.7",
"react-textarea-autosize": "^8.5.9",
"recharts": "2.15.1",
"recharts": "3.2.1",
"rehype-pretty-code": "^0.14.1",
"rimraf": "^6.0.1",
"shadcn": "3.4.2",
Expand Down
2 changes: 1 addition & 1 deletion apps/v4/public/r/styles/new-york-v4/chart-bar-active.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"files": [
{
"path": "registry/new-york-v4/charts/chart-bar-active.tsx",
"content": "\"use client\"\n\nimport { TrendingUp } from \"lucide-react\"\nimport { Bar, BarChart, CartesianGrid, Rectangle, XAxis } from \"recharts\"\n\nimport {\n Card,\n CardContent,\n CardDescription,\n CardFooter,\n CardHeader,\n CardTitle,\n} from \"@/registry/new-york-v4/ui/card\"\nimport {\n ChartConfig,\n ChartContainer,\n ChartTooltip,\n ChartTooltipContent,\n} from \"@/registry/new-york-v4/ui/chart\"\n\nexport const description = \"A bar chart with an active bar\"\n\nconst chartData = [\n { browser: \"chrome\", visitors: 187, fill: \"var(--color-chrome)\" },\n { browser: \"safari\", visitors: 200, fill: \"var(--color-safari)\" },\n { browser: \"firefox\", visitors: 275, fill: \"var(--color-firefox)\" },\n { browser: \"edge\", visitors: 173, fill: \"var(--color-edge)\" },\n { browser: \"other\", visitors: 90, fill: \"var(--color-other)\" },\n]\n\nconst chartConfig = {\n visitors: {\n label: \"Visitors\",\n },\n chrome: {\n label: \"Chrome\",\n color: \"var(--chart-1)\",\n },\n safari: {\n label: \"Safari\",\n color: \"var(--chart-2)\",\n },\n firefox: {\n label: \"Firefox\",\n color: \"var(--chart-3)\",\n },\n edge: {\n label: \"Edge\",\n color: \"var(--chart-4)\",\n },\n other: {\n label: \"Other\",\n color: \"var(--chart-5)\",\n },\n} satisfies ChartConfig\n\nexport function ChartBarActive() {\n return (\n <Card>\n <CardHeader>\n <CardTitle>Bar Chart - Active</CardTitle>\n <CardDescription>January - June 2024</CardDescription>\n </CardHeader>\n <CardContent>\n <ChartContainer config={chartConfig}>\n <BarChart accessibilityLayer data={chartData}>\n <CartesianGrid vertical={false} />\n <XAxis\n dataKey=\"browser\"\n tickLine={false}\n tickMargin={10}\n axisLine={false}\n tickFormatter={(value) =>\n chartConfig[value as keyof typeof chartConfig]?.label\n }\n />\n <ChartTooltip\n cursor={false}\n content={<ChartTooltipContent hideLabel />}\n />\n <Bar\n dataKey=\"visitors\"\n strokeWidth={2}\n radius={8}\n activeIndex={2}\n activeBar={({ ...props }) => {\n return (\n <Rectangle\n {...props}\n fillOpacity={0.8}\n stroke={props.payload.fill}\n strokeDasharray={4}\n strokeDashoffset={4}\n />\n )\n }}\n />\n </BarChart>\n </ChartContainer>\n </CardContent>\n <CardFooter className=\"flex-col items-start gap-2 text-sm\">\n <div className=\"flex gap-2 leading-none font-medium\">\n Trending up by 5.2% this month <TrendingUp className=\"h-4 w-4\" />\n </div>\n <div className=\"text-muted-foreground leading-none\">\n Showing total visitors for the last 6 months\n </div>\n </CardFooter>\n </Card>\n )\n}\n",
"content": "\"use client\"\n\nimport { TrendingUp } from \"lucide-react\"\nimport { Bar, BarChart, CartesianGrid, Rectangle, XAxis } from \"recharts\"\n\nimport {\n Card,\n CardContent,\n CardDescription,\n CardFooter,\n CardHeader,\n CardTitle,\n} from \"@/registry/new-york-v4/ui/card\"\nimport {\n ChartConfig,\n ChartContainer,\n ChartTooltip,\n ChartTooltipContent,\n} from \"@/registry/new-york-v4/ui/chart\"\n\nexport const description = \"A bar chart with an active bar\"\n\nconst chartData = [\n { browser: \"chrome\", visitors: 187, fill: \"var(--color-chrome)\" },\n { browser: \"safari\", visitors: 200, fill: \"var(--color-safari)\" },\n { browser: \"firefox\", visitors: 275, fill: \"var(--color-firefox)\" },\n { browser: \"edge\", visitors: 173, fill: \"var(--color-edge)\" },\n { browser: \"other\", visitors: 90, fill: \"var(--color-other)\" },\n]\n\nconst chartConfig = {\n visitors: {\n label: \"Visitors\",\n },\n chrome: {\n label: \"Chrome\",\n color: \"var(--chart-1)\",\n },\n safari: {\n label: \"Safari\",\n color: \"var(--chart-2)\",\n },\n firefox: {\n label: \"Firefox\",\n color: \"var(--chart-3)\",\n },\n edge: {\n label: \"Edge\",\n color: \"var(--chart-4)\",\n },\n other: {\n label: \"Other\",\n color: \"var(--chart-5)\",\n },\n} satisfies ChartConfig\n\nexport function ChartBarActive() {\n return (\n <Card>\n <CardHeader>\n <CardTitle>Bar Chart - Active</CardTitle>\n <CardDescription>January - June 2024</CardDescription>\n </CardHeader>\n <CardContent>\n <ChartContainer config={chartConfig}>\n <BarChart accessibilityLayer data={chartData}>\n <CartesianGrid vertical={false} />\n <XAxis\n dataKey=\"browser\"\n tickLine={false}\n tickMargin={10}\n axisLine={false}\n tickFormatter={(value) =>\n chartConfig[value as keyof typeof chartConfig]?.label\n }\n />\n <ChartTooltip\n cursor={false}\n content={<ChartTooltipContent hideLabel />}\n defaultIndex={2}\n />\n <Bar\n dataKey=\"visitors\"\n strokeWidth={2}\n radius={8}\n activeBar={({ ...props }) => {\n return (\n <Rectangle\n {...props}\n fillOpacity={0.8}\n stroke={props.payload.fill}\n strokeDasharray={4}\n strokeDashoffset={4}\n />\n )\n }}\n />\n </BarChart>\n </ChartContainer>\n </CardContent>\n <CardFooter className=\"flex-col items-start gap-2 text-sm\">\n <div className=\"flex gap-2 leading-none font-medium\">\n Trending up by 5.2% this month <TrendingUp className=\"h-4 w-4\" />\n </div>\n <div className=\"text-muted-foreground leading-none\">\n Showing total visitors for the last 6 months\n </div>\n </CardFooter>\n </Card>\n )\n}\n",
"type": "registry:block"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"files": [
{
"path": "registry/new-york-v4/charts/chart-bar-label-custom.tsx",
"content": "\"use client\"\n\nimport { TrendingUp } from \"lucide-react\"\nimport { Bar, BarChart, CartesianGrid, LabelList, XAxis, YAxis } from \"recharts\"\n\nimport {\n Card,\n CardContent,\n CardDescription,\n CardFooter,\n CardHeader,\n CardTitle,\n} from \"@/registry/new-york-v4/ui/card\"\nimport {\n ChartConfig,\n ChartContainer,\n ChartTooltip,\n ChartTooltipContent,\n} from \"@/registry/new-york-v4/ui/chart\"\n\nexport const description = \"A bar chart with a custom label\"\n\nconst chartData = [\n { month: \"January\", desktop: 186, mobile: 80 },\n { month: \"February\", desktop: 305, mobile: 200 },\n { month: \"March\", desktop: 237, mobile: 120 },\n { month: \"April\", desktop: 73, mobile: 190 },\n { month: \"May\", desktop: 209, mobile: 130 },\n { month: \"June\", desktop: 214, mobile: 140 },\n]\n\nconst chartConfig = {\n desktop: {\n label: \"Desktop\",\n color: \"var(--chart-2)\",\n },\n mobile: {\n label: \"Mobile\",\n color: \"var(--chart-2)\",\n },\n label: {\n color: \"var(--background)\",\n },\n} satisfies ChartConfig\n\nexport function ChartBarLabelCustom() {\n return (\n <Card>\n <CardHeader>\n <CardTitle>Bar Chart - Custom Label</CardTitle>\n <CardDescription>January - June 2024</CardDescription>\n </CardHeader>\n <CardContent>\n <ChartContainer config={chartConfig}>\n <BarChart\n accessibilityLayer\n data={chartData}\n layout=\"vertical\"\n margin={{\n right: 16,\n }}\n >\n <CartesianGrid horizontal={false} />\n <YAxis\n dataKey=\"month\"\n type=\"category\"\n tickLine={false}\n tickMargin={10}\n axisLine={false}\n tickFormatter={(value) => value.slice(0, 3)}\n hide\n />\n <XAxis dataKey=\"desktop\" type=\"number\" hide />\n <ChartTooltip\n cursor={false}\n content={<ChartTooltipContent indicator=\"line\" />}\n />\n <Bar\n dataKey=\"desktop\"\n layout=\"vertical\"\n fill=\"var(--color-desktop)\"\n radius={4}\n >\n <LabelList\n dataKey=\"month\"\n position=\"insideLeft\"\n offset={8}\n className=\"fill-(--color-label)\"\n fontSize={12}\n />\n <LabelList\n dataKey=\"desktop\"\n position=\"right\"\n offset={8}\n className=\"fill-foreground\"\n fontSize={12}\n />\n </Bar>\n </BarChart>\n </ChartContainer>\n </CardContent>\n <CardFooter className=\"flex-col items-start gap-2 text-sm\">\n <div className=\"flex gap-2 leading-none font-medium\">\n Trending up by 5.2% this month <TrendingUp className=\"h-4 w-4\" />\n </div>\n <div className=\"text-muted-foreground leading-none\">\n Showing total visitors for the last 6 months\n </div>\n </CardFooter>\n </Card>\n )\n}\n",
"content": "\"use client\"\n\nimport { TrendingUp } from \"lucide-react\"\nimport { Bar, BarChart, CartesianGrid, LabelList, XAxis, YAxis } from \"recharts\"\n\nimport {\n Card,\n CardContent,\n CardDescription,\n CardFooter,\n CardHeader,\n CardTitle,\n} from \"@/registry/new-york-v4/ui/card\"\nimport {\n ChartConfig,\n ChartContainer,\n ChartTooltip,\n ChartTooltipContent,\n} from \"@/registry/new-york-v4/ui/chart\"\n\nexport const description = \"A bar chart with a custom label\"\n\nconst chartData = [\n { month: \"January\", desktop: 186, mobile: 80 },\n { month: \"February\", desktop: 305, mobile: 200 },\n { month: \"March\", desktop: 237, mobile: 120 },\n { month: \"April\", desktop: 73, mobile: 190 },\n { month: \"May\", desktop: 209, mobile: 130 },\n { month: \"June\", desktop: 214, mobile: 140 },\n]\n\nconst chartConfig = {\n desktop: {\n label: \"Desktop\",\n color: \"var(--chart-2)\",\n },\n mobile: {\n label: \"Mobile\",\n color: \"var(--chart-2)\",\n },\n label: {\n color: \"var(--background)\",\n },\n} satisfies ChartConfig\n\nexport function ChartBarLabelCustom() {\n return (\n <Card>\n <CardHeader>\n <CardTitle>Bar Chart - Custom Label</CardTitle>\n <CardDescription>January - June 2024</CardDescription>\n </CardHeader>\n <CardContent>\n <ChartContainer config={chartConfig}>\n <BarChart\n accessibilityLayer\n data={chartData}\n layout=\"vertical\"\n margin={{\n right: 16,\n }}\n >\n <CartesianGrid horizontal={false} />\n <YAxis\n dataKey=\"month\"\n type=\"category\"\n tickLine={false}\n tickMargin={10}\n axisLine={false}\n tickFormatter={(value) => value.slice(0, 3)}\n hide\n />\n <XAxis dataKey=\"desktop\" type=\"number\" hide />\n <ChartTooltip\n cursor={false}\n content={<ChartTooltipContent indicator=\"line\" />}\n />\n <Bar dataKey=\"desktop\" fill=\"var(--color-desktop)\" radius={4}>\n <LabelList\n dataKey=\"month\"\n position=\"insideLeft\"\n offset={8}\n className=\"fill-(--color-label)\"\n fontSize={12}\n />\n <LabelList\n dataKey=\"desktop\"\n position=\"right\"\n offset={8}\n className=\"fill-foreground\"\n fontSize={12}\n />\n </Bar>\n </BarChart>\n </ChartContainer>\n </CardContent>\n <CardFooter className=\"flex-col items-start gap-2 text-sm\">\n <div className=\"flex gap-2 leading-none font-medium\">\n Trending up by 5.2% this month <TrendingUp className=\"h-4 w-4\" />\n </div>\n <div className=\"text-muted-foreground leading-none\">\n Showing total visitors for the last 6 months\n </div>\n </CardFooter>\n </Card>\n )\n}\n",
"type": "registry:block"
}
],
Expand Down
Loading