前言:echarts.js是百度的开源图表库,可以绘制各种类型的图表,那么在vue中如何使用echarts呢?
一、安装echarts.js
npm install echarts --save
二、在main.js中引入
import echarts from "echarts";
Vue.prototype.$echarts = echarts;
三、试着写个demo
在template里面写
<template>
<div id="app">
<div id="main1" style="width: 600px;height:400px;"></div>
</div>
</template>
在methods里写配置
methods: {
drawChart() {
// 基于准备好的dom,初始化echarts实例
let myChart = this.$echarts.init(document.getElementById("main"));
// 指定图表的配置项和数据
let option = {
title: {
text: "ECharts 入门示例"
},
tooltip: {},
legend: {
data: ["销量"]
},
xAxis: {
data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
},
yAxis: {},
series: [
{
name: "销量",
type: "bar",
data: this.year[this.nowYear]
}
]
};
myChart.setOption(option);
}
},
在mouted里调用
mounted() {
this.drawChart();
}
之后就可以看着官方文档愉快的使用Echarts啦!