> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/huntabyte/shadcn-svelte/llms.txt
> Use this file to discover all available pages before exploring further.

# Chart

> Beautiful charts. Built using LayerChart. Copy and paste into your apps.

<Note>
  **Important:** LayerChart v2 is still in pre-release and is actively evolving. Only use if you're comfortable with potential breaking changes before stable v2.

  Your feedback will be invaluable in shaping the release and features. Current development status can be tracked [here](https://github.com/techniq/layerchart/pull/449).
</Note>

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 other components are are fully customizable to fit your project.

[Browse the Charts Library](https://www.shadcn-svelte.com/charts)

## Component

We use [LayerChart](https://next.layerchart.com) under the hood.

We designed the `Chart` component with composition in mind. **You build your charts using LayerChart components and only bring in custom components, such as `ChartTooltip`, when and where you need it**

```svelte /Chart.Container/ /Chart.Tooltip/ theme={null}
<script lang="ts">
  import * as Chart from "$lib/components/ui/chart/index.js";
  import { BarChart } from "layerchart";

  const data = [
    // ...
  ];
</script>

<Chart.Container>
  <BarChart {data} x="date" y="value">
    {#snippet tooltip()}
      <Chart.Tooltip />
    {/snippet}
  </BarChart>
</Chart.Container>
```

We do not wrap LayerChart. This means you're not locked into an abstraction. When a new LayerChart version is released, you can follow the official upgrade path to upgrade your charts.

**The components are yours**.

## Installation

<Tabs>
  <Tab title="CLI">
    ```bash theme={null}
    npx shadcn-svelte@next add chart
    ```
  </Tab>

  <Tab title="Manual">
    <Steps>
      <Step title="Install layerchart">
        ```bash theme={null}
        npm install layerchart@next -D
        ```
      </Step>

      <Step title="Add chart colors to CSS">
        Add the following colors to your CSS file:

        ```css title="src/app.css" theme={null}
        :root {
          --chart-1: oklch(0.646 0.222 41.116);
          --chart-2: oklch(0.6 0.118 184.704);
          --chart-3: oklch(0.398 0.07 227.392);
          --chart-4: oklch(0.828 0.189 84.429);
          --chart-5: oklch(0.769 0.188 70.08);
        }

        .dark {
          --chart-1: oklch(0.488 0.243 264.376);
          --chart-2: oklch(0.696 0.17 162.48);
          --chart-3: oklch(0.769 0.188 70.08);
          --chart-4: oklch(0.627 0.265 303.9);
          --chart-5: oklch(0.645 0.246 16.439);
        }

        @theme inline {
          --color-chart-1: var(--chart-1);
          --color-chart-2: var(--chart-2);
          --color-chart-3: var(--chart-3);
          --color-chart-4: var(--chart-4);
          --color-chart-5: var(--chart-5);
        }
        ```
      </Step>

      <Step title="Copy component code">
        Copy and paste the component source code into your project.
      </Step>
    </Steps>
  </Tab>
</Tabs>

## Your First Chart

Let's build your first chart. We'll build a bar chart with an axis, grid, tooltip, and legend.

<Steps>
  <Step title="Define your data">
    The following data represents the number of desktop and mobile users for each month.

    <Note>
      **Note:** Your data can be in any shape. You are not limited to the shape of the data below. Use the `dataKey` prop to map your data to the chart.
    </Note>

    ```svelte title="lib/components/example-chart.svelte" theme={null}
    <script lang="ts">
      const chartData = [
        { month: "January", desktop: 186, mobile: 80 },
        { month: "February", desktop: 305, mobile: 200 },
        { month: "March", desktop: 237, mobile: 120 },
        { month: "April", desktop: 73, mobile: 190 },
        { month: "May", desktop: 209, mobile: 130 },
        { month: "June", desktop: 214, mobile: 140 },
      ];
    </script>
    ```
  </Step>

  <Step title="Define your chart config">
    The chart config holds configuration for the chart. This is where you place human-readable strings, such as labels, icons, and color tokens for theming.

    ```svelte title="lib/components/example-chart.svelte" theme={null}
    <script lang="ts">
      import * as Chart from "$lib/components/ui/chart/index.js";

      const chartConfig = {
        desktop: {
          label: "Desktop",
          color: "#2563eb",
        },
        mobile: {
          label: "Mobile",
          color: "#60a5fa",
        },
      } satisfies Chart.ChartConfig;
    </script>
    ```
  </Step>

  <Step title="Build your chart">
    You can now build your chart using LayerChart components. We're using the `BarChart` component in this example, which is one of LayerChart's "Simplified Charts".

    These components handle a lot of the common chart scaffolding for you, while allowing you to customize them to your liking.
  </Step>
</Steps>

## 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.

```svelte theme={null}
<script lang="ts">
  import MonitorIcon from "@lucide/svelte/icons/monitor";
  import * as Chart from "$lib/components/ui/chart/index.js";

  const chartConfig = {
    desktop: {
      label: "Desktop",
      icon: MonitorIcon,
      // 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 Chart.ChartConfig;
</script>
```

## 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

<Steps>
  <Step title="Define colors in CSS">
    ```css {5-6,13-14} title="src/routes/layout.css" theme={null}
    :root {
      --background: oklch(1 0 0);
      --foreground: oklch(0.145 0 0);
      /* ... */
      --chart-1: oklch(0.646 0.222 41.116);
      --chart-2: oklch(0.6 0.118 184.704);
    }

    .dark {
      --background: oklch(0.145 0 0);
      --foreground: oklch(0.985 0 0);
      /* ... */
      --chart-1: oklch(0.488 0.243 264.376);
      --chart-2: oklch(0.696 0.17 162.48);
    }
    ```
  </Step>

  <Step title="Add color to chartConfig">
    ```svelte {5,9} theme={null}
    <script lang="ts">
      const chartConfig = {
        desktop: {
          label: "Desktop",
          color: "var(--chart-1)",
        },
        mobile: {
          label: "Mobile",
          color: "var(--chart-2)",
        },
      } satisfies Chart.ChartConfig;
    </script>
    ```
  </Step>
</Steps>

### hex, hsl or oklch

You can also define your colors directly in the chart config. Use the color format you prefer.

```svelte theme={null}
<script lang="ts">
  const chartConfig = {
    desktop: {
      label: "Desktop",
      color: "#2563eb",
    },
  } satisfies Chart.ChartConfig;
</script>
```

### Using Colors

To use the theme colors in your chart, reference the colors using the format `var(--color-KEY)`.

#### Components

```svelte theme={null}
<Bar fill="var(--color-desktop)" />
```

#### Chart Data

```ts theme={null}
const chartData = [
  { browser: "chrome", visitors: 275, color: "var(--color-chrome)" },
  { browser: "safari", visitors: 200, color: "var(--color-safari)" },
];
```

#### Tailwind

```svelte theme={null}
<Label class="fill-(--color-desktop)" />
```

## 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 `<Chart.Tooltip>` component. You can use this component to add custom tooltips to your chart.

### 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.                          |
| `label`          | string                   | A custom label for the tooltip                          |
| `labelFormatter` | function                 | A function to format the label.                         |
| `formatter`      | Snippet                  | A snippet to provide flexible rendering of the tooltip. |

### 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.

```svelte /browser/ theme={null}
<script lang="ts">
  const chartData = [
    { browser: "chrome", visitors: 187, color: "var(--color-chrome)" },
    { browser: "safari", visitors: 200, color: "var(--color-safari)" },
  ];

  const chartConfig = {
    visitors: {
      label: "Total Visitors",
    },
    chrome: {
      label: "Chrome",
      color: "var(--chart-1)",
    },
    safari: {
      label: "Safari",
      color: "var(--chart-2)",
    },
  } satisfies ChartConfig;
</script>

<Chart.Tooltip labelKey="visitors" nameKey="browser" />
```

This will use `Total Visitors` for label and `Chrome` and `Safari` for the tooltip names.

## Links

* [Source Code](https://github.com/huntabyte/shadcn-svelte/tree/next/sites/docs/src/lib/registry/ui/chart)
