rpt_figure.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //本文档是图形输出的接口实现
  2. const JpcFigureOutput = {
  3. offsetX: 0,
  4. offsetY: 0,
  5. scaleFactor: 1,
  6. _iniCavas: function(canvas, width, height) {
  7. if (canvas) {
  8. let ctx = canvas.getContext('2d');
  9. }
  10. },
  11. _draw: function(ctx, figure, saveAsData = false) {
  12. if (figure.imageData) {
  13. //直接图形输出
  14. ctx.putImageData(figure.imageData, figure.area.Left + this.offsetX, figure.area.Top + this.offsetY);
  15. } else {
  16. let width = figure.area.Right - figure.area.Left;
  17. let height = figure.area.Bottom - figure.area.Top;
  18. let xSteps = figure.coordinateAxis.axisX.steps;
  19. let ySteps = figure.coordinateAxis.axisY.steps;
  20. let xStepW = width / xSteps;
  21. let yStepH = height / ySteps;
  22. ctx.save();
  23. ctx.translate(0.5,0.5);
  24. ctx.beginPath();
  25. if (figure.coordinateAxis.axisX.isShowLine) {
  26. //画竖线
  27. for (let idx = 0; idx < xSteps; idx++) {
  28. ctx.lineWidth = 1;
  29. ctx.strokeStyle = 'black';
  30. ctx.moveTo(Math.round(figure.area.Left + xStepW * (idx + 1) + this.offsetX), figure.area.Bottom + this.offsetY);
  31. ctx.lineTo(Math.round(figure.area.Left + xStepW * (idx + 1) + this.offsetX), figure.area.Top + this.offsetY);
  32. }
  33. }
  34. if (figure.coordinateAxis.axisY.isShowLine) {
  35. //画横线
  36. for (let idx = 0; idx < ySteps; idx++) {
  37. ctx.lineWidth = 1;
  38. ctx.strokeStyle = 'black';
  39. ctx.moveTo(figure.area.Left + this.offsetX, Math.round(figure.area.Bottom - yStepH * (idx + 1) + this.offsetY));
  40. ctx.lineTo(figure.area.Right + this.offsetX, Math.round(figure.area.Bottom - yStepH * (idx + 1) + this.offsetY));
  41. }
  42. }
  43. ctx.stroke();
  44. for (let ctIdx = 0; ctIdx < figure.contents.length; ctIdx++) {
  45. const content = figure.contents[ctIdx];
  46. ctx.beginPath();
  47. for (let idx = 0; idx < content.values.length; idx++) {
  48. let xPos = figure.area.Left + xStepW * idx + xStepW / 2 + this.offsetX;
  49. let yPos = figure.area.Bottom - (content.values[idx] / content.maxValue) * height + this.offsetY;
  50. ctx.lineWidth = 1;
  51. ctx.strokeStyle = content.color;
  52. if (content.style === 'dash') ctx.setLineDash([10, 5]);
  53. switch (figure.config.dispType) {
  54. case 'line':
  55. //折线图
  56. if (idx === 0) {
  57. ctx.moveTo(xPos, yPos);
  58. } else {
  59. ctx.lineTo(xPos, yPos);
  60. }
  61. break;
  62. case 'pole':
  63. //柱图
  64. break;
  65. default:
  66. //....
  67. break;
  68. }
  69. }
  70. ctx.stroke();
  71. }
  72. ctx.restore();
  73. if (saveAsData) {
  74. //保存当前图形数据
  75. figure.imageData = ctx.getImageData(figure.area.Left + this.offsetX, figure.area.Top + this.offsetY, width, height);
  76. }
  77. }
  78. },
  79. drawToCanvas: function(canvas, currentPageData) {
  80. //预览用
  81. if (canvas && currentPageData) {
  82. const ctx = canvas.getContext("2d");
  83. if (currentPageData.figure_cells && currentPageData.figure_cells.length > 0) {
  84. for (let figure of currentPageData.figure_cells) {
  85. this._draw(ctx, figure, false);
  86. }
  87. }
  88. }
  89. },
  90. _drawPdf: function(doc, figure) {
  91. const PDF_SCALE = 0.75;
  92. if (figure.imageData) {
  93. //直接图形输出
  94. } else {
  95. let width = figure.area.Right - figure.area.Left;
  96. let height = figure.area.Bottom - figure.area.Top;
  97. let xSteps = figure.coordinateAxis.axisX.steps;
  98. let ySteps = figure.coordinateAxis.axisY.steps;
  99. let xStepW = width / xSteps;
  100. let yStepH = height / ySteps;
  101. if (figure.coordinateAxis.axisX.isShowLine) {
  102. //画竖线
  103. for (let idx = 0; idx < xSteps; idx++) {
  104. doc.setLineWidth(0.8);
  105. doc.setDrawColor('black');
  106. doc.line((Math.round(figure.area.Left + xStepW * (idx + 1) + this.offsetX)) * PDF_SCALE, (figure.area.Bottom + this.offsetY) * PDF_SCALE,
  107. (Math.round(figure.area.Left + xStepW * (idx + 1) + this.offsetX)) * PDF_SCALE, (figure.area.Top + this.offsetY) * PDF_SCALE);
  108. }
  109. }
  110. if (figure.coordinateAxis.axisY.isShowLine) {
  111. //画横线
  112. for (let idx = 0; idx < ySteps; idx++) {
  113. doc.setLineWidth(0.8);
  114. doc.setDrawColor('black');
  115. doc.line((figure.area.Left + this.offsetX) * PDF_SCALE, (Math.round(figure.area.Bottom - yStepH * (idx + 1) + this.offsetY)) * PDF_SCALE,
  116. (figure.area.Right + this.offsetX) * PDF_SCALE, (Math.round(figure.area.Bottom - yStepH * (idx + 1) + this.offsetY)) * PDF_SCALE);
  117. }
  118. }
  119. for (let ctIdx = 0; ctIdx < figure.contents.length; ctIdx++) {
  120. const content = figure.contents[ctIdx];
  121. let preXPos = 0, preYPos = 0;
  122. for (let idx = 0; idx < content.values.length; idx++) {
  123. let xPos = figure.area.Left + xStepW * idx + xStepW / 2 + this.offsetX;
  124. let yPos = figure.area.Bottom - (content.values[idx] / content.maxValue) * height + this.offsetY;
  125. doc.setLineWidth(0.8);
  126. doc.setDrawColor(content.color);
  127. if (content.style === 'dash') doc.setLineDash([10, 5]);
  128. switch (figure.config.dispType) {
  129. case 'line':
  130. //折线图
  131. if (idx > 0) {
  132. // ctx.lineTo(xPos, yPos);
  133. doc.line(preXPos * PDF_SCALE, preYPos * PDF_SCALE, xPos * PDF_SCALE, yPos * PDF_SCALE);
  134. }
  135. preXPos = xPos;
  136. preYPos = yPos;
  137. break;
  138. case 'pole':
  139. //柱图
  140. break;
  141. default:
  142. //....
  143. break;
  144. }
  145. }
  146. }
  147. }
  148. },
  149. drawToPdf: function(doc, currentPageData) {
  150. //导出PDF用
  151. if (doc && currentPageData) {
  152. if (currentPageData.figure_cells && currentPageData.figure_cells.length > 0) {
  153. for (let figure of currentPageData.figure_cells) {
  154. this._drawPdf(doc, figure);
  155. }
  156. }
  157. }
  158. },
  159. _drawSvg: function(figure, isHtoV, actArea) {
  160. let rst = [];
  161. if (figure.imageData) {
  162. //直接图形输出
  163. } else {
  164. let width = figure.area.Right - figure.area.Left;
  165. let height = figure.area.Bottom - figure.area.Top;
  166. let xSteps = figure.coordinateAxis.axisX.steps;
  167. let ySteps = figure.coordinateAxis.axisY.steps;
  168. let xStepW = width / xSteps;
  169. let yStepH = height / ySteps;
  170. if (figure.coordinateAxis.axisX.isShowLine) {
  171. //画竖线
  172. for (let idx = 0; idx < xSteps; idx++) {
  173. rst.push("<line x1='" + Math.round(figure.area.Left + xStepW * (idx + 1) + this.offsetX) + "' y1='" + (figure.area.Bottom + this.offsetY) +
  174. "' x2='" + Math.round(figure.area.Left + xStepW * (idx + 1) + this.offsetX) + "' y2='" + (figure.area.Top + this.offsetY) +
  175. "' style='stroke:rgb(0,0,0);stroke-width:1'" + HtoVStr + "/>")
  176. }
  177. }
  178. if (figure.coordinateAxis.axisY.isShowLine) {
  179. //画横线
  180. for (let idx = 0; idx < ySteps; idx++) {
  181. rst.push("<line x1='" + (figure.area.Left + this.offsetX) + "' y1='" + Math.round(figure.area.Bottom - yStepH * (idx + 1) + this.offsetY) +
  182. "' x2='" + (figure.area.Right + this.offsetX) + "' y2='" + Math.round(figure.area.Bottom - yStepH * (idx + 1) + this.offsetY) +
  183. "' style='stroke:rgb(0,0,0);stroke-width:1'" + HtoVStr + "/>")
  184. }
  185. }
  186. let HtoVStr = "";
  187. if (isHtoV) {
  188. //引用了padding后,top坐标不用考虑offset了
  189. HtoVStr = ` transform="translate(${(actArea.Bottom - actArea.Top + 2)},0) rotate(90)"`;
  190. }
  191. for (let ctIdx = 0; ctIdx < figure.contents.length; ctIdx++) {
  192. const content = figure.contents[ctIdx];
  193. let pointStr = '';
  194. let stroke_dasharray = '';
  195. // ctx.strokeStyle = content.color;
  196. if (content.style === 'dash') stroke_dasharray = `10 5`;
  197. for (let idx = 0; idx < content.values.length; idx++) {
  198. let xPos = figure.area.Left + xStepW * idx + xStepW / 2 + this.offsetX;
  199. let yPos = figure.area.Bottom - (content.values[idx] / content.maxValue) * height + this.offsetY;
  200. if (idx > 0) pointStr = pointStr + ',';
  201. pointStr = pointStr + `${xPos},${yPos}`;
  202. switch (figure.config.dispType) {
  203. case 'line':
  204. //折线图
  205. if (idx === content.values.length - 1) {
  206. // ctx.moveTo(xPos, yPos);
  207. rst.push("<polyline points='" + pointStr +
  208. "' style='fill:none;stroke:" + content.color + ";stroke-width:1;stroke-dasharray:" + stroke_dasharray + "'" + HtoVStr + "/>")
  209. }
  210. break;
  211. case 'pole':
  212. //柱图
  213. break;
  214. default:
  215. //....
  216. break;
  217. }
  218. }
  219. }
  220. }
  221. return rst;
  222. },
  223. toSvgData: function(currentPageData, isHtoV, actArea) {
  224. //打印用
  225. let rst = [];
  226. if (currentPageData) {
  227. if (currentPageData.figure_cells && currentPageData.figure_cells.length > 0) {
  228. for (let figure of currentPageData.figure_cells) {
  229. rst = rst.concat(this._drawSvg(figure, isHtoV, actArea));
  230. }
  231. }
  232. }
  233. return rst;
  234. },
  235. toImageData: function(canvas, pageData) {
  236. //导出Excel用
  237. if (canvas && pageData) {
  238. const ctx = canvas.getContext("2d");
  239. for (let currentPageData of pageData.items) {
  240. if (currentPageData.figure_cells && currentPageData.figure_cells.length > 0) {
  241. for (let fIdx = 0; fIdx < currentPageData.figure_cells.length; fIdx++) {
  242. this._draw(ctx, currentPageData.figure_cells[fIdx], true);
  243. // if (fIdx === currentPageData.figure_cells.length - 1) {
  244. // this._draw(ctx, currentPageData.figure_cells[fIdx], true);
  245. // } else {
  246. // this._draw(ctx, currentPageData.figure_cells[fIdx], false);
  247. // }
  248. }
  249. }
  250. }
  251. }
  252. },
  253. getBase64: function(canvas, currentPageData) {
  254. let rst = null;
  255. if (canvas && currentPageData) {
  256. const ctx = canvas.getContext("2d");
  257. if (currentPageData.figure_cells && currentPageData.figure_cells.length > 0) {
  258. for (let fIdx = 0; fIdx < currentPageData.figure_cells.length - 1; fIdx++) {
  259. if (fIdx === currentPageData.figure_cells.length - 1) {
  260. canvas.width = figure.area.Right - figure.area.Left;
  261. canvas.height = figure.area.Bottom - figure.area.Top;
  262. ctx.putImageData(figure.imageData, 0, 0);
  263. rst = canvas.toDataURL();
  264. }
  265. }
  266. }
  267. }
  268. return rst;
  269. },
  270. };