# Data table

> Show rows and columns as an interactive table with formats, sorting, search, paging, row density and a total row, from a declarative spec.

A data table lists exact values, one row per record. Use it when readers need to look up a number, compare several fields on one record, or scan a list longer than a chart can label. Tables render to the DOM, not SVG, from a declarative spec.

When the question is how values compare, a [bar chart](/docs/comparison/bar/) answers it faster. Add [column types](/docs/tables/column-types/) such as inline bars and heatmaps to get some of that into a table.

## A basic table

A table spec has `type: "table"`, the `data` rows and a `columns` list. Each column names a `key` in the rows and a `label`, and takes a d3-format string in `format`. `search: true` adds a filter box and `pagination` splits the rows into pages. Click a header to sort.

```json
{
  "type": "table",
  "data": [
    {
      "code": "IN",
      "country": "India",
      "population": 1438069596,
      "gdpPerCapita": 9818,
      "lifeExpectancy": 68.3
    },
    {
      "code": "CN",
      "country": "China",
      "population": 1410710000,
      "gdpPerCapita": 23846,
      "lifeExpectancy": 76.1
    },
    {
      "code": "US",
      "country": "United States",
      "population": 336806231,
      "gdpPerCapita": 75489,
      "lifeExpectancy": 79.2
    },
    {
      "code": "ID",
      "country": "Indonesia",
      "population": 281190067,
      "gdpPerCapita": 14470,
      "lifeExpectancy": 69
    },
    {
      "code": "BR",
      "country": "Brazil",
      "population": 211140729,
      "gdpPerCapita": 19652,
      "lifeExpectancy": 75.3
    },
    {
      "code": "MX",
      "country": "Mexico",
      "population": 129739759,
      "gdpPerCapita": 22040,
      "lifeExpectancy": 76.9
    },
    {
      "code": "JP",
      "country": "Japan",
      "population": 124516650,
      "gdpPerCapita": 46107,
      "lifeExpectancy": 83.6
    },
    {
      "code": "DE",
      "country": "Germany",
      "population": 83287273,
      "gdpPerCapita": 63676,
      "lifeExpectancy": 80.8
    },
    {
      "code": "GB",
      "country": "United Kingdom",
      "population": 68492000,
      "gdpPerCapita": 53139,
      "lifeExpectancy": 81.4
    },
    {
      "code": "FR",
      "country": "France",
      "population": 68372286,
      "gdpPerCapita": 54799,
      "lifeExpectancy": 82.4
    },
    {
      "code": "KR",
      "country": "South Korea",
      "population": 51712619,
      "gdpPerCapita": 55071,
      "lifeExpectancy": 81.9
    },
    {
      "code": "CA",
      "country": "Canada",
      "population": 40083484,
      "gdpPerCapita": 58321,
      "lifeExpectancy": 82.2
    }
  ],
  "columns": [
    {
      "key": "country",
      "label": "Country",
      "sortable": true
    },
    {
      "key": "population",
      "label": "Population",
      "format": ",.0f",
      "sortable": true,
      "align": "right"
    },
    {
      "key": "gdpPerCapita",
      "label": "GDP/capita (PPP)",
      "format": "$,.0f",
      "sortable": true,
      "align": "right"
    },
    {
      "key": "lifeExpectancy",
      "label": "Life exp.",
      "format": ".1f",
      "sortable": true,
      "align": "right"
    }
  ],
  "chrome": {
    "title": "Twelve of the world's largest economies",
    "subtitle": "Population, income and longevity for the largest economies"
  },
  "search": true,
  "pagination": {
    "pageSize": 8
  }
}
```

## Starting with a sort

`sort` sets the order before the reader touches a header, here by year-to-date change, highest first. `sortable` on a column turns the header click on or off. `density: "condensed"` shrinks the rows so twelve fit without paging.

