Chart
Beautiful charts. Built using Unovis. Copy and paste into your apps.
Introducing Charts. A collection of chart components that you can copy and paste into your apps.
Charts are designed to look great out of the box. They work well with the other components and are fully customizable to fit your project.
Component
We use Unovis under the hood.
We designed the chart component with composition in mind. You build your charts using Unovis components and bring in custom components, such as ChartTooltipContent
, when and where you need it.
import { VisArea, VisTooltip } from "@unovis/solid"
import {
ChartContainer,
ChartCrosshair,
ChartTooltipContent,
} from "@/components/ui/charts"
const MyChart = () => {
return (
<ChartContainer data={data} type="xy" config={chartConfig}>
<VisArea
...
/>
<ChartCrosshair
...
template={(props) => (
<ChartTooltipContent labelKey="month" indicator="line" {...props} />
)}
/>
<VisTooltip ... />
</ChartContainer>
)
}
export default MyChart
Installation
Run the following command to install chart.tsx
pnpm dlx shadcn-solid@latest add chart
Check the Manual
tab for the CSS file based on your CSS framework
Chart Config
The chart config is where you define the labels, icons and colors for a chart.
It is intentionally decoupled from chart data.
This allows you to share config and color tokens between charts. It can also works independently for cases where your data or color tokens live remotely or in a different format.
import { Monitor } from "lucide-solid"
import { type ChartConfig } from "@/components/ui/chart"
const chartConfig = {
desktop: {
label: "Desktop",
icon: Monitor,
// A color like 'hsl(220, 98%, 61%)' or 'var(--color-name)'
color: "#2563eb",
// OR a theme object with 'light' and 'dark' keys
theme: {
light: "#2563eb",
dark: "#dc2626",
},
},
} satisfies ChartConfig
Theming
Charts has built-in support for theming. You can use css variables (recommended) or color values in any color format, such as hex, hsl or oklch.
CSS Variables
Define your colors in your css file
@layer base {
:root {
--background: 0 0% 100%;
--foreground: 240 10% 3.9%;
// ...
--chart-1: 12 76% 61%;
--chart-2: 173 58% 39%;
}
[data-kb-theme="dark"] {
--background: 240 10% 3.9%;
--foreground: 0 0% 100%;
// ...
--chart-1: 220 70% 50%;
--chart-2: 160 60% 45%;
}
}
Add the color to your chartConfig
const chartConfig = {
desktop: {
label: "Desktop",
color: "hsl(var(--chart-1))",
},
mobile: {
label: "Mobile",
color: "hsl(var(--chart-2))",
},
} satisfies ChartConfig
hex, hsl or oklch
You can also define your colors directly in the chart config. Use the color format you prefer.
const chartConfig = {
desktop: {
label: "Desktop",
color: "#2563eb",
},
} satisfies ChartConfig
Using Colors
To use the theme colors in your chart, reference the colors using the format var(--color-KEY)
.
Components
<VisArea ... color="var(--color-desktop)" />
Chart Data
const data = [
{ browser: "chrome", visitors: 275, fill: "var(--color-chrome)" },
{ browser: "safari", visitors: 200, fill: "var(--color-safari)" },
]
Tooltip
A chart tooltip contains a label, name, indicator and value. You can use a combination of these to customize your tooltip.
You can turn on/off any of these using the hideLabel
, hideIndicator
props and customize the indicator style using the indicator
prop.
Use labelKey
and nameKey
to use a custom key for the tooltip label and name.
Chart comes with the <ChartCrosshair>
and <ChartTooltipContent>
components. You can use these two components to add custom tooltips to your chart.
import { ChartCrosshair, ChartTooltipContent } from "@/components/ui/chart"
<ChartCrosshair
template={(props) => (
<ChartTooltipContent labelKey="month" indicator="line" {...props} />
)}
/>
Or
<VisTooltip
triggers={{
[StackedBar.selectors.bar]: (d: DataRecord, x) => {
const container = document.createElement("div")
const Component = () => (
<ChartTooltipContent
data={d}
x={x}
config={chartConfig}
labelKey="month"
/>
)
render(() => <Component />, container)
return container.innerHTML
},
}}
/>
Props
Use the following props to customize the tooltip.
Prop | Type | Description |
---|---|---|
labelKey | string | The config or data key to use for the label. |
nameKey | string | The config or data key to use for the name. |
indicator | dot line or dashed | The indicator style for the tooltip. |
hideLabel | boolean | Whether to hide the label. |
hideIndicator | boolean | Whether to hide the indicator. |
Colors
Colors are automatically referenced from the chart config.
Custom
To use a custom key for tooltip label and names, use the labelKey
and nameKey
props.
const data = [
{ browser: "chrome", visitors: 187, fill: "var(--color-chrome)" },
{ browser: "safari", visitors: 200, fill: "var(--color-safari)" },
]
const chartConfig = {
visitors: {
label: "Total Visitors",
},
chrome: {
label: "Chrome",
color: "hsl(var(--chart-1))",
},
safari: {
label: "Safari",
color: "hsl(var(--chart-2))",
},
} satisfies ChartConfig
<ChartCrosshair
template={(props) => <ChartTooltipContent labelKey="visitors" {...props} />}
/>
This will use Total Visitors
for label and Chrome
and Safari
for the tooltip names.