# Totals & formatting

> Layouts, subtotals and grand totals, number formats per measure, and conditional formatting with color scales, data bars and thresholds.

The look of the table is part of the report: layout, totals, number formats and conditional formats are stored in it and saved with it. Users change them from the toolbar's **Layout** and **Totals** menus and from the value chip menu.

## Layouts

| `layout` | Row headers |
| --- | --- |
| `'compact'` (default) | One column for all row fields, indented by level. Saves width and suits deep hierarchies. |
| `'outline'` | One column per row field; each row shows its label in its own field's column. |
| `'tabular'` | One column per row field, with the labels of every level on the leaf rows, like a flat table. Set `repeatRowLabels: true` to repeat parent labels on every row instead of only the first row of each group. |

```ts
studio.setReport({ ...studio.getReport(), options: { ...studio.getReport().options, layout: 'tabular', repeatRowLabels: true } });
```

The tabular layout with repeated labels is the one to use before copying data into another tool.

## Subtotals and grand totals

| Option | Default | Effect |
| --- | --- | --- |
| `grandTotalRow` | `true` | A "Grand Total" row |
| `grandTotalColumn` | `true` | A "Grand Total" column for each measure |
| `rowSubtotals` | `true` | A total row for every parent row member, labelled "North Total" and so on |
| `columnSubtotals` | `true` | A total column for every parent column member |
| `subtotalPosition` | `'top'` | `'top'` puts each subtotal on the group's own row, `'bottom'` below the group |
| `grandTotalPosition` | `'bottom'` | `'top'` or `'bottom'` |

The toolbar's **Totals** menu toggles each of these, along with **Stacked column headers** and **Repeat row labels (tabular)**.

