# Cohort table

Retention by cohort as a shaded triangle, a weighted average, and the curve of the cohort you point at drawn against the average.

> chatcn Pro: needs a Pro license (https://chatcn.dev/pro). Install from the `@chatcn-pro` registry with your license token; setup at https://chatcn.dev/docs/chatcn-pro.

Example: Cohort table. Nine weekly cohorts. Point at Feb 9, after the onboarding redesign, to see it hold above the average. Preview: https://chatcn.dev/preview/cohort-table-demo

## Installation

```bash
npx shadcn@latest add @chatcn-pro/cohort-table
```

## Usage

```tsx
import { CohortTable } from "@/components/ui/chatcn/pro/cohort-table"

<CohortTable
  cohorts={[
    { label: "Feb 9", size: 1245, values: [1, 0.69, 0.55, 0.46] },   // share still active per period
    { label: "Feb 16", size: 1382, values: [1, 0.7, 0.54] },
  ]}
  labels={{ title: "Weekly retention, active users", period: (n) => `Week ${n}` }}
/>
```

Each row is a cohort, each column a period after it started, and every cell is shaded on one palette color by how many are still active; periods that have not happened yet stay empty, so it reads as the familiar triangle. The last row is the average weighted by cohort size. Point at a cell, or walk the cells with the arrow keys, and that cohort's curve is drawn above the table against the average (dashed), with the numbers said in words, so a cohort that holds better or worse stands out at once. Switch between percent and people. On narrow screens the periods scroll while the cohort names stay.

## Examples

### States

Example: States. People instead of percent on another color, and a narrow frame where the periods scroll. Preview: https://chatcn.dev/preview/cohort-table-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `cohorts` | `Cohort[]` | `{ label, size, values }`; values are shares from 0 to 1 |
| `palette` | `number` | Palette color for the shading. Default 1 |
| `defaultMetric` | `"percent" | "count"` | What the cells show first |
| `locale` | `string` | Number formatting |
| `labels` | `Partial<CohortTableLabels>` | Title, period names and every other string |

## Keyboard

| Key | Action |
| --- | --- |
| Tab | The first cell; one tab stop for the grid |
| Arrow keys | Move between cells (reversed right to left) |

## Accessibility

- A real table with row and column headers; every cell is a button named with the cohort, the period, the share and the people.
- The line above the table says the cohort's value against the average in words.

## Motion

The cohort's curve fades in over 200 ms when it changes. Nothing else moves.

Source: https://chatcn.dev/docs/components/cohort-table