SpringBoot整合Echarts繪制靜態(tài)數(shù)據柱狀圖和餅圖
idea創(chuàng)建spring boot項目



下載echarts

把echarts.min.js文件放到項目中。
項目目錄

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sid.spark</groupId>
<artifactId>webspark</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>webspark</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>application.properties
配置項目訪問端口9999,配置前綴/sid
server.port=9999 server.servlet.context-path=/sid
HelloSpringBoot.java
package com.sid.spark.webspark;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@RestController
public class HelloSpringBoot {
@RequestMapping(value="/hello",method = RequestMethod.GET)
public String sayHello(){
return "Hello Spring Boot!";
}
@RequestMapping(value="/first",method = RequestMethod.GET)
public ModelAndView firstDemo(){
return new ModelAndView("test");//跟templates文件夾下的test.html名字一樣,返回這個界面
}
@RequestMapping(value="/courseClickCount",method = RequestMethod.GET)
public ModelAndView courseClickCountStat(){
return new ModelAndView("demo");//跟templates文件夾下的demo.html名字一樣,返回這個界面
}
}test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- 引入 ECharts 文件 -->
<script src="js/echarts.min.js"></script>
</head>
<body>
<!-- 為 ECharts 準備一個具備大?。▽捀撸┑?DOM -->
<div id="main" style="width: 600px;height:400px;position:absolute;top:50%;left: 50%;margin-top: -200px;margin-left: -300px;"></div>
<script type="text/javascript">
// 基于準備好的dom,初始化echarts實例
var myChart = echarts.init(document.getElementById('main'));//main是<div id="main" style="width: 600px;height:400px;"></div>的id
// 指定圖表的配置項和數(shù)據
var option = {
title: {
text: 'ECharts 入門示例'
},
tooltip: {},
legend: {
data:['銷量']
},
xAxis: {
data: ["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"]
},
yAxis: {},
series: [{
name: '銷量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
// 使用剛指定的配置項和數(shù)據顯示圖表。
myChart.setOption(option);
</script>
</body>
</html>demo.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Demo</title>
<!-- 引入 ECharts 文件 -->
<script src="js/echarts.min.js"></script>
</head>
<body>
<!-- 為 ECharts 準備一個具備大?。▽捀撸┑?DOM -->
<div id="main" style="width: 600px;height:400px;position:absolute;top:50%;left: 50%;margin-top: -200px;margin-left: -300px;"></div>
<script type="text/javascript">
// 基于準備好的dom,初始化echarts實例
var myChart = echarts.init(document.getElementById('main'));//main是<div id="main" style="width: 600px;height:400px;"></div>的id
// 指定圖表的配置項和數(shù)據
var option = {
title : {
text: 'Spark Streaming實戰(zhàn)課程訪問量實時統(tǒng)計',
subtext: '實戰(zhàn)課程訪問次數(shù)',
x:'center'
},
tooltip : {
trigger: 'item',
formatter: "{a} <br/> : {c} (wppm3vysvbp%)"
},
legend: {
orient: 'vertical',
left: 'left',
data: ['Spark SQL實戰(zhàn)','Hadoop基礎','Storm實戰(zhàn)','Spark Streaming實戰(zhàn)','理論']
},
series : [
{
name: '訪問次數(shù)',
type: 'pie',
radius : '55%',
center: ['50%', '60%'],
data:[
{value:3350, name:'Spark SQL實戰(zhàn)'},
{value:3100, name:'Hadoop基礎'},
{value:2340, name:'Storm實戰(zhàn)'},
{value:1350, name:'Spark Streaming實戰(zhàn)'},
{value:15480, name:'理論'}
],
itemStyle: {
emphasis: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
};
// 使用剛指定的配置項和數(shù)據顯示圖表。
myChart.setOption(option);
</script>
</body>
</html>運行項目

訪問http://localhost:9999/sid/hello

http://localhost:9999/sid/first

http://localhost:9999/sid/courseClickCount

以上就是SpringBoot整合Echarts繪制靜態(tài)數(shù)據柱狀圖和餅圖的詳細內容,更多關于SpringBoot Echarts繪制柱狀圖和餅圖的資料請關注腳本之家其它相關文章!
相關文章
Spring Transaction事務實現(xiàn)流程源碼解析
此文就Spring 事務實現(xiàn)流程進行源碼解析,我們可以借此對Spring框架更多一層理解,下面以xml形式創(chuàng)建一個事務進行分析2022-09-09
Java統(tǒng)計代碼的執(zhí)行時間的常見方法總結
在日常開發(fā)中經常需要測試一些代碼的執(zhí)行時間,所以本文就匯總了一些 Java 中比較常用的執(zhí)行時間統(tǒng)計方法,文中的示例代碼講解詳細,有需要的小伙伴可以參考下2026-05-05
詳解MyBatis Mapper 代理實現(xiàn)數(shù)據庫調用原理
這篇文章主要介紹了詳解MyBatis Mapper 代理實現(xiàn)數(shù)據庫調用原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10

