rpt_figure.js 12 KB

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