```json
{
  "type": "table",
  "data": [
    {
      "ticker": "AAPL",
      "name": "Apple",
      "sector": "Technology",
      "price": 189.84,
      "ytdChange": 48.2,
      "trend": [
        142,
        155,
        165,
        170,
        178,
        185,
        190,
        189
      ],
      "flows": [
        3,
        6,
        -2,
        8
      ]
    },
    {
      "ticker": "MSFT",
      "name": "Microsoft",
      "sector": "Technology",
      "price": 378.91,
      "ytdChange": 57.8,
      "trend": [
        240,
        275,
        295,
        310,
        330,
        345,
        365,
        379
      ],
      "flows": [
        12,
        9,
        5,
        14
      ]
    },
    {
      "ticker": "GOOGL",
      "name": "Alphabet",
      "sector": "Communication",
      "price": 141.8,
      "ytdChange": 58.3,
      "trend": [
        89,
        95,
        108,
        120,
        125,
        130,
        138,
        142
      ],
      "flows": [
        7,
        4,
        9,
        11
      ]
    },
    {
      "ticker": "AMZN",
      "name": "Amazon",
      "sector": "Consumer",
      "price": 153.42,
      "ytdChange": 82.6,
      "trend": [
        84,
        98,
        105,
        112,
        128,
        135,
        145,
        153
      ],
      "flows": [
        10,
        8,
        13,
        20
      ]
    },
    {
      "ticker": "NVDA",
      "name": "NVIDIA",
      "sector": "Technology",
      "price": 495.22,
      "ytdChange": 239,
      "trend": [
        146,
        230,
        280,
        320,
        390,
        420,
        470,
        495
      ],
      "flows": [
        40,
        55,
        62,
        88
      ]
    },
    {
      "ticker": "META",
      "name": "Meta",
      "sector": "Communication",
      "price": 353.96,
      "ytdChange": 194.1,
      "trend": [
        120,
        165,
        210,
        240,
        290,
        310,
        340,
        354
      ],
      "flows": [
        18,
        25,
        34,
        40
      ]
    },
    {
      "ticker": "TSLA",
      "name": "Tesla",
      "sector": "Consumer",
      "price": 248.48,
      "ytdChange": 101.7,
      "trend": [
        123,
        180,
        195,
        175,
        260,
        240,
        255,
        248
      ],
      "flows": [
        15,
        -6,
        22,
        -3
      ]
    },
    {
      "ticker": "JPM",
      "name": "JPMorgan",
      "sector": "Financials",
      "price": 170.1,
      "ytdChange": 26.8,
      "trend": [
        134,
        138,
        140,
        145,
        150,
        155,
        163,
        170
      ],
      "flows": [
        5,
        4,
        6,
        7
      ]
    },
    {
      "ticker": "V",
      "name": "Visa",
      "sector": "Financials",
      "price": 260.38,
      "ytdChange": 25.2,
      "trend": [
        208,
        218,
        225,
        230,
        240,
        248,
        255,
        260
      ],
      "flows": [
        4,
        5,
        5,
        6
      ]
    },
    {
      "ticker": "JNJ",
      "name": "Johnson & Johnson",
      "sector": "Healthcare",
      "price": 156.74,
      "ytdChange": -11.3,
      "trend": [
        177,
        170,
        162,
        155,
        160,
        158,
        155,
        157
      ],
      "flows": [
        -3,
        -5,
        2,
        -1
      ]
    },
    {
      "ticker": "WMT",
      "name": "Walmart",
      "sector": "Consumer",
      "price": 162.5,
      "ytdChange": 14.8,
      "trend": [
        141,
        145,
        148,
        150,
        155,
        158,
        160,
        163
      ],
      "flows": [
        2,
        3,
        3,
        4
      ]
    },
    {
      "ticker": "PG",
      "name": "Procter & Gamble",
      "sector": "Consumer",
      "price": 154.3,
      "ytdChange": 2.1,
      "trend": [
        151,
        150,
        148,
        152,
        153,
        150,
        152,
        154
      ],
      "flows": [
        1,
        -2,
        2,
        1
      ]
    }
  ],
  "columns": [
    {
      "key": "ticker",
      "label": "Ticker",
      "sortable": true
    },
    {
      "key": "name",
      "label": "Company",
      "sortable": true
    },
    {
      "key": "sector",
      "label": "Sector",
      "sortable": true
    },
    {
      "key": "price",
      "label": "Price",
      "format": "$,.2f",
      "sortable": true,
      "align": "right"
    },
    {
      "key": "ytdChange",
      "label": "YTD %",
      "format": "+.1f",
      "sortable": true,
      "align": "right"
    }
  ],
  "sort": {
    "column": "ytdChange",
    "direction": "desc"
  },
  "density": "condensed",
  "chrome": {
    "title": "The biggest gainers lead the table",
    "subtitle": "Click a header to sort by another column"
  }
}
```

