gaoluyang
2026-06-29 27cd042df9aca0383a49f3514bc21958dd890912
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<script lang="ts" setup>
import type { EChartsOption, EchartsUIType } from '../../../../packages/effects/plugins/src/echarts';
 
import type { ErpPurchaseStatisticsApi } from '#/api/erp/statistics/purchase';
import type { ErpSaleStatisticsApi } from '#/api/erp/statistics/sale';
 
import { onMounted, ref, watch } from 'vue';
 
import { EchartsUI, useEcharts } from '../../../../packages/effects/plugins/src/echarts';
 
import { Card } from 'ant-design-vue';
 
import {
  getPurchaseSummary,
  getPurchaseTimeSummary,
} from '#/api/erp/statistics/purchase';
import { getSaleSummary, getSaleTimeSummary } from '#/api/erp/statistics/sale';
 
interface Props {
  title: string;
  type?: 'purchase' | 'sale';
}
 
const props = withDefaults(defineProps<Props>(), {
  type: 'sale',
});
 
/** 销售统计数据 */
const saleSummary = ref<ErpSaleStatisticsApi.SaleSummaryRespVO>(); // 销售概况统计
const saleTimeSummaryList = ref<ErpSaleStatisticsApi.SaleTimeSummaryRespVO[]>(); // 销售时段统计
const getSaleStatistics = async () => {
  saleSummary.value = await getSaleSummary();
  saleTimeSummaryList.value = await getSaleTimeSummary();
};
 
/** 采购统计数据 */
const purchaseSummary = ref<ErpPurchaseStatisticsApi.PurchaseSummaryRespVO>(); // 采购概况统计
const purchaseTimeSummaryList =
  ref<ErpPurchaseStatisticsApi.PurchaseTimeSummaryRespVO[]>(); // 采购时段统计
const getPurchaseStatistics = async () => {
  purchaseSummary.value = await getPurchaseSummary();
  purchaseTimeSummaryList.value = await getPurchaseTimeSummary();
};
 
/** 获取当前类型的时段数据 */
const currentTimeSummaryList = ref<Array<{ price: number; time: string }>>();
 
const chartRef = ref<EchartsUIType>();
const { renderEcharts } = useEcharts(chartRef);
 
/** 折线图配置 */
const lineChartOptions: EChartsOption = {
  grid: {
    left: 20,
    right: 20,
    bottom: 20,
    top: 80,
    containLabel: true,
  },
  legend: {
    top: 50,
  },
  series: [
    {
      name: '金额',
      type: 'line',
      smooth: true,
      areaStyle: {},
      data: [],
    },
  ],
  toolbox: {
    feature: {
      dataZoom: {
        yAxisIndex: false,
      },
      brush: {
        type: ['lineX', 'clear'],
      },
      saveAsImage: {
        show: true,
        name: props.title,
      },
    },
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'cross',
    },
    padding: [5, 10],
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    axisTick: {
      show: false,
    },
    data: [],
  },
  yAxis: {
    axisTick: {
      show: false,
    },
  },
};
 
/** 初始化数据 */
const initData = async () => {
  if (props.type === 'sale') {
    await getSaleStatistics();
    currentTimeSummaryList.value = saleTimeSummaryList.value;
  } else {
    await getPurchaseStatistics();
    currentTimeSummaryList.value = purchaseTimeSummaryList.value;
  }
};
 
/** 监听数据变化并更新图表 */
watch(
  () => currentTimeSummaryList.value,
  (val) => {
    if (!val || val.length === 0) {
      return;
    }
    // 更新图表数据
    const xAxisData = val.map((item) => item.time);
    const seriesData = val.map((item) => item.price);
    const options = {
      ...lineChartOptions,
      xAxis: {
        ...lineChartOptions.xAxis,
        data: xAxisData,
      },
      series: [
        {
          ...(lineChartOptions.series as any)[0],
          data: seriesData,
        },
      ],
    };
    renderEcharts(options);
  },
  { immediate: true },
);
 
/** 组件挂载时初始化数据 */
onMounted(() => {
  initData();
});
</script>
 
<template>
  <Card>
    <template #title>
      <span>{{ title }}</span>
    </template>
    <!-- 折线图 -->
    <EchartsUI ref="chartRef" />
  </Card>
</template>