1、后端编写
1.1 Controller控制层编写
package com.gmw.musicserver.controller;
import com.gmw.musicserver.commonutils.R;
import com.gmw.musicserver.entity.Consumer;
import com.gmw.musicserver.entity.Singer;
import com.gmw.musicserver.entity.Song;
import com.gmw.musicserver.entity.SongSheet;
import com.gmw.musicserver.service.ConsumerService;
import com.gmw.musicserver.service.SingerService;
import com.gmw.musicserver.service.SongService;
import com.gmw.musicserver.service.SongSheetService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* <p>
* 首页接口模块
* </p>
*
* @author 未进化的程序猿
* @since 2022-01-26
*/
@RestController
@RequestMapping("/musicserver/home")
public class HomeController {
public static final Logger logger = LoggerFactory.getLogger(HomeController.class);
//用户service接口
@Autowired
public ConsumerService consumerService;
//歌曲service接口
@Autowired
public SongService songService;
//歌手service接口
@Autowired
public SingerService singerService;
//歌单service接口
@Autowired
public SongSheetService songSheetService;
/**
* 查询所有的用户
* @return
*/
@GetMapping(value = "/allConsumer")
public R allConusmer(){
List<Consumer> consumerList = this.consumerService.allConsumer();
return R.ok().data("list",consumerList).data("count",consumerList.size());
}
/**
* 查询所有的歌曲
* @return
*/
@GetMapping(value = "/allSong")
public R allSong(){
List<Song> songList = this.songService.allSong();
return R.ok().data("list",songList).data("count",songList.size());
}
/**
* 查询所有的歌手
* @return
*/
@GetMapping(value = "/allSinger")
public R allSinger(){
List<Singer> singerList = this.singerService.allSinger();
return R.ok().data("list",singerList).data("count",singerList.size());
}
/**
* 查询所有的歌单
* @return
*/
@GetMapping(value = "/allSongList")
public R allSongList(){
List<SongSheet> songSheetList = this.songSheetService.allSongList();
return R.ok().data("list",songSheetList).data("count",songSheetList.size());
}
}
2、前端编写
<template>
<div>
<el-row :gutter="20" class="mgb20">
<el-col :span="6">
<el-card>
<div class="grid-content">
<div class="grid-cont-center">
<div class="grid-num">{{consumerCount}}</div>
<div>用户总数</div>
</div>
</div>
</el-card>
</el-col>
<el-col :span="6">
<el-card>
<div class="grid-content">
<div class="grid-cont-center">
<div class="grid-num">{{songCount}}</div>
<div>歌曲总数</div>
</div>
</div>
</el-card>
</el-col>
<el-col :span="6">
<el-card>
<div class="grid-content">
<div class="grid-cont-center">
<div class="grid-num">{{singerCount}}</div>
<div>歌手数量</div>
</div>
</div>
</el-card>
</el-col>
<el-col :span="6">
<el-card>
<div class="grid-content">
<div class="grid-cont-center">
<div class="grid-num">{{songListCount}}</div>
<div>歌单数量</div>
</div>
</div>
</el-card>
</el-col>
</el-row>
<el-row :gutter="20" class="mgb20">
<el-col :span="12">
<h3 class="mgb20">用户性别比例</h3>
<div style="background-color:white">
<ve-pie :data="consumerSex" :theme="options"></ve-pie>
</div>
</el-col>
<el-col :span="12">
<h3 class="mgb20">歌曲类型分布</h3>
<div style="background-color:white">
<ve-histogram :data="songStyle"></ve-histogram>
</div>
</el-col>
</el-row>
<el-row :gutter="20" class="mgb20">
<el-col :span="12">
<h3 class="mgb20">歌手性别比例</h3>
<div style="background-color:white">
<ve-pie :data="singerSex"></ve-pie>
</div>
</el-col>
<el-col :span="12">
<h3 class="mgb20">歌手国籍分布</h3>
<div style="background-color:white">
<ve-histogram :data="country"></ve-histogram>
</div>
</el-col>
</el-row>
</div>
</template>
<script>
import {getAllSinger,getAllConusmer,getAllSong,getAllSongList} from '../api/home/index';
export default {
data(){
return {
consumerCount: 0, //用户总数
songCount: 0, //歌曲总数
singerCount: 0, //歌手数量
songListCount: 0, //歌单数量
consumer: [], //所有用户
consumerSex:{ //按性别分类的用户数
columns: ['性别','总数'],
rows: [
{'性别': '男','总数': 0},
{'性别': '女','总数': 0}
]
},
options: {
color: ['#87cefa','#ffc0cb']
},
// options1: {
// color: ['yellow']
// },
songStyle:{ //按歌单风格分类
columns: ['风格','总数'],
rows: [
{'风格': '华语','总数': 0},
{'风格': '粤语','总数': 0},
{'风格': '欧美','总数': 0},
{'风格': '日韩','总数': 0},
{'风格': 'BGM','总数': 0},
{'风格': '轻音乐','总数': 0},
{'风格': '乐器','总数': 0}
]
},
singerSex:{ //按性别分类的歌手数
columns: ['性别','总数'],
rows: [
{'性别': '女','总数': 0},
{'性别': '男','总数': 0},
{'性别': '组合','总数': 0},
{'性别': '不明','总数': 0}
]
},
country:{
columns: ['国籍','总数'],
rows: [
{'国籍': '中国','总数': 0},
{'国籍': '韩国','总数': 0},
{'国籍': '日本','总数': 0},
{'国籍': '美国','总数': 0},
{'国籍': '新加坡','总数': 0},
{'国籍': '意大利','总数': 0},
{'国籍': '马来西亚','总数': 0},
{'国籍': '西班牙','总数': 0}
]
}
}
},
created() {
},
mounted() {
this.getConsumer();
this.getSong();
this.getSinger();
this.getSongList();
},
methods: {
getConsumer() { //用户总数
getAllConusmer().then(res => {
this.consumer = res.data.list;
this.consumerCount = res.data.count;
this.consumerSex.rows[0]['总数'] = this.setSex(1,this.consumer);
this.consumerSex.rows[1]['总数'] = this.setSex(0,this.consumer);
})
},
setSex(sex,val) { //根据性别获取用户数
let count = 0;
for(let item of val){
if(sex == item.sex){
count++;
}
}
return count;
},
getSong() { //歌曲总数
getAllSong().then(res => {
this.songCount = res.data.count;
})
},
getSinger() { //歌手数量
getAllSinger().then(res => {
this.singerCount = res.data.count;
this.singerSex.rows[0]['总数'] = this.setSex(0,res.data.list);
this.singerSex.rows[1]['总数'] = this.setSex(1,res.data.list);
this.singerSex.rows[2]['总数'] = this.setSex(2,res.data.list);
this.singerSex.rows[3]['总数'] = this.setSex(3,res.data.list);
for(let item of res.data.list){
this.getByCountry(item.location);
}
})
},
getSongList() { //歌单数量
getAllSongList().then(res => {
this.songListCount = res.data.count;
for(let item of res.data.list){
this.getByStyle(item.style);
}
})
},
getByStyle(style) { //根据歌单风格获取数量
for(let item of this.songStyle.rows){
if(style.includes(item['风格'])){
item['总数']++;
}
}
},
getByCountry(location) { //根据国籍获取数量
for(let item of this.country.rows){
if(location.includes(item['国籍'])){
item['总数']++;
}
}
}
}
}
</script>
<style scoped>
.grid-content {
display: flex;
align-items: center;
height: 50px;
}
.grid-cont-center {
flex: 1;
text-align: center;
font-size: 14px;
color: darkgray;
}
.grid-num {
font-size: 30px;
font-weight: bold;
}
</style>
2.1 用户/歌曲/歌手/歌单数量显示
定义临时变量
2.2 用户性别比例饼状图显示
动态更新性别比例数据
2.3 歌曲类型柱状图显示
动态更新歌曲比例数据
…..