echarts.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import WxCanvas from './wx-canvas';
  2. import * as echarts from './wx-echarts';
  3. const darkTheme = require('./dark');
  4. let ctx;
  5. Component({
  6. properties: {
  7. canvasId: {
  8. type: String,
  9. value: 'ec-canvas'
  10. },
  11. data: {
  12. type: Object,
  13. value: {}
  14. },
  15. ec: {
  16. type: Object
  17. }
  18. },
  19. data: {
  20. size: {
  21. height: 240
  22. }
  23. },
  24. ready: function () {
  25. if (!this.data.ec) {
  26. console.warn('组件需绑定 ec 变量,例:<ec-canvas id="mychart-dom-bar" ' +
  27. 'canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>');
  28. return;
  29. }
  30. if (!this.data.ec.lazyLoad) {
  31. this.init();
  32. }
  33. },
  34. lifetimes: {
  35. attached: function () {
  36. const _ts = this;
  37. let dataAttr = this.data.data.attrs,
  38. obj = JSON.parse(decodeURIComponent(dataAttr.value));
  39. obj.option.color = ['#60acfc', '#32d3eb', '#5bc49f', '#feb64d', '#ff7c7c', '#9287e7'];
  40. if (obj.height) {
  41. _ts.setData({
  42. size: {
  43. height: obj.height
  44. }
  45. })
  46. }
  47. _ts.data.ec = {};
  48. _ts.data.ec.onInit = function (canvas, width, height) {
  49. echarts.registerTheme('dark', darkTheme);
  50. const theme = global._theme === 'dark' ? 'dark' : null,
  51. chart = echarts.init(canvas, theme, {
  52. width: width,
  53. height: height
  54. });
  55. canvas.setChart(chart);
  56. chart.setOption(obj.option);
  57. return chart;
  58. };
  59. },
  60. moved: function () {},
  61. detached: () => {
  62. },
  63. },
  64. methods: {
  65. init: function (callback) {
  66. const version = wx.version.version.split('.').map(n => parseInt(n, 10));
  67. const isValid = version[0] > 1 || (version[0] === 1 && version[1] > 9) ||
  68. (version[0] === 1 && version[1] === 9 && version[2] >= 91);
  69. if (!isValid) {
  70. console.error('微信基础库版本过低,需大于等于 1.9.91。' +
  71. '参见:https://github.com/ecomfe/echarts-for-weixin' +
  72. '#%E5%BE%AE%E4%BF%A1%E7%89%88%E6%9C%AC%E8%A6%81%E6%B1%82');
  73. return;
  74. }
  75. ctx = wx.createCanvasContext(this.data.canvasId, this);
  76. const canvas = new WxCanvas(ctx, this.data.canvasId);
  77. echarts.setCanvasCreator(() => {
  78. return canvas;
  79. });
  80. var query = wx.createSelectorQuery().in(this);
  81. query.select('.ec-canvas').boundingClientRect(res => {
  82. if (typeof callback === 'function') {
  83. this.chart = callback(canvas, res.width, res.height);
  84. } else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
  85. this.chart = this.data.ec.onInit(canvas, res.width, res.height);
  86. } else {
  87. this.triggerEvent('init', {
  88. canvas: canvas,
  89. width: res.width,
  90. height: res.height
  91. });
  92. }
  93. }).exec();
  94. },
  95. canvasToTempFilePath(opt) {
  96. if (!opt.canvasId) {
  97. opt.canvasId = this.data.canvasId;
  98. }
  99. ctx.draw(true, () => {
  100. wx.canvasToTempFilePath(opt, this);
  101. });
  102. },
  103. touchStart(e) {
  104. if (this.chart && e.touches.length > 0) {
  105. var touch = e.touches[0];
  106. var handler = this.chart.getZr().handler;
  107. handler.dispatch('mousedown', {
  108. zrX: touch.x,
  109. zrY: touch.y
  110. });
  111. handler.dispatch('mousemove', {
  112. zrX: touch.x,
  113. zrY: touch.y
  114. });
  115. handler.processGesture(wrapTouch(e), 'start');
  116. }
  117. },
  118. touchMove(e) {
  119. if (this.chart && e.touches.length > 0) {
  120. var touch = e.touches[0];
  121. var handler = this.chart.getZr().handler;
  122. handler.dispatch('mousemove', {
  123. zrX: touch.x,
  124. zrY: touch.y
  125. });
  126. handler.processGesture(wrapTouch(e), 'change');
  127. }
  128. },
  129. touchEnd(e) {
  130. if (this.chart) {
  131. const touch = e.changedTouches ? e.changedTouches[0] : {};
  132. var handler = this.chart.getZr().handler;
  133. handler.dispatch('mouseup', {
  134. zrX: touch.x,
  135. zrY: touch.y
  136. });
  137. handler.dispatch('click', {
  138. zrX: touch.x,
  139. zrY: touch.y
  140. });
  141. handler.processGesture(wrapTouch(e), 'end');
  142. }
  143. }
  144. }
  145. });
  146. function wrapTouch(event) {
  147. for (let i = 0; i < event.touches.length; ++i) {
  148. const touch = event.touches[i];
  149. touch.offsetX = touch.x;
  150. touch.offsetY = touch.y;
  151. }
  152. return event;
  153. }