安装Echarts依赖
1.前提:使用vue-cli创建的项目
2.执行命令,默认安装最新版本的echarts
npm install echarts -S
3.在main.js中绑定echarts到vue的原型链上
import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts
4.在组件中引入即可
<template>
<div class="root">
<div class="chart" id="chart"></div>
</div>
</template>
<script>
export default {
name: "EchartsDemo",
mounted() {
this.initChart();
},
methods:{
initChart(){
// 基于准备好的dom,初始化echarts实例
const myChart = this.$echarts.init(document.getElementById('chart'));
// 绘制图表
myChart.setOption({
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
xAxis: {
data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
});
}
}
}
</script>
<style scoped lang="stylus">
.root
width 100%
height 100%
display flex
justify-content center
align-items center
width 500px
height 500px
</style>