Skip to content
OpenChart

Server-side chart rendering with static SVG

renderStaticSVG turns a chart spec into an SVG string in Node, with no browser. The output works with JavaScript off but has no tooltips or animation.

020406080Change in house price index (%)MaineNew HampshireRhode IslandVermontNew JerseyColoradoOregonNorth DakotaLouisianaDistrict of Columbia85.9%83.2%82.1%81.0%79.8%45.9%45.8%43.6%30.7%14.6%House prices rose 85.9% in Maine from 2019 to 2026 and 14.6% in DCChange in the all-transactions house price index, 2019 Q4 to 2026 Q2: the 5 jurisdictions with the largest and smallest increases020406080Change in house price index (%)MaineNew HampshireRhode IslandVermontNew JerseyColoradoOregonNorth DakotaLouisianaDistrict of Columbia85.9%83.2%82.1%81.0%79.8%45.9%45.8%43.6%30.7%14.6%House prices rose 85.9% in Maine from 2019 to 2026 and 14.6% in DCChange in the all-transactions house price index, 2019 Q4 to 2026 Q2: the 5 jurisdictions with the largest and smallest increases
House price growth by state, 2019Q4 to 2026Q2: the five highest and five lowest. Source: Federal Housing Finance Agency, House Price Index (all-transactions, by state).

Made with OpenChart. How to build this bar chart

From late 2019 to mid-2026, house prices rose 85.9% in Maine and 14.6% in DC. A chart of that gap might need to reach an RSS reader, an email or a browser with JavaScript off, and none of those can run chart code, so the chart has to arrive as a finished image.

A static site build or a report generator can run the library, and drawing the chart there saves the reader’s browser the work. renderStaticSVG takes a chart spec and returns an SVG string on the server, with no browser or DOM. Every chart on this page is rendered that way at build time, and a reader without JavaScript sees only that.

020406080Change in house price index (%)MaineNew HampshireRhode IslandVermontNew JerseyColoradoOregonNorth DakotaLouisianaDistrict of Columbia85.9%83.2%82.1%81.0%79.8%45.9%45.8%43.6%30.7%14.6%House prices rose 85.9% in Maine from 2019 to 2026 and 14.6% in DCChange in the all-transactions house price index, 2019 Q4 to 2026 Q2: the 5 jurisdictions with the largest and smallest increases020406080Change in house price index (%)MaineNew HampshireRhode IslandVermontNew JerseyColoradoOregonNorth DakotaLouisianaDistrict of Columbia85.9%83.2%82.1%81.0%79.8%45.9%45.8%43.6%30.7%14.6%House prices rose 85.9% in Maine from 2019 to 2026 and 14.6% in DCChange in the all-transactions house price index, 2019 Q4 to 2026 Q2: the 5 jurisdictions with the largest and smallest increases
House price growth by state, 2019Q4 to 2026Q2: the five highest and five lowest. Source: Federal Housing Finance Agency, House Price Index (all-transactions, by state).

Made with OpenChart. How to build this bar chart

