Highcharts css styles when exporting

2020-02-16 04:00发布

问题:

I have the following graph in digital. See image:

but when using the Highchart's hamburguer CSS menu to export to PDF or JPG or PNG I get the graph like this: See image:

As you can see the CSS styles, fonts and size are not kept. How can we achieve to keep them? TIA for your help.

回答1:

You can pass your CSS styles in your post request to export server.

var options = {
  chart: {},
  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],
    type: 'column'
  }]
}

var chart = Highcharts.chart('container', options);

var data = {
    options: JSON.stringify(options),
    resources: {
        // You can add your CSS styles here
        css: ".highcharts-background { fill: #efefef; stroke: #a4edba; stroke-width: 2px}"
    },
    filename: 'test.png',
    type: 'image/png',
    async: true
}

var exportUrl = 'https://export.highcharts.com/';
$.post(exportUrl, data, function(data) {
    var imageUrl = exportUrl + data;
    var urlCreator = window.URL || window.webkitURL;
    document.querySelector("#image").src = imageUrl;
    fetch(imageUrl).then(response => response.blob()).then(data => {console.log(data)});
})

Live example: https://jsfiddle.net/nfbcq865/