今天的主要内容包括:Vue安装ECharts、Vue整合ECharts、后台实现ECharts数据传输接口、前端实现ECharts动态数据传输等内容。今天主要是图表相关的内容,这也是我们Web应用不可缺少的内容,下面就开始我们今天的学习!
<template>
<div>
<el-col :span="12">
<div id="main" style="width: 500px;height: 400px">div>
el-col>
<el-col :span="12">
<div id="pie" style="width: 500px;height: 400px">div>
el-col>
div>
template>
<script>
// 引入Echarts
import * as echarts from 'echarts'
export default {
name: "Home",
data() {
return {}
},
// 页面元素渲染之后再触发
mounted() {
// 柱状折线图
var option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
data: [150, 230, 224, 218, 135, 147, 260],
type: 'line'
},
{
data: [150, 230, 224, 218, 135, 147, 260],
type: 'bar'
}
]
};
var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
myChart.setOption(option);
// 饼图
var pieOption = {
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%',
data: [
{value: 1048, name: 'Search Engine'},
{value: 735, name: 'Direct'},
{value: 580, name: 'Email'},
{value: 484, name: 'Union Ads'},
{value: 300, name: 'Video Ads'}
],
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
var pieDom = document.getElementById('pie');
var pieChart = echarts.init(pieDom);
pieChart.setOption(pieOption);
}
}
script>
<style scoped>
style>
/**
* 按季度获取数据
*
* @return 返回季度数据
*/
@GetMapping("/members")
public Result members() {
List<User> list = userService.list();
int q1 = 0; // 第一季度
int q2 = 0; // 第二季度
int q3 = 0; // 第三季度
int q4 = 0; // 第四季度
for (User user : list) {
Date createTime = user.getCreateTime();
Quarter quarter = DateUtil.quarterEnum(createTime);
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 Result.success(CollUtil.newArrayList(q1, q2, q3, q4));
}
<template>
<div>
<el-row :gutter="10" style="margin-bottom: 150px">
<el-col :span="6">
<el-card style="color: #409EFF">
<div><i class="el-icon-user-solid">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 style="color: #edd014">
<div><i class="el-icon-money">i>销售总量div>
<div style="padding: 10px 0;text-align: center;font-weight: bold">
$1000000
div>
el-card>
el-col>
<el-col :span="6">
<el-card style="color: #00c317">
<div><i class="el-icon-bank-card">i>收益总额div>
<div style="padding: 10px 0;text-align: center;font-weight: bold">
$1000000
div>
el-card>
el-col>
<el-col :span="6">
<el-card style="color: #dd2215">
<div><i class="el-icon-s-shop">i>门店总数div>
<div style="padding: 10px 0;text-align: center;font-weight: bold">
20
div>
el-card>
el-col>
el-row>
<el-col :span="12">
<div id="main" style="width: 500px;height: 500px">div>
el-col>
<el-col :span="12">
<div id="pie" style="width: 500px;height: 500px">div>
el-col>
div>
template>
<script>
// 引入Echarts
import * as echarts from 'echarts'
export default {
name: "Home",
data() {
return {}
},
// 页面元素渲染之后再触发
mounted() {
// 柱状折线图
var option = {
title: {
text: '各季度会员数量统计',
subtext: '趋势图',
left: 'center'
},
tooltip: {
trigger: 'item'
},
legend: {
orient: 'vertical',
left: 'left'
},
xAxis: {
type: 'category',
data: ["第一季度", "第二季度", "第三季度", "第四季度"]
},
yAxis: {
type: 'value'
},
series: [
{
name: "星巴克",
data: [],
type: 'bar'
},
{
name: "星巴克",
data: [],
type: 'line'
},
{
name: "瑞幸咖啡",
data: [],
type: 'bar'
},
{
name: "瑞幸咖啡",
data: [],
type: 'line'
}
]
};
var chartDom = document.getElementById('main');
var myChart = echarts.init(chartDom);
this.request.get("/echarts/members").then(res => {
// 填空
option.series[0].data = res.data;
option.series[1].data = res.data;
option.series[2].data = [5, 6, 7, 8];
option.series[3].data = [5, 6, 7, 8];
// 在数据准备完毕之后再set
myChart.setOption(option);
})
// 饼图
var pieOption = {
title: {
text: '各季度会员数量统计',
subtext: '比例图',
left: 'center'
},
tooltip: {
trigger: 'item',
formatter: '{a}
{b} : {c} ({d}%)'
},
legend: {
orient: 'vertical',
left: 'left'
},
series: [
{
name: '星巴克',
type: 'pie',
radius: '35%',
center: ['25%', '50%'],
label: { //饼图图形上的文本标签
normal: {
show: true,
position: 'inner', //标签的位置
textStyle: {
fontWeight: 300,
fontSize: 14, //文字的字体大小
color: "#fff"
},
formatter: '{d}%'
}
},
data: [], // 填空
},
{
name: '瑞幸咖啡',
type: 'pie',
radius: '45%',
center: ['75%', '60%'],
label: { //饼图图形上的文本标签
normal: {
show: true,
position: 'inner', //标签的位置
textStyle: {
fontWeight: 300,
fontSize: 14, //文字的字体大小
color: "#fff"
},
formatter: '{c}({d}%)'
}
},
data: [{name: "第一季度", value: 5},
{name: "第二季度", value: 6},
{name: "第三季度", value: 7},
{name: "第四季度", value: 8},], // 填空
}
]
};
var pieDom = document.getElementById('pie');
var pieChart = echarts.init(pieDom);
this.request.get("/echarts/members").then(res => {
pieOption.series[0].data = [
{name: "第一季度", value: res.data[0]},
{name: "第二季度", value: res.data[1]},
{name: "第三季度", value: res.data[2]},
{name: "第四季度", value: res.data[3]},
]
pieChart.setOption(pieOption)
})
}
}
script>
<style scoped>
style>
以上就是今天学习的全部内容,内容不是很多,跟着我演示的一步一步做即可。明天将会给大家带来关于SpringBoot和Vue实现权限菜单功能的相关内容。明天见!