一、背景
在Web應用開發(fā)中,經常需要使用圖表來展示數據,而Echarts是一個非常優(yōu)秀的圖表庫。SpringBoot是一個非常流行的Java Web框架,它可以快速搭建Web應用。本文將介紹如何使用SpringBoot集成Echarts,實現展示用戶人數和性別的功能。
二、功能實現流程
1. 創(chuàng)建數據庫表
首先,我們需要創(chuàng)建數據庫表,用于存儲用戶信息。本文中,我們創(chuàng)建一個名為“user”的表,用于存儲用戶的id、姓名、性別等信息。
2. 創(chuàng)建SpringBoot項目
使用IDEA等工具,創(chuàng)建一個SpringBoot項目,并加入相應的依賴。本文中,我們加入的主要依賴有SpringBoot、MyBatis、MySQL等。
3. 實現數據訪問層
使用MyBatis框架,實現對用戶信息的增刪改查等操作。同時,我們也需要實現一個接口用于查詢用戶的性別比例。
4. 實現業(yè)務邏輯層
在業(yè)務邏輯層中,我們需要完成對應的功能代碼。主要包括:添加用戶、刪除用戶、修改用戶信息、查詢所有用戶、查詢用戶性別比例等操作。其中,查詢用戶性別比例的操作需要使用Echarts來生成相應的圖表。
5. 實現控制層
在控制層中,我們需要編寫對應的接口,用于接收前端的請求,并調用相應的業(yè)務邏輯層方法來完成相應的操作。同時,我們也需要編寫相應的HTML頁面,用于展示用戶人數和性別比例的圖表。
6. 實現前端頁面
使用Vue框架,實現前端頁面,并通過接口調用,將后臺數據展示到前端頁面上。
三、代碼實現
本文提供一個示例代碼,代碼實現過程中,使用了SpringBoot、MyBatis、MySQL等主流技術,實現了基本的增刪改查操作、以及查詢用戶性別比例并用Echarts展示的功能。示例代碼中的表名為“user”,根據實際情況可做相應修改。
- 數據庫設計
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`gender` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
- 數據訪問層代碼
@Mapper
@Component
public interface UserDao {
int insert(User user);
int delete(int userId);
int update(User user);
List<User> selectAll();
int countByGender(int gender);
}
- 業(yè)務邏輯層代碼
@Service
public class UserService {
@Autowired
private UserDao userDao;
public void addUser(User user) {
userDao.insert(user);
}
public void deleteUser(int userId) {
userDao.delete(userId);
}
public void updateUser(User user) {
userDao.update(user);
}
public List<User> getAllUsers() {
return userDao.selectAll();
}
public Map<String, Integer> getUserGenderRatio() {
int maleCount = userDao.countByGender(1);
int femaleCount = userDao.countByGender(0);
Map<String, Integer> resultMap = new HashMap<>();
resultMap.put("male", maleCount);
resultMap.put("female", femaleCount);
return resultMap;
}
}
- 控制層代碼
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/add")
public String addUser(@RequestBody User user) {
userService.addUser(user);
return "success";
}
@PostMapping("/delete")
public String deleteUser(@RequestParam("userId") int userId) {
userService.deleteUser(userId);
return "success";
}
@PostMapping("/update")
public String updateUser(@RequestBody User user) {
userService.updateUser(user);
return "success";
}
@GetMapping("/all")
public List<User> getAllUsers() {
return userService.getAllUsers();
}
@GetMapping("/ratio")
public Map<String, Integer> getUserGenderRatio() {
return userService.getUserGenderRatio();
}
}
- 前端頁面代碼
<!DOCTYPE html>
<html>
<head>
<title>用戶信息展示</title>
<meta charset="utf-8">
<!-- 引入Echarts -->
<script src="https://cdn.jsdelivr.net/npm/echarts@4.9.0/dist/echarts.min.js"></script>
<!-- 引入Vue -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- 引入axios -->
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
<!-- 顯示用戶總數 -->
<p>共有{{ userList.length }}個用戶</p>
<!-- 顯示用戶性別比例 -->
<div id="genderRatio" style="width: 600px;height:400px;"></div>
</div>
<script>
new Vue({
el: '#app',
data: {
userList: []
},
mounted() {
this.getUserList();
this.showGenderRatio();
},
methods: {
getUserList() {
axios.get('/users/all')
.then(response => {
this.userList = response.data;
})
.catch(error => {
console.log(error);
})
},
showGenderRatio() {
axios.get('/users/ratio')
.then(response => {
let maleCount = response.data.male;
let femaleCount = response.data.female;
// 使用Echarts生成圖表
let chart = echarts.init(document.getElementById('genderRatio'));
chart.setOption({
title: {
text: '用戶性別比例',
subtext: '男性/女性'
},
tooltip: {
trigger: 'item',
formatter: '{a} <br/>: {c} (n5n3t3z%)'
},
legend: {
orient: 'vertical',
left: 'left',
data: ['男性', '女性']
},
series: [
{
name: '性別',
type: 'pie',
radius: ['50%', '70%'],
avoidLabelOverlap: false,
label: {
show: false,
position: 'center'
},
emphasis: {
label: {
show: true,
fontSize: '30',
fontWeight: 'bold'
}
},
labelLine: {
show: false
},
data: [
{value: maleCount, name: '男性'},
{value: femaleCount, name: '女性'}
]
}
]
});
})
.catch(error => {
console.log(error);
})
}
}
});
</script>
</body>
</html>
四、總結
本文介紹了如何使用SpringBoot集成Echarts,實現展示用戶人數和性別的功能。通過數據庫設計、實現數據訪問層、業(yè)務邏輯層和控制層的代碼編寫,以及前端頁面的開發(fā),本文詳細地介紹了SpringBoot整合Echarts的實現步驟和代碼。
其中,使用Vue框架進行前端頁面開發(fā),增加了Web應用的可擴展性和易用性。同時,通過使用Echarts圖表庫,我們可以讓數據呈現更為直觀清晰,增強用戶體驗。文章來源:http://www.zghlxwxcb.cn/news/detail-436238.html
希望本文對讀者在Web應用開發(fā)中的圖表展示有所幫助。文章來源地址http://www.zghlxwxcb.cn/news/detail-436238.html
到了這里,關于SpringBoot整合Echarts實現用戶人數和性別展示的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!