如何通过图表外的按钮将Highchart图表导出为PDF?

时间:2013-12-13 17:42:56

标签: javascript jquery pdf charts highcharts

我希望在button outside a Highchart chartexport PDF图表。

  • 这样做是否存在Highchart方法?
  • ajax调用是强制性的吗?

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 ?
    });
});

这是我想要的JsFiddle

以下是JsFiddle,解决方案为wergeld

2 个答案:

答案 0 :(得分:21)

请参阅API。这将在exportChart下进行介绍。 基本上这样做:

var chart = $('#container').highcharts();
chart.exportChart({
    type: 'application/pdf',
    filename: 'my-pdf'
});

答案 1 :(得分:1)

查看Highcharts的exporting属性。您需要将type设置为application/pdf