{
  "mark": {
    "type": "bar"
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "data": [
    {
      "state": "Maine",
      "change": 85.9
    },
    {
      "state": "New Hampshire",
      "change": 83.2
    },
    {
      "state": "Rhode Island",
      "change": 82.1
    },
    {
      "state": "Vermont",
      "change": 81
    },
    {
      "state": "New Jersey",
      "change": 79.8
    },
    {
      "state": "Colorado",
      "change": 45.9
    },
    {
      "state": "Oregon",
      "change": 45.8
    },
    {
      "state": "North Dakota",
      "change": 43.6
    },
    {
      "state": "Louisiana",
      "change": 30.7
    },
    {
      "state": "District of Columbia",
      "change": 14.6
    }
  ],
  "encoding": {
    "x": {
      "field": "change",
      "type": "quantitative",
      "axis": {
        "title": "Change in house price index (%)",
        "format": ".0f"
      }
    },
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": "-x"
    }
  },
  "labels": {
    "format": ".1f",
    "suffix": "%"
  },
  "chrome": {
    "title": "House prices rose 85.9% in Maine from 2019 to 2026 and 14.6% in DC",
    "subtitle": "Change in the all-transactions house price index, 2019 Q4 to 2026 Q2: the 5 jurisdictions with the largest and smallest increases"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar"
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "encoding": {
    "x": {
      "field": "change",
      "type": "quantitative",
      "axis": {
        "title": "Change in house price index (%)",
        "format": ".0f"
      }
    },
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": "-x"
    }
  },
  "labels": {
    "format": ".1f",
    "suffix": "%"
  },
  "chrome": {
    "title": "House prices rose 85.9% in Maine from 2019 to 2026 and 14.6% in DC",
    "subtitle": "Change in the all-transactions house price index, 2019 Q4 to 2026 Q2: the 5 jurisdictions with the largest and smallest increases"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar"
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "encoding": {
    "x": {
      "field": "change",
      "type": "quantitative",
      "axis": {
        "title": "Change in house price index (%)",
        "format": ".0f"
      }
    },
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": "-x"
    }
  },
  "labels": {
    "format": ".1f",
    "suffix": "%"
  },
  "chrome": {
    "title": "House prices rose 85.9% in Maine from 2019 to 2026 and 14.6% in DC",
    "subtitle": "Change in the all-transactions house price index, 2019 Q4 to 2026 Q2: the 5 jurisdictions with the largest and smallest increases"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar"
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "encoding": {
    "x": {
      "field": "change",
      "type": "quantitative",
      "axis": {
        "title": "Change in house price index (%)",
        "format": ".0f"
      }
    },
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": "-x"
    }
  },
  "labels": {
    "format": ".1f",
    "suffix": "%"
  },
  "chrome": {
    "title": "House prices rose 85.9% in Maine from 2019 to 2026 and 14.6% in DC",
    "subtitle": "Change in the all-transactions house price index, 2019 Q4 to 2026 Q2: the 5 jurisdictions with the largest and smallest increases"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar"
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "encoding": {
    "x": {
      "field": "change",
      "type": "quantitative",
      "axis": {
        "title": "Change in house price index (%)",
        "format": ".0f"
      }
    },
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": "-x"
    }
  },
  "labels": {
    "format": ".1f",
    "suffix": "%"
  },
  "chrome": {
    "title": "House prices rose 85.9% in Maine from 2019 to 2026 and 14.6% in DC",
    "subtitle": "Change in the all-transactions house price index, 2019 Q4 to 2026 Q2: the 5 jurisdictions with the largest and smallest increases"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

Calling renderStaticSVG

Import it from the static entry of the vanilla package and give it a spec and a size:

import { writeFile } from "node:fs/promises";
import { renderStaticSVG } from "@opendata-ai/openchart-vanilla/static";
import { spec } from "./house-prices.js";

const svg = renderStaticSVG(spec, {
  width: 720,
  height: 420,
  idPrefix: "house-prices-light",
  darkMode: "off",
});
await writeFile("house-prices.svg", svg);

It runs in plain Node, where window and document don’t exist, with no setup. The house price chart comes out at about 15 KB, 2 KB gzipped, and the same spec and options give the same bytes every time.

idPrefix namespaces the ids inside the SVG, so give each figure in a document its own.

Temporal axes can depend on the server’s timezone, so build with TZ=UTC if dates have to match between machines.

The server has no fonts to measure, so it estimates how wide each piece of text is, and a long title can break at a different word than it does in the live chart. Pass measureText, a function that returns the width of a string at a font size and weight, if line breaks must match.

Dark mode

The server has no color scheme to detect, and darkMode: "auto" gives the light version. Render the chart twice, once with "off" and once with "force", using different prefixes, and let your page’s CSS show the one that matches the reader. That is what this site does for every figure.

Limits of static output

A static SVG has no animation, no scripts, no tooltips, no keyboard navigation and no hidden data table. It does keep role="img" and a label built from the chart’s title, which for the chart above reads “Bar chart showing House prices rose 85.9% in Maine from 2019 to 2026 and 14.6% in DC (10 data points)”. Write the finding in a caption and put the values in an HTML table, as the accessibility post describes.

It also supports fewer specs than the live library. Charts, layered charts, bar lists, tile maps and geographic maps work. Tables, Sankey diagrams and network graphs do not.

Adding the live chart

A reader who needs to hover for exact values or step through points from the keyboard needs the live chart. Most pages want both. Ship the static SVG so the page has a chart immediately and without JavaScript, then mount the live chart over it from the same spec.

PNG output

The output is always SVG. Where a link preview or an email needs a raster image, convert it with a rasterizer such as sharp; the machine needs the chart’s font installed, because the SVG names its font without embedding it. To export from a live chart in the browser instead, see export.

The spec

The spec is plain JSON, so the same file can feed the server render and a live createChart call in the browser. The chart below maps net to the horizontal axis, state to the vertical axis, and a direction field to color.

−250,000−200,000−150,000−100,000−50,000050,000100,000150,000People (tax-filer exemptions)CaliforniaNew YorkIllinoisNew JerseyMassachusettsTennesseeSouth CarolinaNorth CarolinaFloridaTexas112,481111,74269,10859,10242,899−28,367−29,871−54,182−157,970−200,547California lost a net 200,547 people to other states in 2022-23,while Texas gained 112,481Net domestic migration in 2022-23, IRS tax-filer exemptions: the 5 largest gains and losses−250,000−200,000−150,000−100,000−50,000050,000100,000150,000People (tax-filer exemptions)CaliforniaNew YorkIllinoisNew JerseyMassachusettsTennesseeSouth CarolinaNorth CarolinaFloridaTexas112,481111,74269,10859,10242,899−28,367−29,871−54,182−157,970−200,547California lost a net 200,547 people to other states in 2022-23,while Texas gained 112,481Net domestic migration in 2022-23, IRS tax-filer exemptions: the 5 largest gains and losses
Live example. Source: IRS Statistics of Income, State-to-State Migration Data.

Made with OpenChart. How to build this bar chart

{
  "mark": {
    "type": "bar"
  },
  "data": [
    {
      "state": "Texas",
      "net": 112481,
      "direction": "Net gain"
    },
    {
      "state": "Florida",
      "net": 111742,
      "direction": "Net gain"
    },
    {
      "state": "North Carolina",
      "net": 69108,
      "direction": "Net gain"
    },
    {
      "state": "South Carolina",
      "net": 59102,
      "direction": "Net gain"
    },
    {
      "state": "Tennessee",
      "net": 42899,
      "direction": "Net gain"
    },
    {
      "state": "Massachusetts",
      "net": -28367,
      "direction": "Net loss"
    },
    {
      "state": "New Jersey",
      "net": -29871,
      "direction": "Net loss"
    },
    {
      "state": "Illinois",
      "net": -54182,
      "direction": "Net loss"
    },
    {
      "state": "New York",
      "net": -157970,
      "direction": "Net loss"
    },
    {
      "state": "California",
      "net": -200547,
      "direction": "Net loss"
    }
  ],
  "encoding": {
    "x": {
      "field": "net",
      "type": "quantitative",
      "axis": {
        "title": "People (tax-filer exemptions)",
        "format": ",.0f"
      }
    },
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": {
        "field": "net",
        "order": "ascending"
      }
    },
    "color": {
      "field": "direction",
      "type": "nominal",
      "scale": {
        "domain": [
          "Net gain",
          "Net loss"
        ]
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        },
        {
          "light": "#b54d00",
          "dark": "#f07a2b"
        }
      ]
    }
  },
  "legend": {
    "show": false
  },
  "labels": {
    "format": ",.0f"
  },
  "overrides": {
    "compact": {
      "chrome": {
        "title": "California lost 200,547 people to other states, Texas gained 112,481"
      }
    },
    "medium": {
      "chrome": {
        "title": "California lost 200,547 people to other states, Texas gained 112,481"
      }
    }
  },
  "chrome": {
    "title": "California lost a net 200,547 people to other states in 2022-23, while Texas gained 112,481",
    "subtitle": "Net domestic migration in 2022-23, IRS tax-filer exemptions: the 5 largest gains and losses"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar"
  },
  "encoding": {
    "x": {
      "field": "net",
      "type": "quantitative",
      "axis": {
        "title": "People (tax-filer exemptions)",
        "format": ",.0f"
      }
    },
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": {
        "field": "net",
        "order": "ascending"
      }
    },
    "color": {
      "field": "direction",
      "type": "nominal",
      "scale": {
        "domain": [
          "Net gain",
          "Net loss"
        ]
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        },
        {
          "light": "#b54d00",
          "dark": "#f07a2b"
        }
      ]
    }
  },
  "legend": {
    "show": false
  },
  "labels": {
    "format": ",.0f"
  },
  "overrides": {
    "compact": {
      "chrome": {
        "title": "California lost 200,547 people to other states, Texas gained 112,481"
      }
    },
    "medium": {
      "chrome": {
        "title": "California lost 200,547 people to other states, Texas gained 112,481"
      }
    }
  },
  "chrome": {
    "title": "California lost a net 200,547 people to other states in 2022-23, while Texas gained 112,481",
    "subtitle": "Net domestic migration in 2022-23, IRS tax-filer exemptions: the 5 largest gains and losses"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar"
  },
  "encoding": {
    "x": {
      "field": "net",
      "type": "quantitative",
      "axis": {
        "title": "People (tax-filer exemptions)",
        "format": ",.0f"
      }
    },
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": {
        "field": "net",
        "order": "ascending"
      }
    },
    "color": {
      "field": "direction",
      "type": "nominal",
      "scale": {
        "domain": [
          "Net gain",
          "Net loss"
        ]
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        },
        {
          "light": "#b54d00",
          "dark": "#f07a2b"
        }
      ]
    }
  },
  "legend": {
    "show": false
  },
  "labels": {
    "format": ",.0f"
  },
  "overrides": {
    "compact": {
      "chrome": {
        "title": "California lost 200,547 people to other states, Texas gained 112,481"
      }
    },
    "medium": {
      "chrome": {
        "title": "California lost 200,547 people to other states, Texas gained 112,481"
      }
    }
  },
  "chrome": {
    "title": "California lost a net 200,547 people to other states in 2022-23, while Texas gained 112,481",
    "subtitle": "Net domestic migration in 2022-23, IRS tax-filer exemptions: the 5 largest gains and losses"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar"
  },
  "encoding": {
    "x": {
      "field": "net",
      "type": "quantitative",
      "axis": {
        "title": "People (tax-filer exemptions)",
        "format": ",.0f"
      }
    },
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": {
        "field": "net",
        "order": "ascending"
      }
    },
    "color": {
      "field": "direction",
      "type": "nominal",
      "scale": {
        "domain": [
          "Net gain",
          "Net loss"
        ]
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        },
        {
          "light": "#b54d00",
          "dark": "#f07a2b"
        }
      ]
    }
  },
  "legend": {
    "show": false
  },
  "labels": {
    "format": ",.0f"
  },
  "overrides": {
    "compact": {
      "chrome": {
        "title": "California lost 200,547 people to other states, Texas gained 112,481"
      }
    },
    "medium": {
      "chrome": {
        "title": "California lost 200,547 people to other states, Texas gained 112,481"
      }
    }
  },
  "chrome": {
    "title": "California lost a net 200,547 people to other states in 2022-23, while Texas gained 112,481",
    "subtitle": "Net domestic migration in 2022-23, IRS tax-filer exemptions: the 5 largest gains and losses"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar"
  },
  "encoding": {
    "x": {
      "field": "net",
      "type": "quantitative",
      "axis": {
        "title": "People (tax-filer exemptions)",
        "format": ",.0f"
      }
    },
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": {
        "field": "net",
        "order": "ascending"
      }
    },
    "color": {
      "field": "direction",
      "type": "nominal",
      "scale": {
        "domain": [
          "Net gain",
          "Net loss"
        ]
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        },
        {
          "light": "#b54d00",
          "dark": "#f07a2b"
        }
      ]
    }
  },
  "legend": {
    "show": false
  },
  "labels": {
    "format": ",.0f"
  },
  "overrides": {
    "compact": {
      "chrome": {
        "title": "California lost 200,547 people to other states, Texas gained 112,481"
      }
    },
    "medium": {
      "chrome": {
        "title": "California lost 200,547 people to other states, Texas gained 112,481"
      }
    }
  },
  "chrome": {
    "title": "California lost a net 200,547 people to other states in 2022-23, while Texas gained 112,481",
    "subtitle": "Net domestic migration in 2022-23, IRS tax-filer exemptions: the 5 largest gains and losses"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

The direction field is added when the rows are prepared, as “Net gain” or “Net loss”. The color scale’s domain fixes that order, and theme.colors.categorical gives each a light and a dark color, so losses stay orange on every render and their outside labels stay readable on both pages. Sorting net in ascending order puts the largest loss first, and labels prints each value on its bar. The anatomy of a spec explains the other blocks.

The static rendering docs give the default for every option, including precision, which sets how many decimals the SVG coordinates keep.