1、下載所需要的庫(kù)
終端輸入 npm install echarts mpvue-echarts --save
將node_modules中的mpvue-echarts文件夾放入components目錄下,只保留其中的src文件夾
2、然后在https://echarts.apache.org/zh/builder.html定制echarts的js文件,然后下載诬辈,放到common目錄下
3、以折線圖為例
<template>
<view>
<view class="wrap">
<mpvue-echarts :onInit="initChart" />
</view>
<view @click="drawAgain">重新繪制</view>
<view @click="drawAgain1">重新繪制2</view>
</view>
</template>
<script>
import * as echarts from '@/common/echarts.min'; /*chart.min.js為在線定制*/
import mpvueEcharts from '@/components/mpvue-echarts/src/echarts.vue';
let chart = null;
export default {
data() {
return {
option: {
backgroundColor: '#fff',
color: ['#ec562e'],
tooltip: {
trigger: 'axis'
},
xAxis: {
type: 'category',
data: ['周一', '周二', '周三']
},
yAxis: {
x: 'center',
type: 'value',
splitLine: {
lineStyle: {
color:'#999999',
type: 'dashed'
}
}
},
series: [
{
name: '價(jià)格',
type: 'line',
smooth: true,
data: [70,50,60],
}
]
}
};
},
methods: {
initChart(canvas, width, height) {
chart = echarts.init(canvas, null, {
width: width,
height: height
});
canvas.setChart(chart);
return chart;
},
drawAgain() {
this.$set(this.option.xAxis, 'data', ['周一', '周二', '周三']);
this.$set(this.option.series[0], 'data', [78, 40, 33]);
chart.setOption(this.option);
},
drawAgain1() {
this.$set(this.option.xAxis, 'data', ['周一', '周二', '周三', '周4']);
this.$set(this.option.series[0], 'data', [78, 40, 33, 50]);
chart.setOption(this.option);
}
},
components: {
mpvueEcharts
}
};
</script>
<style>
.wrap {
width: 100%;
height: 300px;
}
</style>
運(yùn)行時(shí)提示this.echarts.setCanvasCreator is not a function的錯(cuò)誤
解決方法:
1、找到components--》mpvue-echarts--》src--》echarts.vue
導(dǎo)入import * as echarts from '@/common/echarts.min'; /echarts.min.js為在線定制/
2磕洪、注釋props中的echarts,如下
export default {
props: {
// echarts: {
// required: true,
// type: Object,
// default() {
// return null;
// },
// },
onInit: {
type: Function,
default: null,
},
canvasId: {
type: String,
default: 'ec-canvas',
},
lazyLoad: {
type: Boolean,
default: false,
},
disableTouch: {
type: Boolean,
default: false,
},
throttleTouch: {
type: Boolean,
default: false,
},
},
3诫龙、在 onReady方法中添加 this.echarts = echarts
onReady() {
this.echarts = echarts //新增
if (!this.echarts) {
console.warn('組件需綁定 echarts 變量析显,例:<ec-canvas id="mychart-dom-bar" '
+ 'canvas-id="mychart-bar" :echarts="echarts"></ec-canvas>');
return;
}
if (!this.lazyLoad) this.init();
},
4、init方法中修改以下3處
const canvasId = this.canvasId; //------修改------
this.ctx = wx.createCanvasContext(canvasId,this);//------修改------
const query = wx.createSelectorQuery().in(this); //------修改------
init(callback) {
const version = wx.version.version.split('.').map(n => parseInt(n, 10));
const isValid = version[0] > 1 || (version[0] === 1 && version[1] > 9)
|| (version[0] === 1 && version[1] === 9 && version[2] >= 91);
if (!isValid) {
console.error('微信基礎(chǔ)庫(kù)版本過(guò)低签赃,需大于等于 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;
}
const canvasId = this.canvasId; //------***修改***------
this.ctx = wx.createCanvasContext(canvasId,this);//------***修改***------
const canvas = new WxCanvas(this.ctx, canvasId);
this.echarts.setCanvasCreator(() => canvas);
const query = wx.createSelectorQuery().in(this); //------***修改***------
query.select(`#${canvasId}`).boundingClientRect((res) => {
if (!res) {
setTimeout(() => this.init(), 50);
return;
}
const { width, height } = res;
if (typeof callback === 'function') {
this.chart = callback(canvas, width, height);
} else if (typeof this.onInit === 'function') {
this.chart = this.onInit(canvas, width, height);
}
if (!this.chart) {
return;
}
const { handler } = this.chart.getZr();
this.handler = handler;
this.processGesture = handler.proxy.processGesture || (() => {});
}).exec();
},
參考文章:
uniapp在微信小程序中使用 ECharts
https://blog.csdn.net/wxh958548129/article/details/107520566
uni-app使用mpvue-echarts報(bào) 的解決辦法
https://ask.dcloud.net.cn/article/36652