# Empirical Cumulative Distribution

> Step-after empirical cumulative probability distribution curve.

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-empirical-cdf).

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

## Install

```bash
npx shadcn@latest add https://shadcn.exchange/r/pacekit/tanstack-charts-empirical-cdf.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`, `d3-shape`
- Registry items: `card`

## Files

### `components/tanstack-charts/line/empirical-cdf.tsx`

```tsx
import { type ChartPoint, d3Curve, defineChart, lineY, rank } 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 { scaleLinear } from "d3-scale";
import { curveStepAfter } from "d3-shape";
import { LineChart } from "lucide-react";

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

interface CdfDatum {
    name: string;
    mpg: number;
    probability?: number;
    rank?: number;
}

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

function generateDemoData(): CdfDatum[] {
    const raw: CdfDatum[] = [];
    for (let i = 0; i < 40; i++) {
        const mpg = 12 + Math.floor(i * 0.8 + Math.random() * 3);
        raw.push({ name: `Vehicle Model #${i + 1}`, mpg });
    }
    raw.sort((a, b) => a.mpg - b.mpg);

    const ranked = rank(raw, { value: "mpg", order: "ascending" });
    return ranked.map((r) => ({
        ...r,
        probability: r.rank / raw.length,
    }));
}

function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
    if (points.length === 0) return { title: "", rows: [] };
    const d = points[0].datum as CdfDatum;
    if (!d) return { title: "", rows: [] };
    const probPct = ((d.probability ?? 0) * 100).toFixed(1);
    return {
        title: d.name,
        rows: [
            { label: "Fuel Economy", value: `${d.mpg} mpg`, color: "var(--chart-1)" },
            { label: "Cumulative Proportion", value: `${probPct}%`, color: "var(--foreground)" },
        ],
    };
}

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

    const definition = useMemo(
        () =>
            defineChart(
                {
                    renderer: motionRenderer,
                    marks: [
                        lineY(data, {
                            id: "empirical-cdf",
                            key: "name",
                            x: "mpg",
                            y: "probability",
                            curve: d3Curve(curveStepAfter),
                            stroke: "var(--chart-1)",
                            strokeWidth: 2,
                        }),
                    ],
                    scales: {
                        x: {
                            scale: scaleLinear,
                            grid: true,
                            axis: { label: "Fuel Economy (mpg)" },
                        },
                        y: {
                            scale: scaleLinear().domain([0, 1]),
                            grid: true,
                            axis: {
                                ticks: { format: (v: number) => `${Math.round(v * 100)}%` },
                                label: "Cumulative Proportion",
                            },
                        },
                    },
                    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],
    );

    return (
        <Card className="w-full max-w-xl">
            <CardHeader>
                <CardTitle>Empirical Cumulative Distribution (ECDF)</CardTitle>
                <CardDescription>
                    Step-after cumulative probability distribution of vehicle fuel economy
                </CardDescription>
            </CardHeader>
            <CardContent>
                <div className="h-[250px] w-full">
                    <Chart
                        definition={definition}
                        renderer={motionRenderer}
                        height={250}
                        ariaLabel="Empirical Cumulative Distribution Chart"
                    />
                </div>
            </CardContent>
            <CardFooter className="flex-col items-start gap-2 text-sm">
                <div className="flex items-center gap-2 leading-none font-medium">
                    50th percentile (median) reached at 24 mpg <LineChart className="h-4 w-4 text-emerald-500" />
                </div>
                <div className="text-muted-foreground leading-none">
                    Curve shows exact empirical percentage of observations below any given value threshold
                </div>
            </CardFooter>
        </Card>
    );
}

export default EmpiricalCdf;
```
