# Tooltip Chart Formatter

A bar chart of daily active energy whose tooltip shows a name, value and kcal unit through a custom formatter.

Tooltip Chart Formatter is an open-source chart from suiss/ui for React. The @esuiss/ui CLI copies its source into your project, so you own the code.

Source: https://ui.suiss.app/docs/charts/tooltip-chart-formatter

**React**

**Example**

```tsx title="components/charts/tooltip-chart-formatter.tsx"
"use client"

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

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

function dayKey(date: Date) {
  return [
    date.getFullYear(),
    String(date.getMonth() + 1).padStart(2, "0"),
    String(date.getDate()).padStart(2, "0"),
  ].join("-")
}

const chartData = [
  512, 604, 455, 389, 662, 598, 431, 520, 690, 447, 575, 402, 633, 558,
]
  .slice(-7)
  .map((energy, index) => ({
    date: dayKey(new Date(2026, 9, index)),
    energy,
  }))

const chartConfig = {
  energy: { label: "Active energy", color: "var(--color-red-500)" },
} satisfies ChartConfig

const weekday = new Intl.DateTimeFormat("en-US", { weekday: "short" })

export function TooltipChartFormatter() {
  return (
    <Card variant="filled" size="sm">
      <CardHeader>
        <CardTitle>With units</CardTitle>
        <CardDescription>A unit next to the value</CardDescription>
      </CardHeader>
      <CardContent>
        <ChartContainer
          config={chartConfig}
          className="aspect-auto h-52 w-full"
        >
          <BarChart
            accessibilityLayer
            data={chartData}
            margin={{ top: 70, right: 4, left: 4, bottom: 0 }}
          >
            <XAxis
              dataKey="date"
              tickLine={false}
              axisLine={false}
              tickMargin={8}
              interval={0}
              tickFormatter={(value: string) =>
                weekday.format(new Date(`${value}T12:00:00`))
              }
            />
            <ChartTooltip
              defaultIndex={3}
              cursor={false}
              content={
                <ChartTooltipContent
                  hideLabel
                  formatter={(value) => (
                    <div className="flex items-baseline gap-1.5">
                      <span className="text-muted-foreground">
                        Active energy
                      </span>
                      <span className="font-semibold text-foreground tabular-nums">
                        {Number(value).toLocaleString("en-US")}
                      </span>
                      <span className="text-xs text-muted-foreground">
                        kcal
                      </span>
                    </div>
                  )}
                />
              }
            />
            <Bar
              dataKey="energy"
              fill="var(--color-energy)"
              radius={4}
              maxBarSize={26}
            />
          </BarChart>
        </ChartContainer>
      </CardContent>
    </Card>
  )
}
```

A week of active-energy bars whose tooltip row is rewritten by a formatter to show a name, value and kcal unit. Use a formatter like this when values need units, thousands separators or extra context.

## Installation

### CLI

```bash title="Terminal"
npx shadcn@latest add @esuiss/tooltip-chart-formatter
```

Works in any project with a `components.json`. See [Installation](https://ui.suiss.app/docs/installation#add-components) for the one-line registry setup. Or use the suiss/ui CLI:

```bash title="Terminal"
npx @esuiss/ui@latest add charts tooltip-chart-formatter
```

### Manual

1. Install the dependencies:

```bash title="Terminal"
npm install recharts
```

2. Add the pieces it builds on: [Card](https://ui.suiss.app/docs/components/card), [Chart](https://ui.suiss.app/docs/components/chart).

3. Copy the source into `components/charts/tooltip-chart-formatter.tsx`:

```tsx title="components/charts/tooltip-chart-formatter.tsx"
"use client"

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

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

function dayKey(date: Date) {
  return [
    date.getFullYear(),
    String(date.getMonth() + 1).padStart(2, "0"),
    String(date.getDate()).padStart(2, "0"),
  ].join("-")
}

const chartData = [
  512, 604, 455, 389, 662, 598, 431, 520, 690, 447, 575, 402, 633, 558,
]
  .slice(-7)
  .map((energy, index) => ({
    date: dayKey(new Date(2026, 9, index)),
    energy,
  }))

const chartConfig = {
  energy: { label: "Active energy", color: "var(--color-red-500)" },
} satisfies ChartConfig

const weekday = new Intl.DateTimeFormat("en-US", { weekday: "short" })

export function TooltipChartFormatter() {
  return (
    <Card variant="filled" size="sm">
      <CardHeader>
        <CardTitle>With units</CardTitle>
        <CardDescription>A unit next to the value</CardDescription>
      </CardHeader>
      <CardContent>
        <ChartContainer
          config={chartConfig}
          className="aspect-auto h-52 w-full"
        >
          <BarChart
            accessibilityLayer
            data={chartData}
            margin={{ top: 70, right: 4, left: 4, bottom: 0 }}
          >
            <XAxis
              dataKey="date"
              tickLine={false}
              axisLine={false}
              tickMargin={8}
              interval={0}
              tickFormatter={(value: string) =>
                weekday.format(new Date(`${value}T12:00:00`))
              }
            />
            <ChartTooltip
              defaultIndex={3}
              cursor={false}
              content={
                <ChartTooltipContent
                  hideLabel
                  formatter={(value) => (
                    <div className="flex items-baseline gap-1.5">
                      <span className="text-muted-foreground">
                        Active energy
                      </span>
                      <span className="font-semibold text-foreground tabular-nums">
                        {Number(value).toLocaleString("en-US")}
                      </span>
                      <span className="text-xs text-muted-foreground">
                        kcal
                      </span>
                    </div>
                  )}
                />
              }
            />
            <Bar
              dataKey="energy"
              fill="var(--color-energy)"
              radius={4}
              maxBarSize={26}
            />
          </BarChart>
        </ChartContainer>
      </CardContent>
    </Card>
  )
}
```

4. Update the import paths to match your project.

## Usage

```tsx
import { TooltipChartFormatter } from "@/components/charts/tooltip-chart-formatter"
```

```tsx
<TooltipChartFormatter />
```

Pass a `formatter` to `ChartTooltipContent`; it receives each row's value and name and returns the markup for that row. Change the label text, number format and unit inside it, and replace `chartData` and `chartConfig` with your series.

## Notes

Dates are parsed at noon so the weekday labels don't shift a day in time zones behind UTC; keep that if you use date strings as categories.

