Skip to content

Commit

Permalink
feat(www): tooltip examples (#4309)
Browse files Browse the repository at this point in the history
* feat(www): more chart examples

* feat(www): add tooltip examples

* feat: add themes support on blocks page

* chore: build registry

* fix: colors on charts

* fix: styles

* fix: downgrade rehype-pretty-code

* chore: fix

* fix: frame

* fix: code

* feat: update charts nav

* fix: use client

* feat: update all charts
  • Loading branch information
shadcn authored Jul 17, 2024
1 parent 210010f commit 12539b3
Show file tree
Hide file tree
Showing 67 changed files with 3,914 additions and 336 deletions.
123 changes: 123 additions & 0 deletions apps/www/__registry__/default/block/chart-tooltip-advanced.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
"use client"

import { Bar, BarChart, XAxis } from "recharts"

import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/registry/default/ui/card"
import {
ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from "@/registry/default/ui/chart"

export const description = "A stacked bar chart with a legend"

const chartData = [
{ date: "2024-07-15", running: 450, swimming: 300 },
{ date: "2024-07-16", running: 380, swimming: 420 },
{ date: "2024-07-17", running: 520, swimming: 120 },
{ date: "2024-07-18", running: 140, swimming: 550 },
{ date: "2024-07-19", running: 600, swimming: 350 },
{ date: "2024-07-20", running: 480, swimming: 400 },
]

const chartConfig = {
running: {
label: "Running",
color: "hsl(var(--chart-1))",
},
swimming: {
label: "Swimming",
color: "hsl(var(--chart-2))",
},
} satisfies ChartConfig

export default function Component() {
return (
<Card>
<CardHeader>
<CardTitle>Tooltip - Advanced</CardTitle>
<CardDescription>
Tooltip with custom formatter and total.
</CardDescription>
</CardHeader>
<CardContent>
<ChartContainer config={chartConfig}>
<BarChart accessibilityLayer data={chartData}>
<XAxis
dataKey="date"
tickLine={false}
tickMargin={10}
axisLine={false}
tickFormatter={(value) => {
return new Date(value).toLocaleDateString("en-US", {
weekday: "short",
})
}}
/>
<Bar
dataKey="running"
stackId="a"
fill="var(--color-running)"
radius={[0, 0, 4, 4]}
/>
<Bar
dataKey="swimming"
stackId="a"
fill="var(--color-swimming)"
radius={[4, 4, 0, 0]}
/>
<ChartTooltip
content={
<ChartTooltipContent
hideLabel
className="w-[180px]"
formatter={(value, name, item, index) => (
<>
<div
className="h-2.5 w-2.5 shrink-0 rounded-[2px] bg-[--color-bg]"
style={
{
"--color-bg": `var(--color-${name})`,
} as React.CSSProperties
}
/>
{chartConfig[name as keyof typeof chartConfig]?.label ||
name}
<div className="ml-auto flex items-baseline gap-0.5 font-mono font-medium tabular-nums text-foreground">
{value}
<span className="font-normal text-muted-foreground">
kcal
</span>
</div>
{/* Add this after the last item */}
{index === 1 && (
<div className="mt-1.5 flex basis-full items-center border-t pt-1.5 text-xs font-medium text-foreground">
Total
<div className="ml-auto flex items-baseline gap-0.5 font-mono font-medium tabular-nums text-foreground">
{item.payload.running + item.payload.swimming}
<span className="font-normal text-muted-foreground">
kcal
</span>
</div>
</div>
)}
</>
)}
/>
}
cursor={false}
defaultIndex={1}
/>
</BarChart>
</ChartContainer>
</CardContent>
</Card>
)
}
86 changes: 86 additions & 0 deletions apps/www/__registry__/default/block/chart-tooltip-default.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"use client"

import { Bar, BarChart, XAxis } from "recharts"

import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/registry/default/ui/card"
import {
ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from "@/registry/default/ui/chart"

export const description = "A stacked bar chart with a legend"

const chartData = [
{ date: "2024-07-15", running: 450, swimming: 300 },
{ date: "2024-07-16", running: 380, swimming: 420 },
{ date: "2024-07-17", running: 520, swimming: 120 },
{ date: "2024-07-18", running: 140, swimming: 550 },
{ date: "2024-07-19", running: 600, swimming: 350 },
{ date: "2024-07-20", running: 480, swimming: 400 },
]

const chartConfig = {
running: {
label: "Running",
color: "hsl(var(--chart-1))",
},
swimming: {
label: "Swimming",
color: "hsl(var(--chart-2))",
},
} satisfies ChartConfig

export default function Component() {
return (
<Card>
<CardHeader>
<CardTitle>Tooltip - Default</CardTitle>
<CardDescription>
Default tooltip with ChartTooltipContent.
</CardDescription>
</CardHeader>
<CardContent>
<ChartContainer config={chartConfig}>
<BarChart accessibilityLayer data={chartData}>
<XAxis
dataKey="date"
tickLine={false}
tickMargin={10}
axisLine={false}
tickFormatter={(value) => {
return new Date(value).toLocaleDateString("en-US", {
weekday: "short",
})
}}
/>
<Bar
dataKey="running"
stackId="a"
fill="var(--color-running)"
radius={[0, 0, 4, 4]}
/>
<Bar
dataKey="swimming"
stackId="a"
fill="var(--color-swimming)"
radius={[4, 4, 0, 0]}
/>
<ChartTooltip
content={<ChartTooltipContent />}
cursor={false}
defaultIndex={1}
/>
</BarChart>
</ChartContainer>
</CardContent>
</Card>
)
}
100 changes: 100 additions & 0 deletions apps/www/__registry__/default/block/chart-tooltip-formatter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"use client"

import { Bar, BarChart, XAxis } from "recharts"

import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/registry/default/ui/card"
import {
ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from "@/registry/default/ui/chart"

export const description = "A stacked bar chart with a legend"

const chartData = [
{ date: "2024-07-15", running: 450, swimming: 300 },
{ date: "2024-07-16", running: 380, swimming: 420 },
{ date: "2024-07-17", running: 520, swimming: 120 },
{ date: "2024-07-18", running: 140, swimming: 550 },
{ date: "2024-07-19", running: 600, swimming: 350 },
{ date: "2024-07-20", running: 480, swimming: 400 },
]

const chartConfig = {
running: {
label: "Running",
color: "hsl(var(--chart-1))",
},
swimming: {
label: "Swimming",
color: "hsl(var(--chart-2))",
},
} satisfies ChartConfig

export default function Component() {
return (
<Card>
<CardHeader>
<CardTitle>Tooltip - Formatter</CardTitle>
<CardDescription>Tooltip with custom formatter .</CardDescription>
</CardHeader>
<CardContent>
<ChartContainer config={chartConfig}>
<BarChart accessibilityLayer data={chartData}>
<XAxis
dataKey="date"
tickLine={false}
tickMargin={10}
axisLine={false}
tickFormatter={(value) => {
return new Date(value).toLocaleDateString("en-US", {
weekday: "short",
})
}}
/>
<Bar
dataKey="running"
stackId="a"
fill="var(--color-running)"
radius={[0, 0, 4, 4]}
/>
<Bar
dataKey="swimming"
stackId="a"
fill="var(--color-swimming)"
radius={[4, 4, 0, 0]}
/>
<ChartTooltip
content={
<ChartTooltipContent
hideLabel
formatter={(value, name) => (
<div className="flex min-w-[130px] items-center text-xs text-muted-foreground">
{chartConfig[name as keyof typeof chartConfig]?.label ||
name}
<div className="ml-auto flex items-baseline gap-0.5 font-mono font-medium tabular-nums text-foreground">
{value}
<span className="font-normal text-muted-foreground">
kcal
</span>
</div>
</div>
)}
/>
}
cursor={false}
defaultIndex={1}
/>
</BarChart>
</ChartContainer>
</CardContent>
</Card>
)
}
87 changes: 87 additions & 0 deletions apps/www/__registry__/default/block/chart-tooltip-icons.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"use client"

import { Footprints, Waves } from "lucide-react"
import { Bar, BarChart, XAxis } from "recharts"

import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/registry/default/ui/card"
import {
ChartConfig,
ChartContainer,
ChartTooltip,
ChartTooltipContent,
} from "@/registry/default/ui/chart"

export const description = "A stacked bar chart with a legend"

const chartData = [
{ date: "2024-07-15", running: 450, swimming: 300 },
{ date: "2024-07-16", running: 380, swimming: 420 },
{ date: "2024-07-17", running: 520, swimming: 120 },
{ date: "2024-07-18", running: 140, swimming: 550 },
{ date: "2024-07-19", running: 600, swimming: 350 },
{ date: "2024-07-20", running: 480, swimming: 400 },
]

const chartConfig = {
running: {
label: "Running",
color: "hsl(var(--chart-1))",
icon: Footprints,
},
swimming: {
label: "Swimming",
color: "hsl(var(--chart-2))",
icon: Waves,
},
} satisfies ChartConfig

export default function Component() {
return (
<Card>
<CardHeader>
<CardTitle>Tooltip - Icons</CardTitle>
<CardDescription>Tooltip with icons.</CardDescription>
</CardHeader>
<CardContent>
<ChartContainer config={chartConfig}>
<BarChart accessibilityLayer data={chartData}>
<XAxis
dataKey="date"
tickLine={false}
tickMargin={10}
axisLine={false}
tickFormatter={(value) => {
return new Date(value).toLocaleDateString("en-US", {
weekday: "short",
})
}}
/>
<Bar
dataKey="running"
stackId="a"
fill="var(--color-running)"
radius={[0, 0, 4, 4]}
/>
<Bar
dataKey="swimming"
stackId="a"
fill="var(--color-swimming)"
radius={[4, 4, 0, 0]}
/>
<ChartTooltip
content={<ChartTooltipContent hideLabel />}
cursor={false}
defaultIndex={1}
/>
</BarChart>
</ChartContainer>
</CardContent>
</Card>
)
}
Loading

0 comments on commit 12539b3

Please sign in to comment.