Highcharts css styles when exporting

2020-02-16 04:15发布

I have the following graph in digital. See image: enter image description here

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: enter image description here

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条回答
Rolldiameter
2楼-- · 2020-02-16 04:48

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/

查看更多
登录 后发表回答