Takumi

Charts

Embed SVG charts in PDF documents with vector output, chart fonts, and accessible descriptions.

Embed a chart as SVG to keep its lines and shapes as vectors. Generate the SVG before rendering the document. For a complete ECharts example, see Charts.

Add a chart

Pass the SVG string to src and set its size in CSS px:

Add a chart
import {  } from "takumi-pdf";
import {  } from "@takumi-rs/helpers";

const  = await (
  < ={{ : 0, : "avoid" }}>
    <
      ={}
      ="Quarterly revenue: 320, 730, 550, and 910"
      ={{ : 560, : 360 }}
    />
    <>Revenue by quarter</>
  </>,
  { : "a4", : await (["Inter"]) },
);

Replace svg with your generated chart. breakInside: "avoid" keeps the chart and caption on one page when they fit. Pick a size that fits inside the page margins.

Fonts for chart labels

render() loads only the font subsets that cover text in the document tree. It does not read text inside an SVG image. If chart labels use characters the surrounding text does not, register their fonts ahead of time. Fonts from registerFont() stay loaded for every render:

Register chart label fonts
import {  } from "takumi-pdf";
import { ,  } from "@takumi-rs/helpers";

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

await .(.(() => .()));

const  = await .(< ={} ="營收" ={{ : 560, : 360 }} />);

Include generated ticks and punctuation in chartText, not only your data labels. Set the chart library's font family to the registered name, here "Noto Sans TC".

Vector output and text

  • SVG paths, supported gradients, and dashed lines stay vector.
  • Embedded bitmaps, and SVG effects that need rasterizing, become pixels.
  • A PNG chart stays raster in the PDF.

SVG labels are drawn as outlines. They stay visible, but readers cannot select, search, or copy them. Put titles, captions, and data tables that should be searchable in the surrounding JSX.

Describe the data

Give the chart a meaningful alt. Tagged output uses it as the figure description. A label such as "Bar chart" names the image but says nothing about its data.

For a detailed chart, add a table of the underlying values. See Reports and statements for a table that continues across pages.

Last updated on