SpringBoot+Vue使用Echarts的方式
前言
在vue項(xiàng)目中使用echarts,本次演示是使用vue2
1 前端準(zhǔn)備
echarts官網(wǎng):
https://echarts.apache.org/zh/index.html
官網(wǎng)提供了基本的使用說明和大量的圖表


1.1 下載echarts
執(zhí)行命令
npm install echarts
直接這樣執(zhí)行很可能會(huì)失敗,建議使用cnpm install echarts,前提是配置鏡像
這樣就代表下載echarts成功了

1.2 引入echarts
引入很簡(jiǎn)單,只需要
// 引入echarts import * as echarts from 'echarts';
1.3 使用echarts
使用echarts也分為幾個(gè)步驟,這里分開說明
1.3.1 準(zhǔn)備echarts容器
創(chuàng)建一個(gè)echarts容器,一般div即可
<div id="main" class="data-class"></div>
1.3.2 初始化echarts
這一步相當(dāng)于讓vue知道,我們要將一個(gè)id為main的容器為echarts展示
// 根據(jù)容器初始化echarts
const myChart = echarts.init(document.getElementById('main'));1.3.3 渲染echarts
給myChart 渲染基本數(shù)據(jù),包括標(biāo)題,樣式,xy軸等基本屬性,這里用的是官方demo
// 繪制圖表
myChart.setOption({
title: {
text: 'ECharts 入門示例'
},
tooltip: {},
xAxis: {
data: ['襯衫', '羊毛衫', '雪紡衫', '褲子', '高跟鞋', '襪子']
},
yAxis: {},
series: [
{
name: '銷量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}
]
});這樣一個(gè)基本的基于vue使用echarts的demo就完事了,查看界面

完整代碼
<template>
<div id="main" class="data-class"></div>
</template>
<script>
// 引入echarts
import * as echarts from 'echarts';
// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
export default {
name: "Data",
data() {
return {
};
},
methods:{
},
mounted(){
// 根據(jù)容器初始化echarts
const myChart = echarts.init(document.getElementById('main'));
// 繪制圖表
myChart.setOption({
title: {
text: 'ECharts 入門示例'
},
tooltip: {},
xAxis: {
data: ['襯衫', '羊毛衫', '雪紡衫', '褲子', '高跟鞋', '襪子']
},
yAxis: {},
series: [
{
name: '銷量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}
]
});
}
}
</script>
<style scoped>
.data-class{
height: 300px;
width: 100%;
}
</style>2 使用接口完成
以上只是一個(gè)demo,工作中都需要統(tǒng)計(jì)業(yè)務(wù)數(shù)據(jù),前后端共同完成
2.1 后端接口準(zhǔn)備
后端只需要提供一個(gè)接口,返回的格式根據(jù)圖表需要的數(shù)據(jù)格式進(jìn)行返回
這里使用隨機(jī)數(shù)模仿一個(gè)業(yè)務(wù)統(tǒng)計(jì)的接口,year代表年份,num代表數(shù)量
@GetMapping("/testStatistics")
public Result testStatistics(){
ArrayList<Map<String,Object>> resultList = new ArrayList<>();
for (int i = 2018; i < 2025; i++) {
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("year",i);
resultMap.put("num", RandomUtil.randomInt(1000,10000));
resultList.add(resultMap);
}
return Result.ok(resultList);
}返回的就是這樣的格式

2.2 前端渲染處理
前端只需要把后端返回的業(yè)務(wù)數(shù)據(jù)渲染到容器中即可,參考代碼:
<template>
<div id="main" class="data-class"></div>
</template>
<script>
// 引入echarts
import * as echarts from 'echarts';
// 引入axios
import axios from "axios";
// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例
export default {
name: "Data",
data() {
return {
};
},
mounted(){
// 請(qǐng)求接口返回視圖數(shù)據(jù)
async function getData() {
try {
// 使用 await 等待異步請(qǐng)求完成
const res = await axios.get('http://localhost:9090/statistics/testStatistics');
// 返回所需數(shù)據(jù)
return res.data.data;
} catch (error) {
// 處理請(qǐng)求錯(cuò)誤
console.error('請(qǐng)求出錯(cuò):', error);
throw error;
}
}
// 異步獲取數(shù)據(jù)
(async () => {
const data = await getData();
// 根據(jù)容器初始化echarts
const myChart = echarts.init(document.getElementById('main'));
// 繪制圖表
myChart.setOption({
title: {
text: '年收入統(tǒng)計(jì)'
},
tooltip: {
},
xAxis: {
data:data.map(item => item.year)
},
yAxis: {},
series: [
{
name: '收入',
type: 'bar',
data: data.map(item => item.num)
}
]
});
})();
}
}
</script>
<style scoped>
.data-class{
height: 300px;
width: 100%;
}
</style>
查看效果

總結(jié)
總體還是很簡(jiǎn)單的,只需要根據(jù)視圖需要的數(shù)據(jù)格式和后端溝通好返回的格式
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Springboot項(xiàng)目中運(yùn)用vue+ElementUI+echarts前后端交互實(shí)現(xiàn)動(dòng)態(tài)圓環(huán)圖(推薦)
- Springboot運(yùn)用vue+echarts前后端交互實(shí)現(xiàn)動(dòng)態(tài)圓環(huán)圖
- 基于Vue3和SpringBoot實(shí)現(xiàn)Web實(shí)時(shí)消息推送功能
- vue+springboot+webtrc+websocket實(shí)現(xiàn)雙人音視頻通話會(huì)議(最新推薦)
- WebRTC實(shí)現(xiàn)雙端音視頻聊天功能(Vue3 + SpringBoot )
- springboot+vue項(xiàng)目怎么解決跨域問題詳解
- SpringBoot+Netty+Vue+WebSocket實(shí)現(xiàn)在線聊天
相關(guān)文章
PowerJob分布式任務(wù)調(diào)度源碼流程解讀
這篇文章主要為大家介紹了PowerJob分布式任務(wù)調(diào)度源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-02-02
解決SpringMVC獲取請(qǐng)求參數(shù)亂碼問題
在使用SpringMVC和thymeleaf進(jìn)行請(qǐng)求參數(shù)處理時(shí),可能會(huì)遇到亂碼問題,對(duì)于GET方法亂碼,可通過修改Tomcat的server.xml文件,添加URIEncoding="UTF-8"解決,而POST方法亂碼,則需在web.xml配置SpringMVC提供的過濾器2024-11-11
HttpMessageConverter報(bào)文信息轉(zhuǎn)換器的深入講解
在Spring中內(nèi)置了大量的HttpMessageConverter,通過請(qǐng)求頭信息中的MIME類型,選擇相應(yīng)的HttpMessageConverter,這篇文章主要給大家介紹了關(guān)于HttpMessageConverter報(bào)文信息轉(zhuǎn)換器的相關(guān)資料,需要的朋友可以參考下2022-01-01
使用Spring Cache時(shí)設(shè)置緩存鍵的注意事項(xiàng)詳解
在現(xiàn)代的Web應(yīng)用中,緩存是提高系統(tǒng)性能和響應(yīng)速度的重要手段之一,Spring框架提供了強(qiáng)大的緩存支持,通過??@Cacheable??、??@CachePut??、??@CacheEvict??等注解可以方便地實(shí)現(xiàn)緩存功能,本文給大家介紹了使用Spring Cache時(shí)設(shè)置緩存鍵的注意事項(xiàng)2025-01-01
關(guān)于BigDecimal類型數(shù)據(jù)的絕對(duì)值和相除求百分比
這篇文章主要介紹了關(guān)于BigDecimal類型數(shù)據(jù)的絕對(duì)值和相除求百分比,Java在java.math包中提供的API類BigDecimal,用來對(duì)超過16位有效位的數(shù)進(jìn)行精確的運(yùn)算,需要的朋友可以參考下2023-07-07
SpringMVC HttpMessageConverter報(bào)文信息轉(zhuǎn)換器
??HttpMessageConverter???,報(bào)文信息轉(zhuǎn)換器,將請(qǐng)求報(bào)文轉(zhuǎn)換為Java對(duì)象,或?qū)ava對(duì)象轉(zhuǎn)換為響應(yīng)報(bào)文。???HttpMessageConverter???提供了兩個(gè)注解和兩個(gè)類型:??@RequestBody,@ResponseBody???,??RequestEntity,ResponseEntity??2023-01-01
舉例講解Java的Jackson庫中ObjectMapper類的使用
這篇文章主要介紹了舉例講解Java的Jackson庫中ObjectMapper類的使用,Jackson庫通常被用來實(shí)現(xiàn)Java的對(duì)象和JSON之間的轉(zhuǎn)換功能,需要的朋友可以參考下2016-01-01
spring?bean標(biāo)簽中的init-method和destroy-method詳解
這篇文章主要介紹了spring?bean標(biāo)簽中的init-method和destroy-method,在很多項(xiàng)目中,經(jīng)常在xml配置文件中看到init-method 或者 destroy-method ,因此整理收集下,方便以后參考和學(xué)習(xí),需要的朋友可以參考下2023-04-04

