Exporting Highcharts into other formats

Highcharts provides a module to export charts into various image formats. There can be two possibilities when considering the export server, that is, to use Highcharts' official exporting server or set up your own server. We will use the export module to export our charts into various formats using the Highcharts CDN.

To enable exporting, the exporting.js file must be included after the highcharts.js file. It can be found in the Highcharts-4.x.x/js/modules folder and can also be included directly from http://code.highcharts.com/modules/exporting.js.

To enable simple exporting, we need to set exporting.enabled to true in our chart configuration object:

$( '...' ).highcharts({
  ...
  exporting: {
    enabled: true
  },
  ...
});

By doing so, a button with a drop-down menu appears at the top-right corner of the chart:

Exporting Highcharts into other formats

The drop-down menu contains various options to export charts into formats including PNG, JPEG, PDF, and SVG.

You can alter various properties including image width and height, filename, and the URL of the exporting server. Button properties can also be changed within the chart by referring to exporting.buttons.contextButton.

You can find more about the exporting module at http://api.highcharts.com/highcharts#exporting.

Exporting charts programmatically

In addition to exporting the context button, we can also export charts programmatically using Highcharts or a local export server.

In the following example, we will export a chart by passing the basic configuration object in an Ajax request to the server. The data sent to the server in the Ajax request contains parameters for the configuration object and file attributes.

The following code elaborates a simple request sent to Highcharts' exporting server:

var chartConfigJSON = {
  "chart": {
    "type": "line"
  },
  "xAxis": {
    "categories": ['Jane', 'John', 'Doe']
  },
  "series": [{
    "name": "Persons",
    "data": [20, 45, 30]
  }]
};

var requestString = "async=true&type=pdf&width=500&options=" + JSON.stringify( chartConfigJSON );

$.ajax({
  type: 'POST',
  data: requestString,
  url: 'http://export.highcharts.com',
  success: function( filename ) {
    window.open( 'http://export.highcharts.com/' + filename );
  }
});

We first define a JSON object with the same structure as the Highcharts configuration object and call it chartConfigJSON. In the next step, we define another variable requestString that contains post parameters appended by the JSON string of chartConfigJSON. We used a JavaScript method JSON.stringify() that converts a JSON to a string.

Next, we post an Ajax request to http://export.highcharts.com containing previously defined parameters. The success function receives the path of the file at Highcharts' export server, which we open in a new window for the user.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.172.56