Charts - Export
Export charts as a PDF from the print dialog, or as an image.
Export is available for the following charts: <LineChartPro />
, <BarChartPro />
, <ScatterChartPro />
, PieChartPro />
, <Heatmap />
, <FunnelChart />
and <RadarChartPro />
.
Enabling export
Charts can be exported using the browser's native print dialog or as an image.
Default toolbar
To enable exporting from the chart's toolbar, you need to enable it by passing the showToolbar
prop to the chart component.
The toolbar then renders a button that opens a menu with the export options.
- Germany
- United Kingdom
- France
Custom toolbar
See the Toolbar composition section for more information on how to create a custom toolbar.
Image export pre-requisites
For image export to work, you need to add the rasterizehtml
npm dependency in your project.
npm install rasterizehtml
Export options
Export behavior can be modified with print and image export options.
Options can be passed to the built-in Toolbar with slotProps.toolbar
.
In that case, the imageExportOptions
must be an array of objects, each representing an image type to export.
Where relevant, the options are automatically shown in the toolbar. You can customize their respective behavior by passing an options object either to slotsProps.toolbar
or to the Export trigger itself if you have a custom toolbar:
// Default toolbar:
<BarChartPro slotProps={{ toolbar: { printOptions, imageExportOptions } }} />
// Custom trigger:
<ChartsToolbarImageExportTrigger options={imageExportOptions} />
<ChartsToolbarPrintExportTrigger options={printExportOptions} />
In the example below, you can toggle which export formats are available to the user.
Additionally, 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 }
]
},
}}
/>
Composition
As detailed in the Composition section, charts can alternatively be composed of more specific components to create custom visualizations.
When exporting a chart, the ChartsWrapper
element is considered the root element of the chart, and every descendant is included in the export.
As such, you need to ensure that the ChartsWrapper
element is the root element of the chart you want to export.
If you want to use a custom wrapper element, you need to use the useChartRootRef()
hook to set the reference to the chart's root element so that exporting works properly, as exemplified below.
- Bar
- Line
apiRef
Print/Export as PDF
The apiRef
prop exposes a exportAsPrint()
method that can be used to open the browser's print dialog.
The print dialog allows you to print the chart or save it as a PDF, as well as configuring other settings.
- Series A
- Series B
Export as image
The apiRef
prop also exposes a exportAsImage()
method to export the chart as an image.
Usage
The function accepts an options object with the type
property, which specifies the image format. The available formats are:
image/png
andimage/jpeg
, which are supported across all supported platforms;image/webp
which is only supported in some browsers.
If the format is not supported by the browser, exportAsImage()
falls back to image/png
.
Additionally, for lossy formats such as image/jpeg
and image/webp
, the options object also accepts the quality
property, which is a number between 0 and 1.
The default value 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.