og

来实现一个uniapp小程序echarts插件

第一步:先从echarts官网下一份echarts.js

官网下的echarts.js,默认是包含所有功能的echarts。如有可以,建议从官网自定义构建来替换这个文件以减小文件大小。

第二步:创建自定义微信小程序canvas类

不多说,直接上代码,我们创建一个文件WxCanvas.js

        
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
export default class WxCanvas { constructor(ctx, canvasId, isNew, canvasNode) { this.ctx = ctx; this.canvasId = canvasId; this.chart = null; this.isNew = isNew if (isNew) { this.canvasNode = canvasNode; } else { this._initStyle(ctx); } // this._initCanvas(zrender, ctx); this._initEvent(); } getContext(contextType) { if (contextType === '2d') { return this.ctx; } } // canvasToTempFilePath(opt) { // if (!opt.canvasId) { // opt.canvasId = this.canvasId; // } // return wx.canvasToTempFilePath(opt, this); // } setChart(chart) { this.chart = chart; } attachEvent() { // noop } detachEvent() { // noop } _initCanvas(zrender, ctx) { zrender.util.getContext = function () { return ctx; }; zrender.util.$override('measureText', function (text, font) { ctx.font = font || '12px sans-serif'; return ctx.measureText(text); }); } _initStyle(ctx) { var styles = ['fillStyle', 'strokeStyle', 'globalAlpha', 'textAlign', 'textBaseAlign', 'shadow', 'lineWidth', 'lineCap', 'lineJoin', 'lineDash', 'miterLimit', 'fontSize' ]; styles.forEach(style => { Object.defineProperty(ctx, style, { set: value => { if (style !== 'fillStyle' && style !== 'strokeStyle' || value !== 'none' && value !== null ) { ctx['set' + style.charAt(0).toUpperCase() + style.slice(1)](value); } } }); }); ctx.createRadialGradient = () => { return ctx.createCircularGradient(arguments); }; } _initEvent() { this.event = {}; const eventNames = [{ wxName: 'touchStart', ecName: 'mousedown' }, { wxName: 'touchMove', ecName: 'mousemove' }, { wxName: 'touchEnd', ecName: 'mouseup' }, { wxName: 'touchEnd', ecName: 'click' }]; eventNames.forEach(name => { this.event[name.wxName] = e => { const touch = e.touches[0]; this.chart.getZr().handler.dispatch(name.ecName, { zrX: name.wxName === 'tap' ? touch.clientX : touch.x, zrY: name.wxName === 'tap' ? touch.clientY : touch.y }); }; }); } set width(w) { if (this.canvasNode) this.canvasNode.width = w } set height(h) { if (this.canvasNode) this.canvasNode.height = h } get width() { if (this.canvasNode) return this.canvasNode.width return 0 } get height() { if (this.canvasNode) return this.canvasNode.height return 0 } }

Article created at   2020/7/29 PM  in category  code ,   264  Views

Related tags: vue js

Article Address: https://www.hooyim.com/article/4