大数据平台的小程序版本

ec-canvas.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import WxCanvas from './wx-canvas';
  2. import * as echarts from './echarts';
  3. let ctx;
  4. function compareVersion(v1, v2) {
  5. v1 = v1.split('.')
  6. v2 = v2.split('.')
  7. const len = Math.max(v1.length, v2.length)
  8. while (v1.length < len) {
  9. v1.push('0')
  10. }
  11. while (v2.length < len) {
  12. v2.push('0')
  13. }
  14. for (let i = 0; i < len; i++) {
  15. const num1 = parseInt(v1[i])
  16. const num2 = parseInt(v2[i])
  17. if (num1 > num2) {
  18. return 1
  19. } else if (num1 < num2) {
  20. return -1
  21. }
  22. }
  23. return 0
  24. }
  25. Component({
  26. properties: {
  27. canvasId: {
  28. type: String,
  29. value: 'ec-canvas'
  30. },
  31. ec: {
  32. type: Object
  33. },
  34. forceUseOldCanvas: {
  35. type: Boolean,
  36. value: false
  37. }
  38. },
  39. data: {
  40. isUseNewCanvas: false
  41. },
  42. ready: function () {
  43. if (!this.data.ec) {
  44. console.warn('组件需绑定 ec 变量,例:<ec-canvas id="mychart-dom-bar" '
  45. + 'canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>');
  46. return;
  47. }
  48. if (!this.data.ec.lazyLoad) {
  49. this.init();
  50. }
  51. },
  52. methods: {
  53. init: function (callback) {
  54. const version = wx.getSystemInfoSync().SDKVersion
  55. const canUseNewCanvas = compareVersion(version, '2.9.0') >= 0;
  56. const forceUseOldCanvas = this.data.forceUseOldCanvas;
  57. const isUseNewCanvas = canUseNewCanvas && !forceUseOldCanvas;
  58. this.setData({ isUseNewCanvas });
  59. if (forceUseOldCanvas && canUseNewCanvas) {
  60. console.warn('开发者强制使用旧canvas,建议关闭');
  61. }
  62. if (isUseNewCanvas) {
  63. // console.log('微信基础库版本大于2.9.0,开始使用<canvas type="2d"/>');
  64. // 2.9.0 可以使用 <canvas type="2d"></canvas>
  65. this.initByNewWay(callback);
  66. } else {
  67. const isValid = compareVersion(version, '1.9.91') >= 0
  68. if (!isValid) {
  69. console.error('微信基础库版本过低,需大于等于 1.9.91。'
  70. + '参见:https://github.com/ecomfe/echarts-for-weixin'
  71. + '#%E5%BE%AE%E4%BF%A1%E7%89%88%E6%9C%AC%E8%A6%81%E6%B1%82');
  72. return;
  73. } else {
  74. console.warn('建议将微信基础库调整大于等于2.9.0版本。升级后绘图将有更好性能');
  75. this.initByOldWay(callback);
  76. }
  77. }
  78. },
  79. initByOldWay(callback) {
  80. // 1.9.91 <= version < 2.9.0:原来的方式初始化
  81. ctx = wx.createCanvasContext(this.data.canvasId, this);
  82. const canvas = new WxCanvas(ctx, this.data.canvasId, false);
  83. echarts.setCanvasCreator(() => {
  84. return canvas;
  85. });
  86. // const canvasDpr = wx.getSystemInfoSync().pixelRatio // 微信旧的canvas不能传入dpr
  87. const canvasDpr = 1
  88. var query = wx.createSelectorQuery().in(this);
  89. query.select('.ec-canvas').boundingClientRect(res => {
  90. if (typeof callback === 'function') {
  91. this.chart = callback(canvas, res.width, res.height, canvasDpr);
  92. }
  93. else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
  94. this.chart = this.data.ec.onInit(canvas, res.width, res.height, canvasDpr);
  95. }
  96. else {
  97. this.triggerEvent('init', {
  98. canvas: canvas,
  99. width: res.width,
  100. height: res.height,
  101. canvasDpr: canvasDpr // 增加了dpr,可方便外面echarts.init
  102. });
  103. }
  104. }).exec();
  105. },
  106. initByNewWay(callback) {
  107. // version >= 2.9.0:使用新的方式初始化
  108. const query = wx.createSelectorQuery().in(this)
  109. query
  110. .select('.ec-canvas')
  111. .fields({ node: true, size: true })
  112. .exec(res => {
  113. const canvasNode = res[0].node
  114. this.canvasNode = canvasNode
  115. const canvasDpr = wx.getSystemInfoSync().pixelRatio
  116. const canvasWidth = res[0].width
  117. const canvasHeight = res[0].height
  118. const ctx = canvasNode.getContext('2d')
  119. const canvas = new WxCanvas(ctx, this.data.canvasId, true, canvasNode)
  120. echarts.setCanvasCreator(() => {
  121. return canvas
  122. })
  123. if (typeof callback === 'function') {
  124. this.chart = callback(canvas, canvasWidth, canvasHeight, canvasDpr)
  125. } else if (this.data.ec && typeof this.data.ec.onInit === 'function') {
  126. this.chart = this.data.ec.onInit(canvas, canvasWidth, canvasHeight, canvasDpr)
  127. } else {
  128. this.triggerEvent('init', {
  129. canvas: canvas,
  130. width: canvasWidth,
  131. height: canvasHeight,
  132. dpr: canvasDpr
  133. })
  134. }
  135. })
  136. },
  137. canvasToTempFilePath(opt) {
  138. if (this.data.isUseNewCanvas) {
  139. // 新版
  140. const query = wx.createSelectorQuery().in(this)
  141. query
  142. .select('.ec-canvas')
  143. .fields({ node: true, size: true })
  144. .exec(res => {
  145. const canvasNode = res[0].node
  146. opt.canvas = canvasNode
  147. wx.canvasToTempFilePath(opt)
  148. })
  149. } else {
  150. // 旧的
  151. if (!opt.canvasId) {
  152. opt.canvasId = this.data.canvasId;
  153. }
  154. ctx.draw(true, () => {
  155. wx.canvasToTempFilePath(opt, this);
  156. });
  157. }
  158. },
  159. touchStart(e) {
  160. if (this.chart && e.touches.length > 0) {
  161. var touch = e.touches[0];
  162. var handler = this.chart.getZr().handler;
  163. handler.dispatch('mousedown', {
  164. zrX: touch.x,
  165. zrY: touch.y
  166. });
  167. handler.dispatch('mousemove', {
  168. zrX: touch.x,
  169. zrY: touch.y
  170. });
  171. handler.processGesture(wrapTouch(e), 'start');
  172. }
  173. },
  174. touchMove(e) {
  175. if (this.chart && e.touches.length > 0) {
  176. var touch = e.touches[0];
  177. var handler = this.chart.getZr().handler;
  178. handler.dispatch('mousemove', {
  179. zrX: touch.x,
  180. zrY: touch.y
  181. });
  182. handler.processGesture(wrapTouch(e), 'change');
  183. }
  184. },
  185. touchEnd(e) {
  186. if (this.chart) {
  187. const touch = e.changedTouches ? e.changedTouches[0] : {};
  188. var handler = this.chart.getZr().handler;
  189. handler.dispatch('mouseup', {
  190. zrX: touch.x,
  191. zrY: touch.y
  192. });
  193. handler.dispatch('click', {
  194. zrX: touch.x,
  195. zrY: touch.y
  196. });
  197. handler.processGesture(wrapTouch(e), 'end');
  198. }
  199. }
  200. }
  201. });
  202. function wrapTouch(event) {
  203. for (let i = 0; i < event.touches.length; ++i) {
  204. const touch = event.touches[i];
  205. touch.offsetX = touch.x;
  206. touch.offsetY = touch.y;
  207. }
  208. return event;
  209. }