Takumi

Charts

Embed an SVG chart in a PDF as vector graphics, load fonts for its labels, and describe its data for screen readers.

A chart in a PDF needs vector lines and shapes, fonts that cover its labels, and a description of its data. Embed the chart as SVG to keep its lines and shapes as vectors, and 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";

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. Pagination lists every break property.

Fonts for chart labels

render() reads the text inside an SVG image, so a font that only chart labels use still loads. Pass fonts that cover every label, tick, and legend entry through fonts:

Load chart label fonts
import { ,  } from "takumi-pdf";

const  = await (< ={} ="營收" ={{ : 560, : 360 }} />, {
  : await (["Noto Sans TC"]), 
});

Set the chart library's font family to the same name, here "Noto Sans TC". Fonts & images covers font loading in full.

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. PDF/A & PDF/UA describes how alt maps to the structure tree. For a detailed chart, add a table of the underlying values. See Reports & statements for a table that continues across pages.

Last updated on