SpringBoot?thymeleaf實現(xiàn)餅狀圖與柱形圖流程介紹
今天給大家?guī)淼氖且粋€用SpringBoot + thymeleaf顯示出餅狀圖和柱形圖
首先我們先創(chuàng)建項目 注意:創(chuàng)建SpringBoot項目時一定要聯(lián)網(wǎng)不然會報錯

項目創(chuàng)建好后我們首先對 application.yml 進行編譯

#指定端口號
server:
port: 8888
#配置mysql數(shù)據(jù)源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/nba?serverTimezone=Asia/Shanghai
username: root
password: root
#配置模板引擎 thymeleaf
thymeleaf:
mode: HTML5
cache: false
suffix: .html
prefix: classpath:/templates/
mybatis:
mapper-locations: classpath:/mapper/*.xml
type-aliases-package: com.bdqn.springboot #放包名
接下來我們寫后端代碼
mapper層
package com.bdqn.springbootexcel.mapper;
import com.bdqn.springbootexcel.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("select * from user")
List<User> find();
@Insert("insert into user ( name, age, sex) values ( #{name}, #{age}, #{sex})")
int add(User user);
}實體類
package com.bdqn.springbootexcel.pojo;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
@Data
public class User {
@ExcelProperty(index = 0,value = "用戶編號")
private Integer id;
@ExcelProperty(index = 1,value = "用戶姓名")
private String name;
@ExcelProperty(index = 2,value = "用戶年齡")
private String age;
@ExcelProperty(index = 3,value = "用戶性別")
private String sex;
}現(xiàn)在編寫最重要的前端代碼
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--餅狀圖-->
<div id="pie" style="width:800px;height:600px;"></div>
<script th:src="@{https://cdn.bootcdn.net/ajax/libs/echarts/5.4.0/echarts.min.js}"></script>
<script>
option = {
title: {
text:'餅圖示例',
subtext:'純屬虛構(gòu)',
left:'center'
},
legend: {
top: 'bottom'
},
tooltip:{
trigger:'item'
},
toolbox: {
show: true,
feature: {
mark: { show: true },
dataView: { show: true, readOnly: false },
restore: { show: true },
saveAsImage: { show: true }
}
},
series: [
{
name: 'Nightingale Chart',
type: 'pie',
radius: [50, 250],
center: ['50%', '50%'],
roseType: 'area',
itemStyle: {
borderRadius: 8
},
data: [
]
}
]
};
var chartDom = document.getElementById('pie');
var myChart = echarts.init(chartDom);
fetch("/pojos_bing").then(response => response.json()).then(res => {
res.forEach(item => {
//name 和 age 都是數(shù)據(jù)庫中的值
option.series[0].data.push({name: item.name,value: item.age})
})
myChart.setOption(option);
})
</script>
<!--柱狀圖-->
<div style="height: 50px;"></div>
<div id="bar" style="width: 1000px;height: 800px;"></div>
<script>
barOption = {
title: {
text: '柱狀圖'
},
legend: {
top: 'top'
},
tooltip: {
trigger: 'axis'
},
xAxis: {
type: 'category',
data: []
},
yAxis: {
type: 'value'
},
series: [
{
name: '11',
data: [],
type: 'bar'
},
{
name: '22',
data: [],
type: 'bar'
}
]
};
var barDom = document.getElementById('bar');
var barChart = echarts.init(barDom);
fetch("/pojos_bing").then(response => response.json()).then(res => {
//name 和 age 都是數(shù)據(jù)庫中的值
const name= res.map(v => v.name);
barOption.xAxis.data = name
const age= res.map(v => v.age);
barOption.series[0].data = age
barChart.setOption(barOption)
})
</script>
</body>
</html>現(xiàn)在我們看看前端展示效果


到此這篇關(guān)于SpringBoot thymeleaf實現(xiàn)餅狀圖與柱形圖流程介紹的文章就介紹到這了,更多相關(guān)SpringBoot餅狀圖與柱形圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Boot深入學(xué)習(xí)數(shù)據(jù)訪問之Spring?Data?JPA與Hibernate的應(yīng)用
Spring?Data?JPA是Spring?Data的子項目,在使用Spring?Data?JPA之前,先了解一下Hibernate,因為Spring?Data?JPA是由Hibernate默認實現(xiàn)的2022-10-10
關(guān)于SpringMVC對Restful風(fēng)格的支持詳解
Restful就是一個資源定位及資源操作的風(fēng)格,不是標準也不是協(xié)議,只是一種風(fēng)格,是對http協(xié)議的詮釋,下面這篇文章主要給大家介紹了關(guān)于SpringMVC對Restful風(fēng)格支持的相關(guān)資料,需要的朋友可以參考下2022-01-01
Shell重啟SpringBoot項目腳本的示例代碼(含服務(wù)守護)
本文介紹了如何使用?Bash?腳本來管理和守護運行服務(wù),將展示一個示例腳本,該腳本可以停止、啟動和守護運行一個服務(wù),并提供了相應(yīng)的解釋和用法說明,文章通過代碼示例介紹的非常詳細,需要的朋友可以參考下2023-11-11
SpringCloud基于Feign的可編程式接口調(diào)用實現(xiàn)
本文主要介紹了SpringCloud基于Feign的可編程式接口調(diào)用實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04

