一:先安装echarts
npm install echarts --save
注:echarts版本下载 4.9.0
vue 2.5.2不支持echarts最新版本会报错
二:在main.js中全局引入
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
三:创建图表
<template>
<div id="app">
<div id="main" style="width:600px;height:400px;"></div>
</div>
</template>
<script>
export default {
name: "app",
methods: {
drawChart() {
// 基于准备好的dom,初始化echarts实例
let myChart = this.$echarts.init(document.getElementById("main"));
// 指定图表的配置项和数据
let option = {
//设置图标的标题
title: {
//注标题
text: "ECharts 入门示例"
},
tooltip: {},
//图例
legend: {
data: ["销量","成本"]
},
//x轴数据
xAxis: {
data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
},
//y轴数据
yAxis: {},
//根据相应的系列进行配置
series: [
{
name: "销量",
type: "bar",
data: [5, 20, 36, 10, 10, 20]
},
{
name: "成本",
type: "bar",
data: [0.5, 2, 3.6, 1, 1, 2]
}
]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
}
},
mounted() {
this.drawChart();
}
};
</script>