# Bar Chart Mixed

> A multi-colored horizontal bar chart tracking browser visitors built with TanStack Charts.

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-bar-mixed).

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

## Install

```bash
npx shadcn@latest add https://shadcn.exchange/r/pacekit/tanstack-charts-bar-mixed.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/bar-chart-mixed.tsx`

```tsx
import { type ChartPoint, barX, defineChart } 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 { TrendingUp } from "lucide-react";

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

interface BrowserDatum {
    browser: string;
    visitors: number;
}

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

function generateDemoData(): BrowserDatum[] {
    return [
        { browser: "chrome", visitors: 275 },
        { browser: "safari", visitors: 200 },
        { browser: "firefox", visitors: 187 },
        { browser: "edge", visitors: 173 },
        { browser: "other", visitors: 90 },
    ];
}

function titleCase(value: string) {
    return value.charAt(0).toUpperCase() + value.slice(1);
}

function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
    return {
        title: String(points[0]?.xValue ?? ""),
        rows: points.map((point) => ({
            label: titleCase(
                String(point.group ?? point.markId.replace(/-?(bars|lines|areas|slices|values|radar)$/u, "")),
            ),
            value: Number(point.yValue ?? point.xValue ?? 0).toLocaleString("en-US"),
            color: point.color,
        })),
    };
}

export function BarChartMixed() {
    const data = useMemo(() => generateDemoData(), []);
    const browserNames = useMemo(() => data.map((row) => row.browser), [data]);

    const definition = useMemo(
        () =>
            defineChart(
                {
                    renderer: motionRenderer,
                    motion: {
                        transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
                    },
                    marks: [
                        barX(data, {
                            id: "visitor-bars",
                            x: "visitors",
                            y: "browser",
                            color: "browser",
                            radius: 5,
                        }),
                    ],
                    scales: {
                        x: { scale: scaleLinear, axis: false },
                        y: {
                            scale: () => scaleBand<string>().paddingInner(0.18).paddingOuter(0.08),
                            axis: {
                                line: false,
                                ticks: { size: 0, padding: 10, format: titleCase },
                            },
                        },
                    },
                    color: {
                        domain: browserNames,
                        range: [
                            "var(--chart-1)",
                            "var(--chart-2)",
                            "var(--chart-3)",
                            "var(--chart-4)",
                            "var(--chart-5)",
                        ],
                    },
                    margin: { top: 5, right: 5, bottom: 5, left: 60 },
                    theme: {
                        foreground: "var(--muted-foreground)",
                        grid: "var(--border)",
                        background: "transparent",
                    },
                },
                {
                    focus: "group-x",
                    tooltip: {
                        use: tooltip,
                        motion: { type: "spring", stiffness: 170, damping: 18 },
                        className:
                            "font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
                        anchor: "group-center",
                        placement: "auto",
                        sort: "color-domain",
                        content: (points) => shadcnTooltipContent(points),
                    },
                },
            ),
        [data, browserNames],
    );

    return (
        <Card className="w-full max-w-xl">
            <CardHeader>
                <CardTitle>Marketing Channel Acquisition</CardTitle>
                <CardDescription>New customer signups by marketing channel</CardDescription>
            </CardHeader>
            <CardContent>
                <div className="h-[250px] w-full">
                    <Chart
                        definition={definition}
                        renderer={motionRenderer}
                        height={250}
                        ariaLabel="Marketing Channel Acquisition Chart"
                    />
                </div>
            </CardContent>
            <CardFooter className="flex-col items-start gap-2 text-sm">
                <div className="flex items-center gap-2 leading-none font-medium">
                    Organic acquisition channel up by 5.2% this month{" "}
                    <TrendingUp className="h-4 w-4 text-emerald-500" />
                </div>
                <div className="text-muted-foreground leading-none">Showing total visitors for the last 6 months</div>
            </CardFooter>
        </Card>
    );
}

export default BarChartMixed;
```
