echarts-uniapp.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. var chartList = {};
  4. const _sfc_main = {
  5. // props: ["canvasId", "reference", "scoreList",'option'],
  6. props: {
  7. canvasId: {
  8. type: String,
  9. default: "echarts"
  10. },
  11. option: {
  12. type: Object,
  13. default: () => {
  14. return {};
  15. }
  16. }
  17. },
  18. watch: {
  19. option: {
  20. immediate: true,
  21. handler(newValue, oldValue) {
  22. this.initChart(newValue);
  23. }
  24. }
  25. },
  26. data() {
  27. return {
  28. ctx: null
  29. };
  30. },
  31. mounted() {
  32. echarts.registerPreprocessor((option) => {
  33. if (option && option.series) {
  34. if (option.series.length > 0) {
  35. option.series.forEach((series) => {
  36. series.progressive = 0;
  37. });
  38. } else if (typeof option.series === "object") {
  39. option.series.progressive = 0;
  40. }
  41. }
  42. });
  43. },
  44. methods: {
  45. getCanvasAttr2d() {
  46. return new Promise((resolve, reject) => {
  47. const query = common_vendor.index.createSelectorQuery().in(this);
  48. query.select("#" + this.canvasId).fields({
  49. node: true,
  50. size: true
  51. }).exec((res) => {
  52. const canvasNode = res[0].node;
  53. this.canvasNode = canvasNode;
  54. const canvasDpr = common_vendor.index.getSystemInfoSync().pixelRatio;
  55. const canvasWidth = res[0].width;
  56. const canvasHeight = res[0].height;
  57. this.ctx = canvasNode.getContext("2d");
  58. const canvas = new WxCanvas(this.ctx, this.canvasId, true, canvasNode);
  59. echarts.setCanvasCreator(() => {
  60. return canvas;
  61. });
  62. resolve({
  63. canvas,
  64. canvasWidth,
  65. canvasHeight,
  66. canvasDpr
  67. });
  68. });
  69. });
  70. },
  71. getCanvasAttr() {
  72. return new Promise((resolve, reject) => {
  73. this.ctx = common_vendor.index.createCanvasContext(this.canvasId, this);
  74. var canvas = new WxCanvas(this.ctx, this.canvasId, false);
  75. echarts.setCanvasCreator(() => {
  76. return canvas;
  77. });
  78. const canvasDpr = 1;
  79. var query = common_vendor.index.createSelectorQuery().in(this);
  80. query.select("#" + this.canvasId).boundingClientRect((res) => {
  81. const canvasWidth = res.width;
  82. const canvasHeight = res.height;
  83. resolve({
  84. canvas,
  85. canvasWidth,
  86. canvasHeight,
  87. canvasDpr
  88. });
  89. }).exec();
  90. });
  91. },
  92. //绘制图表
  93. async initChart(option) {
  94. const canvasAttr = await this.getCanvasAttr2d();
  95. const {
  96. canvas,
  97. canvasWidth,
  98. canvasHeight,
  99. canvasDpr
  100. } = canvasAttr;
  101. chartList[this.canvasId] = echarts.init(canvas, null, {
  102. width: canvasWidth,
  103. height: canvasHeight,
  104. devicePixelRatio: canvasDpr
  105. // new
  106. });
  107. canvas.setChart(chartList[this.canvasId]);
  108. chartList[this.canvasId].setOption(option ? option : this.option);
  109. },
  110. //生成图片
  111. canvasToTempFilePath(opt) {
  112. var query = common_vendor.index.createSelectorQuery().in(this);
  113. query.select("#" + this.canvasId).fields({
  114. node: true,
  115. size: true
  116. }).exec((res) => {
  117. const canvasNode = res[0].node;
  118. opt.canvas = canvasNode;
  119. common_vendor.index.canvasToTempFilePath(opt, this);
  120. });
  121. },
  122. touchStart(e) {
  123. if (chartList[this.canvasId] && e.touches.length > 0) {
  124. var touch = e.touches[0];
  125. var handler = chartList[this.canvasId].getZr().handler;
  126. handler.dispatch("mousedown", {
  127. zrX: touch.x,
  128. zrY: touch.y
  129. });
  130. handler.dispatch("mousemove", {
  131. zrX: touch.x,
  132. zrY: touch.y
  133. });
  134. handler.processGesture(wrapTouch(e), "start");
  135. }
  136. },
  137. touchMove(e) {
  138. if (chartList[this.canvasId] && e.touches.length > 0) {
  139. var touch = e.touches[0];
  140. var handler = chartList[this.canvasId].getZr().handler;
  141. handler.dispatch("mousemove", {
  142. zrX: touch.x,
  143. zrY: touch.y
  144. });
  145. handler.processGesture(wrapTouch(e), "change");
  146. }
  147. },
  148. touchEnd(e) {
  149. if (chartList[this.canvasId]) {
  150. const touch = e.changedTouches ? e.changedTouches[0] : {};
  151. var handler = chartList[this.canvasId].getZr().handler;
  152. handler.dispatch("mouseup", {
  153. zrX: touch.x,
  154. zrY: touch.y
  155. });
  156. handler.dispatch("click", {
  157. zrX: touch.x,
  158. zrY: touch.y
  159. });
  160. handler.processGesture(wrapTouch(e), "end");
  161. }
  162. }
  163. }
  164. };
  165. function wrapTouch(event) {
  166. for (let i = 0; i < event.touches.length; ++i) {
  167. const touch = event.touches[i];
  168. touch.offsetX = touch.x;
  169. touch.offsetY = touch.y;
  170. }
  171. return event;
  172. }
  173. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  174. return {
  175. a: $props.canvasId,
  176. b: $props.canvasId,
  177. c: common_vendor.o((...args) => $options.touchStart && $options.touchStart(...args)),
  178. d: common_vendor.o((...args) => $options.touchMove && $options.touchMove(...args)),
  179. e: common_vendor.o((...args) => $options.touchEnd && $options.touchEnd(...args))
  180. };
  181. }
  182. const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-ed5954d6"], ["__file", "E:/psy_web_share/components/echarts-uniapp/echarts-uniapp.vue"]]);
  183. tt.createComponent(Component);