React Chart - Client-Side Exporting and Printing

DevExtreme data visualization UI components can be displayed on any device and support client-side printing and exporting. This guide shows how to configure these features and use the API to export and print a component.

NOTE

Printouts and exported files in non-SVG formats display only the following SVG elements:

  • Shapes, text, and images: circle, image, path, rect, text, tspan
  • Containers: g, svg
  • Definitions: clipPath, defs, linearGradient, pattern, radialGradient

Note the following limitations for supported elements:

  • path elements support only the following commands: M, L, C, A, and Z.
  • rect elements must declare x and y attributes.
  • circle elements must declare cx and cy attributes.
  • transform attributes support only the following functions: translate, rotate, and scale.

If you use unsupported SVG markup in patterns (registerPattern()) or Chart templates (for example, legend.markerTemplate), configure the export.svgToCanvas function to export this markup.

Exported SVG files contain all elements without additional configuration.

User Interaction

To export or print a UI component, a user clicks the "Exporting/Printing" button and selects a command from the drop-down menu. The Print command opens the browser's Print window that lets the user select preferred printing settings and send the print job to the printer. The other commands save a file of the selected format in the user's local storage.

DevExtreme HTML5 DataVisualization Charts Export Print

You can enable both exporting and printing by setting the export.enabled property to true. If you need only exporting to be available to the user, disable printing by assigning false to the export.printingEnabled property.

App.js
  • import React from 'react';
  • import Chart, {
  • Export
  • } from 'devextreme-react/chart';
  •  
  • export default function App() {
  • return (
  • <Chart ... >
  • <Export
  • enabled={true}
  • printingEnabled={false}
  • />
  • </Chart>
  • );
  • }

If you want to restrict the set of formats available for exporting, change the export.formats array. You can also specify the default name for the exported file using the fileName property.

App.js
  • import React from 'react';
  • import Chart, {
  • Export
  • } from 'devextreme-react/chart';
  •  
  • const exportFormats = ['PNG', 'JPEG'];
  •  
  • export default function App() {
  • return (
  • <Chart ... >
  • <Export
  • enabled={true}
  • formats={exportFormats}
  • fileName="exported_chart"
  • />
  • </Chart>
  • );
  • }

API

To export a UI component using the API, call the exportTo(fileName, format) method passing the needed file name and format ("PNG", "PDF", "JPEG", "SVG" or "GIF") as the arguments. To print a UI component, call the print() method. This command opens the browser's Print window.

App.js
  • import React from 'react';
  • import Chart, { Export } from 'devextreme-react/chart';
  •  
  • class App extends React.Component {
  • constructor(props) {
  • super(props);
  • this.chartRef = React.createRef();
  • }
  •  
  • render() {
  • return (
  • <Chart ref={this.chartRef} ... >
  • <Export enabled={true}>
  • </Chart>
  • );
  • }
  •  
  • get chart() {
  • return this.chartRef.current.instance();
  • }
  •  
  • exportChart() {
  • this.chart.exportTo('Exported Chart', 'PDF');
  • }
  •  
  • printChart() {
  • this.chart.print();
  • }
  • }
  •  
  • export default App;

You can also export several UI components at once using their SVG markup. Gather the markup from all required UI components by calling the DevExpress.viz.getMarkup(widgetInstances) method, and then pass the markup to the DevExpress.viz.exportFromMarkup(markup, options) method.

App.js
  • import React, { useRef, useCallback } from 'react';
  • import DxChart from 'devextreme-react/chart';
  • import { getMarkup, exportFromMarkup } from 'devextreme/viz/export';
  •  
  • export default function App() {
  • const chart1 = useRef(null);
  • const chart2 = useRef(null);
  •  
  • const exportMultipleCharts = useCallback(() => {
  • const chartMarkup = getMarkup([chart1.current.instance(), chart2.current.instance()]);
  • exportFromMarkup(chartMarkup, {
  • height: 768,
  • width: 1024,
  • fileName: "Exported Charts",
  • format: "PDF"
  • });
  • }, []);
  •  
  • return (
  • <React.Fragment>
  • <Chart ref={chart1}>
  • {/* ... */}
  • </Chart>
  • <Chart ref={chart2}>
  • {/* ... */}
  • </Chart>
  • </React.Fragment>
  • );
  • }

Events

DevExtreme data visualization UI components raise the following exporting-related events.

  • exporting
    Allows you to request exporting details or prevent exporting.

  • exported
    Allows you to notify an end user when exporting is completed.

  • fileSaving
    Allows you to access exported data in the BLOB format and/or prevent it from being saved in a file on the user's local storage.

You can handle these events with functions. If the handling functions are not going to be changed at runtime, assign them to the onExporting, onExported and onFileSaving properties when you configure the UI component.

App.js
  • import React from 'react';
  • import Chart, {
  • Export
  • } from 'devextreme-react/chart';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <Chart
  • onExporting={this.onExporting}
  • onExported={this.onExported}
  • onFileSaving={this.onFileSaving}
  • >
  • <Export enabled={true}/>
  • </Chart>
  • );
  • }
  •  
  • onExporting(e) {}
  • onExported(e) {}
  • onFileSaving(e) {}
  • }
  •  
  • export default App;
See Also