# Recharts Mixed Bars

> Hybrid bar chart combining stacked bars alongside single comparison bars.

A shadcn/ui block from the [@pacekit](https://ui.pacekit.dev/?utm_source=shadcn.exchange) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/pacekit/tanstack-charts-recharts-mixed-bars).

- Page, with a live preview: https://shadcn.exchange/r/pacekit/tanstack-charts-recharts-mixed-bars
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/pacekit/tanstack-charts-recharts-mixed-bars-light-816b31d24f8a52d0.png
- Registry item: https://ui.pacekit.dev/r/tanstack-charts-recharts-mixed-bars.json
- Listed: 2026-10-21, source last changed 2026-10-21

## Install

```bash
npx shadcn@latest add https://shadcn.exchange/r/pacekit/tanstack-charts-recharts-mixed-bars.json
```

Run it in a project that has shadcn/ui set up. It adds the files below and installs what they depend on.

## Dependencies

- npm packages: `@tanstack/charts`, `@tanstack/react-charts`, `d3-scale`
- Registry items: `card`

## Files

### `components/tanstack-charts/bar/recharts-mixed-bars.tsx`

```tsx
import { type ChartPoint, barY, defineChart, group } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";

import { scaleBand, scaleLinear } from "d3-scale";
import { Columns3 } from "lucide-react";

import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";

interface WeatherDatum {
    date: Date;
    precipitation: number;
    wind: number;
    temp_max: number;
}

type BarSlot = "stack" | "independent";

const motionRenderer = motion({
    initial: "always",
    transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});

function generateDemoData(): WeatherDatum[] {
    const results: WeatherDatum[] = [];
    for (let i = 0; i < 7; i++) {
        const date = new Date(Date.UTC(2024, 6, 1 + i));
        results.push({
            date,
            precipitation: Math.floor(4 + Math.random() * 12),
            wind: Math.floor(3 + Math.random() * 8),
            temp_max: Math.floor(18 + Math.random() * 10),
        });
    }
    return results;
}

function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
    if (points.length === 0) return { title: "", rows: [] };
    const d = points[0].datum as WeatherDatum;
    if (!d) return { title: "", rows: [] };
    return {
        title: d.date.toLocaleDateString("en-US", {
            weekday: "short",
            month: "short",
            day: "numeric",
            timeZone: "UTC",
        }),
        rows: [
            { label: "Precipitation", value: `${d.precipitation} mm`, color: "var(--chart-1)" },
            { label: "Wind Speed", value: `${d.wind} km/h`, color: "var(--chart-2)" },
            { label: "Max Temp", value: `${d.temp_max}°C`, color: "var(--chart-3)" },
        ],
    };
}

export function RechartsMixedBars() {
    const data = useMemo(() => generateDemoData(), []);

    const groupScale = useMemo(() => scaleBand<BarSlot>().domain(["stack", "independent"]).paddingInner(0.1), []);

    const definition = useMemo(
        () =>
            defineChart(
                {
                    renderer: motionRenderer,
                    marks: [
                        barY(data, {
                            x: "date",
                            y1: 0,
                            y2: "precipitation",
                            z: () => "stack",
                            fill: "var(--chart-1)",
                            layout: group({ scale: groupScale }),
                            inset: 1,
                            radius: 2,
                        }),
                        barY(data, {
                            x: "date",
                            y1: "precipitation",
                            y2: (row: WeatherDatum) => row.precipitation + row.wind,
                            z: () => "stack",
                            fill: "var(--chart-2)",
                            layout: group({ scale: groupScale }),
                            inset: 1,
                            radius: 2,
                        }),
                        barY(data, {
                            x: "date",
                            y: "temp_max",
                            z: () => "independent",
                            fill: "var(--chart-3)",
                            layout: group({ scale: groupScale }),
                            inset: 1,
                            radius: 2,
                        }),
                    ],
                    scales: {
                        x: {
                            scale: () => scaleBand<Date>().paddingInner(0.2).paddingOuter(0.05),
                            axis: {
                                ticks: {
                                    format: (v: Date) =>
                                        v.toLocaleDateString("en-US", { month: "short", day: "numeric", timeZone: "UTC" }),
                                },
                                label: "Date",
                            },
                        },
                        y: { scale: scaleLinear, grid: true, axis: { label: "Value Metrics" } },
                    },
                    margin: { top: 24, right: 24, bottom: 32, left: 45 },
                    theme: {
                        foreground: "var(--muted-foreground)",
                        grid: "var(--border)",
                        background: "transparent",
                    },
                },
                {
                    keyboard: true,
                    tooltip: {
                        use: tooltip,
                        className:
                            "font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
                        anchor: "pointer",
                        placement: "auto",
                        content: (points) => shadcnTooltipContent(points),
                    },
                },
            ),
        [data, groupScale],
    );

    return (
        <Card className="w-full max-w-xl">
            <CardHeader>
                <CardTitle>Mixed Stacked & Grouped Bars</CardTitle>
                <CardDescription>
                    Hybrid bar chart combining stacked bars alongside independent adjacent bars
                </CardDescription>
            </CardHeader>
            <CardContent>
                <div className="h-[250px] w-full">
                    <Chart
                        definition={definition}
                        renderer={motionRenderer}
                        height={250}
                        ariaLabel="Mixed Stacked & Grouped Bars Chart"
                    />
                </div>
            </CardContent>
            <CardFooter className="flex-col items-start gap-2 text-sm">
                <div className="flex items-center gap-2 leading-none font-medium">
                    Stacked slot combines precipitation + wind alongside temperature bar{" "}
                    <Columns3 className="h-4 w-4 text-emerald-500" />
                </div>
                <div className="text-muted-foreground leading-none">
                    Custom group layout scale allocates inner slots for stacked and unstacked series
                </div>
            </CardFooter>
        </Card>
    );
}

export default RechartsMixedBars;
```
