在服务器端代码上运行Highcharts以添加到Word文档 [英] Run Highcharts on Server side code to add to Word document
问题描述
我在ASP.Net MVC项目上使用Highcharts. 所有这些都可以在客户端/js上很好地工作,但是我需要通过该项目创建一个Word文档,其中包括您可以在屏幕上看到的这些图表之一.
我目前正在使用Novacode的docx库来创建/修改Word文件,这很好用.我可以轻松添加图像,并且如有必要,可以使用.Nets图表库创建基本外观的图表,但我更喜欢使用Highcharts图表.
有人知道如何通过控制器(即服务器端)来创建Highcharts图表或获取图像以供在文档中使用.我能找到的唯一示例仍然需要一定程度的JS来完成.
我最终使用以下命令解决了这个问题:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://export.highcharts.com");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync("", content).Result;
if (response.IsSuccessStatusCode)
{
FileStream fileStream = new FileStream(pathname, FileMode.Create, FileAccess.Write, FileShare.None);
response.Content.CopyToAsync(fileStream).ContinueWith(
(copyTask) =>
{
fileStream.Close();
});
}
else
{
Console.WriteLine($"Failed to poste data. Status code:{response.StatusCode}");
}
我通过以下选项构建的json: https://www.highcharts.com/docs/export-module/export-module-overview
为粗略的示例表示歉意,我显然会修改并整理我的文章,但想在其他人认为有用的情况下给出答案.
I am using Highcharts on an ASP.Net MVC Project. All works great on client side/js however I have a requirement to create a word document via the project that includes one of these charts you can see on screen.
I currently use Novacode's docx library to create/modify word files and this works great. I can add images easily and if necessary I can create the basic looking charts with .Nets charting library but I'd prefer to use the Highcharts ones.
Does anyone know how via a Controller (i.e. server side) I can create a Highcharts chart or get the image for one to use in the document. The only examples I can find still require some level of JS to accomplish it.
I have resolved this myself in the end using the following:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://export.highcharts.com");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync("", content).Result;
if (response.IsSuccessStatusCode)
{
FileStream fileStream = new FileStream(pathname, FileMode.Create, FileAccess.Write, FileShare.None);
response.Content.CopyToAsync(fileStream).ContinueWith(
(copyTask) =>
{
fileStream.Close();
});
}
else
{
Console.WriteLine($"Failed to poste data. Status code:{response.StatusCode}");
}
The json for it I constructed via the options here: https://www.highcharts.com/docs/export-module/export-module-overview
Apologies for the crude example which I will obviously modify and clean up my end but wanted to give the answer to this should anyone else find it useful.
这篇关于在服务器端代码上运行Highcharts以添加到Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!