最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于springboot實(shí)現(xiàn)數(shù)據(jù)可視化的示例代碼

 更新時(shí)間:2022年07月11日 09:18:41   作者:妙烏  
本文主要介紹了基于springboot實(shí)現(xiàn)數(shù)據(jù)可視化,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>

前言:

數(shù)據(jù)可視化就是將數(shù)據(jù)轉(zhuǎn)換成圖或表等,以一種更直觀的方式展現(xiàn)和呈現(xiàn)數(shù)據(jù)。通過“可視化”的方式,我們看不懂的數(shù)據(jù)通過圖形化的手段進(jìn)行有效地表達(dá),準(zhǔn)確高效、簡(jiǎn)潔全面地傳遞某種信息,甚至我們幫助發(fā)現(xiàn)某種規(guī)律和特征,挖掘數(shù)據(jù)背后的價(jià)值?,F(xiàn)在,提出一種方案,基于springboot框架,將excel表格中的數(shù)據(jù)提取出來,前端使用echarts框架,通過柱形圖和餅狀圖對(duì)數(shù)據(jù)進(jìn)行直觀展示

Excel數(shù)據(jù)源展示

創(chuàng)建Registration.xlsx表格和fruit_sales頁面,同時(shí)創(chuàng)建相關(guān)水果銷售數(shù)據(jù)

在這里插入圖片描述

結(jié)構(gòu)一覽

在這里插入圖片描述

?? echarts,min.js下載鏈接

一、讀取Excel表格中的數(shù)據(jù)

本項(xiàng)目使用springboot整合hutool第三方類庫實(shí)現(xiàn)對(duì)excel文件中數(shù)據(jù)采用流的方式進(jìn)行讀取,詳情參看hutool官方文檔

Hutool官方文檔鏈接

1.1 創(chuàng)建springboot工程,導(dǎo)入相關(guān)依賴

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.4.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.16</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.16</version>
        </dependency>

1.2 創(chuàng)建測(cè)試類TestExcel

package com.allin.data_view;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.apache.poi.util.IOUtils;
import org.junit.Test;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TestExcel {

    @Test
    public  void test() throws Exception{
        File file = new File("D:\\2022learn\\springboot-test\\springboot-office\\data_view\\Registration.xlsx");

        FileInputStream input = new FileInputStream(file);

        MultipartFile multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
        // 1.獲取上傳文件輸入流
        InputStream inputStream = null;
        try{
            inputStream = multipartFile.getInputStream();
        }catch (Exception e){
        }
        // 2.應(yīng)用HUtool ExcelUtil獲取ExcelReader指定輸入流和sheet
        ExcelReader excelReader = ExcelUtil.getReader(inputStream, "fruit_sales");
        // 可以加上表頭驗(yàn)證
        // 3.讀取第二行到最后一行數(shù)據(jù)
        //List<List<Object>> read = excelReader.read(1, excelReader.getRowCount());
        List<Map<String,Object>> read = excelReader.readAll();
        for (Map<String,Object> objects : read) {
            Set<String> keys = objects.keySet();
            for(String key:keys){
                System.out.println(key + ":" + objects.get(key));
            }
            System.out.println();
        }
    }
}

?? 測(cè)試結(jié)果:

在這里插入圖片描述

二、采用柱形圖顯示Excel表格數(shù)據(jù)

在這里插入圖片描述

2.1 前端代碼

?? 在springboot框架中創(chuàng)建index.html,使用ajax動(dòng)態(tài)獲取后端數(shù)據(jù)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>echarts-bar</title>
    <script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
    <script src="js/echarts.min.js"></script>
