ec-canvas.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import WxCanvas from './wx-canvas';
  2. import * as echarts from './echarts';
  3. let ctx;
  4. Component({
  5. properties: {
  6. canvasId: {
  7. type: String,
  8. value: 'ec-canvas'
  9. },
  10. ec: {
  11. type: Object
  12. },
  13. tuData:{//这是新增的参数
  14. type: Object
  15. },
  16. flag:{//这是新增的参数
  17. type: Object
  18. }
  19. },
  20. data: {
  21. },
  22. ready: function () {
  23. if (!this.data.ec) {
  24. console.warn('组件需绑定 ec 变量,例:<ec-canvas id="mychart-dom-bar" '
  25. + 'canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>');
  26. return;
  27. }
  28. if (!this.data.ec.lazyLoad) {
  29. this.init();
  30. }
  31. },
  32. methods: {
  33. init: function (callback) {
  34. const version = wx.version.version.split('.').map(n => parseInt(n, 10));
  35. const isValid = version[0] > 1 || (version[0] === 1 && version[1] > 9)
  36. || (version[0] === 1 && version[1] === 9 && version[2] >= 91);
  37. if (!isValid) {
  38. console.error('微信基础库版本过低,需大于等于 1.9.91。'
  39. + '参见:https://github.com/ecomfe/echarts-for-weixin'
  40. + '#%E5%BE%AE%E4%BF%A1%E7%89%88%E6%9C%AC%E8%A6%81%E6%B1%82');
  41. return;
  42. }
  43. ctx = wx.createCanvasContext(this.data.canvasId, this);
  44. const canvas = new WxCanvas(ctx, this.data.canvasId);
  45. echarts.setCanvasCreator(() => {
  46. return canvas;
  47. });
  48. var query = wx.createSelectorQuery().in(this);
  49. query.select('.ec-canvas').boundingClientRect(res => {
  50. if (typeof callback === 'function') {
  51. this.chart = callback(canvas, res.width, res.height);
  52. }
  53. else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
  54. this.chart = this.data.ec.onInit(canvas, res.width, res.height,this.data.tuData,this.data.flag);
  55. }
  56. else {
  57. this.triggerEvent('init', {
  58. canvas: canvas,
  59. width: res.width,
  60. height: res.height
  61. });
  62. }
  63. }).exec();
  64. },
  65. canvasToTempFilePath(opt) {
  66. if (!opt.canvasId) {
  67. opt.canvasId = this.data.canvasId;
  68. }
  69. ctx.draw(true, () => {
  70. wx.canvasToTempFilePath(opt, this);
  71. });
  72. },
  73. touchStart(e) {
  74. if (this.chart && e.touches.length > 0) {
  75. var touch = e.touches[0];
  76. var handler = this.chart.getZr().handler;
  77. handler.dispatch('mousedown', {
  78. zrX: touch.x,
  79. zrY: touch.y
  80. });
  81. handler.dispatch('mousemove', {
  82. zrX: touch.x,
  83. zrY: touch.y
  84. });
  85. handler.processGesture(wrapTouch(e), 'start');
  86. }
  87. },
  88. touchMove(e) {
  89. if (this.chart && e.touches.length > 0) {
  90. var touch = e.touches[0];
  91. var handler = this.chart.getZr().handler;
  92. handler.dispatch('mousemove', {
  93. zrX: touch.x,
  94. zrY: touch.y
  95. });
  96. handler.processGesture(wrapTouch(e), 'change');
  97. }
  98. },
  99. touchEnd(e) {
  100. if (this.chart) {
  101. const touch = e.changedTouches ? e.changedTouches[0] : {};
  102. var handler = this.chart.getZr().handler;
  103. handler.dispatch('mouseup', {
  104. zrX: touch.x,
  105. zrY: touch.y
  106. });
  107. handler.dispatch('click', {
  108. zrX: touch.x,
  109. zrY: touch.y
  110. });
  111. handler.processGesture(wrapTouch(e), 'end');
  112. }
  113. }
  114. }
  115. });
  116. function wrapTouch(event) {
  117. for (let i = 0; i < event.touches.length; ++i) {
  118. const touch = event.touches[i];
  119. touch.offsetX = touch.x;
  120. touch.offsetY = touch.y;
  121. }
  122. return event;
  123. }