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
引入很簡單,只需要
// 引入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(){
// 請求接口返回視圖數(shù)據(jù)
async function getData() {
try {
// 使用 await 等待異步請求完成
const res = await axios.get('http://localhost:9090/statistics/testStatistics');
// 返回所需數(shù)據(jù)
return res.data.data;
} catch (error) {
// 處理請求錯(cuò)誤
console.error('請求出錯(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é)
總體還是很簡單的,只需要根據(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)文章
利用Java實(shí)現(xiàn)在線圖片URL轉(zhuǎn)換為Base64以及反向解析成圖片
Base64 是一種將二進(jìn)制數(shù)據(jù)編碼為 ASCII 字符串格式的方法,常用于在網(wǎng)絡(luò)中安全傳輸圖片、文件等內(nèi)容,本文給大家介紹了如何利用Java實(shí)現(xiàn)在線圖片URL轉(zhuǎn)換為Base64以及反向解析成圖片,文章有相關(guān)的代碼和圖文供大家參考,需要的朋友可以參考下2025-03-03
java實(shí)現(xiàn)線上環(huán)境遠(yuǎn)程debug調(diào)試過程
本文介紹了在Docker容器中配置Tomcat和Spring Boot進(jìn)行遠(yuǎn)程調(diào)試的方法,通過配置`CATALINA_OPTS`或`JPDA`參數(shù),可以在測試環(huán)境中啟動(dòng)Tomcat或Spring Boot應(yīng)用的debug模式,然后在本地IDEA中設(shè)置斷點(diǎn)進(jìn)行調(diào)試,從而更高效地定位和解決問題2025-02-02
鄰接表無向圖的Java語言實(shí)現(xiàn)完整源碼
這篇文章主要介紹了鄰接表無向圖的Java語言實(shí)現(xiàn)完整源碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
springboot webflux 過濾器(使用RouterFunction實(shí)現(xiàn))
這篇文章主要介紹了springboot webflux 過濾器(使用RouterFunction實(shí)現(xiàn)),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(40)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07
SpringBoot設(shè)置Session失效時(shí)間的解決方案
當(dāng)過期時(shí)間是大于1分鐘的時(shí)候是沒有什么問題的,但是如果設(shè)置過期時(shí)間小于1分鐘,就會(huì)失效,這篇文章主要介紹了SpringBoot設(shè)置Session失效時(shí)間的解決方案,需要的朋友可以參考下2024-05-05

