Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export a Highchart chart to PDF thanks to a button outside the chart?

I would like to have a button outside a Highchart chart that export the chart in PDF.

  • Does it exist a Highchart method to do that ?
  • Does an ajax call is mandatory ?

HTML :

<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>

<div id="container" style="height: 400px; margin-top: 1em"></div>
<button id="export2pdf">Export to PDF</button>

Javascript :

$(function () {
    window.chart = new Highcharts.Chart({
        chart: {renderTo : 'container'},
        title: {
            text: 'Exporting module is loaded but buttons are disabled'
        },
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },

        series: [{
            data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]        
        }],
        exporting: {
            enabled: true
        }
    });

    $("#export2pdf").click(function(){
        // Export method ?
        // Ajax call ?
    });
});

Here is a JsFiddle of what I would like.

Here is a JsFiddle with the solution of wergeld.

like image 916
kmas Avatar asked Dec 13 '13 17:12

kmas


2 Answers

See the API. This is covered here under exportChart. Essentially do this:

var chart = $('#container').highcharts();
chart.exportChart({
    type: 'application/pdf',
    filename: 'my-pdf'
});
like image 103
wergeld Avatar answered Oct 06 '22 09:10

wergeld


Check out the exporting properties for Highcharts. You will want to set type to application/pdf.

like image 27
Tomanow Avatar answered Oct 06 '22 11:10

Tomanow