Skip to Content

Reference

Custom charts and the theme

The theme values a custom chart gets in the embed, how to follow your brand with them, and how to size and test the chart.

A custom chart is drawn by your own code in a sandboxed frame inside the card. It follows the same brand as the cards around it: the embed sends your theme into the frame as ctx.theme, each time it calls your chart’s render(ctx).

Show custom charts in embeds has the setup and the CSP your page needs.

The optional theme keys below require the plugin release that ships with this docs update ({{PLUGIN_VERSION}} or later).

The theme a chart gets

10 keys are always there. 6 more depend on the embed’s theme or page settings. The unbranded primaryColor keeps the host’s original value for older chart bundles that use it.

KeyTypeComes fromUse it for
fontFamilystringThe themeThe embed font, with a system sans stack after it, so a font that cannot load never falls to a serif.
textColorstringThe themeThe chart text color.
textSecondaryColorstringThe themeAxis labels and other quiet text.
backgroundstringThe themeThe page background of the embed.
surfaceColorstringThe themeThe card background.
borderColorstringThe themeBorders and grid lines.
primaryColorstringdashboard.ctaColor or the hostThe authored color when the active mode has a brand. The host fallback can be bare RGB channels such as 13, 13, 13. Wrap only bare channels in rgb() when you use the value in CSS.
fontSizenumberThe themeThe chart text size in px.
borderRadiusnumberThe themeThe corner radius in px.
palettestring[]The themeThe chart colors, in order.
fontsoptionalgeneral.fontsYour font files. The embed loads them into the frame for you, so fontFamily draws in your font. Do not load src from chart code: the frame blocks font requests.
derivedoptionalThe brand color, dashboard.ctaColorhover, active, selected, subtle, focusRing, onBrand: the brand states the cards use.
gradientoptionalchart.gradient, when enabled{ startColor, endColor, direction } for a bar fill.
densityoptionalgeneral.densitycompact means one step less padding.
legendPositionoptionalchart.legend.positionWhere to draw your legend, if you draw one.
messagesoptionalThe custom-messages attributeYour words for the text keys you set, in the viewer language, for example messages['empty.title'].

The palette has at most 32 colors. Each string is at most 256 characters.

An optional key can be missing. Always keep a fallback for it.

Follow your brand

Use the MCP tool custom_chart_scaffold to start a chart project. The example below reads the first palette color for fill and the derived brand color for hover. It converts primaryColor to a CSS color for both fallbacks.

function asCssColor(value) {
  const channels = value.trim();
  return /^\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}$/.test(channels)
    ? `rgb(${channels})`
    : value;
}

globalThis.__dbnCustomChart = {
  render(ctx) {
    const { theme, container } = ctx;
    container.style.fontFamily = theme.fontFamily; // your font, already loaded
    container.style.color = theme.textColor;
    container.style.background = theme.surfaceColor;
    container.style.borderRadius = `${theme.borderRadius}px`;
    const primaryColor = asCssColor(theme.primaryColor);
    const fill = theme.palette[0] ?? primaryColor;
    const hover = theme.derived?.hover ?? primaryColor;
    const emptyText = theme.messages?.['empty.title'] ?? 'No data';
    // Draw with fill, hover and emptyText.
  },
};

What styles what

  • The card: it is a normal card. Theme fields and the parts card, card-header, card-title, card-actions (with card-actions--open) and chart style it, as any other card.
  • Inside the frame: style your markup from ctx.theme and your chart code. The dashboard’s viewer chart settings stay in the built-in chart editor.

Size the chart

With sizing-mode="auto", call dbnChart.resize(height) from your chart when its content needs a height. The height is whole px, from 80 to 1200. The card takes that height, and the embed sends dbn-height-change with the new total. In a dashboard grid the card keeps its grid height and the chart fills it.

Your font files and the CSP

The embed reads each general.fonts file with a credential-free request and hands the bytes to the chart frame. Allow its host in connect-src for the request and in font-src for the usual font load. For a cross-origin font, configure CORS on the font response for the page that contains the embed, including when that page runs in an iframe. If the file fails to load, the chart uses the system sans font.

Test a custom chart

The chart draws inside two frames. In Playwright, go through both with frameLocator:

const chart = page
  .frameLocator('dbn-dashboard iframe') // the frame in the card
  .frameLocator('iframe');              // your chart's frame
await expect(chart.locator('.my-chart-bar')).toHaveCount(5);

.my-chart-bar is a class in your own chart markup. The card itself takes the part selectors: page.locator('[part~="card-title"]').

Next, test the rest of the embed with Playwright.