安装echarts依赖
npm install echarts -S
创建图表
全局引入
// 引入echarts
import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts
- report.vue
<template>
<div>
<!-- 卡片 -->
<el-card>
<div id="myChart" :style="{ width: '700px', height: '400px' }"></div>
</el-card>
</div>
</template>
<script>
// 引入封装好的axios请求
import { httpGetReportList } from "@/http/report_index.js";
// 引入lodash库(需要先安装 npm i -save lodash )
import _ from "lodash";
export default {
props: {},
data() {
return {
// 自己的options配置项
options: {
title: {
text: "用户来源",
},
tooltip: {
trigger: "axis",
axisPointer: {
type: "cross",
label: {
backgroundColor: "#E9EEF3",
},
},
},
grid: {
left: "3%",
right: "4%",
bottom: "3%",
containLabel: true,
},
xAxis: [
{
boundaryGap: false,
},
],
yAxis: [
{
type: "value",
},
],
},
};
},
methods: {
async drawLine() {
// 基于准备好的dom,初始化echarts实例
let myChart = this.$echarts.init(document.getElementById("myChart"));
// 准备数据和配置项
let { data: res } = await httpGetReportList();
if (res.meta.status !== 200) {
this.$message.error("获取图表数据失败");
}
// 将后台获取的数据和自己设置的options合并
const result = _.merge(res.data, this.options);
// 展示数据 绘制图表
myChart.setOption(result);
},
},
mounted() {
this.drawLine();
},
components: {},
};
</script>
<style scoped lang="less">
</style>