**`PivotReportOptions`** (interface from `@birchui/pivot-studio`, full reference: https://docs.birchui.com/api/pivot-studio.md)

```ts
interface PivotReportOptions
```

| Property | Type | Description |
| --- | --- | --- |
| `layout?` | `PivotLayoutName` |  |
| `grandTotalRow?` | `boolean` |  |
| `grandTotalColumn?` | `boolean` |  |
| `rowSubtotals?` | `boolean` |  |
| `columnSubtotals?` | `boolean` |  |
| `grandTotalPosition?` | `'top' \| 'bottom'` |  |
| `subtotalPosition?` | `'top' \| 'bottom'` |  |
| `stackedHeaders?` | `boolean` |  |
| `repeatRowLabels?` | `boolean` |  |
| `emptyCellText?` | `string` |  |

Totals are aggregated from the records, so averages, medians and distinct counts in total rows are exact. See [Calculations](https://docs.birchui.com/pivot-studio/calculations.md#totals-are-exact).

### Column headers

With several column fields, `stackedHeaders: true` (the default) draws one header row per level, with parent members spanning their children, such as "2026" over "Q1" to "Q4". `stackedHeaders: false` draws one header row with combined labels.

### Empty cells

Cells without records are empty by default. Set `emptyCellText`, for example `'–'`, to show a placeholder; a measure's `nullText` format overrides it for that measure.

### Expanding and collapsing

Every parent member can be collapsed to its subtotal. Users click the toggle next to a label, use the keyboard (see [Accessibility](https://docs.birchui.com/pivot-studio/accessibility.md)), or the toolbar's **Expand all** and **Collapse all**. From code:

```ts
studio.collapseAll('rows');
studio.setExpanded('rows', ['North'], true);
studio.expandAll();
```

The expansion state is part of the report, so a saved report reopens with the same members expanded.

## Number formats

Each measure has a number format. Set it on the value field, for all measures of a field in `report.formats`, or as a field default in the [field definitions](https://docs.birchui.com/pivot-studio/data.md#field-definitions):

```ts
const report: PivotReport = {
  slice: {
    rows: [{ field: 'store' }],
    values: [
      { field: 'revenue', format: { style: 'currency', currency: 'GBP', decimals: 0 } },
      { field: 'margin', aggregation: 'avg', format: { decimals: 1, suffix: ' per order' } },
      { field: 'units' },
    ],
  },
  formats: {
    units: { thousands: true, decimals: 0, nullText: '–' },
  },
};
```

**`PivotNumberFormat`** (interface from `@birchui/pivot-studio`, full reference: https://docs.birchui.com/api/pivot-studio.md)

```ts
interface PivotNumberFormat
```

| Property | Type | Description |
| --- | --- | --- |
| `style?` | `'number' \| 'currency' \| 'percent'` |  |
| `currency?` | `string` |  |
| `decimals?` | `number` |  |
| `thousands?` | `boolean` |  |
| `prefix?` | `string` |  |
| `suffix?` | `string` |  |
| `nullText?` | `string` |  |
| `excel?` | `string` |  |

| Property | Meaning | Default |
| --- | --- | --- |
| `style` | `'number'`, `'currency'` or `'percent'` | `'number'` |
| `currency` | ISO currency code for `style: 'currency'` | `'USD'` |
| `decimals` | Fixed number of decimal places | Up to 2; 0 for counts and sums of whole numbers; the currency's own for currencies |
| `thousands` | Group digits (1,250,000) | `true` |
| `prefix`, `suffix` | Text before and after the number | none |
| `nullText` | Text for empty cells of this measure | `emptyCellText` |
| `excel` | An Excel number format code used in exports instead of the generated one | generated |

Formats follow the widget's `locale`, so `de-DE` shows `1.250.000,00 €`. The format used is, in order: the value field's `format`, the field's entry in `report.formats`, then the field definition's `format`.

Calculations bring their own defaults: percentages from `showAs` use percent style with one decimal, counts and distinct counts use whole numbers, and min or max of a date field show dates. Users can set 0, 1 or 2 decimal places from the value chip's **Decimal places** menu.

In Excel exports every measure gets a matching number format code, so values stay numbers in the spreadsheet; set `excel` to use your own code, such as `'#,##0,"K"'` for thousands.

## Conditional formatting

`conditionalFormats` is a list of rules. Each applies to one measure (`measure: 'sum(revenue)'`) or, without `measure`, to every measure. Totals are excluded unless you set `includeTotals: true`, so a large grand total does not flatten the scale.

```ts title="store-performance.ts"
const report: PivotReport = {
  slice: {
    rows: [{ field: 'region' }, { field: 'store' }],
    columns: [{ field: 'orderDate.quarter' }],
    values: [{ field: 'revenue' }, { field: 'margin', showAs: 'percentDifferenceFromPrevious', along: 'columns', id: 'marginTrend' }],
  },
  conditionalFormats: [
    { type: 'dataBar', measure: 'sum(revenue)' },
    { type: 'colorScale', measure: 'marginTrend', minColor: '#f8696b', midColor: '#ffffff', maxColor: '#63be7b' },
    { type: 'threshold', measure: 'marginTrend', op: 'lt', value: -0.1, bold: true, color: '#a40e26' },
  ],
};
```

**`ConditionalFormatRule`** (type from `@birchui/pivot-studio`, full reference: https://docs.birchui.com/api/pivot-studio.md)

```ts
type ConditionalFormatRule = ColorScaleRule | DataBarRule | ThresholdRule;
```

### Color scale

`colorScale` colors each cell on a gradient from the measure's lowest to its highest value. Without colors it uses red, yellow and green (`#f8696b`, `#ffeb84`, `#63be7b`); with `minColor` and `maxColor` only, it is a two-color scale. The text color switches between dark and light automatically so values stay readable.

### Data bars

`dataBar` draws a bar in each cell proportional to its value, in `color` (default `#638ec6`) for positive values and `negativeColor` (default `#e5534b`) for negative ones. Data bars suit comparisons within a column, such as revenue per store.

### Thresholds

`threshold` styles cells whose value meets a condition, with the same operators as [value filters](https://docs.birchui.com/pivot-studio/filtering-and-sorting.md#value-filters): `gt`, `gte`, `lt`, `lte`, `eq`, `ne`, `between`, `notBetween`. It can set `background`, `color`, `bold` and a `className` for your own CSS:

```ts
{ type: 'threshold', measure: 'sum(units)', op: 'between', value: 0, valueTo: 50, className: 'low-stock' }
```

When several rules match a cell, they are applied in order. Color scales and threshold backgrounds and bold text are carried into [Excel exports](https://docs.birchui.com/pivot-studio/drill-through-and-export.md#excel-export); data bars are shown on screen only.

> **Note**
>
> Do not rely on color alone. Color scales and data bars repeat what the number already says, which keeps the table readable for users with low vision or color-vision deficiencies. Thresholds can add `bold` or a class with an icon when the state itself matters.

## Theming

Pivot Studio uses the shared `--lq-*` theme properties and the `lq-pivot` class prefix, with `theme: 'light' | 'dark' | 'auto'` (the default is `'light'`) and `density: 'compact' | 'normal' | 'comfortable'`. It adds a few properties of its own:

| Property | Used for |
| --- | --- |
| `--lq-pivot-panel-width` | Width of the field list |
| `--lq-pivot-chip-bg` | Chip background in the zones |
| `--lq-pivot-subtotal-bg` | Subtotal rows and columns |
| `--lq-pivot-grand-bg` | Grand total rows and columns |
| `--lq-pivot-negative` | Negative values |

```css
#sales-report .lq-pivot {
  --lq-pivot-panel-width: 300px;
  --lq-pivot-subtotal-bg: #f3f6fa;
}
```

The widget's stylesheet is injected into the document head when the first widget is created, after your own stylesheets, so give overrides at least one more class or ID than the rule they replace, as above.
