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:'純屬虛構', 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)在我們看看前端展示效果
到此這篇關于SpringBoot thymeleaf實現(xiàn)餅狀圖與柱形圖流程介紹的文章就介紹到這了,更多相關SpringBoot餅狀圖與柱形圖內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
IDEA設置生成帶注釋的getter和setter的圖文教程
通常我們用idea默認生成的getter和setter方法是不帶注釋的,當然,我們同樣可以設置idea像MyEclipse一樣生成帶有Javadoc的模板,具體設置方法,大家參考下本文2018-05-05SpringBoot使用Netty實現(xiàn)遠程調用的示例
這篇文章主要介紹了SpringBoot使用Netty實現(xiàn)遠程調用的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10Java 數(shù)據(jù)結構線性表之順序存儲詳解原理
線性表的順序存儲是指用一組地址連續(xù)的存儲單元依次存儲線性表中的各個元素、使得線性表中在邏輯結構上相鄰的數(shù)據(jù)元素存儲在相鄰的物理存儲單元中,即通過數(shù)據(jù)元素物理存儲的相鄰關系來反映數(shù)據(jù)元素之間邏輯上的相鄰關系2021-10-10Mybatis-Plus集成Sharding-JDBC與Flyway實現(xiàn)多租戶分庫分表實戰(zhàn)
這篇文章主要為大家介紹了Mybatis-Plus集成Sharding-JDBC與Flyway實現(xiàn)多租戶分庫分表實戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11