经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Echarts » 查看文章
将ECharts图表插入到Word文档中
来源:cnblogs  作者:林晓lx  时间:2023/11/17 9:19:59  对本文有异议

@


如何通过ECharts在后台生成图片,然后插入到Word文档中?

首先要解决一个问题:总所周知,ECharts是前端的一个图表库,如何在后台调用JS代码?这里就要用到PhantomJS了。

PhantomJS是一个基于WebKit的JavaScript API,它使用QtWebKit作为核心浏览器的功能,使用WebKit编译、解释和执行JavaScript代码。任何可以在基于WebKit的浏览器上做的事情,它都能做到。它不仅是一个隐形的浏览器,提供了诸如CSS选择器、支持Web标准、DOM操作、JSON、HTML5、Canvas、SVG等功能,同时也提供了处理文件I/O的操作。

之前写过一个文档模板工具,其中可以通过占位符插入图片。

用PhantomJS生成ECharts图表的Png图片,利用文档模板工具插入图片即可实现这个需求。

下面就来看看如何实现。

在后端调用JS代码

创建一个.netstandard2.1的类库项目。为了方便调用,我们安装一个PhantomJS包装器:NReco.PhantomJS

  1. dotnet add package NReco.PhantomJS --version 1.1.0

这只是一个包装器,因此还需要一个可执行文件,前往官网下载PhantomJS。

因为直接使用编译好的可执行文件,因此需要下载对应的平台版本,这里我下载了Windows以及Linux 64-bit版本。

将下载好的可执行文件解压放置在项目根目录下的libs目录中。

在这里插入图片描述

这样我们可以直接在.net中调用PhantomJS了。

准备ECharts库

jQuery

下载jquery-3.6.3.min.js: https://code.jquery.com/jquery-3.6.3.min.js

ECharts

下载echarts.min.js: https://github.com/apache/echarts/tree/5.4.3/dist

ECharts转换器

echarts-convert在github上有众多版本,echarts-convert的代码来源于这里:https://github.com/wadezhan/billfeller.github.io/issues/85

这里选择

  1. (function () {
  2. var system = require('system');
  3. var fs = require('fs');
  4. var config = {
  5. // define the location of js files
  6. JQUERY: 'jquery-3.6.3.min.js',
  7. //ESL: 'esl.js',
  8. ECHARTS: 'echarts.min.js',
  9. // default container width and height
  10. DEFAULT_WIDTH: '1920',
  11. DEFAULT_HEIGHT: '800'
  12. }, parseParams, render, pick, usage;
  13. usage = function () {
  14. console.log("\nUsage: phantomjs echarts-convert.js -options options -outfile filename -width width -height height"
  15. + "OR"
  16. + "Usage: phantomjs echarts-convert.js -infile URL -outfile filename -width width -height height\n");
  17. };
  18. pick = function () {
  19. var args = arguments, i, arg, length = args.length;
  20. for (i = 0; i < length; i += 1) {
  21. arg = args[i];
  22. if (arg !== undefined && arg !== null && arg !== 'null' && arg != '0') {
  23. return arg;
  24. }
  25. }
  26. };
  27. parseParams = function () {
  28. var map = {}, i, key;
  29. console.log("--logs:\n")
  30. console.log(system.args)
  31. if (system.args.length < 2) {
  32. usage();
  33. phantom.exit();
  34. }
  35. for (i = 0; i < system.args.length; i += 1) {
  36. if (system.args[i].charAt(0) === '-') {
  37. key = system.args[i].substr(1, i.length);
  38. if (key === 'infile') {
  39. // get string from file
  40. // force translate the key from infile to options.
  41. key = 'options';
  42. try {
  43. map[key] = fs.read(system.args[i + 1]).replace(/^\s+/, '');
  44. } catch (e) {
  45. console.log('Error: cannot find file, ' + system.args[i + 1]);
  46. phantom.exit();
  47. }
  48. } else {
  49. map[key] = system.args[i + 1].replace(/^\s+/, '');
  50. }
  51. }
  52. }
  53. return map;
  54. };
  55. render = function (params) {
  56. var page = require('webpage').create(), createChart;
  57. var bodyMale = config.SVG_MALE;
  58. page.onConsoleMessage = function (msg) {
  59. console.log(msg);
  60. };
  61. page.onAlert = function (msg) {
  62. console.log(msg);
  63. };
  64. createChart = function (inputOption, width, height, config) {
  65. var counter = 0;
  66. function decrementImgCounter() {
  67. counter -= 1;
  68. if (counter < 1) {
  69. console.log(messages.imagesLoaded);
  70. }
  71. }
  72. function loadScript(varStr, codeStr) {
  73. var script = $('<script>').attr('type', 'text/javascript');
  74. script.html('var ' + varStr + ' = ' + codeStr);
  75. document.getElementsByTagName("head")[0].appendChild(script[0]);
  76. if (window[varStr] !== undefined) {
  77. console.log('Echarts.' + varStr + ' has been parsed');
  78. }
  79. }
  80. function loadImages() {
  81. var images = $('image'), i, img;
  82. if (./Assets/images.length > 0) {
  83. counter = images.length;
  84. for (i = 0; i < images.length; i += 1) {
  85. img = new Image();
  86. img.onload = img.onerror = decrementImgCounter;
  87. img.src = images[i].getAttribute('href');
  88. }
  89. } else {
  90. console.log('The images have been loaded');
  91. }
  92. }
  93. // load opitons
  94. if (inputOption != 'undefined') {
  95. // parse the options
  96. loadScript('options', inputOption);
  97. // disable the animation
  98. options.animation = false;
  99. }
  100. // we render the image, so we need set background to white.
  101. $(document.body).css('backgroundColor', 'white');
  102. var container = $("<div>").appendTo(document.body);
  103. container.attr('id', 'container');
  104. container.css({
  105. width: width,
  106. height: height
  107. });
  108. // render the chart
  109. var myChart = echarts.init(container[0]);
  110. myChart.setOption(options);
  111. // load images
  112. loadImages();
  113. return myChart.getDataURL();
  114. };
  115. // parse the params
  116. page.open("about:blank", function (status) {
  117. // inject the dependency js
  118. page.injectJs(config.ESL);
  119. page.injectJs(config.JQUERY);
  120. page.injectJs(config.ECHARTS);
  121. var width = pick(params.width, config.DEFAULT_WIDTH);
  122. var height = pick(params.height, config.DEFAULT_HEIGHT);
  123. // create the chart
  124. var base64 = page.evaluate(createChart, params.options, width, height, config);
  125. //fs.write("base64.txt", base64);
  126. // define the clip-rectangle
  127. page.clipRect = {
  128. top: 0,
  129. left: 0,
  130. width: width,
  131. height: height
  132. };
  133. // render the image
  134. page.render(params.outfile);
  135. console.log('render complete:' + params.outfile);
  136. // exit
  137. phantom.exit();
  138. });
  139. };
  140. // get the args
  141. var params = parseParams();
  142. // validate the params
  143. if (params.options === undefined || params.options.length === 0) {
  144. console.log("ERROR: No options or infile found.");
  145. usage();
  146. phantom.exit();
  147. }
  148. // set the default out file
  149. if (params.outfile === undefined) {
  150. var tmpDir = fs.workingDirectory + '/tmp';
  151. // exists tmpDir and is it writable?
  152. if (!fs.exists(tmpDir)) {
  153. try {
  154. fs.makeDirectory(tmpDir);
  155. } catch (e) {
  156. console.log('ERROR: Cannot make tmp directory');
  157. }
  158. }
  159. params.outfile = tmpDir + "/" + new Date().getTime() + ".png";
  160. }
  161. // render the image
  162. render(params);
  163. }());

