首頁 > 軟體

SpringBoot thymeleaf實現餅狀圖與柱形圖流程介紹

2022-12-03 14:01:16

今天給大家帶來的是一個用SpringBoot + thymeleaf顯示出餅狀圖和柱形圖

首先我們先建立專案 注意:建立SpringBoot專案時一定要聯網不然會報錯

專案建立好後我們首先對 application.yml 進行編譯

#指定埠號
server:
 port: 8888
#設定mysql資料來源
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;
}

現在編寫最重要的前端程式碼

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 都是資料庫中的值
                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 都是資料庫中的值
            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>

現在我們看看前端展示效果

到此這篇關於SpringBoot thymeleaf實現餅狀圖與柱形圖流程介紹的文章就介紹到這了,更多相關SpringBoot餅狀圖與柱形圖內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com