Angular Chart - Client-Side Exporting and Printing

Although DevExtreme data visualization UI components can be displayed on any device, a user may need a UI component printed or in the form of a document. For these cases, the UI components provide client-side exporting and printing. This guide shows how to configure these features for the user, and how to export and print a UI component using the API.

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.

HTML
TypeScript
  • <dx-chart ... >
  • <dxo-export
  • [enabled]="true"
  • [printingEnabled]="false">
  • </dxo-export>
  • </dx-chart>
  • import { DxChartModule } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • // ...
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxChartModule
  • ],
  • // ...
  • })

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.

HTML
TypeScript
  • <dx-chart ... >
  • <dxo-export
  • [enabled]="true"
  • [formats]="['PNG', 'JPEG']"
  • fileName="exported_chart">
  • </dxo-export>
  • </dx-chart>
  • import { DxChartModule } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • // ...
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxChartModule
  • ],
  • // ...
  • })

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.

TypeScript
  • import { ..., ViewChild } from "@angular/core";
  • import { DxChartModule, DxChartComponent } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • @ViewChild(DxChartComponent, { static: false }) chart: DxChartComponent;
  • // Prior to Angular 8
  • // @ViewChild(DxChartComponent) chart: DxChartComponent;
  • exportChart () {
  • this.chart.instance.exportTo('Exported Chart', 'PDF');
  • };
  • printChart () {
  • this.chart.instance.print();
  • };
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxChartModule
  • ],
  • // ...
  • })

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.

TypeScript
HTML
  • import { ..., ViewChild } from "@angular/core";
  • import { DxChartModule, DxChartComponent } from "devextreme-angular";
  • import { getMarkup, exportFromMarkup } from "devextreme/viz/export";
  • // ...
  • export class AppComponent {
  • @ViewChild('chartContainer1', { static: false }) chart1: DxChartComponent;
  • @ViewChild('chartContainer2', { static: false }) chart2: DxChartComponent;
  • // Prior to Angular 8
  • // @ViewChild('chartContainer1') chart1: DxChartComponent;
  • // @ViewChild('chartContainer2') chart2: DxChartComponent;
  • exportMultipleCharts () {
  • const chartMarkup = getMarkup([this.chart1.instance, this.chart2.instance]);
  • exportFromMarkup(chartMarkup, {
  • height: 768,
  • width: 1024,
  • fileName: "Exported Charts",
  • format: "PDF"
  • });
  • };
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxChartModule
  • ],
  • // ...
  • })
  • <dx-chart #chartContainer1 ... ></dx-chart>
  • <dx-chart #chartContainer2 ... ></dx-chart>

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.

HTML
TypeScript
  • <dx-chart ...
  • (onExporting)="onExporting($event)"
  • (onExported)="onExported($event)"
  • (onFileSaving)="onFileSaving($event)">
  • </dx-chart>
  • import { DxChartModule } from "devextreme-angular";
  • // ...
  • export class AppComponent {
  • onExporting (e) {
  • // Handler of the "exporting" event
  • };
  • onExported (e) {
  • // Handler of the "exported" event
  • };
  • onFileSaving (e) {
  • // Handler of the "fileSaving" event
  • }
  • }
  • @NgModule({
  • imports: [
  • // ...
  • DxChartModule
  • ],
  • // ...
  • })
See Also