template6.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * @Author: 54xavier
  3. * @LastEditors: admin@54xavier.cn
  4. * @Date: 2023-02-28 14:00:03
  5. * @LastEditTime: 2023-08-08 19:32:55
  6. */
  7. export const name = "Echarts实现";
  8. export const desc = "通过HTML元素实现Echarts-svg";
  9. export const author = "54xavier";
  10. export const link = "https://ccsimple.gitee.io/vue-plugin-hiprint/";
  11. // url 或者 base64 或者 require('../../../assets/logo.png')
  12. export const preview =
  13. "https://gitee.com/CcSimple/vue-plugin-hiprint/raw/main/res/template6.png";
  14. function getRandomInt({ min = 10, max = 200 }) {
  15. return Math.floor(Math.random() * (max - min + 1)) + min;
  16. }
  17. export const printData = {
  18. lineData: Array.from(new Array(7), () => getRandomInt({})),
  19. barData: Array.from(new Array(7), () => getRandomInt({})),
  20. pieData: Array.from(new Array(5), (_, i) => ({
  21. value: getRandomInt({}),
  22. name: `系列${i}`,
  23. })),
  24. };
  25. export const json = {
  26. panels: [
  27. {
  28. index: 0,
  29. name: 1,
  30. height: 297,
  31. width: 210,
  32. paperHeader: 0,
  33. paperFooter: 842,
  34. printElements: [
  35. {
  36. options: {
  37. left: 0,
  38. top: 0,
  39. height: 1,
  40. width: 1,
  41. formatter: `function(t, e, printData) {
  42. var script = document.createElement("script");
  43. script.setAttribute("type", "text/javascript");
  44. script.setAttribute("src", "https://cdnjs.cloudflare.com/ajax/libs/echarts/5.4.3/echarts.min.js");
  45. return script;
  46. }`,
  47. },
  48. printElementType: {
  49. // 此元素为 Echarts 依赖,此方法仅浏览器打印(print)生效,print2可能会无法加载依赖,需要手动在electron-hiprint项目中引入依赖
  50. // 如果不使用 print2 可将此依赖全局添加到你自己项目中,只需设计、预览时能加载到此依赖即可
  51. title: "Echarts依赖",
  52. type: "html",
  53. },
  54. },
  55. {
  56. options: {
  57. left: 12,
  58. top: 12,
  59. height: 200,
  60. width: 200,
  61. formatter: `function(t, e, printData) {
  62. if (window.echarts) {
  63. var echartDom = document.createElement("div");
  64. echartDom.style.width = "267px";
  65. echartDom.style.height = "267px";
  66. var echartInstance = echarts.init(echartDom,null,{renderer: "svg"})
  67. echartInstance.setOption({
  68. animation: false,
  69. grid: {
  70. top: 10,
  71. right: 20,
  72. bottom: 30,
  73. left: 36,
  74. },
  75. xAxis: {
  76. type: 'category',
  77. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  78. },
  79. yAxis: {
  80. type: 'value'
  81. },
  82. series: [
  83. {
  84. data: printData?.lineData || [150, 230, 224, 218, 135, 147, 260],
  85. type: 'line'
  86. }
  87. ]
  88. })
  89. return echartDom;
  90. } else {
  91. return '<div style="width: 267px;height: 267px; border: 1px solid;">点击打印预览查看</div>'
  92. }
  93. }`,
  94. },
  95. printElementType: { title: "html", type: "html" },
  96. },
  97. {
  98. options: {
  99. left: 230,
  100. top: 12,
  101. height: 200,
  102. width: 200,
  103. formatter: `function(t, e, printData) {
  104. if (window.echarts) {
  105. var echartDom = document.createElement("div");
  106. echartDom.style.width = "267px";
  107. echartDom.style.height = "267px";
  108. var echartInstance = echarts.init(echartDom,null,{renderer: "svg"})
  109. echartInstance.setOption({
  110. animation: false,
  111. grid: {
  112. top: 10,
  113. right: 20,
  114. bottom: 30,
  115. left: 36,
  116. },
  117. xAxis: {
  118. type: 'category',
  119. data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  120. },
  121. yAxis: {
  122. type: 'value'
  123. },
  124. series: [
  125. {
  126. data: printData?.barData || [150, 230, 224, 218, 135, 147, 260],
  127. type: 'bar'
  128. }
  129. ]
  130. })
  131. return echartDom;
  132. } else {
  133. return '<div style="width: 267px;height: 267px; border: 1px solid;">点击打印预览查看</div>'
  134. }
  135. }`,
  136. },
  137. printElementType: { title: "html", type: "html" },
  138. },
  139. {
  140. options: {
  141. left: 12,
  142. top: 230,
  143. height: 200,
  144. width: 200,
  145. formatter: `function(t, e, printData) {
  146. if (window.echarts) {
  147. var echartDom = document.createElement("div");
  148. echartDom.style.width = "267px";
  149. echartDom.style.height = "267px";
  150. var echartInstance = echarts.init(echartDom,null,{renderer: "svg"})
  151. echartInstance.setOption({
  152. animation: false,
  153. series: [
  154. {
  155. name: 'Access From',
  156. type: 'pie',
  157. radius: ['40%', '70%'],
  158. avoidLabelOverlap: false,
  159. itemStyle: {
  160. borderRadius: 10,
  161. borderColor: '#fff',
  162. borderWidth: 2
  163. },
  164. label: {
  165. show: false,
  166. position: 'center'
  167. },
  168. emphasis: {
  169. label: {
  170. show: true,
  171. fontSize: 40,
  172. fontWeight: 'bold'
  173. }
  174. },
  175. labelLine: {
  176. show: false
  177. },
  178. data: printData?.pieData || [
  179. { value: 1048, name: 'Search Engine' },
  180. { value: 735, name: 'Direct' },
  181. { value: 580, name: 'Email' },
  182. { value: 484, name: 'Union Ads' },
  183. { value: 300, name: 'Video Ads' }
  184. ]
  185. }
  186. ]
  187. })
  188. return echartDom;
  189. } else {
  190. return '<div style="width: 267px;height: 267px; border: 1px solid;">点击打印预览查看</div>'
  191. }
  192. }`,
  193. },
  194. printElementType: { title: "html", type: "html" },
  195. },
  196. {
  197. options: {
  198. left: 12,
  199. top: 460,
  200. height: 12,
  201. width: 418,
  202. title:
  203. "说明:此处模版以一个不可见 html 元素加载 echarts ,故第一次打开无法显示预览,但后续的 print api 能够正常显示,你可以在你的项目中全局加载 echarts 依赖,保证设计、预览时能加载到 echarts 依赖,即可删除用于引入依赖的html元素。使用 print2 需要在 electron-hiprint 项目渲染层中添加该依赖,否则客户端首次打印都无法正常渲染 echarts",
  204. },
  205. printElementType: { title: "说明", type: "text" },
  206. },
  207. ],
  208. paperNumberLeft: 565.5,
  209. paperNumberTop: 819,
  210. paperNumberContinue: true,
  211. watermarkOptions: {},
  212. },
  213. ],
  214. };
  215. export default {
  216. preview: preview,
  217. name: name,
  218. desc: desc,
  219. author: author,
  220. link: link,
  221. printData: printData,
  222. json: json,
  223. };