将上述文件放在项目根目录下的js目录中。

在这里插入图片描述

我们来测试一下是否能生成一个简单的ECharts图表。

创建一个option.json

在这里插入图片描述

首先指定一个option,在官方示例 https://echarts.apache.org/examples/zh/index.html 中,随意找一个柱状图的sample,复制option对象内容到新创建的option.json文件中

  1. {
  2. "tooltip": {
  3. "trigger": "axis",
  4. "axisPointer": {
  5. "type": "shadow"
  6. }
  7. },
  8. "grid": {
  9. "left": "3%",
  10. "right": "4%",
  11. "bottom": "3%",
  12. "containLabel": true
  13. },
  14. "xAxis": [
  15. {
  16. "type": "category",
  17. "data": [ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" ],
  18. "axisTick": {
  19. "alignWithLabel": true
  20. }
  21. }
  22. ],
  23. "yAxis": [
  24. {
  25. "type": "value"
  26. }
  27. ],
  28. "series": [
  29. {
  30. "name": "Direct",
  31. "type": "bar",
  32. "barWidth": "60%",
  33. "data": [ 10, 52, 200, 334, 390, 330, 220 ]
  34. }
  35. ]
  36. }

Program.cs中调用ECharts转换器:

  1. static void Main(string[] args)
  2. {
  3. var phantomJS = new PhantomJS();
  4. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  5. {
  6. phantomJS.ToolPath = Path.Combine(basePath, "libs\\phantomjs-2.1.1-windows\\bin");
  7. }
  8. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  9. {
  10. phantomJS.ToolPath = Path.Combine(basePath, "libs\\phantomjs-2.1.1-linux-x86_64\\bin");
  11. }
  12. var scriptPath = Path.Combine(basePath, "js\\echarts-converts.js");
  13. var optionPath = Path.Combine(basePath, "js\\option.json");
  14. phantomJS.OutputReceived += (sender, e) =>
  15. {
  16. Console.WriteLine("PhantomJS output: {0}", e.Data);
  17. };
  18. phantomJS.Run(scriptPath, new string[] { "-infile", optionPath });
  19. phantomJS.Abort();
  20. }

打印如下

在这里插入图片描述

打开输出路径看到生成的图片。

在这里插入图片描述

生成Word文档

为了方便集成,我加.NET中构件ECharts中可能用的全部数据结构。
这里感谢https://github.com/idoku/EChartsSDK这个项目,代码基本都是从这里拷贝过来的。

这样可以通过指定ChartOption对象,生成图片。

  1. var option = new ChartOption()
  2. {
  3. title = new List<Title>()
  4. {
  5. new Title (){
  6. text=title, left="center"}
  7. },
  8. tooltip = new ToolTip(),
  9. legend = new Legend()
  10. {
  11. orient = OrientType.vertical,
  12. left = "left"
  13. },
  14. series = new object[]
  15. {
  16. new {
  17. name= "Access From",
  18. type="pie",
  19. data=new object[]
  20. {
  21. new { value= failedCount, name="异常" },
  22. new { value= passCount, name="正常" },
  23. }
  24. }
  25. }
  26. }

根据Document Template Tool图片占位符格式:#字段名称[宽度,高度]#,

在上一章的Sample基础上,在ReportTemplate2.docx中添加图片占位符。

在这里插入图片描述

生成后的文档如下:

在这里插入图片描述

项目地址

库封装

https://github.com/jevonsflash/EChartsGen

本文示例 EChartsGen_DocTemplateTool_Sample

https://github.com/jevonsflash/EChartsGen/tree/master/EChartsGen_DocTemplateTool_Sample

原文链接:https://www.cnblogs.com/jevonsflash/p/17833456.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号