Highcharts 有一个导出服务器,他们在 GitHub 存储库中有一些示例: https://github.com/highcharts/node-export-server/blob/master/tests/node/scenarios/custom_code_from_string.json
如果您复制任何示例并调用导出服务器https://export.highcharts.com 它返回为一个错误的请求。
这不起作用:
{export:
{
// JSONDATA
}
}
这正在工作:
{
// JSONDATA
}
问题是导出服务器只接受导出对象主体。因此,如果我想使用回调,我不能因为它位于不同的块中。
这是我的简单控制台应用程序从导出服务器获取图像:
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 = "{\r\n \"export\": {\r\n \"options\": {\r\n \"chart\": {\r\n \"type\": \"column\"\r\n },\r\n \"title\": {\r\n \"text\": \"Custom code (from string)\"\r\n },\r\n \"yAxis\": [\r\n {\r\n \"title\": {\r\n \"text\": \"Primary axis\"\r\n }\r\n },\r\n {\r\n \"opposite\": true,\r\n \"title\": {\r\n \"text\": \"Secondary axis\"\r\n }\r\n }\r\n ],\r\n \"plotOptions\": {\r\n \"column\": {\r\n \"borderRadius\": 5\r\n }\r\n },\r\n \"series\": [\r\n {\r\n \"data\": [1, 3, 2, 4]\r\n },\r\n {\r\n \"data\": [324, 124, 547, 221],\r\n \"yAxis\": 1\r\n }\r\n ]\r\n }\r\n },\r\n \"customLogic\": {\r\n \"allowCodeExecution\": true,\r\n \"allowFileResources\": true,\r\n \"customCode\": \"Highcharts.setOptions({chart:{events:{render:function (){this.renderer.image('https://www.highcharts.com/samples/graphics/sun.png',75,50,20,20).add();}}}});\"\r\n }\r\n}";
// Gets image and 200 result but the custom logic is obviously missing.
string jsonData = "{\r\n \"options\": {\r\n \"chart\": {\r\n \"type\": \"column\"\r\n },\r\n \"title\": {\r\n \"text\": \"Custom code (from string)\"\r\n },\r\n \"yAxis\": [\r\n {\r\n \"title\": {\r\n \"text\": \"Primary axis\"\r\n }\r\n },\r\n {\r\n \"opposite\": true,\r\n \"title\": {\r\n \"text\": \"Secondary axis\"\r\n }\r\n }\r\n ],\r\n \"plotOptions\": {\r\n \"column\": {\r\n \"borderRadius\": 5\r\n }\r\n },\r\n \"series\": [\r\n {\r\n \"data\": [1, 3, 2, 4]\r\n },\r\n {\r\n \"data\": [324, 124, 547, 221],\r\n \"yAxis\": 1\r\n }\r\n ]\r\n }\r\n }";
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}");
}
}
}
}
}
我将 jsonData 变量放在好的和坏的示例中(需要取消注释)。很明显,Hihghcharts 自己的示例在其服务器上根本不起作用。 我做错了什么或者为什么它不起作用? 谢谢!
我尝试了很多方法,我想知道为什么 Highcharts 示例不能在 Highcharts 导出服务器上运行?如果可能的话,我想获得一个带有自定义 JS 函数的工作示例。就像用 JS 代码更新图表中的标题或值一样。
官方部署的导出服务器(https://export.highcharts.com/)不提供以下功能:
这些标志:
"allowCodeExecution": false,
"allowFileResources": false
在正式部署的服务器上被禁用。
如果您想利用导出服务器的全部功能,包括自定义代码执行和处理外部 URL,您需要根据 GitHub 上包含的指南设置您自己的导出服务器:https:// github.com/highcharts/node-export-server?tab=readme-ov-file#highcharts-nodejs-export-server
您可以在其中启用
allowCodeExecution
和 allowCodeResources
。