Highcharts - export to base64

Viewed 11717

Is there a way to take a highcharts graph, and get a base64 representation of the it?

In other words, the equivalent of first exporting it to PNG or JPG (I don't care which) and then getting a base64 string of that image.

3 Answers

You can directly get base64 from http://export.highcharts.com by passing required parameter in request.

let chartData = {
        infile: CHART_DATA,
        b64: true // Bool, set to true to get base64 back instead of binary.
        width: 600,
        constr : "Chart"
    }

You can use below snippet to get base64

fetch("https://export.highcharts.com/", {
  "headers": {
    "content-type": "application/json",
  },
  "body": "{\"infile\":\"{\\n    \\\"xAxis\\\": {\\n        \\\"categories\\\": [\\n            \\\"Jan\\\",\\n            \\\"Feb\\\",\\n            \\\"Mar\\\",\\n            \\\"Apr\\\",\\n            \\\"May\\\",\\n            \\\"Jun\\\",\\n            \\\"Jul\\\",\\n            \\\"Aug\\\",\\n            \\\"Sep\\\",\\n            \\\"Oct\\\",\\n            \\\"Nov\\\",\\n            \\\"Dec\\\"\\n        ]\\n    },\\n    \\\"series\\\": [\\n        {\\n            \\\"data\\\": [1,3,2,4],\\n            \\\"type\\\": \\\"line\\\"\\n        },\\n        {\\n            \\\"data\\\": [5,3,4,2],\\n            \\\"type\\\":\\\"line\\\"\\n        }\\n    ]\\n}\\n\",\"width\":600,\"constr\":\"Chart\",\"b64\":true}",
  "method": "POST",
  "mode": "cors"
}).then(function(response) {
    // The response is a Response instance.
    return response.text();
  }).then(function(data) {
    console.log(data);  //  base64 data
  }).catch(function(err) { console.log(err);})

Related