Client-Side Export and Printing

Although DevExtreme data visualization widgets can be displayed on any device, a user may need a widget printed or in the form of a document. For these cases, the widgets provide client-side export and printing. This guide shows how to configure these features for the user, and how to export and print the PieChart widget using the API. It also explains how to set up a server-side proxy, which is necessary if you plan to support export and printing in IE9 and Safari on MacOS.

Watch Video

User Interaction

To export or print the PieChart, 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 JavaScript PieChart Export Menu

To enable both export and printing, assign true to the export.enabled option. If you need only export to be available to the user, disable printing by assigning false to the export.printingEnabled option.

jQuery
JavaScript
$(function() {
    $("#pieChartContainer").dxPieChart({
        // ...
        export: {
            enabled: true,
            printingEnabled: false
        }
    });
});
Angular
HTML
<dx-pie-chart ... >
    <dxo-export
        [enabled]="true"
        [printingEnabled]="false">
    </dxo-export>
</dx-pie-chart>

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

jQuery
JavaScript
$(function() {
    $("#pieChartContainer").dxPieChart({
        // ...
        export: {
            enabled: true,
            formats: ["PNG", "JPEG"],
            fileName: "exported_chart"
        }
    });
});
Angular
HTML
<dx-pie-chart ... >
    <dxo-export
        [enabled]="true"
        [formats]="['PNG', 'JPEG']"
        fileName="exported_chart">
    </dxo-export>
</dx-pie-chart>

To support export and printing in IE9 and Safari on MacOS, you need to set up a proxy on your server. For details, see the Set Up a Server-Side Proxy topic.

API

To export the PieChart 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 the PieChart, call the print() method. This command opens the browser's Print window.

jQuery
JavaScript
var pieChart = $("#pieChartContainer").dxPieChart("instance");
pieChart.exportTo('Exported Chart', 'PDF');
pieChart.print();
Angular
TypeScript
import { ..., ViewChild } from '@angular/core';
import { DxPieChartModule, DxPieChartComponent } from 'devextreme-angular';
// ...
export class AppComponent {
    @ViewChild(DxPieChartComponent) pieChart: DxPieChartComponent;
    exportChart () {
        this.pieChart.instance.exportTo('Exported Chart', 'PDF');
    };
    printChart () {
        this.pieChart.instance.print();
    };
}

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

jQuery
JavaScript
var pieChart1 = $("#pieChartContainer1").dxPieChart("instance");
var pieChart2 = $("#pieChartContainer2").dxPieChart("instance");
var chartMarkup = DevExpress.viz.getMarkup([pieChart1, pieChart2]);

DevExpress.viz.exportFromMarkup(chartMarkup, {
    height: 768,
    width: 1024,
    fileName: "Exported Charts",
    format: "PDF"
});
Angular
TypeScript
HTML
import { ..., ViewChild } from '@angular/core';
import { DxPieChartModule, DxPieChartComponent } from 'devextreme-angular';
import exportMethods from 'devextreme/viz/export';
// ...
export class AppComponent {
    @ViewChild('pieChartContainer1') pieChart1: DxPieChartComponent;
    @ViewChild('pieChartContainer2') pieChart2: DxPieChartComponent;
    exportSeveralCharts () {
        let chartMarkup = exportMethods.getMarkup([this.pieChart1.instance, this.pieChart2.instance]);
        exportMethods.exportFromMarkup(chartMarkup, {
            height: 768,
            width: 1024,
            fileName: "Exported Charts",
            format: "PDF"
        });
    };
}
<dx-pie-chart id="pieChartContainer1" ... ></dx-pie-chart>
<dx-pie-chart id="pieChartContainer2" ... ></dx-pie-chart>

Events

DevExtreme data visualization widgets raise the following export-related events.

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

  • exported
    Allows you to notify an end user when export 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 options when you configure the widget.

jQuery
JavaScript
$(function() {
    $("#pieChartContainer").dxPieChart({
        // ...
        onExporting: function (e) {
            // Handler of the "exporting" event
        },
        onExported: function (e) {
            // Handler of the "exported" event
        },
        onFileSaving: function (e) {
            // Handler of the "fileSaving" event
        }
    });
});
Angular
HTML
TypeScript
<dx-pie-chart ...
    (onExporting)="onExporting($event)"
    (onExported)="onExported($event)"
    (onFileSaving)="onFileSaving($event)">
</dx-pie-chart>
export class AppComponent {
    onExporting (e) {
        // Handler of the "exporting" event
    };
    onExported (e) {
        // Handler of the "exported" event
    };
    onFileSaving (e) {
        // Handler of the "fileSaving" event
    }
}

Otherwise, or if you need several handlers for a single event, subscribe to the export-related events using the on(eventName, eventHandler) method. This approach is more typical of jQuery.

JavaScript
var exportedHandler1 = function (e) {
    // First handler of the "exported" event
};

var exportedHandler2 = function (e) {
    // Second handler of the "exported" event
};

$("#pieChartContainer").dxPieChart("instance")
    .on("exported", exportedHandler1)
    .on("exported", exportedHandler2);
See Also

Set Up a Server-Side Proxy

See the instructions here.