> For the complete documentation index, see [llms.txt](https://docs.rierino.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rierino.com/sdk-dv-ui/tables/pivot-table.md). Agents can also search and read this documentation through its [MCP server](https://docs.rierino.com/~gitbook/mcp).

# Pivot Table

Groups rows by fields of your choice and totals a value, in a table viewers can rearrange.

{% tabs %}
{% tab title="Definition" %}

```json
{
  "sources": [{ "key": "orders", "url": "SourceVisData?queryId=orders", "params": {} }],
  "layout": {
    "type": "RPivotTable",
    "bind": { "query": "SELECT region, city, [month], orders, revenue FROM orders" },
    "props": {
      "rows": ["region", "city"],
      "cols": ["month"],
      "vals": ["revenue"],
      "aggregatorName": "Sum",
      "classes": { "rootExtra": "overflow-auto min-h-96" }
    }
  }
}
```

{% endtab %}

{% tab title="Sample data" %}
The rows the dashboard's sources return.

`orders` (30 rows)

```json
[
  { "month": "2026-07", "city": "Boston", "region": "North", "orders": 40, "revenue": 3200 },
  { "month": "2026-07", "city": "Chicago", "region": "North", "orders": 53, "revenue": 3777 },
  { "month": "2026-07", "city": "Austin", "region": "South", "orders": 41, "revenue": 4354 },
  { "month": "2026-07", "city": "Miami", "region": "South", "orders": 54, "revenue": 4931 },
  { "month": "2026-07", "city": "Denver", "region": "West", "orders": 42, "revenue": 5508 },
  { "month": "2026-08", "city": "Boston", "region": "North", "orders": 47, "revenue": 4291 },
  { "month": "2026-08", "city": "Chicago", "region": "North", "orders": 60, "revenue": 4868 },
  { "month": "2026-08", "city": "Austin", "region": "South", "orders": 48, "revenue": 5445 },
  { "month": "2026-08", "city": "Miami", "region": "South", "orders": 61, "revenue": 3622 },
  { "month": "2026-08", "city": "Denver", "region": "West", "orders": 49, "revenue": 4199 },
  { "month": "2026-09", "city": "Boston", "region": "North", "orders": 54, "revenue": 5382 },
  { "month": "2026-09", "city": "Chicago", "region": "North", "orders": 42, "revenue": 5959 },
  { "month": "2026-09", "city": "Austin", "region": "South", "orders": 55, "revenue": 4136 },
  { "month": "2026-09", "city": "Miami", "region": "South", "orders": 43, "revenue": 4713 },
  { "month": "2026-09", "city": "Denver", "region": "West", "orders": 56, "revenue": 5290 },
  { "month": "2026-10", "city": "Boston", "region": "North", "orders": 61, "revenue": 4073 },
  { "month": "2026-10", "city": "Chicago", "region": "North", "orders": 49, "revenue": 4650 },
  { "month": "2026-10", "city": "Austin", "region": "South", "orders": 62, "revenue": 5227 },
  { "month": "2026-10", "city": "Miami", "region": "South", "orders": 50, "revenue": 5804 },
  { "month": "2026-10", "city": "Denver", "region": "West", "orders": 63, "revenue": 3981 },
  { "month": "2026-11", "city": "Boston", "region": "North", "orders": 43, "revenue": 5164 },
  { "month": "2026-11", "city": "Chicago", "region": "North", "orders": 56, "revenue": 5741 },
  { "month": "2026-11", "city": "Austin", "region": "South", "orders": 44, "revenue": 6318 },
  { "month": "2026-11", "city": "Miami", "region": "South", "orders": 57, "revenue": 4495 },
  { "month": "2026-11", "city": "Denver", "region": "West", "orders": 45, "revenue": 5072 },
  { "month": "2026-12", "city": "Boston", "region": "North", "orders": 50, "revenue": 6255 },
  { "month": "2026-12", "city": "Chicago", "region": "North", "orders": 63, "revenue": 4432 },
  { "month": "2026-12", "city": "Austin", "region": "South", "orders": 51, "revenue": 5009 },
  { "month": "2026-12", "city": "Miami", "region": "South", "orders": 64, "revenue": 5586 },
  { "month": "2026-12", "city": "Denver", "region": "West", "orders": 52, "revenue": 6163 }
]
```

{% endtab %}
{% endtabs %}

## Usage

```json
{
  "type": "RPivotTable",
  "bind": { "query": "SELECT region, city, [month], orders, revenue FROM orders" },
  "props": {
    "rows": ["region", "city"],
    "cols": ["month"],
    "vals": ["revenue"],
    "aggregatorName": "Sum",
    "classes": { "rootExtra": "overflow-auto min-h-96" }
  }
}
```

The query returns plain rows, **not grouped**: the pivot table groups them itself. `rows` and `cols` name the fields whose values become its row and column headers. Each cell combines the `vals` field of the rows in that group, with `aggregatorName` (a sum, by default), and the last row and column show the totals.

The pivot table is built on [react-pivottable](https://github.com/plotly/react-pivottable).

## Changing it on the dashboard

Viewers can change the pivot table without editing the dashboard:

* **drag** a field between the bar of unused fields, the rows (left) and the columns (top);
* click the **arrow** next to a field to choose which of its values to include;
* choose the **renderer** (top left), the **aggregator** and its **value field** below it;
* click **↕** and **↔** to sort the rows and columns by their labels or by their totals.

Their changes last until the dashboard is reloaded. They aren't saved, and the definition's settings are used again after a reload.

## Examples

### Heatmap

`rendererName` sets how the result is drawn. `Table Heatmap` colours each cell by its value: the higher, the redder. Here the cells are the average number of orders, and `region` is hidden from the viewer.

{% tabs %}
{% tab title="Definition" %}

```json
{
  "sources": [{ "key": "orders", "url": "SourceVisData?queryId=orders", "params": {} }],
  "layout": {
    "type": "RPivotTable",
    "bind": { "query": "SELECT region, city, [month], orders, revenue FROM orders" },
    "props": {
      "rows": ["city"],
      "cols": ["month"],
      "vals": ["orders"],
      "aggregatorName": "Average",
      "rendererName": "Table Heatmap",
      "hiddenAttributes": ["region"],
      "hiddenFromDragDrop": ["orders", "revenue"],
      "classes": { "rootExtra": "overflow-auto min-h-96" }
    }
  }
}
```

{% endtab %}

{% tab title="Sample data" %}
The rows the dashboard's sources return.

`orders` (30 rows)

```json
[
  { "month": "2026-07", "city": "Boston", "region": "North", "orders": 40, "revenue": 3200 },
  { "month": "2026-07", "city": "Chicago", "region": "North", "orders": 53, "revenue": 3777 },
  { "month": "2026-07", "city": "Austin", "region": "South", "orders": 41, "revenue": 4354 },
  { "month": "2026-07", "city": "Miami", "region": "South", "orders": 54, "revenue": 4931 },
  { "month": "2026-07", "city": "Denver", "region": "West", "orders": 42, "revenue": 5508 },
  { "month": "2026-08", "city": "Boston", "region": "North", "orders": 47, "revenue": 4291 },
  { "month": "2026-08", "city": "Chicago", "region": "North", "orders": 60, "revenue": 4868 },
  { "month": "2026-08", "city": "Austin", "region": "South", "orders": 48, "revenue": 5445 },
  { "month": "2026-08", "city": "Miami", "region": "South", "orders": 61, "revenue": 3622 },
  { "month": "2026-08", "city": "Denver", "region": "West", "orders": 49, "revenue": 4199 },
  { "month": "2026-09", "city": "Boston", "region": "North", "orders": 54, "revenue": 5382 },
  { "month": "2026-09", "city": "Chicago", "region": "North", "orders": 42, "revenue": 5959 },
  { "month": "2026-09", "city": "Austin", "region": "South", "orders": 55, "revenue": 4136 },
  { "month": "2026-09", "city": "Miami", "region": "South", "orders": 43, "revenue": 4713 },
  { "month": "2026-09", "city": "Denver", "region": "West", "orders": 56, "revenue": 5290 },
  { "month": "2026-10", "city": "Boston", "region": "North", "orders": 61, "revenue": 4073 },
  { "month": "2026-10", "city": "Chicago", "region": "North", "orders": 49, "revenue": 4650 },
  { "month": "2026-10", "city": "Austin", "region": "South", "orders": 62, "revenue": 5227 },
  { "month": "2026-10", "city": "Miami", "region": "South", "orders": 50, "revenue": 5804 },
  { "month": "2026-10", "city": "Denver", "region": "West", "orders": 63, "revenue": 3981 },
  { "month": "2026-11", "city": "Boston", "region": "North", "orders": 43, "revenue": 5164 },
  { "month": "2026-11", "city": "Chicago", "region": "North", "orders": 56, "revenue": 5741 },
  { "month": "2026-11", "city": "Austin", "region": "South", "orders": 44, "revenue": 6318 },
  { "month": "2026-11", "city": "Miami", "region": "South", "orders": 57, "revenue": 4495 },
  { "month": "2026-11", "city": "Denver", "region": "West", "orders": 45, "revenue": 5072 },
  { "month": "2026-12", "city": "Boston", "region": "North", "orders": 50, "revenue": 6255 },
  { "month": "2026-12", "city": "Chicago", "region": "North", "orders": 63, "revenue": 4432 },
  { "month": "2026-12", "city": "Austin", "region": "South", "orders": 51, "revenue": 5009 },
  { "month": "2026-12", "city": "Miami", "region": "South", "orders": 64, "revenue": 5586 },
  { "month": "2026-12", "city": "Denver", "region": "West", "orders": 52, "revenue": 6163 }
]
```

{% endtab %}
{% endtabs %}

```json
{
  "type": "RPivotTable",
  "bind": { "query": "SELECT region, city, [month], orders, revenue FROM orders" },
  "props": {
    "rows": ["city"],
    "cols": ["month"],
    "vals": ["orders"],
    "aggregatorName": "Average",
    "rendererName": "Table Heatmap",
    "hiddenAttributes": ["region"],
    "hiddenFromDragDrop": ["orders", "revenue"],
    "classes": { "rootExtra": "overflow-auto min-h-96" }
  }
}
```

## Hiding fields

Every field of the query is offered to the viewer. To limit that, leave a field out of the query, or list it in:

| Prop                   | The field is                                                      |
| ---------------------- | ----------------------------------------------------------------- |
| `hiddenAttributes`     | not offered at all                                                |
| `hiddenFromAggregator` | not offered as the value field (but can still be a row or column) |
| `hiddenFromDragDrop`   | not offered as a row or column (but can still be the value field) |

Measures such as `orders` and `revenue` usually go in `hiddenFromDragDrop`: grouping by them rarely makes sense, and they take up room in the unused fields bar.

## Aggregators

`aggregatorName` is one of the names below, spelled exactly. Most take one field in `vals`; **Sum over Sum** takes two, and **Count** and the **Count as Fraction** ones need none.

| Aggregator                                                | Cell shows                                                           |
| --------------------------------------------------------- | -------------------------------------------------------------------- |
| `Count`                                                   | Number of rows                                                       |
| `Count Unique Values`                                     | Number of different values of the field                              |
| `List Unique Values`                                      | The different values, separated by commas                            |
| `Sum`, `Integer Sum`                                      | Total of the field (with two decimals, or none)                      |
| `Average`, `Median`                                       | Average or median of the field                                       |
| `Sample Variance`, `Sample Standard Deviation`            | Spread of the field                                                  |
| `Minimum`, `Maximum`                                      | Smallest or largest value                                            |
| `First`, `Last`                                           | First or last value, in sort order                                   |
| `Sum over Sum`                                            | Total of the first field divided by the total of the second          |
| `Sum as Fraction of Total`, `… of Rows`, `… of Columns`   | Total of the field as a percentage of the grand, row or column total |
| `Count as Fraction of Total`, `… of Rows`, `… of Columns` | Number of rows as a percentage of the grand, row or column total     |

Numbers are formatted in US style (`1,234.50`), and fractions as percentages (`12.3%`). The format can't be changed from the definition.

## Renderers

`rendererName` is one of:

| Renderer            | Draws                                                           |
| ------------------- | --------------------------------------------------------------- |
| `Table` (default)   | A table                                                         |
| `Table Heatmap`     | A table, each cell coloured by its value against all the others |
| `Table Row Heatmap` | The same, against the other cells of its row                    |
| `Table Col Heatmap` | The same, against the other cells of its column                 |
| `Exportable TSV`    | The result as tab-separated text, to copy into a spreadsheet    |

There are no chart renderers: use an E-Charts or Plotly chart for those.

## Size

The pivot table takes the width it needs and doesn't shrink: the field list on the left alone is about 300px wide. Give it `overflow-auto` so it scrolls sideways when its space is narrower, as in the examples above.

{% hint style="warning" %}
**Leave room for the value lists**

With `overflow-auto`, the value list that opens from a field's arrow can't go past the bottom of the pivot table: the part that doesn't fit has to be scrolled to. A minimum height such as `min-h-96` (the largest one in the stylesheet) leaves room for most lists.
{% endhint %}

## Props

| Prop                   | Type       | Default   | Description                                                  |
| ---------------------- | ---------- | --------- | ------------------------------------------------------------ |
| `rows`                 | `string[]` |           | Fields whose values are the row headers, outermost first.    |
| `cols`                 | `string[]` |           | Fields whose values are the column headers, outermost first. |
| `vals`                 | `string[]` |           | Field(s) the aggregator combines; two for Sum over Sum.      |
| `aggregatorName`       | `string`   | `"Sum"`   | How the values of each cell are combined, see Aggregators.   |
| `rendererName`         | `string`   | `"Table"` | How the result is drawn, see Renderers.                      |
| `hiddenAttributes`     | `string[]` |           | Fields not offered to the viewer at all.                     |
| `hiddenFromAggregator` | `string[]` |           | Fields not offered as the value field.                       |
| `hiddenFromDragDrop`   | `string[]` |           | Fields not offered as rows or columns.                       |
| `sample`               | `object[]` | `[]`      | Rows used when there is no data, for designing.              |
| `classes`              | `object`   |           | Class names for the parts, see Classes below.                |

In the Rierino designer: **Columns** (`cols`), **Rows**, **Values** (`vals`), **Hidden Attributes**, **Hidden From Aggregator**, **Hidden From Drag & Drop** and **Classes**. The designer has no setting for `aggregatorName` or `rendererName`: set them in the item's JSON.

## Classes

| Part   | Built-in classes | Styles                            |
| ------ | ---------------- | --------------------------------- |
| `root` | none             | The pivot table with its controls |

Set `classes.root` to replace the built-in classes, or `classes.rootExtra` to add to them. The table itself is styled by the package's stylesheet (`pvt…` classes) and can't be changed from the definition.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.rierino.com/sdk-dv-ui/tables/pivot-table.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
