UNPKG

qf-taro-echarts

Version:

为易观千帆taro项目开发,适用于Taro项目的ECharts图表组件

311 lines (281 loc) 9.14 kB
import WxCanvas from './wx-canvas'; import * as echarts from './echarts'; let ctx; let _mys = ((type) => { switch (type) { case 'weapp': return wx break; case 'alipay': return my break; case 'tt': return tt break; } })(process.env.TARO_ENV) function compareVersion(v1, v2) { v1 = v1.split('.') v2 = v2.split('.') const len = Math.max(v1.length, v2.length) while (v1.length < len) { v1.push('0') } while (v2.length < len) { v2.push('0') } for (let i = 0; i < len; i++) { const num1 = parseInt(v1[i]) const num2 = parseInt(v2[i]) if (num1 > num2) { return 1 } else if (num1 < num2) { return -1 } } return 0 } Component({ properties: { canvasId: { type: String, value: 'ec-canvas' }, echeight: { type: String, value: '200px' }, ec: { type: Object }, forceUseOldCanvas: { type: Boolean, value: false } }, data: { isUseNewCanvas: true, isEchartLoading: true, // 初始化渲染使用 startY: 0, endY: 0 }, ready: function () { if (!this.data.ec) { console.warn('组件需绑定 ec 变量,例:<ec-canvas id="mychart-dom-bar" ' + 'canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>'); return; } if (!this.data.ec.lazyLoad) { console.warn('i am ready') this.init(); } }, detached: function () { console.warn('i am 销毁时------->>>>>>>') this.setData({ isEchartLoading: true }) }, methods: { init: function (callback) { const version = _mys.getSystemInfoSync().SDKVersion const canUseNewCanvas = compareVersion(version, '2.9.0') >= 0; console.warn('canUseNewCanvas=-====2.9.0======>',canUseNewCanvas,'当前版本:',version) const forceUseOldCanvas = this.data.forceUseOldCanvas; const isUseNewCanvas = canUseNewCanvas && !forceUseOldCanvas; this.setData({ isUseNewCanvas }); if (forceUseOldCanvas && canUseNewCanvas) { console.warn('开发者强制使用旧canvas,建议关闭'); } console.warn('callback', callback) if (process.env.TARO_ENV === 'tt') { this.setData({ isUseNewCanvas: false }); this.initByOldWay(callback); return; } if (isUseNewCanvas) { // console.log('微信基础库版本大于2.9.0,开始使用<canvas type="2d"/>'); // 2.9.0 可以使用 <canvas type="2d"></canvas> this.initByNewWay(callback); } else { const isValid = compareVersion(version, '1.9.91') >= 0 if (!isValid) { console.error('微信基础库版本过低,需大于等于 1.9.91。' + '参见:https://github.com/ecomfe/echarts-for-weixin' + '#%E5%BE%AE%E4%BF%A1%E7%89%88%E6%9C%AC%E8%A6%81%E6%B1%82'); return; } else { console.warn('建议将微信基础库调整大于等于2.9.0版本。升级后绘图将有更好性能'); this.initByOldWay(callback); } } }, initByOldWay(callback) { // 1.9.91 <= version < 2.9.0:原来的方式初始化 ctx = _mys.createCanvasContext(this.data.canvasId, this); const canvas = new WxCanvas(ctx, this.data.canvasId, false); // console.warn('ctx==150== componentWillMount ==>', ctx, this.data.canvasId, callback, _mys.createSelectorQuery().in(this)) echarts.setCanvasCreator(() => { return canvas; }); const canvasDpr = _mys.getSystemInfoSync().pixelRatio // 微信旧的canvas不能传入dpr // const canvasDpr = 1 var query = _mys.createSelectorQuery().in(this); // SelectorQuery不到时抛出 if (!query._component) { return } query.select('.ec-canvas').boundingClientRect(res => { console.warn('res========>',res) if (typeof callback === 'function') { this.setData({ isEchartLoading: false }, () => { // console.warn('res========>1',res) this.chart = callback(canvas, res.width, res.height, canvasDpr); }); } else if (this.data.ec && typeof this.data.ec.onInit === 'function') { this.setData({ isEchartLoading: false }, () => { // console.warn('res========>2',res) this.chart = this.data.ec.onInit(canvas, res.width, res.height, canvasDpr); }); } else { this.setData({ isEchartLoading: false }, () => { // console.warn('res========>3',res) this.triggerEvent('init', { canvas: canvas, width: res.width, height: res.height, canvasDpr: canvasDpr // 增加了dpr,可方便外面echarts.init }); }); } }).exec(); }, initByNewWay(callback) { // version >= 2.9.0:使用新的方式初始化 const query = _mys.createSelectorQuery().in(this) query .select('.ec-canvas') .fields({ node: true, size: true }) .exec(res => { console.warn('res======>',res) const canvasNode = res[0].node this.canvasNode = canvasNode const canvasDpr = _mys.getSystemInfoSync().pixelRatio const canvasWidth = res[0].width const canvasHeight = res[0].height const ctx = canvasNode.getContext('2d') const canvas = new WxCanvas(ctx, this.data.canvasId, true, canvasNode) echarts.setCanvasCreator(() => { return canvas }) if (typeof callback === 'function') { this.setData({ isEchartLoading: false }, () => { this.chart = callback(canvas, canvasWidth, canvasHeight, canvasDpr) }); } else if (this.data.ec && typeof this.data.ec.onInit === 'function') { this.setData({ isEchartLoading: false }, () => { this.chart = this.data.ec.onInit(canvas, canvasWidth, canvasHeight, canvasDpr) }); } else { this.setData({ isEchartLoading: false }, () => { this.triggerEvent('init', { canvas: canvas, width: canvasWidth, height: canvasHeight, dpr: canvasDpr }) }); } }) }, errorCanvas: function (callback) { console.warn('errorCanvas======>',callback) }, canvasToTempFilePath(opt) { if (this.data.isUseNewCanvas) { // 新版 const query = _mys.createSelectorQuery().in(this) query .select('.ec-canvas') .fields({ node: true, size: true }) .exec(res => { const canvasNode = res[0].node opt.canvas = canvasNode _mys.canvasToTempFilePath(opt) }) } else { // 旧的 if (!opt.canvasId) { opt.canvasId = this.data.canvasId; } ctx.draw(true, () => { _mys.canvasToTempFilePath(opt, this); }); } }, touchStart(e) { if (this.chart && e.touches.length > 0) { var touch = e.touches[0]; var handler = this.chart.getZr().handler; console.warn('touchStart--------startY', this.data.startY) // 滑动起点的坐标 this.setData({ startY: touch.pageY, endY: 0 }) handler.dispatch('mousedown', { zrX: touch.x, zrY: touch.y }); handler.dispatch('mousemove', { zrX: touch.x, zrY: touch.y }); handler.processGesture(wrapTouch(e), 'start'); } }, touchMove(e) { if (this.chart && e.touches.length > 0) { var touch = e.touches[0]; var handler = this.chart.getZr().handler; // 滑动距离计算 var distanceY = touch.pageY - this.data.startY // console.warn('touchMove--------distanceY', distanceY) // 记录滑动结束点 this.setData({ endY: touch.pageY }) // 有滑动距离就干掉 if (distanceY !== 0) { return; } handler.dispatch('mousemove', { zrX: touch.x, zrY: touch.y }); handler.processGesture(wrapTouch(e), 'change'); } }, touchEnd(e) { if (this.chart) { const touch = e.changedTouches ? e.changedTouches[0] : {}; var handler = this.chart.getZr().handler; // 滑动距离计算 var distanceY = this.data.endY - this.data.startY console.warn('touchEnd--------distanceY', distanceY) // 记录滑动结束点 this.setData({ startY: 0, endY: 0 }) handler.dispatch('mouseup', { zrX: touch.x, zrY: touch.y }); handler.dispatch('click', { zrX: touch.x, zrY: touch.y }); handler.processGesture(wrapTouch(e), 'end'); } } } }); function wrapTouch(event) { for (let i = 0; i < event.touches.length; ++i) { const touch = event.touches[i]; touch.offsetX = touch.x; touch.offsetY = touch.y; } return event; }