plus-ui/src/views/dashboard/OrderLineChart.vue

236 lines
5.6 KiB
Vue
Raw Normal View History

<template>
2025-06-07 15:51:05 +08:00
<el-card style="margin: 20px 20px; font-size: 14px">
<template v-slot:header>
<div class="clearfix">
<el-row>
<el-col :span="6">
<div style="font-weight: bold; font-size: 16px">订单统计</div>
</el-col>
<el-col :span="18">
<div style="text-align: right">
<el-radio-group v-model="params.type" size="small" @change="orderStat">
<el-radio-button label="1">近七日</el-radio-button>
<el-radio-button label="2">近三十日</el-radio-button>
</el-radio-group>
</div>
</el-col>
</el-row>
</div>
</template>
<div :style="{ minHeight: height, minWidth: width }">
<div ref="chart" class="chart" :style="{ height: height, width: width }" />
</div>
</el-card>
</template>
<script>
2025-06-07 15:51:05 +08:00
import { orderStatistics } from '@/api/statistics';
import * as echarts from 'echarts';
2025-06-07 15:51:05 +08:00
require('echarts/theme/macarons'); // echarts theme
import resize from './mixins/resize';
export default {
mixins: [resize],
2025-06-07 15:51:05 +08:00
name: 'OrderLineChart',
props: {
width: {
type: String,
default: '100%'
},
height: {
type: String,
default: '440px'
},
autoResize: {
type: Boolean,
default: true
2025-06-07 15:51:05 +08:00
}
},
data() {
return {
pickerOptions: {
2025-06-07 15:51:05 +08:00
shortcuts: [
{
text: '最近一周',
onClick(picker) {
const end = new Date();
const start = new Date();
start.setFullYear(2018);
start.setMonth(10);
start.setDate(1);
end.setTime(start.getTime() + 3600 * 1000 * 24 * 7);
picker.$emit('pick', [start, end]);
}
},
{
text: '最近一月',
onClick(picker) {
const end = new Date();
const start = new Date();
start.setFullYear(2018);
start.setMonth(10);
start.setDate(1);
end.setTime(start.getTime() + 3600 * 1000 * 24 * 30);
picker.$emit('pick', [start, end]);
}
}
2025-06-07 15:51:05 +08:00
]
},
orderCountDate: '',
params: {
type: 2
},
2025-06-07 15:51:05 +08:00
chartData: {}
};
},
mounted() {
this.$nextTick(() => {
2025-06-07 15:51:05 +08:00
this.initChart();
});
},
2025-06-07 15:51:05 +08:00
beforeUnmount() {
if (!this.chart) {
2025-06-07 15:51:05 +08:00
return;
}
2025-06-07 15:51:05 +08:00
this.chart.dispose();
this.chart = null;
},
created() {
2025-06-07 15:51:05 +08:00
this.orderStat();
},
methods: {
initChart() {
2025-06-07 15:51:05 +08:00
this.chart = echarts.init(this.$refs.chart, 'macarons');
this.setOptions(this.chartData);
},
2025-06-07 15:51:05 +08:00
setOptions({ dateList, orderAmount, orderCount } = {}) {
this.chart.setOption({
title: {
text: '订单量趋势',
textStyle: {
fontSize: 14,
fontWeight: 'bolder',
2025-06-07 15:51:05 +08:00
color: '#000000' // 主标题文字颜色
}
},
xAxis: {
data: dateList,
type: 'category',
boundaryGap: false,
axisLabel: {
rotate: 40
},
textStyle: {
2025-06-07 15:51:05 +08:00
fontSize: 12
},
axisTick: {
show: false
},
axisLine: {
lineStyle: {
2025-06-07 15:51:05 +08:00
color: '#000000'
}
2025-06-07 15:51:05 +08:00
}
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross'
},
padding: [5, 10]
},
yAxis: [
{
type: 'value',
name: '金额',
position: 'left',
alignTicks: true,
2025-06-07 15:51:05 +08:00
splitLine: {
show: false
},
2025-06-07 15:51:05 +08:00
axisTick: {
show: false
},
axisLine: {
show: false,
lineStyle: {
2025-06-07 15:51:05 +08:00
color: '#000000'
}
2025-06-07 15:51:05 +08:00
}
},
{
type: 'value',
name: '数量',
2025-06-07 15:51:05 +08:00
minInterval: 1,
position: 'right',
2025-06-07 15:51:05 +08:00
splitLine: {
show: false
},
2025-06-07 15:51:05 +08:00
axisTick: {
show: false
},
axisLine: {
show: false,
lineStyle: {
2025-06-07 15:51:05 +08:00
color: '#000000'
}
2025-06-07 15:51:05 +08:00
}
}
],
legend: {
data: ['订单金额', '订单数']
},
series: [
{
name: '订单金额',
itemStyle: {
normal: {
2025-06-07 15:51:05 +08:00
color: '#5b8ff9'
}
},
type: 'bar',
barMaxWidth: 20,
data: orderAmount,
animationDuration: 2800,
animationEasing: 'cubicInOut'
},
{
name: '订单数',
smooth: true,
type: 'line',
itemStyle: {
normal: {
2025-06-07 15:51:05 +08:00
color: '#5ccfd9'
}
},
yAxisIndex: 1,
data: orderCount,
animationDuration: 2800,
animationEasing: 'quadraticOut'
}
2025-06-07 15:51:05 +08:00
]
});
},
handleDateChange() {
this.getData();
},
2025-06-07 15:51:05 +08:00
orderStat() {
orderStatistics(this.params).then((res) => {
const orderAmount = res.map((it) => {
return it.orderAmount;
});
const orderCount = res.map((it) => {
return it.orderCount;
});
const dateList = res.map((it) => {
return it.date.substr(5);
});
this.chartData = { dateList, orderAmount, orderCount };
this.initChart();
});
}
}
2025-06-07 15:51:05 +08:00
};
</script>
2025-06-07 15:51:05 +08:00
<style scoped lang="scss"></style>