</head>
<body>
<!-- 為ECharts準(zhǔn)備一個(gè)具備大?。▽捀撸┑腄om -->
<div id="main" style="width: 1400px;height:500px;"></div>
<script type="text/javascript">
    // 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
    var myChart = echarts.init(document.getElementById('main'));

    // 指定圖表的配置項(xiàng)和數(shù)據(jù)
    myChart.setOption({
        title: {
            text: '水果銷售情況柱形圖'
        },
        tooltip: {},
        legend: {
            data:['銷量']
        },
        xAxis: {
            data: [],
            axisLabel:{
                interval: 0
            }
        },
        yAxis: {},
        series: [{
            name: '銷量',
            type: 'bar',
            data: []
        }]
    });

    myChart.showLoading();

    var names=[];    //類別數(shù)組(實(shí)際用來盛放X軸坐標(biāo)值)
    var nums=[];    //銷量數(shù)組(實(shí)際用來盛放Y坐標(biāo)值)

    $.ajax({
        type : "get",
        async : false,            //異步請(qǐng)求(同步請(qǐng)求將會(huì)鎖住瀏覽器,用戶其他操作必須等待請(qǐng)求完成才可以執(zhí)行)
        url : "list",    //請(qǐng)求發(fā)送到TestServlet處
        data : {},
        dataType : "json",        //返回?cái)?shù)據(jù)形式為json
        success : function(result) {
            //請(qǐng)求成功時(shí)執(zhí)行該函數(shù)內(nèi)容,result即為服務(wù)器返回的json對(duì)象
            var data = result.data;
            if (data) {
                for(var i=0;i<data.length;i++){
                    names.push(data[i].name);    //挨個(gè)取出類別并填入類別數(shù)組
                }
                for(var i=0;i<data.length;i++){
                    nums.push(data[i].count);    //挨個(gè)取出銷量并填入銷量數(shù)組
                }
                myChart.hideLoading();    //隱藏加載動(dòng)畫
                myChart.setOption({        //加載數(shù)據(jù)圖表
                    xAxis: {
                        data: names
                    },
                    series: [{
                        // 根據(jù)名字對(duì)應(yīng)到相應(yīng)的系列
                        name: '銷量',
                        data: nums
                    }]
                });

            }
        },
        error : function() {
            //請(qǐng)求失敗時(shí)執(zhí)行該函數(shù)
            alert("圖表請(qǐng)求數(shù)據(jù)失敗!");
            myChart.hideLoading();
        }
    })
</script>
</body>
</html>

2.2 后端代碼

2.2.1 Controller層代碼

?? 前端代碼會(huì)發(fā)送list請(qǐng)求到后端請(qǐng)求數(shù)據(jù),controller層響應(yīng)請(qǐng)求,通過service層獲取表格數(shù)據(jù),返回map類型的數(shù)據(jù)

package com.allin.controller;

import com.allin.service.GetDataFromExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/")
public class IndexController {

    @Autowired
    private GetDataFromExcel getDataFromExcel;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index(){
        return "index";
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getList() throws Exception {
        Map<String,Object> map = new HashMap<>();
        List<Map<String,Object>> list = getDataFromExcel.getData();
        map.put("msg","ok");
        map.put("data",list);
        list.forEach(System.out::println);

        return map;
    }
}

2.1.3 Service層代碼

?? 在service包下創(chuàng)建GetDataFromExcel類用于從Excel表格中獲取數(shù)據(jù)

package com.allin.service;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import org.apache.poi.util.IOUtils;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Service
public class GetDataFromExcel {
    public List<Map<String,Object>> getData() throws Exception{
        File file = new File("D:\\2022learn\\springboot-test\\springboot-office\\data_view\\Registration.xlsx");

        FileInputStream input = new FileInputStream(file);

        MultipartFile multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
        // 1.獲取上傳文件輸入流
        InputStream inputStream = null;
        try{
            inputStream = multipartFile.getInputStream();
        }catch (Exception e){
        }
        // 2.應(yīng)用HUtool ExcelUtil獲取ExcelReader指定輸入流和sheet
        ExcelReader excelReader = ExcelUtil.getReader(inputStream, "fruit_sales");
        // 可以加上表頭驗(yàn)證
        // 3.讀取第二行到最后一行數(shù)據(jù)
        //List<List<Object>> read = excelReader.read(1, excelReader.getRowCount());
        List<Map<String,Object>> read = excelReader.readAll();
        return read;
    }
}

三、采用餅狀圖顯示Excel表格數(shù)據(jù)

在這里插入圖片描述

3.1 前端代碼

?? 在springboot框架中創(chuàng)建index1.html,使用ajax動(dòng)態(tài)獲取后端數(shù)據(jù)

<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org" style="height: 100%">
<head>
  <meta charset="utf-8">
  <title>echarts-pie</title>
  <script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
  <script src="js/echarts.min.js"></script>
</head>
<body style="height: 100%; margin: 0">
  <div id="main" style="height: 100%"></div>

