feat(user-view): 新增近12月消费趋势图表

在用户余额页面下方添加了一个图表,展示用户近12个月的消费趋势。使用ECharts库初始化图表并设置选项,包括标题、工具提示、图例、网格、工具箱等。消费金额数据和月份标签已硬编码,需根据实际后端数据进行动态更新。

引入了echarts库并更新了相关代码,确保图表能在页面加载时正确显示。此更新旨在提供给用户更直观的消费反馈,帮助用户更好地了解自己的消费模式。

BREAKING CHANGE: 引入了ECharts库的新版本5.5.1,可能会与现有的样式或依赖产生冲突。需要在项目中进行详细的测试,确保没有引入新的问题。
master
wxy 2024-08-26 15:01:12 +08:00
parent 1a79b2005e
commit 78064468c1
2 changed files with 110 additions and 2 deletions

View File

@ -40,7 +40,7 @@
"axios": "0.24.0", "axios": "0.24.0",
"clipboard": "2.0.8", "clipboard": "2.0.8",
"core-js": "3.25.3", "core-js": "3.25.3",
"echarts": "5.4.0", "echarts": "^5.5.1",
"element-ui": "2.15.14", "element-ui": "2.15.14",
"file-saver": "2.0.5", "file-saver": "2.0.5",
"fuse.js": "6.4.3", "fuse.js": "6.4.3",

View File

@ -5,15 +5,27 @@
<p>余额{{ userBalanceData.userBalance || '加载中...' }}</p> <p>余额{{ userBalanceData.userBalance || '加载中...' }}</p>
<el-button type="primary" @click="navigateToRecharge"></el-button> <el-button type="primary" @click="navigateToRecharge"></el-button>
</el-card> </el-card>
<el-card style="margin-top: 50px">
<div>
<!-- 装ECharts的容器 -->
<div id="main" style="width: 100%; height: 520px; background: #fff">
</div>
</div>
</el-card>
</div> </div>
</template> </template>
<script> <script>
import { userBalance } from "@/api/system/user"; // import { userBalance } from "@/api/system/user"; //
import * as echarts from 'echarts' //echarts
export default { export default {
data() { data() {
return { return {
charts: "",
opinionData: ["155", "400", "900", "800", "300", "900", "270", "684", "165", "0", "300", "150"], //
userBalanceData: { userBalanceData: {
userBalance: '加载中...' userBalance: '加载中...'
} }
@ -22,6 +34,9 @@ export default {
created() { created() {
this.fetchUserBalance(); this.fetchUserBalance();
}, },
mounted() {
this.drawLine();
},
methods: { methods: {
navigateToRecharge() { navigateToRecharge() {
this.$router.push('/money/zfb'); this.$router.push('/money/zfb');
@ -37,7 +52,7 @@ export default {
const response = await userBalance(userId); const response = await userBalance(userId);
if (response.data) { if (response.data) {
console.log(response.data) console.log(response.data)
this.userBalanceData.userBalance = response.data; // response.data this.userBalanceData.userBalance = response.data;
} else { } else {
this.userBalanceData.userBalance = {userBalance: '获取失败'}; this.userBalanceData.userBalance = {userBalance: '获取失败'};
} }
@ -46,6 +61,99 @@ export default {
console.error('Error fetching user balance:', error); console.error('Error fetching user balance:', error);
} }
}, },
drawLine() {
// 线
this.charts = echarts.init(document.getElementById('main'));
// 线
this.charts.setOption({
title: {
left: "3%",
top: "5%",
text: "近12月消费趋势", //
},
tooltip: {
trigger: "axis",
},
legend: {
align: "right",
left: "3%",
top: "15%",
data: ["消费金额"], //
},
grid: {
top: "30%",
left: "5%",
right: "5%",
bottom: "5%",
containLabel: true,
},
toolbox: {
feature: {
saveAsImage: {},
},
},
// x
xAxis: {
type: "category",
boundaryGap: true,
axisTick: {
alignWithLabel: true,
},
//
data: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
},
// y
yAxis: {
type: "value",
boundaryGap: true,
splitNumber: 4,
interval: 250,
},
//
series: [
{
name: "消费金额", //
type: "line",
stack: "总量", //
data: this.opinionData, //
areaStyle: {
color: {
type: "linear",
x: 0,
y: 0,
x2: 0,
y2: 1,
colorStops: [
{
offset: 0,
color: "rgb(255,200,213)",
},
{
offset: 1,
color: "#ffffff",
},
],
global: false,
},
},
itemStyle: {
color: "rgb(255,96,64)",
lineStyle: {
color: "rgb(255,96,64)",
},
},
},
],
});
},
}, },
}; };
</script> </script>