React Sankey - Client-Side Exporting and Printing

User Interaction

To export or print the UI component, a user clicks Exporting/Printing and selects a command from the drop-down menu.

The Print command opens the browser's Print window. This window allows a user to specify the print settings and send the print job to the printer.

The other commands save a file in the selected format on the user's device.

Sankey Export Menu

Set export.enabled to true to enable exporting and printing. To allow a user to only export, assign false to export.printingEnabled.

App.js
  • import React from 'react';
  • import Sankey, { Export } from 'devextreme-react/sankey';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <Sankey ... >
  • <Export
  • enabled={true}
  • printingEnabled={false}
  • />
  • </Sankey>
  • )
  • }
  • }
  •  
  • export default App;

Change the export.formats array to limit the set of export formats. You can also set the fileName property to specify the export file's name.

App.js
  • import React from 'react';
  • import Sankey, { Export } from 'devextreme-react/sankey';
  •  
  • const exportFormats = ['PNG', 'JPEG'];
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <Sankey ... >
  • <Export
  • enabled={true}
  • formats={exportFormats}
  • fileName="exported_sankey"
  • />
  • </Sankey>
  • )
  • }
  • }
  •  
  • export default App;

API

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

App.js
  • import React from 'react';
  • import Sankey from 'devextreme-react/sankey';
  •  
  • class App extends React.Component {
  • constructor(props) {
  • super(props);
  • this.sankeyRef = React.createRef();
  • }
  • render() {
  • return (
  • <Sankey ref={this.sankeyRef} />
  • )
  • }
  • get sankey() {
  • return this.sankeyRef.current.instance;
  • }
  • exportSankey () {
  • return this.sankey.exportTo("exported_sankey", "PDF");
  • }
  • printSankey() {
  • return this.sankey.print();
  • }
  • }
  •  
  • export default App;

You can also export several UI components simultaneously using their SVG markup. Call the DevExpress.viz.getMarkup(widgetInstances) method to collect the markup from all the required UI components and pass it to the DevExpress.viz.exportFromMarkup(markup, options) method.

App.js
  • import React from 'react';
  • import Sankey from 'devextreme-react/sankey';
  • import exportMethods from 'devextreme/viz/export';
  •  
  • class App extends React.Component {
  • constructor(props) {
  • super(props);
  • this.sankey1Ref = React.createRef();
  • this.sankey2Ref = React.createRef();
  • }
  • render() {
  • return (
  • <Sankey ref={this.sankey1Ref} />
  • <Sankey ref={this.sankey2Ref} />
  • )
  • }
  • get sankey1() {
  • return this.sankey1Ref.current.instance;
  • }
  • get sankey2() {
  • return this.sankey2Ref.current.instance;
  • }
  • exportSeveralSankeys () {
  • const sankeyMarkup = exportMethods.getMarkup([this.sankey1, this.sankey2]);
  • exportMethods.exportFromMarkup(sankeyMarkup, {
  • height: 768,
  • width: 1024,
  • fileName: "exported_sankeys",
  • format: "PDF"
  • });
  • }
  • }
  •  
  • export default App;

Events

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

  • exporting
    Allows you to request export 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 on the user's device.

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 Sankey from 'devextreme-react/sankey';
  •  
  • class App extends React.Component {
  • render() {
  • return (
  • <Sankey
  • onExporting={this.onExporting}
  • onExported={this.onExported}
  • onFileSaving={this.onFileSaving}
  • />
  • )
  • }
  • onExporting(e) {
  • // Handler of the "exporting" event
  • };
  • onExported(e) {
  • // Handler of the "exported" event
  • };
  • onFileSaving(e) {
  • // Handler of the "fileSaving" event
  • }
  • }
  •  
  • export default App;

Otherwise (or if you need several handlers for a single event), subscribe to the exporting-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
  • };
  •  
  • $("#sankeyContainer").dxSankey("instance")
  • .on("exported", exportedHandler1)
  • .on("exported", exportedHandler2);
See Also