## A total row

`totalRow: true` adds a sticky footer that sums each quantitative text, bar or heatmap column; delta and sparkline columns stay blank. The sum follows search and sort, not the current page. Set `total: false` on a column to leave its total cell blank.

```json
{
  "type": "table",
  "data": [
    {
      "company": "Amazon",
      "logo": "data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%220%200%2040%2040%22%3E%3Crect%20width%3D%2240%22%20height%3D%2240%22%20rx%3D%229%22%20fill%3D%22%23ff9900%22%2F%3E%3Ctext%20x%3D%2220%22%20y%3D%2227%22%20font-family%3D%22Inter%2C%20system-ui%2C%20sans-serif%22%20font-size%3D%2222%22%20font-weight%3D%22700%22%20fill%3D%22%23ffffff%22%20text-anchor%3D%22middle%22%3EA%3C%2Ftext%3E%3C%2Fsvg%3E",
      "revenue": 638,
      "quarterly": [
        143,
        148,
        159,
        188
      ]
    },
    {
      "company": "Apple",
      "logo": "data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%220%200%2040%2040%22%3E%3Crect%20width%3D%2240%22%20height%3D%2240%22%20rx%3D%229%22%20fill%3D%22%23111827%22%2F%3E%3Ctext%20x%3D%2220%22%20y%3D%2227%22%20font-family%3D%22Inter%2C%20system-ui%2C%20sans-serif%22%20font-size%3D%2222%22%20font-weight%3D%22700%22%20fill%3D%22%23ffffff%22%20text-anchor%3D%22middle%22%3EA%3C%2Ftext%3E%3C%2Fsvg%3E",
      "revenue": 391,
      "quarterly": [
        90,
        86,
        95,
        120
      ]
    },
    {
      "company": "Alphabet",
      "logo": "data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%220%200%2040%2040%22%3E%3Crect%20width%3D%2240%22%20height%3D%2240%22%20rx%3D%229%22%20fill%3D%22%234285f4%22%2F%3E%3Ctext%20x%3D%2220%22%20y%3D%2227%22%20font-family%3D%22Inter%2C%20system-ui%2C%20sans-serif%22%20font-size%3D%2222%22%20font-weight%3D%22700%22%20fill%3D%22%23ffffff%22%20text-anchor%3D%22middle%22%3EG%3C%2Ftext%3E%3C%2Fsvg%3E",
      "revenue": 350,
      "quarterly": [
        80,
        85,
        88,
        97
      ]
    },
    {
      "company": "Microsoft",
      "logo": "data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%220%200%2040%2040%22%3E%3Crect%20width%3D%2240%22%20height%3D%2240%22%20rx%3D%229%22%20fill%3D%22%230078d4%22%2F%3E%3Ctext%20x%3D%2220%22%20y%3D%2227%22%20font-family%3D%22Inter%2C%20system-ui%2C%20sans-serif%22%20font-size%3D%2222%22%20font-weight%3D%22700%22%20fill%3D%22%23ffffff%22%20text-anchor%3D%22middle%22%3EM%3C%2Ftext%3E%3C%2Fsvg%3E",
      "revenue": 245,
      "quarterly": [
        57,
        62,
        62,
        64
      ]
    },
    {
      "company": "Meta",
      "logo": "data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%220%200%2040%2040%22%3E%3Crect%20width%3D%2240%22%20height%3D%2240%22%20rx%3D%229%22%20fill%3D%22%230866ff%22%2F%3E%3Ctext%20x%3D%2220%22%20y%3D%2227%22%20font-family%3D%22Inter%2C%20system-ui%2C%20sans-serif%22%20font-size%3D%2222%22%20font-weight%3D%22700%22%20fill%3D%22%23ffffff%22%20text-anchor%3D%22middle%22%3EM%3C%2Ftext%3E%3C%2Fsvg%3E",
      "revenue": 164,
      "quarterly": [
        36,
        39,
        41,
        48
      ]
    }
  ],
  "columns": [
    {
      "key": "company",
      "label": "Company"
    },
    {
      "key": "revenue",
      "label": "FY24 revenue ($B)",
      "format": ",.0f",
      "bar": {}
    }
  ],
  "density": "condensed",
  "totalRow": true,
  "chrome": {
    "title": "Big tech, summed",
    "subtitle": "Condensed rows with a sticky total over the filtered set"
  }
}
```

