SpringBoot?thymeleaf實(shí)現(xiàn)餅狀圖與柱形圖流程介紹
今天給大家?guī)?lái)的是一個(gè)用SpringBoot + thymeleaf顯示出餅狀圖和柱形圖
首先我們先創(chuàng)建項(xiàng)目 注意:創(chuàng)建SpringBoot項(xiàng)目時(shí)一定要聯(lián)網(wǎng)不然會(huì)報(bào)錯(cuò)

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

#指定端口號(hào)
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 #放包名
接下來(lái)我們寫后端代碼
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);
}實(shí)體類
package com.bdqn.springbootexcel.pojo;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
@Data
public class User {
@ExcelProperty(index = 0,value = "用戶編號(hào)")
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ù)庫(kù)中的值
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ù)庫(kù)中的值
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實(shí)現(xiàn)餅狀圖與柱形圖流程介紹的文章就介紹到這了,更多相關(guān)SpringBoot餅狀圖與柱形圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA設(shè)置生成帶注釋的getter和setter的圖文教程
通常我們用idea默認(rèn)生成的getter和setter方法是不帶注釋的,當(dāng)然,我們同樣可以設(shè)置idea像MyEclipse一樣生成帶有Javadoc的模板,具體設(shè)置方法,大家參考下本文2018-05-05
SpringBoot使用Netty實(shí)現(xiàn)遠(yuǎn)程調(diào)用的示例
這篇文章主要介紹了SpringBoot使用Netty實(shí)現(xiàn)遠(yuǎn)程調(diào)用的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Java8函數(shù)式編程應(yīng)用小結(jié)
Java8非常重要的就是引入了函數(shù)式編程的思想,使得這門經(jīng)典的面向?qū)ο笳Z(yǔ)言有了函數(shù)式的編程方式,彌補(bǔ)了很大程度上的不足,函數(shù)式思想在處理復(fù)雜問題上有著更為令人稱贊的特性,本文給大家介紹Java8函數(shù)式編程應(yīng)用小結(jié),感興趣的朋友一起看看吧2023-12-12
Java 數(shù)據(jù)結(jié)構(gòu)線性表之順序存儲(chǔ)詳解原理
線性表的順序存儲(chǔ)是指用一組地址連續(xù)的存儲(chǔ)單元依次存儲(chǔ)線性表中的各個(gè)元素、使得線性表中在邏輯結(jié)構(gòu)上相鄰的數(shù)據(jù)元素存儲(chǔ)在相鄰的物理存儲(chǔ)單元中,即通過數(shù)據(jù)元素物理存儲(chǔ)的相鄰關(guān)系來(lái)反映數(shù)據(jù)元素之間邏輯上的相鄰關(guān)系2021-10-10
Mybatis-Plus集成Sharding-JDBC與Flyway實(shí)現(xiàn)多租戶分庫(kù)分表實(shí)戰(zhàn)
這篇文章主要為大家介紹了Mybatis-Plus集成Sharding-JDBC與Flyway實(shí)現(xiàn)多租戶分庫(kù)分表實(shí)戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11