  <script type="text/javascript">
    var mychart1 = echarts.init(document.getElementById('main'));

    mychart1.setOption({
      title: {
        text: '水果銷量統(tǒng)計(jì)餅狀圖',
        left: 'center'
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        left: 'left'
      },
      series: [
        {
          name: 'Access From',
          type: 'pie',
          radius: '50%',
          data: []
        }],
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    });

    mychart1.showLoading();

    var names=[];
    var nums=[];

    $.ajax({
      type : "get",
      async : false,            //異步請(qǐng)求(同步請(qǐng)求將會(huì)鎖住瀏覽器,用戶其他操作必須等待請(qǐng)求完成才可以執(zhí)行)
      url : "list",    //請(qǐng)求發(fā)送到TestServlet處
      data : {},
      dataType : "json",        //返回?cái)?shù)據(jù)形式為json
      success : function(result) {
        //請(qǐng)求成功時(shí)執(zhí)行該函數(shù)內(nèi)容,result即為服務(wù)器返回的json對(duì)象
        var data = result.data;
        if (data) {
          for(var i=0;i<data.length;i++){
            names.push(data[i].name);    //挨個(gè)取出類別并填入類別數(shù)組
            nums[i] = {value: data[i].count,name:data[i].name};
          }
/*          for(var i=0;i<data.length;i++){
            nums.push(data[i].count);    //挨個(gè)取出銷量并填入銷量數(shù)組
          }*/
          mychart1.hideLoading();    //隱藏加載動(dòng)畫
          mychart1.setOption({        //加載數(shù)據(jù)圖表
            series: {
              type: 'pie',
              radius: '55%',
              center: ['50%','60%'],
              data: nums,
              emphasis: {
                itemStyle: {
                  shadowBlur: 10,
                  shadowOffsetX: 0,
                  shadowColor: 'rgb(0,0,0,0.5)'
                }
              }
            },
          });
        }
      },
      error : function() {
        //請(qǐng)求失敗時(shí)執(zhí)行該函數(shù)
        alert("圖表請(qǐng)求數(shù)據(jù)失敗!");
        mychart1.hideLoading();
      }
    })


    window.addEventListener('resize', myChart.resize);
  </script>
</body>
</html>

3.2 后端代碼

?? 因與柱狀圖獲取數(shù)據(jù)來源一樣,故Controller層與Service層代碼同柱狀圖

?? 參考資料

Echarts 動(dòng)態(tài)加載數(shù)據(jù)庫中的數(shù)據(jù)

Hutool讀取Excel內(nèi)容

到此這篇關(guān)于基于springboot實(shí)現(xiàn)數(shù)據(jù)可視化的文章就介紹到這了,更多相關(guān)基于springboot實(shí)現(xiàn)數(shù)據(jù)可視化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

南溪县| 伊吾县| 宝山区| 慈利县| 凉城县| 宿州市| 彰化县| 仁布县| 锡林浩特市| 兴海县| 上虞市| 海丰县| 商南县| 温宿县| 拉萨市| 仁化县| 宁陕县| 五指山市| 西乌| 葫芦岛市| 香河县| 荥阳市| 文昌市| 洛扎县| 青海省| 徐水县| 搜索| 临武县| 敦化市| 凌云县| 合川市| 奇台县| 盐池县| 枣庄市| 阿拉善右旗| 琼中| 莱西市| 玉树县| 呼玛县| 都安| 腾冲县|