---
title: "Building custom visualizations"
description: "Use the Custom Visualizations SDK to build, develop, and package your own chart types for Metabase."
url: "https://www.metabase.com/docs/latest/developers-guide/custom-visualizations"
version: "65"
---

> For the complete documentation index, see [llms.txt](https://www.metabase.com/docs/llms.txt).

# Building custom visualizations

> Custom visualizations is only available on [Pro](https://www.metabase.com/product/pro) and [Enterprise](https://www.metabase.com/product/enterprise) plans (both self-hosted and on Metabase Cloud).

You can create a custom chart type for Metabase that you build with React and TypeScript and ship as a plugin.

You scaffold a project with the `@metabase/custom-viz` package, write your visualization, and package it into a `.tgz` bundle. An admin uploads the plugin to Metabase (see [Custom visualizations](https://www.metabase.com/docs/latest/questions/visualizations/custom.md)), and you're in business.

## Overview of a custom visualization

A custom visualization is a small React app that Metabase renders in place of a built-in chart.

Building a custom viz from scaffolding to adding it to your Metabase looks something like:

1. **Scaffold** a project with the `@metabase/custom-viz` CLI. The command sets up the build, the manifest, and a working starter visualization.
2. **Develop** against a locally running Metabase with hot reload while you write your component and settings.
3. **Handle the data**: read query results from `series`, wire up clicks and tooltips, and add any settings your chart needs.
4. **Match the look** with Metabase's formatters, theme variables, and color scheme.
5. **Build and package** the project into a `.tgz` bundle.
6. **Add it to your Metabase**: an admin uploads the bundle, and your chart type becomes available in your Metabase.

## Prerequisites

- Node.js 22 or newer.
- Familiarity with React and TypeScript.
- A Metabase on a [Pro or Enterprise plan](https://www.metabase.com/pricing) to load your plugin into.

## Scaffold a custom visualization project

Generate a new project with the `@metabase/custom-viz` CLI:

```
npx @metabase/custom-viz init my-viz
```

Then install dependencies and start the dev server:

```
cd my-viz
npm install
npm run dev
```

`npm run dev` runs in watch mode and rebuilds your plugin on every change.

### Project structure

```
src/
  index.tsx             # Your visualization code — start here
metabase-plugin.json    # Plugin manifest (name, icon, version)
public/
  assets/
    icon.svg            # Visualization icon (shown in the chart type picker)
package.json
vite.config.ts          # Build configuration — don't edit
tsconfig.json
```

Only `index.tsx` has to export the factory. For a more sophisticated plugin, you'd want to split the component, settings, types, and helpers into their own modules (check out the [calendar-heatmap example](#example-plugins), which keeps the definition in `index.tsx`, the React component in `Visualization.tsx`, and chart configuration and utilities under `src/`).

### The starter visualization

The scaffold ships a complete, working example: a chart that shows a thumbs-up emoji (👍) when a single numeric result meets a `threshold` setting, and a thumbs-down (👎) otherwise.

## Develop against a running Metabase

To develop your plugin against a live Metabase with hot reload:

1. Start Metabase with the `MB_CUSTOM_VIZ_PLUGIN_DEV_MODE_ENABLED` environment variable set to `true`. Dev mode is meant for local development, so you can only turn it on with this environment variable. Like any Metabase that runs custom visualizations, this local instance needs a [Pro or Enterprise](https://www.metabase.com/pricing) token.
2. Run `npm run dev` in your project. By default, the dev server listens on `http://localhost:5174`.
3. In Metabase, go to **Admin** > **Settings** > **Custom visualizations** > **Development** and set the **Dev server URL** to your dev server's address.

Your plugin shows up in the **Custom visualizations** section of the visualization sidebar (alongside any installed plugins) and is labeled as a dev visualization.

If you're running Metabase in a Docker container, you'll need to set the **Dev server URL** to:

```
http://host.docker.internal:5174
```

## The plugin manifest

Every plugin includes a `metabase-plugin.json` file at the root of the project:

```json
{
  "name": "my-viz",
  "icon": "icon.svg",
  "metabase": {
    "version": ">=1.62 <1.64"
  }
}
```

| Field              | Description                                                                                                                                                                                   |
| ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name`             | Unique identifier for the plugin. Metabase registers your visualization under this name and uses it to match replacement bundles.                                                             |
| `icon`             | Path to the visualization icon (SVG recommended). Metabase serves the icon automatically. It's the only file Metabase serves alongside your bundle. See [Bundling assets](#bundling-assets).  |
| `metabase.version` | Semver range of Metabase versions the plugin supports (for example, `">=1.62 <1.64"`). Keep the range closed on both ends. See [Versioning and compatibility](#versioning-and-compatibility). |
| `sdk.version`      | The exact `@metabase/custom-viz` version the plugin was built with. Written automatically at pack time — don't set it by hand.                                                                |

## Defining a visualization

`src/index.tsx` exports a factory function. Metabase calls the function with two helpers: `defineSetting` (for declaring settings) and the current `locale`. The factory function should return the result of `defineConfig`, which wraps your `VisualizationComponent`.

```tsx
import {
  defineConfig,
  type CreateCustomVisualization,
  type CustomVisualizationProps,
} from "@metabase/custom-viz";

type Settings = {
  threshold?: number;
};

const createVisualization: CreateCustomVisualization<Settings> = ({
  defineSetting,
  locale,
}) => {
  const VisualizationComponent = ({
    series,
    settings,
    width,
    height,
  }: CustomVisualizationProps<Settings>) => {
    // Render your visualization with React
    return <div>{/* ... */}</div>;
  };

  return defineConfig<Settings>({
    id: "my-viz",
    getName: () => "My visualization",
    minSize: { width: 2, height: 2 },
    defaultSize: { width: 6, height: 4 },
    checkRenderable(series, settings) {
      // Throw if the visualization can't render with this data or these settings
      if (series.length === 0) {
        throw new Error("No data");
      }
    },
    settings: {
      threshold: defineSetting({
        id: "threshold",
        title: "Threshold",
        widget: "number",
      }),
    },
    VisualizationComponent,
  });
};

export default createVisualization;
```

### Visualization definition properties

| Property                 | Type                                | Description                                                                                 |
| ------------------------ | ----------------------------------- | ------------------------------------------------------------------------------------------- |
| `id`                     | `string`                            | Identifier for the visualization definition.                                                |
| `getName()`              | `() => string`                      | Display name for the visualization.                                                         |
| `minSize`                | `{ width, height }`                 | Minimum size on a dashboard grid.                                                           |
| `defaultSize`            | `{ width, height }`                 | Default size on a dashboard grid.                                                           |
| `noHeader`               | `boolean`                           | When `true`, hides the default card title and description header.                           |
| `canSavePng`             | `boolean`                           | Set to `true` to enable PNG export of the live, interactive chart. Disabled by default.     |
| `checkRenderable`        | `(series, settings) => void`        | Let people know the chart doesn't work with the current data or settings.                   |
| `settings`               | `Record<string, SettingDefinition>` | Map of setting definitions created with `defineSetting()`.                                  |
| `VisualizationComponent` | `React.ComponentType`               | The interactive React component that renders the visualization in questions and dashboards. |

### Props passed to your component

| Prop               | Type                                     | Description                                                                                                                      |
| ------------------ | ---------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| `series`           | `Series`                                 | Query results — an array of series; each has `data.rows` and `data.cols`.                                                        |
| `settings`         | `CustomVisualizationSettings<TSettings>` | The resolved visualization settings.                                                                                             |
| `width`            | `number \| null`                         | Container width in pixels. `null` until the first measure — render `null` to avoid a flash.                                      |
| `height`           | `number \| null`                         | Container height in pixels. `null` until the first measure.                                                                      |
| `renderingContext` | `RenderingContext`                       | Host helpers for colors, text measurement, and the current color scheme — see [Formatting and theming](#formatting-and-theming). |
| `onClick`          | `(clickObject) => void`                  | Call to trigger drill-through actions on a data point. Pass `null` to close the drill-through popover.                           |
| `onHover`          | `(hoverObject?) => void`                 | Call to show a tooltip on a data point. Pass `null` to hide it.                                                                  |

## Handling query results

`series` is an array of result sets, with one entry per series on the chart. A single question produces one entry; a dashboard card with [multiple series](https://www.metabase.com/docs/latest/dashboards/multiple-series.md) produces several entries. Each entry has a `data` object:

- `data.rows`: an array of rows; each row is an array of cell values in column order. Row order is preserved, so when you map rows to chart points one-to-one, a point's index maps straight back to `data.rows[i]`. Useful for grabbing the whole row, not just the clicked cell.
- `data.cols`: an array of column objects describing each value. The fields you'll reach for most: `name` (database column name), `display_name` (label shown in the UI), `base_type` (Metabase type, for example `"type/Integer"`), and `semantic_type` (for example `"type/Currency"` or `"type/Latitude"`).

```tsx
const [{ data }] = series;
const total = data.rows.reduce((sum, [value]) => sum + Number(value), 0);
```

To classify a column without matching type strings by hand, use the column-type predicates the SDK exports: `isNumeric`, `isDate`, `isString`, `isBoolean`, `isCurrency`, `isLatitude`, `isCoordinate`, `isFK`, `isPK`, `isCategory`, `isURL`. These predicates take a `Column` and resolve type metadata from the host, so they only work inside a running Metabase. See [Formatting and theming](#formatting-and-theming).

```tsx
import { isNumeric } from "@metabase/custom-viz";

const numericColumns = data.cols.filter(isNumeric);
```

## Clicks and tooltips

Your component receives `onClick` and `onHover`. Call them with an object that identifies the data point being interacted with. Metabase positions popovers from it, and for clicks it offers the matching drill-through actions (filter by this value, view these rows, and so on).

```tsx
<rect
  onClick={(event) =>
    onClick({
      value: row[1],
      column: cols[1],
      dimensions: [{ value: row[0], column: cols[0] }],
      event: event.nativeEvent,
      element: event.currentTarget,
    })
  }
  onMouseMove={(event) =>
    onHover({
      element: event.currentTarget,
      data: cols.map((col, i) => ({
        col,
        value: row[i],
        key: col.display_name,
      })),
    })
  }
  onMouseLeave={() => onHover(null)}
/>
```

Pass `null` to `onHover` to dismiss the tooltip. Metabase waits a tick before hiding it, so moving between adjacent elements doesn't flicker. Pass `null` to `onClick` to close the drill-through popover, for example when the same element is clicked again or when your chart scrolls. `onClick` also takes an `origin: { row, cols }` when a drill-through needs the whole row, not just the clicked cell. It can take a `data` array of `{ col, value }` pairs (one per column) when an action needs every column's value.

The hover object accepts more than `element` and `data`. Optional fields like `index` and `seriesIndex` (to highlight a series in the legend) and `value`, `column`, `dimensions`, and `event` (for a simpler single-point tooltip) are available when you need them.

## Settings and widgets

Define settings with the `defineSetting()` helper. Each setting shows up in the visualization settings sidebar.

```tsx
settings: {
  threshold: defineSetting({
    id: "threshold",
    title: "Threshold",
    getSection: () => "Display",
    widget: "number",
    getDefault: () => 0,
    getProps: () => ({
      placeholder: "Enter threshold",
      options: { isNonNegative: true },
    }),
  }),
},
```

### Setting definition properties

| Property                       | Description                                                                                      |
| ------------------------------ | ------------------------------------------------------------------------------------------------ |
| `id`                           | Unique key — has to match the key in your `Settings` type.                                       |
| `title`                        | Label shown in the sidebar.                                                                      |
| `getSection()`                 | Function returning the section the setting appears under (for example, `"Data"` or `"Display"`). |
| `group`                        | Sub-heading within a section for grouping related settings.                                      |
| `index`                        | Display order within a group.                                                                    |
| `inline`                       | When `true`, renders the widget on the same line as `title` (handy for `"toggle"`).              |
| `widget`                       | A [built-in widget](#built-in-widgets) name, or a [custom React component](#custom-widgets).     |
| `getDefault(series, settings)` | Computes the default value when none is stored.                                                  |
| `getValue(series, settings)`   | Always-computed value — overrides the stored value on every render.                              |
| `getProps(series, settings)`   | Returns widget-specific props.                                                                   |
| `isValid(series, settings)`    | Return `false` to discard a stored value and fall back to `getDefault`.                          |
| `readDependencies`             | Setting IDs that have to resolve before this one.                                                |
| `writeDependencies`            | Setting IDs whose current values are persisted when this setting changes.                        |
| `eraseDependencies`            | Setting IDs reset to `null` when this setting changes.                                           |
| `persistDefault`               | When `true`, writes the value from `getDefault` to stored settings on first render.              |

### Reserved setting ids

Metabase adds a few settings of its own to every custom visualization. They power built-in behavior, like the column formatting popover that opens when a `"field"` or `"fields"` widget sets `showColumnSetting: true`. The setting ids `column_settings` and `column` are reserved for those settings: TypeScript rejects a `Settings` type that declares either key, and Metabase ignores any setting definition that uses one (and logs a warning to the console).

Your visualization can still _read_ the per-column formatting: the resolved `settings` object includes `column`, a function that returns a column's effective formatting settings. See [Formatting and theming](#formatting-and-theming) for how to apply it with `formatValue`.

### Built-in widgets

Widgets for the settings UI.

| Widget               | `getProps()` return type                                                   | Description              |
| -------------------- | -------------------------------------------------------------------------- | ------------------------ |
| `"input"`            | `{ placeholder? }`                                                         | Text input               |
| `"number"`           | `{ placeholder?, options?: { isInteger?, isNonNegative? } }`               | Numeric input            |
| `"toggle"`           | _(none — omit `getProps`)_                                                 | Boolean toggle           |
| `"radio"`            | `{ options: { name, value }[] }`                                           | Radio button group       |
| `"select"`           | `{ options: { name, value }[], placeholder?, placeholderNoOptions? }`      | Dropdown                 |
| `"segmentedControl"` | `{ options: { name, value }[] }`                                           | Segmented button control |
| `"color"`            | `{ title? }`                                                               | Color picker             |
| `"multiselect"`      | `{ options: { label, value }[], placeholder?, placeholderNoOptions? }`     | Multi-select dropdown    |
| `"field"`            | `{ columns, options: { name, value }[], showColumnSetting? }`              | Single column picker     |
| `"fields"`           | `{ columns, options: { name, value }[], addAnother?, showColumnSetting? }` | Multi-column picker      |

### Custom widgets

When the built-in widgets don't fit, set `widget` to your own React component instead of a built-in name. Metabase renders the component in the settings sidebar, inside the same [sandbox](#sandbox-restrictions) as your visualization. A widget that reaches for a blocked API is removed, so keep widgets to plain inputs and display.

Metabase injects these props into your widget component (import the type with `BaseWidgetProps<TValue, TSettings>`):

| Prop               | Type                  | Description                             |
| ------------------ | --------------------- | --------------------------------------- |
| `id`               | `string`              | The setting's `id`.                     |
| `value`            | `TValue \| undefined` | The setting's current value.            |
| `onChange`         | `(value?) => void`    | Update this setting's value.            |
| `onChangeSettings` | `(settings) => void`  | Update other settings at the same time. |

Add any extra props your component needs with `getProps()`. Its return type is your component's own props, minus the base props Metabase injects.

```tsx
import { defineConfig, type BaseWidgetProps } from "@metabase/custom-viz";

type Settings = { label?: string };

function LabelWidget({ value, onChange }: BaseWidgetProps<string, Settings>) {
  return (
    <input value={value ?? ""} onChange={(e) => onChange(e.target.value)} />
  );
}

// ...in your visualization's settings:
settings: {
  label: defineSetting({
    id: "label",
    title: "Label",
    widget: LabelWidget,
  }),
},
```

## Formatting and theming

Render numbers, dates, and currencies the way the rest of Metabase does with `formatValue`. Pass the cell's column to pick up that column's formatting settings, or override with options like `currency`, `decimals`, `compact`, or `date_style`:

```tsx
import { formatValue } from "@metabase/custom-viz";

formatValue(row[1], { column: cols[1] });
formatValue(0.084, { number_style: "percent", decimals: 1 }); // "8.4%"
```

To honor the formatting people pick in the column formatting popover (the one `showColumnSetting: true` enables on `"field"` and `"fields"` widgets - see [built-in widgets](#built-in-widgets)), format with `settings.column(col)`. It resolves a column's effective settings (instance-wide defaults, the column's metadata settings, and the card-level popover settings, merged in that order) into ready-to-use `formatValue` options:

```tsx
formatValue(row[1], settings.column?.(cols[1]));
```

`formatValue` and the column-type predicates (like `isNumeric` and `isDate`) read formatting and type metadata from Metabase. If you call them outside of Metabase, like in a unit test, they'll throw `Metabase Viz API not initialized`.

For layout math (like fitting labels or sizing axes), use the `renderingContext` prop: `renderingContext.measureText(text, style)` returns `{ width, height }` in pixels, where `style` is `{ size, weight, family }`. The optional `style.family` defaults to the font your Metabase is rendering with, so measurements match what people see; pass it explicitly only when you render text in a different font. That font is also available as `renderingContext.fontFamily` for styling your own markup.

To match Metabase's look (and follow [dark mode](https://www.metabase.com/docs/latest/people-and-groups/account-settings.md#theme)), you have two paths. For anything you render as DOM or SVG, you can style with Metabase's CSS variables: `var(--mb-color-brand)` and the other `--mb-color-*` variables, and the theme follows automatically.

Canvas-based charting libraries (like ECharts and Chart.js) can't read CSS variables, so in those cases you need explicit color values. Resolve a Metabase color name to its current theme value with `renderingContext.getColor("brand")`, or branch on `renderingContext.colorScheme` (`"light"` or `"dark"`) to pick your own colors per theme. See the [calendar-heatmap example](#example-plugins) for one built with ECharts.

## Bundling assets

The build produces a single JavaScript bundle (`dist/index.js`), and the [icon](#the-visualization-icon) is the only file Metabase serves alongside it. Metabase doesn't serve arbitrary static files, so bundling images into your plugin is the most reliable approach. The [sandbox](#sandbox-restrictions) blocks scripted network access like `fetch` and `XMLHttpRequest`, but it doesn't stop the browser from loading an `<img>` or CSS `url()`: an external image still loads as long as its domain is allowed by the image-domains Content Security Policy (see below).

Bundled images always render, including when an admin has turned on [Restrict image domains](https://www.metabase.com/docs/latest/configuring-metabase/domains.md#restrict-where-images-can-load-from). That Content Security Policy setting limits which external hosts images can load from, but inline and `data:` images ship inside your bundle, so they're never blocked.

Your `npm` dependencies are bundled in too. You can pull in a charting library (the calendar-heatmap example bundles [ECharts](https://echarts.apache.org/)), but everything ships in that single `dist/index.js`, so your code and its dependencies all count toward the packaged plugin's [size limits](#build-and-package-the-plugin).

So anything your visualization renders has to live inside that bundle. For images, you have a few options:

- **Inline SVG or emoji.** What the starter visualization does (it renders 👍 / 👎). Drop the SVG markup straight into your JSX.
- **Import the image.** Import an image from `src/` and the bundler inlines small files as a base64 data URL. Vite inlines assets below its `assetsInlineLimit` (4 KB by default); larger files are emitted as separate assets that won't ship in the single bundle, so keep imported images small or raise the limit.
- **Embed a data URL directly.** Paste a `data:image/png;base64,...` string into your component's `src`.

```tsx
import logo from "./logo.svg"; // inlined as a data URL at build time

const VisualizationComponent = () => <img src={logo} alt="" />;
```

## The visualization icon

The icon shows up in the chart type picker and elsewhere in the Metabase UI.

- Declare it with `"icon"` in `metabase-plugin.json`. The default location is `public/assets/icon.svg`.
- Use `currentColor` for fills and strokes so the icon adapts to light and dark themes, as well as to hover and active states (like when it's highlighted in a menu):

```svg
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
  <path d="..." stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
</svg>
```

- For more control, you can use Metabase's CSS variables inside an inline SVG, like `fill="var(--mb-color-brand)"`.
- Keep the icon simple and monochromatic. Skip gradients and multiple colors.

## Build and package the plugin

Run:

```
npm run build
```

This compiles `src/` to `dist/` and packages the result into `<name>-<version>.tgz` at the project root. The archive contains `metabase-plugin.json`, `dist/index.js`, and the whitelisted icon under `dist/assets/`, and has to come in under 5 MiB. The packaging step also rejects an archive whose uncompressed contents exceed 25 MiB. You don't need to commit `dist/`.

Packaging runs through the `@metabase/custom-viz` CLI, so you can also run it on its own:

```
npx @metabase/custom-viz pack
```

> **Upgrading an older project**: earlier versions (`1.*`) of the CLI scaffolded a `pack.mjs` script into your project. To switch to the built-in command instead, update `@metabase/custom-viz` to the latest version, set `build` to `vite build && metabase-custom-viz pack` in `package.json`, then delete `pack.mjs` and the `tar-stream` and `@types/tar-stream` devDependencies.

For uploading and managing plugins, see [Custom visualizations](https://www.metabase.com/docs/latest/questions/visualizations/custom.md).

## Versioning and compatibility

Metabase runs two compatibility checks on a plugin, and neither one blocks anything (the plugin uploads and runs either way). When a check fails, Metabase shows a [compatibility warning](https://www.metabase.com/docs/latest/questions/visualizations/custom.md#compatibility-warnings-are-heads-ups-not-errors) on the **Manage visualizations** page.

The second check is the Metabase version, which you declare yourself. Set `metabase.version` in `metabase-plugin.json` to an [npm semver range](https://github.com/npm/node-semver#ranges) like `">=1.62 <1.64"`, closed on both ends. Metabase versions are [license.major.minor](https://www.metabase.com/version-support), so include the license prefix: a bare `">=62 <64"` won't match anything.

## Custom visualization limitations

### Sandbox restrictions

Metabase runs plugin code in an isolated sandbox, so a visualization works only from the `series` and `settings` it's given. The sandbox blocks:

- **Network access**: `fetch`, `XMLHttpRequest`, `WebSocket`, `EventSource`, `Worker`, `SharedWorker`, `RTCPeerConnection`, `WebTransport`, `BroadcastChannel`, `navigator.sendBeacon`, and `FontFace.load`. You can't call Metabase's APIs or any other service.
- **Browser storage and cookies**: `localStorage`, `sessionStorage`, `indexedDB`, the Cache API, `document.cookie`, and the `CookieStore` API.
- **Device and credential APIs**: clipboard, geolocation, camera and microphone, service workers, the Credentials and Permissions APIs, USB, Bluetooth, HID, serial, WebXR, and Web Share.
- **Browser UI**: `window.open`, dialogs (`alert`, `confirm`, `prompt`, `print`), notifications, modal dialogs, fullscreen, and payment requests.
- **Navigation and the rest of the app**: history changes, the host page's URL and referrer, and any DOM outside the plugin's own container.
- **Unsafe DOM and timing APIs**: `document.write`, `execCommand`, constructable stylesheets, raw HTML parsers (`DOMParser`, `setHTMLUnsafe`, `XSLTProcessor`), and resource-timing APIs that expose other requests the page has made.

### Custom visualizations only work in authenticated embeds

[Modular embeds](https://www.metabase.com/docs/latest/embedding/modular-embedding.md) can render custom visualizations when you allowlist them with the `allowedCustomVisualizations` setting, both with web components and with the [React SDK](https://www.metabase.com/docs/latest/embedding/sdk/introduction.md). See [Custom visualizations in embeds](https://www.metabase.com/docs/latest/embedding/custom-visualizations.md).

In [guest embeds](https://www.metabase.com/docs/latest/embedding/introduction.md) and [public links](https://www.metabase.com/docs/latest/embedding/public-links.md), any question that uses a custom visualization falls back to the default visualization for the query's results.

### Custom visualizations don't render in exports and subscriptions

Static renders (like charts in alerts or dashboard subscriptions) fall back to a default visualization for the query's data shape.

## Example plugins

- [Calendar heatmap](https://github.com/metabase/custom-viz-calendar-heatmap). Read through `src/` for an example of `checkRenderable`, settings, and rendering against `series` data.
- [Thumbs](https://github.com/metabase/custom-viz-thumbs). Thumbs up or down depending on a threshold.

## Further reading

- [Custom visualizations](https://www.metabase.com/docs/latest/questions/visualizations/custom.md)
- [`@metabase/custom-viz` on npm](https://www.npmjs.com/package/@metabase/custom-viz)
- [Visualization overview](https://www.metabase.com/docs/latest/questions/visualizations/visualizing-results.md)
