SpringBoot 2.x 學習筆記(4):整合Echarts
阿新 • • 發佈:2019-01-03
1、下載Echarts
http://echarts.baidu.com/index.html
2、新增echarts.js到專案中
在resources目錄下建立js目錄,然後將剛才下載的echarts.js檔案放到js目錄下。
不知道為什麼我在IntelliJ IDEA工具下,建立對應的js目錄失敗,出現“static.js”或者“static.css”目錄。只好刪除之前失敗的目錄“static.js”,然後重新在resources下建立static目錄、js目錄、css目錄,再將js目錄和css目錄拖入static目錄即可。
3、檢視Echarts官方樣例
進入http://echarts.baidu.com/tutorial.html
參考“5 分鐘上手 ECharts”
4、參考上面樣例程式碼,編寫測試程式
在template目錄下建立測試頁面echarts.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>測試Echarts</title> <!-- 引入 ECharts 檔案 --> <script type="application/javascript" th:src="@{js/echarts.js}"></script> </head> <body> <!-- 為ECharts準備一個具備大小(寬高)的Dom --> <div id="main" style="width: 600px;height:400px;"></div> <script type="text/javascript"> // 基於準備好的dom,初始化echarts例項 var myChart = echarts.init(document.getElementById('main')); // 指定圖表的配置項和資料 var option = { title: { text: 'ECharts 入門示例' }, tooltip: {}, legend: { data:['銷量'] }, xAxis: { data: ["襯衫","羊毛衫","雪紡衫","褲子","高跟鞋","襪子"] }, yAxis: {}, series: [{ name: '銷量', type: 'bar', data: [5, 20, 36, 10, 10, 20] }] }; // 使用剛指定的配置項和資料顯示圖表。 myChart.setOption(option); </script> </body> </html>
建立控制器
package cn.hadron.eba.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class IndexController { @RequestMapping("/echarts") public String echarts(Model model){ System.out.println("IndexController.echarts"); return "echarts"; } }