先安装echarts依赖
npm install echarts --save
功能代码
<template>
<div class="app-container ">
<el-row >
<el-col :span="6">
<el-card shadow="always">
<div class="statistics-item-wrapper">
<div class="statistics-item-detail">
<span>Total Accounts</span>
</div>
<div class="statistics-item-num">
<span>{{state.countData.totalAccountCount}}</span>
</div>
</div>
</el-card>
</el-col>
<el-col :span="6">
<el-card shadow="always">
<div class="statistics-item-wrapper">
<div class="statistics-item-detail">
<span>Total Boats</span>
</div>
<div class="statistics-item-num">
<span>{{state.countData.totalBoatCount}}</span>
</div>
</div>
</el-card>
</el-col>
<el-col :span="6">
<el-card shadow="always">
<div class="statistics-item-wrapper">
<div class="statistics-item-detail">
<span>Total Engines</span>
</div>
<div class="statistics-item-num">
<span>{{state.countData.totalEngineCount}}</span>
</div>
</div>
</el-card>
</el-col>
<el-col :span="6">
<el-card shadow="always">
<div class="statistics-item-wrapper">
<div class="statistics-item-detail">
<span>Total Devices</span>
</div>
<div class="statistics-item-num">
<span>{{state.countData.totalDeviceCount}}</span>
</div>
</div>
</el-card>
</el-col>
</el-row>
<el-row >
<el-col :span="12">
<el-card shadow="always">
<div class="chart-title-wrapper">
<div class="chart-title">
<span>Accounts</span>
</div>
<div class="chart-date-wrapper">
<div class="chart-date-week" :class="{active: state.isAccountWeekActive}" @click="showChartByWeek('account-chart')">
<span >Week</span>
</div>
<div class="chart-date-month" :class="{active: state.isAccountMonthActive}" @click="showChartByMonth('account-chart')">
<span >Month</span>
</div>
</div>
</div>
<div class="chart-body-wrapper">
<div class="chart-body" id="account-chart">
</div>
</div>
</el-card>
</el-col>
<el-col :span="12">
<el-card shadow="always">
<div class="chart-title-wrapper">
<div class="chart-title">
<span>Boats</span>
</div>
<div class="chart-date-wrapper">
<div class="chart-date-week" :class="{active: state.isBoatWeekActive}" @click="showChartByWeek('boat-chart')">Week</div>
<div class="chart-date-month" :class="{active: state.isBoatMonthActive}" @click="showChartByMonth('boat-chart')">Month</div>
</div>
</div>
<div class="chart-body-wrapper">
<div class="chart-body" id="boat-chart">
</div>
</div>
</el-card>
</el-col>
</el-row>
<el-row >
<el-col :span="12">
<el-card shadow="always">
<div class="chart-title-wrapper">
<div class="chart-title">
<span>Engines</span>
</div>
<div class="chart-date-wrapper">
<div class="chart-date-week" :class="{active: state.isEngineWeekActive}" @click="showChartByWeek('engine-chart')">Week</div>
<div class="chart-date-month" :class="{active: state.isEngineMonthActive}" @click="showChartByMonth('engine-chart')">Month</div>
</div>
</div>
<div class="chart-body-wrapper">
<div class="chart-body" id="engine-chart">
</div>
</div>
</el-card>
</el-col>
<el-col :span="12">
<el-card shadow="always">
<div class="chart-title-wrapper">
<div class="chart-title">
<span>Devices</span>
</div>
<div class="chart-date-wrapper">
<div class="chart-date-week" :class="{active: state.isDeviceWeekActive}" @click="showChartByWeek('device-chart')">Week</div>
<div class="chart-date-month" :class="{active: state.isDeviceMonthActive}" @click="showChartByMonth('device-chart')">Month</div>
</div>
</div>
<div class="chart-body-wrapper">
<div class="chart-body" id="device-chart">
</div>
</div>
</el-card>
</el-col>
</el-row>
</div>
</template>
<script setup>
import { reactive } from "vue";
import * as api from '@/api/system/dashboard';
import * as echarts from 'echarts';
const state = reactive({
dashboardChart: {
account:null,
boat:null,
engine:null,
device:null,
},
isAccountWeekActive: false,
isAccountMonthActive: false,
isBoatWeekActive: false,
isBoatMonthActive: false,
isEngineWeekActive: false,
isEngineMonthActive: false,
isDeviceWeekActive: false,
isDeviceMonthActive: false,
loading: false,
countData: {
totalAccountCount: 0,
totalBoatCount: 0,
totalEngineCount: 0,
totalDeviceCount: 0,
},
});
function totalCount() {
state.loading = true;
api
.totalCount()
.then((res) => {
state.countData = res;
})
.finally(() => {
state.loading = false;
});
}
function statisticsChart(){
state.loading = true;
api
.statisticsChart()
.then((res) => {
state.dashboardChart = res;
initChartByWeek('account-chart', state.dashboardChart.account);
initChartByWeek('boat-chart', state.dashboardChart.boat);
initChartByWeek('engine-chart', state.dashboardChart.engine);
initChartByWeek('device-chart', state.dashboardChart.device);
state.isAccountWeekActive=true;
state.isBoatWeekActive=true;
state.isEngineWeekActive=true;
state.isDeviceWeekActive=true;
})
.finally(() => {
state.loading = false;
});
}
function getDataByElId(elId, dateType) {
let data;
if (elId === 'account-chart') {
data = state.dashboardChart.account;
if('week' === dateType){
state.isAccountWeekActive=true;
state.isAccountMonthActive=false;
}else{
state.isAccountWeekActive=false;
state.isAccountMonthActive=true;
}
} else if (elId === 'boat-chart') {
data = state.dashboardChart.boat;
if('week' === dateType){
state.isBoatWeekActive=true;
state.isBoatMonthActive=false;
}else{
state.isBoatWeekActive=false;
state.isBoatMonthActive=true;
}
} else if (elId === 'engine-chart') {
data = state.dashboardChart.engine;
if('week' === dateType){
state.isEngineWeekActive=true;
state.isEngineMonthActive=false;
}else{
state.isEngineWeekActive=false;
state.isEngineMonthActive=true;
}
} else if (elId === 'device-chart') {
data = state.dashboardChart.device;
if('week' === dateType){
state.isDeviceWeekActive=true;
state.isDeviceMonthActive=false;
}else{
state.isDeviceWeekActive=false;
state.isDeviceMonthActive=true;
}
}
return data;
}
function initChartByWeek(elId, data){
let rawData = {
xData: [],
yData: [],
}
if (data && data.weekList && data.weekList.length > 0) {
data.weekList.forEach(weekData => {
rawData.xData.push(weekData.date);
rawData.yData.push(weekData.value);
});
}
showChart(rawData, elId);
}
function showChartByWeek(elId){
let data = getDataByElId(elId, 'week');
let rawData = {
xData: [],
yData: [],
}
if (data && data.weekList && data.weekList.length > 0) {
data.weekList.forEach(weekData => {
rawData.xData.push(weekData.date);
rawData.yData.push(weekData.value);
});
}
showChart(rawData, elId);
}
function showChartByMonth(elId){
let data = getDataByElId(elId, 'month');
let rawData = {
xData: [],
yData: [],
}
if (data && data.monthList && data.monthList.length > 0) {
data.monthList.forEach(monthData => {
rawData.xData.push(monthData.date);
rawData.yData.push(monthData.value);
});
}
showChart(rawData, elId);
}
function showChart(rawData, elId) {
let myChart = echarts.init(document.getElementById(elId));
let option = {
xAxis: {
type: 'category',
data: rawData.xData
},
tooltip: {
trigger: 'axis'
},
yAxis: {
type: 'value'
},
grid: {
x: 45,
y: 30,
x2: 15,
y2: 35
},
series: [
{
data: rawData.yData,
type: 'line',
smooth: true
}
]
};
myChart.setOption(option);
}
totalCount();
statisticsChart();
</script>
<style scoped lang="scss">
.active{
border-color:
background-color:
}
.app-container {
.statistics-item-wrapper{
height: 70px;
display: flex;
justify-content: space-between;
align-items: center;
.statistics-item-detail{
font-size: 20px;
font-weight: bold;
}
.statistics-item-num{
font-size: 27px;
font-weight: bold;
}
}
.el-card.is-always-shadow {
margin: 10px;
border-radius: 10px;
box-shadow: 0px 10px 18px
:deep .el-card__body {
}
}
.chart-title-wrapper{
display: flex;
justify-content: space-between;
margin: 0 10px;
.chart-date-wrapper{
display: flex;
justify-content: space-between;
.chart-date-week, .chart-date-month{
border-radius: 8px;
height: 30px;
width: 70px;
line-height: 30px;
text-align: center;
border: 1px solid
cursor: pointer;
}
}
}
.chart-body-wrapper {
height: 280px;
margin: 0 10px;
.chart-body {
height: 260px;
width: 100%;
}
}
}
</style>