echarts-uniapp.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <!-- #ifdef MP-WEIXIN || MP-TOUTIAO -->
  3. <canvas type="2d" class="echarts" :canvas-id="canvasId" :id="canvasId" @touchstart="touchStart"
  4. @touchmove="touchMove" @touchend="touchEnd" />
  5. <!-- #endif -->
  6. <!-- #ifndef MP-WEIXIN || MP-TOUTIAO -->
  7. <canvas class="echarts" :canvas-id="canvasId" :id="canvasId" @touchstart="touchStart" @touchmove="touchMove"
  8. @touchend="touchEnd" />
  9. <!-- #endif -->
  10. </template>
  11. <script>
  12. /**
  13. * echartsForUniApp echart兼容uni-app
  14. * @description echart兼容uni-app
  15. * @property {Object} option 图表数据
  16. * @property {String} canvasId 画布id
  17. * @example <echarts ref="echarts" :option="option" canvasId="echarts"></echarts>
  18. */
  19. // import WxCanvas from './wx-canvas.js';
  20. // import * as echarts from './echarts.min.js';
  21. // import * as echarts from 'echarts'
  22. var chartList = {}
  23. export default {
  24. // props: ["canvasId", "reference", "scoreList",'option'],
  25. props: {
  26. canvasId: {
  27. type: String,
  28. default: 'echarts'
  29. },
  30. option: {
  31. type: Object,
  32. default: () => {
  33. return {}
  34. }
  35. },
  36. },
  37. watch: {
  38. option: {
  39. immediate: true,
  40. handler(newValue, oldValue) {
  41. this.initChart(newValue)
  42. }
  43. }
  44. },
  45. data() {
  46. return {
  47. ctx: null
  48. }
  49. },
  50. mounted() {
  51. // Disable prograssive because drawImage doesn't support DOM as parameter
  52. // See https://developers.weixin.qq.com/miniprogram/dev/api/canvas/CanvasContext.drawImage.html
  53. echarts.registerPreprocessor(option => {
  54. if (option && option.series) {
  55. if (option.series.length > 0) {
  56. option.series.forEach(series => {
  57. series.progressive = 0;
  58. });
  59. } else if (typeof option.series === 'object') {
  60. option.series.progressive = 0;
  61. }
  62. }
  63. });
  64. },
  65. methods: {
  66. getCanvasAttr2d() {
  67. return new Promise((resolve, reject) => {
  68. const query = uni.createSelectorQuery().in(this)
  69. query
  70. .select('#' + this.canvasId)
  71. .fields({
  72. node: true,
  73. size: true
  74. })
  75. .exec(res => {
  76. const canvasNode = res[0].node
  77. this.canvasNode = canvasNode
  78. const canvasDpr = uni.getSystemInfoSync().pixelRatio
  79. const canvasWidth = res[0].width
  80. const canvasHeight = res[0].height
  81. this.ctx = canvasNode.getContext('2d')
  82. const canvas = new WxCanvas(this.ctx, this.canvasId, true, canvasNode)
  83. echarts.setCanvasCreator(() => {
  84. return canvas
  85. })
  86. resolve({
  87. canvas,
  88. canvasWidth,
  89. canvasHeight,
  90. canvasDpr
  91. })
  92. })
  93. });
  94. },
  95. getCanvasAttr() {
  96. return new Promise((resolve, reject) => {
  97. this.ctx = uni.createCanvasContext(this.canvasId, this);
  98. var canvas = new WxCanvas(this.ctx, this.canvasId, false);
  99. echarts.setCanvasCreator(() => {
  100. return canvas;
  101. });
  102. const canvasDpr = 1
  103. var query = uni.createSelectorQuery()
  104. // #ifndef MP-ALIPAY
  105. .in(this)
  106. // #endif
  107. query.select('#' + this.canvasId).boundingClientRect(res => {
  108. const canvasWidth = res.width
  109. const canvasHeight = res.height
  110. resolve({
  111. canvas,
  112. canvasWidth,
  113. canvasHeight,
  114. canvasDpr
  115. })
  116. }).exec();
  117. });
  118. },
  119. // #ifdef H5
  120. //H5绘制图表
  121. initChart(option) {
  122. console.log('查看option是否有值')
  123. console.log(option)
  124. let that = this;
  125. setTimeout(function() {
  126. that.ctx = uni.createCanvasContext(that.canvasId, that);
  127. chartList[that.canvasId] = echarts.init(document.getElementById(that.canvasId));
  128. chartList[that.canvasId].setOption(option ? option : that.option);
  129. }, 1000)
  130. },
  131. //H5生成图片
  132. canvasToTempFilePath(opt) {
  133. const base64 = chartList[this.canvasId].getDataURL()
  134. opt.success && opt.success({
  135. tempFilePath: base64
  136. })
  137. },
  138. // #endif
  139. // #ifndef H5
  140. //绘制图表
  141. async initChart(option) {
  142. // #ifdef MP-WEIXIN || MP-TOUTIAO
  143. const canvasAttr = await this.getCanvasAttr2d();
  144. // #endif
  145. // #ifndef MP-WEIXIN || MP-TOUTIAO
  146. const canvasAttr = await this.getCanvasAttr();
  147. // #endif
  148. const {
  149. canvas,
  150. canvasWidth,
  151. canvasHeight,
  152. canvasDpr
  153. } = canvasAttr
  154. chartList[this.canvasId] = echarts.init(canvas, null, {
  155. width: canvasWidth,
  156. height: canvasHeight,
  157. devicePixelRatio: canvasDpr // new
  158. });
  159. canvas.setChart(chartList[this.canvasId]);
  160. chartList[this.canvasId].setOption(option ? option : this.option);
  161. },
  162. //生成图片
  163. canvasToTempFilePath(opt) {
  164. // #ifdef MP-WEIXIN || MP-TOUTIAO
  165. var query = uni.createSelectorQuery()
  166. // #ifndef MP-ALIPAY
  167. .in(this)
  168. // #endif
  169. query.select('#' + this.canvasId).fields({
  170. node: true,
  171. size: true
  172. }).exec(res => {
  173. const canvasNode = res[0].node
  174. opt.canvas = canvasNode
  175. uni.canvasToTempFilePath(opt, this)
  176. })
  177. // #endif
  178. // #ifndef MP-WEIXIN || MP-TOUTIAO
  179. if (!opt.canvasId) {
  180. opt.canvasId = this.canvasId;
  181. }
  182. this.ctx.draw(true, () => {
  183. uni.canvasToTempFilePath(opt, this);
  184. });
  185. // #endif
  186. },
  187. // #endif
  188. touchStart(e) {
  189. if (chartList[this.canvasId] && e.touches.length > 0) {
  190. var touch = e.touches[0];
  191. var handler = chartList[this.canvasId].getZr().handler;
  192. handler.dispatch('mousedown', {
  193. zrX: touch.x,
  194. zrY: touch.y
  195. });
  196. handler.dispatch('mousemove', {
  197. zrX: touch.x,
  198. zrY: touch.y
  199. });
  200. handler.processGesture(wrapTouch(e), 'start');
  201. }
  202. },
  203. touchMove(e) {
  204. if (chartList[this.canvasId] && e.touches.length > 0) {
  205. var touch = e.touches[0];
  206. var handler = chartList[this.canvasId].getZr().handler;
  207. handler.dispatch('mousemove', {
  208. zrX: touch.x,
  209. zrY: touch.y
  210. });
  211. handler.processGesture(wrapTouch(e), 'change');
  212. }
  213. },
  214. touchEnd(e) {
  215. if (chartList[this.canvasId]) {
  216. const touch = e.changedTouches ? e.changedTouches[0] : {};
  217. var handler = chartList[this.canvasId].getZr().handler;
  218. handler.dispatch('mouseup', {
  219. zrX: touch.x,
  220. zrY: touch.y
  221. });
  222. handler.dispatch('click', {
  223. zrX: touch.x,
  224. zrY: touch.y
  225. });
  226. handler.processGesture(wrapTouch(e), 'end');
  227. }
  228. }
  229. }
  230. }
  231. function wrapTouch(event) {
  232. for (let i = 0; i < event.touches.length; ++i) {
  233. const touch = event.touches[i];
  234. touch.offsetX = touch.x;
  235. touch.offsetY = touch.y;
  236. }
  237. return event;
  238. }
  239. </script>
  240. <style lang="scss" scoped>
  241. .echarts {
  242. width: 100%;
  243. height: 100%;
  244. }
  245. </style>