Highcharts has an export server and they had some examples in their GitHub repo:
https://github.com/highcharts/node-export-server/blob/master/tests/node/scenarios/custom_code_from_string.json
If you copy any of the examples and call the export server https://export.highcharts.com
it gets back as a bad request.
This is not working:
{export:
{
// JSONDATA
}
}
This is working:
{
// JSONDATA
}
The problem is the export server only accept the export object body. So if I want to use the callback I can’t cause it is in a different block.
Here is my simple console app getting image from the export server:
using System.Net.Http.Headers;
using System.Text;
namespace chartImageScraper
{
class Program
{
static async Task Main(string[] args)
{
string apiUrl = "https://export.highcharts.com/";
// 400 Bad Request cause the JSON structure and the custom logic in it.
//string jsonData = "{rn "export": {rn "options": {rn "chart": {rn "type": "column"rn },rn "title": {rn "text": "Custom code (from string)"rn },rn "yAxis": [rn {rn "title": {rn "text": "Primary axis"rn }rn },rn {rn "opposite": true,rn "title": {rn "text": "Secondary axis"rn }rn }rn ],rn "plotOptions": {rn "column": {rn "borderRadius": 5rn }rn },rn "series": [rn {rn "data": [1, 3, 2, 4]rn },rn {rn "data": [324, 124, 547, 221],rn "yAxis": 1rn }rn ]rn }rn },rn "customLogic": {rn "allowCodeExecution": true,rn "allowFileResources": true,rn "customCode": "Highcharts.setOptions({chart:{events:{render:function (){this.renderer.image('https://www.highcharts.com/samples/graphics/sun.png',75,50,20,20).add();}}}});"rn }rn}";
// Gets image and 200 result but the custom logic is obviously missing.
string jsonData = "{rn "options": {rn "chart": {rn "type": "column"rn },rn "title": {rn "text": "Custom code (from string)"rn },rn "yAxis": [rn {rn "title": {rn "text": "Primary axis"rn }rn },rn {rn "opposite": true,rn "title": {rn "text": "Secondary axis"rn }rn }rn ],rn "plotOptions": {rn "column": {rn "borderRadius": 5rn }rn },rn "series": [rn {rn "data": [1, 3, 2, 4]rn },rn {rn "data": [324, 124, 547, 221],rn "yAxis": 1rn }rn ]rn }rn }";
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(apiUrl);
// Adding necessary headers
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// Sending POST request with JSON data
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "relativeAddress");
request.Content = new StringContent(jsonData, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.SendAsync(request);
var test = response.Content.ReadAsStringAsync().Result;
// Checking if request was successful
if (response.IsSuccessStatusCode)
{
// Reading response content as a byte array
var imageData = await response.Content.ReadAsByteArrayAsync();
// Saving the image file
await File.WriteAllBytesAsync($"chart_image_{DateTime.Now:yyyyMMddHHmmss}.png", imageData);
Console.WriteLine("Image saved successfully.");
}
else
{
Console.WriteLine($"Failed to retrieve image. Status code: {response.StatusCode}");
}
}
}
}
}
I put the jsonData variable the good and the bad examples (need to uncomment it). It is clear that Hihghcharts own examples not working at all on their server.
Am I doing something wrong or why it is not working?
Thank you!
I tried a lot of things and I would like to know why the Highcharts example not works on the Highcharts export server? If it is possible I would like to get a working example with the custom JS functions. Like update title or values in the chart with JS code.
Gábor Balázs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.