﻿---
title: Rendering
description: How the charts pack draws a chart node through a SceneRenderer, and how a host chooses which one.
url: https://docs-v3-preview.elastic.dev/isomer/primitives/charts/rendering
---

# Rendering
Every chart primitive's `react` renderer is a shim. It resolves the node to a [Scene](https://docs-v3-preview.elastic.dev/isomer/primitives/charts/scene), picks a `SceneRenderer`, and hands it the Scene. The shim draws nothing itself, so replacing how a chart is drawn is configuration: primitives, compositions and materializers stay the same.
```ts
import {
  type ReactSceneRenderer,
  chartsPack,
  createChartsPack,
} from '@elastic/isomer-primitives-charts';
import { createIsomerRuntime } from '@elastic/isomer-runtime';

createIsomerRuntime({ packs: [chartsPack] });

// A renderer that draws on Node, such as the Elastic Charts engine's; the pack ships none.
declare const hostImageRenderer: ReactSceneRenderer;

const imagePack = createChartsPack({ sceneRenderers: [hostImageRenderer] });
```

`chartsPack` registers `metric`, `legacyMetric`, `xyChart`, `dataTable`, `partitionChart`, `heatmap`, `gauge`, `tagCloud`, `grid` and `unsupported` with their defaults: charts resolve with [`resolveChart`](/isomer/primitives/charts/scene#resolvechart) and draw with the pack's fallback, the Scene's summary sentence, until a host registers an engine. `createChartsPack` takes the pack's renderer preferences, and a `resolveScene` in place of `resolveChart`, with the same signature: `(node, { scheme, size?, locale?, timeZone? }) => Scene`.
A `snapshot` or `html` render reads each chart node several times: its height estimate, then a pass that collects styles and the pass that draws. The pack resolves a node once for each set of options and draws each Scene once per renderer, and keeps neither past the task the render runs in. A `resolveScene` and a renderer are therefore pure: the same input gives the same output. A different `context.onEvent` draws afresh.
The pack declares an [input budget](/isomer/sdk/packs#inputbudget), 250,000 values and 10,000,000 characters, because chart nodes carry their data inline. A runtime that registers it validates compositions up to that size unless the host sets a budget of its own.

## Choosing a renderer

For each chart node, the shim tries renderers in this order and takes the first that runs in the render's environment and supports the Scene's kind:
1. `context.sceneRenderers`, the host's preference for this render;
2. the pack's `sceneRenderers`, given to `createChartsPack`;
3. the pack's own renderers: its table renderer for `table` Scenes (see [`dataTable`](/isomer/primitives/charts/data-table#surfaces)), its legacy metric renderer for `legacyMetric` Scenes (see [`legacyMetric`](/isomer/primitives/charts/legacy-metric#surfaces)), then the fallback, which draws the Scene's summary sentence for every kind in a browser and on Node.

The `snapshot` surface takes no host context, so a host that wants a particular engine in images names it in the pack's `sceneRenderers`. A renderer is a plain value:
```ts
const metricTiles: SceneRenderer<ReactNode> = {
  id: 'metric-tiles',
  environments: ['browser'],
  supports: (kind) => kind === 'metric',
  render: (scene, env) => <MetricTiles scene={scene} nodeId={env.nodeId} />,
};

runtime.surfaces.react.render(composition, {
  context: { sceneRenderers: [metricTiles] },
});
```

Here metric nodes draw as `MetricTiles` in the browser, and every other kind, or any render outside a browser, keeps the pack's choice.

## The render context

`ChartsRenderContext` extends the SDK's render context. Every field is optional.

| Field                | Effect                                                                                                               |
|----------------------|----------------------------------------------------------------------------------------------------------------------|
| `sceneRenderers`     | Renderers tried before the pack's own                                                                                |
| `environment`        | `browser` or `node`. Absent: detected from the global scope                                                          |
| `scheme`             | `light` or `dark`. Absent: the frame theme's `scheme` field on `snapshot`, else `light`                              |
| `locale`, `timeZone` | Passed to the resolver. Absent: `resolveChart`'s defaults, `en` and `UTC`, for dates whose format names no time zone |
| `size`               | The box the chart draws in. Absent: the chart's [size spec](#chart-size)                                             |

A host that loads the charts pack without a style adapter names the runtime's type parameters to pass these fields, as [Runtime](/isomer/runtime/runtime#typing-the-render-context) shows.
When the context carries `onEvent`, the shim passes the renderer an `onEvent` that wraps each `ChartEvent` with `toActionEvent`. Without it the renderer gets none and the chart is not interactive.
The shim puts the renderer's output in one `div` that carries the [node anchor](/isomer/sdk/rendering#node-anchors), and draws the Scene's `link` below the chart in the Scene's text and background colors, on one line 18 px tall that clips a longer label, so every renderer gets it without drawing it. A host or grid panel `size` holds that line: the chart resolves for the height left above it.

## Chart size

A chart in a grid panel draws in the box the panel leaves it, and a host can pass any chart a `size`. Without either, a chart takes its kind's size spec, as Lens's chart size specs do. Every length is the pack theme's:

| Kind                                  | Spec                                                                                                                                                                                                   |
|---------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `xyChart` over time, drawn vertically | 16:9, at least 300 px tall                                                                                                                                                                             |
| Any other `xyChart`                   | 16:9. Lens fills its container instead; outside a sized container there is nothing to fill                                                                                                             |
| `metric` with one tile                | 300:160, at most 300 px wide                                                                                                                                                                           |
| `metric` with several tiles           | One square tile side per tile, in the tile grid's columns and rows, at most 200 px per column                                                                                                          |
| `partitionChart`                      | 16:9. Lens fills its container instead, as for an `xyChart`                                                                                                                                            |
| `heatmap`                             | 16:9. Lens fills its container instead, as for an `xyChart`                                                                                                                                            |
| `gauge`                               | Lens's spec for its shape, at its largest box: 3:1 up to 600 px wide for a horizontal bullet, 2:3 up to 400 px for a vertical one, 2:1.25, 1.1:1 and 1:1 up to 600 px for a semicircle, arc and circle |
| `tagCloud`                            | 16:9. Lens fills its container instead, as for an `xyChart`                                                                                                                                            |
| `legacyMetric`                        | 16:9, at least as tall as its tiles' estimated boxes wrapped into rows at the chart's width. Lens fills its container instead, as for an `xyChart`                                                     |
| `dataTable`                           | None: the table is its rows tall                                                                                                                                                                       |

In a browser, a `fluid` renderer's `div` takes the spec as CSS (`width: 100%`, `aspect-ratio`, `min-height`, `max-width`, from the `chartsScene-sized` class), and the renderer fills it; a `legacyMetric`'s `min-height` wraps its tiles at the width the surface lays out in, as below; any other renderer, such as the summary fallback, sizes itself. On Node there is no container to fill, and a `sized` renderer draws a fixed-size picture wherever it runs, so in either case the chart resolves for the spec's box at the width the surface lays out in: the frame's `contentWidth` on `snapshot`, or an `html` render's `width`. That is 928 px on the charts frame's 960 px page, where a 16:9 chart is 522 px tall. Where no surface states a width, it is the pack theme's chart width, 480 px. A spec's greatest width still holds, so one `metric` tile stays 300 px wide, and `metric`, `xyChart`, `partitionChart`, `heatmap`, `gauge`, `tagCloud` and `legacyMetric` report that box's height, plus the link line when there is a link, to `snapshot` frames as `metrics.snapshotHeight`. A `sized` renderer gets a `dataTable` at that width and its rows' height.
A chart on its own grows taller as its page grows wider, so its picture's memory grows with the square of the page's width. A host that takes a `width` from a request bounds it.

## Styling

The pack authors its CSS with [Distillate](https://elastic.github.io/distillate/) and declares its distillery, so the `html` and `snapshot` surfaces style its nodes with no host setup, beside another Distillate pack too. Its renderers resolve class names through the render context's `resolveClassName`. Without one, as on the `react` surface, they write Distillate's readable names, and the host ships `chartsStylesheet()`, every rule the pack draws with. Every class name starts with `charts`, so none collides with another library's.
Every declaration is in a style module. What only a node or a Scene knows, such as a cell's color, a bar's extent or a panel's place on the grid, reaches its rule through a module-local Distillate var: the rule declares the var's default and reads it, and the renderer sets the value on the same element with `varStyle` (a length through `px`, since a custom property takes no unitless length), so an element's `style` holds only custom properties its classes declare. `createVarsModule` authors such a module, returning its `StyleHandle`s, `LocalVarGroup`s and `stylesheet()` (every rule it authors, variants included), and `cls` resolves its handles; all four helpers are on `./scene`, so a chart engine styles the few elements it draws itself, such as the Elastic Charts engine's secondary-metric text, with the pack's distillery, and `chartsStylesheet()` carries those rules once the engine is loaded; a module's own `stylesheet()` returns its rules whatever has loaded, which is what the engine's `elasticChartsStylesheet()` ships. The one exception is `chartsFrame`: a frame's `wrap` gets no class-name resolution, so the frame draws its surround with inline styles.

## When a chart cannot be drawn

A chart whose resolver or renderer throws does not fail the render. On every surface it draws `CHART_FAILURE_TEXT`, "This chart could not be drawn.", under its title in place of the chart, in the node's anchor on `react`, and the charts beside it draw as usual. `createChartsPack({ onFailure })` is told the error and the node, once for each node a render could not draw; without it the error is dropped. An `onFailure` that throws fails the render, which is what a test harness wants.
```ts
createChartsPack({
  onFailure: (error, node) => logger.warn({ error, type: node.type }),
});
```

A renderer that returns a component draws inside React's own render, after the pack has returned, so what it throws there reaches the host's error boundary and not the note.

## Text, Markdown and Slack

The `text` and `markdown` renderers take no render context. They resolve the Scene for the light scheme and write its `summary`, its `description` and each emphasis `note`, then its `table`: a header row of column labels, one row per data row, in `data` order, and a `dataTable`'s footer row. Columns the Scene flags as empty are left out, and an empty cell carries the Scene's empty-value text. Markdown writes the table as a GFM table. Last comes the Scene's `link`, as `label: href` (a Markdown link). The fallback renderer draws the summary, description and notes, each a paragraph with no margin, and the shim draws the link below it. A `link` whose `href` fails the SDK's navigation policy is dropped before any surface draws it.
```text
Requests over time: Count of records by @timestamp per hour, from 98 to 210.

@timestamp per hour | Count of records
00:00 | 120
01:00 | 98
```

On Slack, `metric`, `legacyMetric`, `xyChart`, `partitionChart`, `heatmap`, `gauge` and `tagCloud` are the pack's `slackAssetTypes`. With `collectAssets`, each chart becomes an `image` block and an upload request for a `snapshot` render of the node, with its text as the alt text, cut to Slack's limit. Without it, a chart is its Markdown converted to Block Kit, so a table past Slack's 100 rows or 20 columns prints as rich text with every cell rather than as a cut `table` block. A `dataTable` is not an asset type: it is always a native `table` block. A `grid` keeps its nested charts' pictures: its title is a header, each section a container, and each panel title a header above its node.

### Parity gates

Two gates hold the pack to its Scenes over every example, nested in a `grid` or not. They record each Scene where the pack resolves and draws it, so a new example or chart primitive is covered without changing them.
- **Surface parity** (`src/surface_parity.test.ts`): on each surface, every chart node of the parsed composition the surface shows, nested in a grid or not but not in a collapsed section or left out by its `surfaces` hint, reaches a Scene or goes as a picture once per time it occurs, as many times as the surface reaches a lone chart of its type, so a surface that drops a chart, even one repeated elsewhere, fails rather than shrinking what is checked. Text writes each Scene's summary, description and notes in order, its table as the rows above, and its link. Markdown, read back with a GFM parser, holds the same sentences, the table as a GFM table cell for cell, and the link. Slack without pictures holds the sentences, the table and the link: a `dataTable`'s is a native table of the Scene's strings in its starting order, cut to Slack's caps, and any other chart's is a native table when it fits those caps and rich text holding every cell beside its column heading when it does not. With pictures, each asset type is an `image` whose alt text is the node's text, cut to Slack's limit, and every other chart is text as without pictures.
- **Renderer parity** (`src/renderer_parity.test.ts`): the examples resolve every Scene kind, and the fallback draws each Scene's summary, description and notes, the table renderer its title, description, column labels, the first page's rows in the starting order cell for cell, its footer and notes, and the legacy metric renderer its summary as its label, each tile's value and, when shown, label in tile order, or its empty text.


## Testing a static renderer

`@elastic/isomer-primitives-charts/testing` holds the checks any renderer that draws a Scene as static markup, for `snapshot` and an image backend, can run with its own test runner. It needs `@elastic/isomer-runtime` and `react-dom`, its optional peers; no other entry reaches them.
- `drawExamples(renderers, environment?)` draws every example of the pack with `renderers` first, in both schemes, through the `react` surface, nested charts included, and returns each drawing (`DrawnScene`: the example, scheme, Scene and markup), every Scene the pack resolved, and the examples.
- `coverageProblems(drawn, supports)` lists every Scene of a kind the renderer supports that an example did not draw, and every supported kind with no content check.
- `drawingProblems(drawing, reading)` lists where a drawing differs from its Scene: its box, accessible name and description (`sceneDescription`), background, and what its kind draws: an xy chart's axis titles and series colors; a metric's tile titles, subtitles, values and secondary text; a partition's slice colors, legend items and values, no word that is none of its slices' or legend's, or an empty chart's note alone; a heatmap's axis titles, cell colors and legend bands, or an empty chart's note alone. A string is drawn (`textDrawn`) when it is one whole entry of the reading's `texts`, or consecutive entries that are its lines, whitespace apart; text that does not fit may be its start and an ellipsis. A string inside a longer entry is not drawn, so read each run of text as its own entry. A partition's words follow the same rule: they divide into words of its slices' and legend items' text, each whole, cut with an ellipsis, or broken across consecutive words. Colors compare by their red, green and blue channels (`rgbOf`), read from exactly these notations: `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb(r, g, b)` or `rgba(r, g, b, a)` with commas, and `rgb(r g b)` or `rgb(r g b / a)` with spaces, each channel a number from 0 to 255. Alpha is not compared. Any other paint matches no color and no other paint: `none`, `currentColor`, a percentage channel, which no renderer here emits, or anything malformed.

Each engine reads its own output into a `DrawingReading` (its size, name, description, background, the strings drawn and the colors painted), with `unescapeMarkup` and `rgbOf` to help. Read what the markup draws wherever it can: an engine that draws into bitmaps can only report what its engine says it drew, which is weaker evidence than text and paint in the markup. Ticks are each engine's to choose, so these checks leave them to the engine's own.
`src/testing/testing.test.ts` holds the checks to a renderer that draws each Scene's visible parts, found by walking the Scene, and to mutations of its output, each of which they fail.

## `grid`

A `grid` draws its panels on 48 columns with CSS grid, one row unit 20 px tall. A grid's or panel's title and description are its header, each one line of text, and the node draws below, clipped to what the panel leaves it. Panels are in [reading order](https://docs-v3-preview.elastic.dev/isomer/primitives/charts/grid) in the markup, so a screen reader and the one-column layout read them alike.
A grid lays out for the width it is given: a host's or a containing panel's `size`, else the width the surface lays the render out in (`layoutWidthOf`), which is `chartsFrame`'s content width on `snapshot` and the `width` an `html` render states. When that width is narrower than 768 px, the grid draws one column in reading order instead. Each panel's node gets a context with the `size` its panel leaves it, and draws at that size; where the grid's own width is known, from a host or a surface, the same width is the node's layout width, so a node from any pack reads the room it has. Given no width, as on `react` without a `size` and on `html` without a `width`, the grid keeps 48 columns and sizes each panel's node for the grid's nominal width, the content width of `chartsFrame`, 928 px, so a chart still fits its panel's height and a grid on `snapshot` fits its page; a browser lays the grid out fluidly, so the renderer of such a node is told `fillWidth`, and the pack's table and legacy metric fill their panel's width instead of drawing at the nominal one. A host that wants one column on a narrow screen measures the grid and passes its `size`, which every panel's node draws at exactly. A grid nested in a panel of such a grid keeps 48 columns too, as it does under a surface's width: its panel reserves the height of its rows, so only a `size` that comes from the host switches a nested grid to one column.
On `snapshot`, each nested node receives the frame theme, so charts inside a grid draw the frame's scheme. `metrics.snapshotHeight` sums the grid's header, the rows, section headers and gaps at the width it is measured for: on its columns, or in one column, each panel as tall as its rows, under 768 px. Text, Markdown and Slack list the panels in reading order under their section titles; a collapsed section is its title alone. On `react`, each panel of a collapsed section is a hidden, empty element carrying its node anchor, so anchors pair with the grid's `children`, which keep every panel for validation.

## `chartsFrame`

`chartsFrame` is a [frame](https://docs-v3-preview.elastic.dev/isomer/runtime/frame) for drawing charts on `snapshot`. A host registers it with the runtime and names it per render, or makes it the `defaultFrame`:
```ts
import { chartsFrame, chartsPack } from '@elastic/isomer-primitives-charts';

const runtime = createIsomerRuntime({
  packs: [chartsPack],
  frames: { charts: chartsFrame },
});

runtime.surfaces.snapshot.render(composition, {
  frame: 'charts',
  theme: 'dark',
});
```

The page is 960 px wide with 16 px of padding, so its content is 928 px, the grid's nominal width. A render's `width` makes the page wider or narrower, and the frame's `contentWidth`, the page less its padding and never less than zero, is the width a grid on it lays out for: `width: 1532` gives a grid 1500 px, and a page under 800 px gives it one column. It draws the composition's title and subtitle, one line each at 20 px and 14 px, then each body node, in a column with 16 px between each. Its sizing is `content`: the image is as tall as the page lays out, and `estimateHeight` is the padding, the title and subtitle lines, each node's `metrics.snapshotHeight` and the gaps between them. Every length is the pack theme's.
Its theme, `ChartsFrameTheme`, is `{ scheme, background, text }` per mode: the scheme charts resolve for, and the page's background and text colors from the Scene's chrome for that scheme. [A runtime's frames share one theme type](/isomer/runtime/frame#a-runtime-is-homogeneous-in-its-theme), so a runtime that registers `chartsFrame` beside another frame gives that frame's theme these keys too.

## `unsupported`

An `unsupported` node draws a note with its title, reason and link, in the page's text color. Text and Markdown write the same three. A link that fails the SDK's navigation policy is dropped.