1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
| <template>
| <el-card style="border-radius: 0" :body-style="{ padding: '1rem 2rem' }" v-loading="loading">
| <template #header>
| <div style="font-weight: bold; font-size: 1.2rem">发货数据统计</div>
| </template>
| <div ref="chartDom" style="height: 25rem; width: 100%"></div>
| </el-card>
| </template>
|
| <script setup>
| import { ref, onMounted, onUnmounted, nextTick } from "vue";
| // 引入echarts
| import * as echarts from "echarts";
| import { GetDeliverDistApi } from "../../api/dashboard";
| import { ElLoading } from 'element-plus'
| const loading=ref(true)
|
| // 创建一个响应式引用来保存DOM元素
| const chartDom = ref(null);
| let chartInstance = null;
|
| const xData = ref([])
| const yData = ref([])
|
| // 初始化ECharts实例并设置配置项(这里以折线图为例,但可灵活替换)
| onMounted(async () => {
| await GetDeliverDistApi().then(res=>{
| console.log(res.data);
|
| xData.value = res.data.map(i=>i.monthNow)
| yData.value = res.data.map(i=>i.deliverCount)
| })
|
|
| await nextTick(); // 确保DOM已经渲染完成
| chartInstance = echarts.init(chartDom.value);
| const option = {
| tooltip: {
| trigger: 'axis',
| axisPointer: {
| type: 'shadow'
| }
| },
| grid: {
| left: '0%',
| right: '0%',
| bottom: '0%',
| top:"2%",
| containLabel: true
| },
| xAxis: [
| {
| type: 'category',
| data: xData.value,
|
| axisTick: {
| alignWithLabel: true
| }
| }
| ],
| yAxis: [
| {
| type: 'value',
| minInterval:1,
| }
| ],
| series: [
| {
| name: '发货数量',
| type: 'bar',
| barWidth: '30%',
| color:"#2aa6fc",
| data: yData.value
| }
| ]
| };
| chartInstance.setOption(option);
| nextTick(() => {
| loading.value = false
| })
| });
|
| // 销毁ECharts实例
| onUnmounted(() => {
| if (chartInstance != null && chartInstance.dispose) {
| chartInstance.dispose();
| }
| });
|
| //窗口大小变化重绘echart
| window.onresize = function () {
| if (chartInstance != null && chartInstance.resize) {
| chartInstance.resize();
| }
| };
| </script>
|
| <style></style>
|
|