Charts
Render ECharts and other SVG charts as PNG images or PDF documents with Takumi.
Generate the chart as an SVG string, then pass it to an <img>. Takumi draws the SVG, so the chart library never runs in a browser. The library must be able to output SVG in your runtime.
Render an ECharts chart
Install ECharts alongside takumi-js:
npm i echarts takumi-js reactThis example draws a bar chart and saves it as a PNG. ECharts uses its SVG server renderer, so it needs no DOM or canvas.
import * as from "echarts";
import { } from "takumi-js";
import { } from "takumi-js/helpers";
import { } from "takumi-js/node";
import { } from "node:fs/promises";
const = .(null, null, {
: "svg",
: true,
: 560,
: 360,
});
.({
: false,
: { : "Inter" },
: { : "category", : ["Q1", "Q2", "Q3", "Q4"] },
: { : "value" },
: [{ : "bar", : [320, 730, 550, 910] }],
});
const = .();
.();
const = new ();
const = await (["Inter"]);
await .(.(() => .()));
const = await (
<
={}
="Quarterly revenue: 320, 730, 550, and 910"
={{ : 560, : 360 }}
/>,
{ : 560, : 360, },
);
await ("revenue.png", );Keep animation: false for static output. With animation on, the SVG can capture an empty or half-drawn chart. Dispose of the ECharts instance once you have the SVG.
The SVG string goes straight into src, so it needs no images entry. In an HTML string, put the SVG markup inside the document.
For PDF output, see Charts in PDF.
Fonts for chart labels
Register fonts that cover every label, tick, and legend entry. Set the chart's font family to the same name.
The fonts render option keeps only the subsets that the document's text uses. Text inside an SVG image does not count, so chart labels can lose their font. Fonts registered with registerFont, as above, stay on the renderer for every render.
For a large family, keep only the subsets the chart text needs. Include generated numeric ticks and punctuation as well as your data labels:
import { , } from "takumi-js/helpers";
import { } from "takumi-js/node";
const = new ();
const = "一月二月三月四月營收0123456789.,−-%";
const = ({
: await (["Noto Sans TC"]),
: ,
});
await .(.(() => .()));Set ECharts textStyle.fontFamily to "Noto Sans TC" and pass renderer to render(). For offline output, load bundled font bytes instead. See Fonts in CI and offline renders.
Use another chart library
Takumi takes the SVG output, not the library's interactive component. Generate the final chart before you call render().
| Library | SVG generation |
|---|---|
| ECharts | ssr: true, then renderToSVGString() |
| Vega or Vega-Lite | Create a Vega view and call toSVG() |
| d3-shape and d3-scale | Build SVG elements from computed paths and coordinates |
| React SVG components | Render supported components with renderToStaticMarkup() |
A library that needs a DOM or canvas needs its own server rendering setup. If it outputs a PNG, pass that image to Takumi instead. A raster chart stays raster in a PDF.
Last updated on