Charts - Export
Let users export a chart as an image or in PDF format.
Charts can be exported as images, or as PDFs using the browser's native print dialog. The exporting feature is available for the following charts:
LineChartProBarChartProScatterChartProPieChartProHeatmapFunnelChartRadarChartProSankeyChart
Implementing exporting
Default toolbar
To enable exporting from the chart's toolbar, pass the showToolbar prop to the chart component.
The toolbar then renders a button that opens a menu with the export options.
Inflation rate in France, Germany and the UK, 1960-2024
- Germany
- United Kingdom
- France
Custom toolbar
See Toolbar—Composition for more information on how to create a custom toolbar.
Image exporting
You must install rasterizehtml to enable image exporting:
npm install rasterizehtmlExport options
Export behavior can be modified with print and image export options.
These options can be passed to the built-in toolbar using slotProps.toolbar, and are then automatically displayed.
You can customize their respective behaviors by passing an options object to slotProps.toolbar, or to the export trigger itself if you're using a custom toolbar:
// Default toolbar:
<BarChartPro slotProps={{ toolbar: { printOptions, imageExportOptions } }} />
// Custom trigger:
<ChartsToolbarImageExportTrigger options={imageExportOptions} />
<ChartsToolbarPrintExportTrigger options={printExportOptions} />
Export formats
To disable the print export, set the disableToolbarButton property to true.
You can customize image export formats by providing an array of objects to the imageExportOptions property.
These objects must contain the type property which specifies the image format.
In the example below, you can toggle which export formats are available to the user. The name of the exported file has been customized to resemble the chart's title.
Population vs GDP Per Capita (USD), 2019
- Europe
- Asia
- North America
- South America
- Africa
- Oceania
const filename = 'Population_vs_GDP_Per_Capita_USD_2019';
<ScatterChartPro
// ...
slotProps={{
toolbar: {
printOptions: { fileName },
imageExportOptions: [
{ type: "image/png" , filename }
]
},
}}
/>Add custom styles before exporting
To add custom styles or modify the chart's appearance before exporting, use the onBeforeExport callback.
When exporting, the chart is first rendered into an iframe and then exported as an image or PDF.
The onBeforeExport callback gives you access to the iframe before the export process starts.
For example, you can add the title and caption to the exported chart as shown below:
Inflation rate in France, Germany and the UK, 1960-2024
- Germany
- United Kingdom
- France
Copy styles
The styles of the page the chart belongs to are copied to the export iframe by default.
You can disable this behavior by setting the copyStyles property to false in the export options.
<BarChartPro slotProps={{ toolbar: { printOptions: { copyStyles: false } } }} />
Exporting composed charts
MUI X Charts may be self-contained or composed of various subcomponents. See Composition for more details on implementing the latter type.
ChartsWrapper is considered the root element of a chart for exporting purposes, and all descendants are included in the export.
To use a custom wrapper instead, you must set the reference to the root element with the useChartRootRef() hook as shown below:
Composite Chart
- Bar
- Line
Content Security Policy (CSP)
If your application uses a Content Security Policy (CSP), you might need to adjust it for exporting to work correctly. See the dedicated document on CSP for more details.
apiRef
Print or export as PDF
The apiRef prop exposes the exportAsPrint() method that can be used to open the browser's print dialog.
The print dialog lets you print the chart or save it as a PDF, as well as configure other settings.
- Series A
- Series B
Export as image
The apiRef prop also exposes the exportAsImage() method to export the chart as an image.
The function accepts an options object with the type property which specifies the image format.
The available formats are:
image/pngandimage/jpegwhich can both be used across all supported platformsimage/webpwhich is only supported in some browsers
If the format is not supported by the browser, exportAsImage() falls back to image/png.
For lossy formats such as image/jpeg and image/webp, the options object accepts the quality property which sets a numerical value between 0 and 1.
The default is 0.9.
- Series A
- Series B
Only applicable to lossy formats.
API
See the documentation below for a complete reference to all of the props and classes available to the components mentioned here.