Takumi

Charts

Generate SVG charts and render them as images or PDF.

Generate a chart as an SVG string, then pass it to an <img> in your document. Takumi renders the SVG without running the chart library in a browser. The library must support generating SVG in your runtime.

Render an ECharts chart

Install ECharts alongside takumi-js:

npm i echarts takumi-js @takumi-rs/helpers react

This example generates 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-rs/helpers";
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  = (await (["Inter"])).(() => ({ ..., : [] }));
const  = await (
  <
    ={}
    ="Quarterly revenue: 320, 730, 550, and 910"
    ={{ : 560, : 360 }}
  />,
  { : 560, : 360,  },
);

await ("revenue.png", );

Keep animation: false for static output. Animated SVG can capture an empty or partially drawn chart. Dispose of the ECharts instance after extracting the SVG.

The SVG string goes directly in src. It does not need an images entry. To include it in an HTML string, insert 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.

googleFonts() returns font subsets with Unicode ranges. Takumi filters those subsets against text in the document tree, which excludes text inside an SVG image. Clearing ranges, as above, keeps every returned subset.

For a large font family, select the subsets against the chart text first. Include generated numeric ticks and punctuation as well as your data labels:

import { ,  } from "@takumi-rs/helpers";

const  = "一月二月三月四月營收0123456789.,−-%";
const  = ({
  : await (["Noto Sans TC"]),
  : ,
}).(() => ({ ..., : [] }));

Pass fonts to render() and set ECharts textStyle.fontFamily to "Noto Sans TC". For offline output, load bundled font bytes instead. See Fonts in CI and offline renders.

Use another chart library

Takumi accepts the SVG output, not the library's interactive component. Generate the final chart before calling render().

ApproachSVG generation
EChartsssr: true, then renderToSVGString()
Vega or Vega-LiteCreate a Vega view and call toSVG()
d3-shape and d3-scaleBuild SVG elements from computed paths and coordinates
React SVG componentsRender supported components with renderToStaticMarkup()

A library that requires a DOM or canvas needs its own server rendering setup. If it produces a PNG, pass that image to Takumi instead. A raster chart remains raster in PDF.

Last updated on

On this page