• uni-app微信小程序使用ECharts


    1. 引入 npm install echarts mpvue-echarts
    2. ECharts 在线构建 定制 echarts 的 js 文件
    3. 新建 common 文件夹,将定制文件放在 common 下
    4. 将 node_modules 下 mpvue-echarts 中 src 文件夹复制到 components 文件夹下
    5. 页面绘制
    <view class="echarts-wrap">
    	 <my-echarts id="mychart-dom-bar" canvas-id="mychart-bar" :echarts="echarts" :onInit="initChart"></my-echarts>
    </view>
    
    • 1
    • 2
    • 3
    import * as echarts from '@/common/echarts.min.js';
    import myEcharts from '@/components/src/echarts.vue';
    export default {
    	components:{myEcharts},
    	data() {
    		return {echarts}
    	},
    	methods: {
    		initChart(canvas, width, height) {
    			let chart = null
    				chart = echarts.init(canvas, null, {
    					width: width,
    					height: height
    				});
    				var option = {...}
    				chart.setOption(option);
    		 		return chart;
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    .echarts-wrap{
    	width: 100%;
    	height:500px;
    }
    
    • 1
    • 2
    • 3
    • 4
    1. 遇到 this.echarts.setCanvasCreator is not a function 报错在 components 下 src 中找到 echarts.vue文件
      引入 import * as echarts from ‘@/common/echarts.min.js’;
      注销掉 props 中 echarts 对象
      找到 this.ctx 和 const query ,给其添加 this 参数
    this.ctx = wx.createCanvasContext(canvasId,this);
    const query = wx.createSelectorQuery().in(this);
    
    • 1
    • 2
    1. 遇到 t.addEventListener is not a function 报错在 common 中找到 echarts.min.js文件
      全文搜索 use strict 在下面增加语句var isDomLevel2 = (typeof window !== ‘undefined’) && !!window.addEventListener
      全文搜索 function Pe(t, e, n, i) 和 function Le(t, e, n, i) 进行修改增加判断,(注意是参数中带有i的,Le 与 Pe 函数里内容可能颠倒,在原有方法上增加对应新判断即可)
    		function Pe(t, e, n, i) {
    			if(isDomLevel2){
    				t.addEventListener(e, n, i)
    			}else{
    				t.attachEvent('on' + e, n)
    			}
    		}
    		function Le(t, e, n, i) {
    			if(isDomLevel2){
    				t.removeEventListener(e, n, i)
    			}else{
    				t.detachEvent('on' + e, n)
    			}
    		}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    8.全文搜索 preventDefault() 修改当前的方法函数增加判断(函数名可能不叫 t_,不影响)

    t_ = function(t) {
    		if(isDomLevel2 ){
    			t.preventDefault(), t.stopPropagation(), t.cancelBubble = !0
    		}else{
    			t.returnValue = false
    			t.cancelBubble = !0
    		}
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    Hyperledger Fabric部署与测试(Ubuntu)
    Flask 入门
    【平面设计作品】以设计触碰心灵,当教育变得遥不可及……
    [附源码]计算机毕业设计springboot基于Java的员工管理系统
    cgroups v1简介
    Keil实现Flash升级跳转(STM32/GD32/HC32)
    Excel中将单元格格式改成文本后,为何要双击数字才会改变?
    不同的二叉搜索树【动态规划】
    模拟赛题解9.13
    10.2servlet基础2
  • 原文地址:https://blog.csdn.net/Hjboke/article/details/126172736