1.下载
npm i echarts -S
2.引入vue
import * as echarts from 'echarts';
3.使用
//<div id="main">的id对应
document.getElementById('main');
4.用一个柱状图和一个饼图举例
<template>
<div>
<el-row :gutter="10" style="margin-bottom: 40px">
<el-col :span="6">
<el-card>
<div style="color:#409EFF;"><i class="el-icon-user">i> 用户总数div>
<div style="padding: 10px 0;text-align: center;font-weight: bold">
100
div>
el-card>
el-col>
<el-col :span="6">
<el-card>
<div style="color:#f56c6c;"><i class="el-icon-s-data">i> 销售总量div>
<div style="padding: 10px 0;text-align: center;font-weight: bold">
$ 100,000,000
div>
el-card>
el-col>
<el-col :span="6">
<el-card>
<div style="color:#67c23a;"><i class="el-icon-coin">i> 收益总额div>
<div style="padding: 10px 0;text-align: center;font-weight: bold">
$ 300,000
div>
el-card>
el-col>
<el-col :span="6">
<el-card>
<div style="color:#e6a23c;"><i class="el-icon-location-information">i> 景点总数div>
<div style="padding: 10px 0;text-align: center;font-weight: bold">
100,000
div>
el-card>
el-col>
el-row>
<--饼图-->
<el-row>
<el-col :span="12">
<div id="main" style="width: 500px;height: 400px">div>
el-col>
<--柱状图-->
<el-col :span="12">
<div id="bar" style="width: 500px;height: 400px">div>
el-col>
el-row>
div>
template>
<script>
import * as echarts from 'echarts';
export default {
name: "Home",
// el:"#main",
data() {
return {}
},
mounted() {//页面元素渲染完之后再触发
var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
var option;
option = {
title: {
text: 'Referer of a Website',
subtext: 'Fake Data',
left: 'center'
},
tooltip: {
trigger: 'item'
},
legend: {
orient: 'vertical',
left: 'left'
},
series: [
{
name: 'Access From',
type: 'pie',
radius: '50%',//调整饼图大小
label: {
normal: {
show: true,
position: 'left',
textStyle: {
fontWeight: 100,
fontSize: 14
},
formatter: '{d}%'
}
},//lable用来显示饼图百分比
data: [],//后端数据传入这里
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
var barChartDom = document.getElementById('bar');
var barMyChart = echarts.init(barChartDom);
var barOption;
barOption = {
xAxis: {
type: 'category',
data: ['第一季度', '第二季度', '第三季度', '第四季度']//相当于柱状图的x轴的值
},
yAxis: {
type: 'value'
},
series: [
{
data: [],//传入后端数据
type: 'line'
}
]
};
this.request.get('/admin/echarts/user').then((res) => {
barOption.series[0].data = res.data//柱状图数据注入
barMyChart.setOption(barOption)
option.series[0].data = [//饼图数据注入
{name: '第一季度', value: res.data[0]},
{name: '第二季度', value: res.data[1]},
{name: '第三季度', value: res.data[2]},
{name: '第四季度', value: res.data[3]}
]
myChart.setOption(option)
})
}
}
script>
5.后端相关代码
@RestController
@RequestMapping("/admin/echarts")
public class EchartsController {
@Autowired
private UserService userService;
@GetMapping("/example")
public R get(){//测试代码,固定值
HashMap<String, Object> map = new HashMap<>();
map.put("x", CollectionUtil.newArrayList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));
map.put("y",CollectionUtil.newArrayList(150, 230, 224, 218, 135, 147, 260));
return new R(true,map);
}
@GetMapping("/user")
public R members(){//从数据库中获取
List<User> list = userService.list();
int q1 = 0;
int q2 = 0;
int q3 = 0;
int q4 = 0;
for (User user : list) {
String createTime = user.getCreateTime();
Date date = DateTimeUtil.formatTime(createTime);
Quarter quarter = DateUtil.quarterEnum(date);
switch (quarter){
case Q1: q1+=1;break;
case Q2: q2+=1;break;
case Q3: q3+=1;break;
case Q4: q4+=1;break;
default:break;
}
}
return new R(true,CollectionUtil.newArrayList(q1,q2,q3,q4));
}
}