第一步:安装echarts依赖
npm install echarts -S
第二步:在main.js中需要全局引入echarts
import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts
之前网上搜的import echarts from 'echarts'方式引用了会报TypeError: Cannot read property 'init' of undefined的错误
第三步:新建一个echarts.vue的文件(名字随意,不重要),页面设置一个div容器,js代码设置参数。
<template>
<div id="myChart" :style="{width: '300px', height: '300px'}"></div>
</template>
<script>
export default {
name: 'hello',
data () {
return {
msg: 'Welcome to Your Vue.js App'
}
},
mounted(){
this.drawLine();
},
methods: {
drawLine(){
// 基于准备好的dom,初始化echarts实例
let myChart = this.$echarts.init(document.getElementById('myChart'))
// 绘制图表
myChart.setOption({
title: { text: '在Vue中使用echarts' },
tooltip: {},
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
});
}
}
}
</script>
第四步:其他表格用法参照echarts官网吧!
END