## Relaxed rows and compact numbers

`density` sets the row height: `"condensed"` is 40px, the default is 48px and `"relaxed"` is 56px. A column `format` can also be the keyword `compact`, which abbreviates large numbers. Tables format at full precision by default, so use `compact` where readers will not compare digits.

```json
{
  "type": "table",
  "data": [
    {
      "code": "IN",
      "country": "India",
      "population": 1438069596,
      "gdpPerCapita": 9818,
      "lifeExpectancy": 68.3
    },
    {
      "code": "CN",
      "country": "China",
      "population": 1410710000,
      "gdpPerCapita": 23846,
      "lifeExpectancy": 76.1
    },
    {
      "code": "US",
      "country": "United States",
      "population": 336806231,
      "gdpPerCapita": 75489,
      "lifeExpectancy": 79.2
    },
    {
      "code": "ID",
      "country": "Indonesia",
      "population": 281190067,
      "gdpPerCapita": 14470,
      "lifeExpectancy": 69
    },
    {
      "code": "BR",
      "country": "Brazil",
      "population": 211140729,
      "gdpPerCapita": 19652,
      "lifeExpectancy": 75.3
    },
    {
      "code": "MX",
      "country": "Mexico",
      "population": 129739759,
      "gdpPerCapita": 22040,
      "lifeExpectancy": 76.9
    }
  ],
  "columns": [
    {
      "key": "country",
      "label": "Country"
    },
    {
      "key": "population",
      "label": "Population",
      "format": "compact"
    },
    {
      "key": "gdpPerCapita",
      "label": "GDP/capita (PPP)",
      "format": "$,.0f"
    },
    {
      "key": "lifeExpectancy",
      "label": "Life exp.",
      "format": ".1f"
    }
  ],
  "density": "relaxed",
  "chrome": {
    "title": "Six of the world's most populous countries",
    "subtitle": "Relaxed density at 56px rows, with the population column formatted compact"
  }
}
```

## Fields that matter

| Field | What it does |
| --- | --- |
| `type: "table"` | Selects the table spec. |
| `columns[].key`, `columns[].label` | The row field to show and the header text. |
| `columns[].format` | A d3-format string, or `percent`, `currency`, `ordinal` or `compact`. |
| `columns[].sortable` | Turns the header click on or off. Default `true`. |
| `columns[].align` | Overrides alignment. Quantitative columns align right by default. |
| `search` | `true` shows a filter box above the table. |
| `pagination` | `{ pageSize }` splits the rows into pages. |
| `sort` | `{ column, direction }`, applied before the reader sorts. |
| `density` | `"condensed"`, `"regular"` or `"relaxed"`. |
| `totalRow` | `true` adds a sticky total footer. |

## Related

- [Column types](/docs/tables/column-types/) for heatmap, bar, sparkline, delta and image cells.
- [Row inspection](/docs/tables/row-inspection/) for finding and comparing rows.
- [Bar list](/docs/comparison/bar-list/) for a ranked list with inline bars and no table chrome.
- [Dashboard tiles](/docs/composition/dashboard-tiles/) for a table sized to a card.
- [TableSpec reference](/docs/spec/table-spec/) lists every table and column field.
