Guide

Exporting a chart

Any chart in the kit can be downloaded as PNG, SVG or PDF. You don't change the chart: you put the svChartExport directive on the element around it and call one of its three methods from your own buttons. The directive is in the Pro package @styloviz-pro/export.

Add the directive

The directive finds the first <svg> inside the element it is on, which is the plot in every chart here. An icon in the title or a swatch in the legend is passed over. So it goes on the wrapper, and the same code works for a line, bar, pie or any other chart.

// npm i @styloviz-pro/export
import { SvChartExportDirective, type SvChartExportFailure } from '@styloviz-pro/export';
import { SvLineChartComponent } from '@styloviz/charts';

@Component({
  imports: [SvLineChartComponent, SvChartExportDirective],
  template: `
    <div svChartExport #chart="svChartExport" fileName="revenue" exportTitle="Revenue, 2026"
         (exportFailed)="failed($event)">
      <sv-line-chart [categories]="months" [series]="series" />
    </div>
    <button type="button" (click)="chart.png()">Download PNG</button>
    <button type="button" (click)="chart.svgFile()">Download SVG</button>
    <button type="button" (click)="chart.pdf()">Download PDF</button>
  `,
})

There is a live version in the Bar Chart gallery.

The three formats

MethodFileNeeds
png()A bitmap at exportScale times the chart's size (2 by default; 3 for print), with the title above the chart and the legend below it.Nothing. The browser draws the chart's own vector onto a canvas.
svgFile()A standalone vector file with the title and legend, and the real text in it, so it can be searched and edited.Nothing.
pdf()The chart and its legend on an A4 or Letter page, with the title as the page heading.jspdf, loaded only when a PDF is asked for.

The chart's computed colours and fonts are copied into the file. Without that, every colour set by a Tailwind class would be lost, because the file cannot see the page's stylesheet. The background is white by default, since a transparent chart looks broken in Slack, Word or on paper. Pass [exportBackground]="null" to keep it transparent.

Charts draw in when they first scroll into view. The file always shows the finished chart: one that is still drawing in, or that nobody has scrolled to yet, exports the same as one that has settled, and the chart on the page carries on from where it was.

The title and the legend

On the page, a chart's title and legend sit beside its <svg>, not inside it. The export draws them into the file, so a chart with several series still says which colour is which once it is pasted into a slide or an email. The legend shows the entries as the page does: the same labels, the same colours, wrapped into rows under the chart.

  • The title is exportTitle when you set it, and the chart's own title when you don't.
  • Text keeps the colour the page gives it, as long as it reads on the export background. A dark-mode page exported on the default white background gets dark text instead, so the title never disappears.
  • [exportDecor]="false" exports the plot alone, with no title or legend.

For a chart of your own, mark the same places and the export picks them up: data-sv-chart-title on the heading, data-sv-legend-item on each legend entry, and data-sv-legend-swatch on the element inside it that paints the colour.

Headings in other languages

In the PNG and the SVG, the browser draws the chart, its title and its legend, so they show in any language, exactly as on screen. The chart and legend in the PDF are the same picture. Only the PDF's heading is PDF text, and the PDF's built-in font draws Western European text only. For a heading in another language, set a TrueType font once, and the heading is real text that can be searched and copied. Give one font per script your data holds; the chart heading uses the first:

// app.config.ts
import { provideSvPdfFont } from '@styloviz-pro/export';

const font = (file: string) => () => fetch(`/fonts/${file}`).then((r) => r.arrayBuffer());

providers: [
  provideSvPdfFont([
    { family: 'Noto Sans', regular: font('NotoSans-Regular.ttf'), bold: font('NotoSans-Bold.ttf') },
    { family: 'Noto Sans Bengali', regular: font('NotoSansBengali-Regular.ttf') },
  ]),
]

The font loads on the first PDF export, not when the app starts, and the Pro data table, pivot table and Gantt chart use it for their PDFs too. It has to be a .ttf file; a .woff2 web font or a CFF-based .otf is refused with an error that says so. Without a font, or for a script the chart PDF cannot lay out as text (Arabic, Hebrew, Bengali, Hindi, Thai and the like), the heading is drawn as a picture by the browser instead, so it still reads correctly. The table PDFs (data table, pivot table) lay out Arabic, Hebrew, the Indic scripts, Thai, Lao, Korean, Chinese and Japanese as text with the same fonts, loading the kit's text shaper when they are exported; a script it does not lay out, such as Khmer or Myanmar, stops a table export with an error that names it.

When an export fails

The PDF format loads jspdf at click time, so an app that has not installed it can only find out at click time. The directive reports that, and any other failure, on (exportFailed) with the format and the error. Show the message next to the button; otherwise the button just looks dead.

Without the directive

The same functions are exported on their own, for when you already have the <svg> element, or want the PNG as a Blob to upload instead of download:

import { svChartToPngBlob, svChartToSvgString } from '@styloviz-pro/export';

const blob = await svChartToPngBlob(svg, { scale: 3 });
const markup = svChartToSvgString(svg, '#ffffff');