MemberStatisticsCard.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <el-card shadow="never">
  3. <template #header>
  4. <CardTitle title="用户统计" />
  5. </template>
  6. <!-- 折线图 -->
  7. <Echart :height="300" :options="lineChartOptions" />
  8. </el-card>
  9. </template>
  10. <script lang="ts" setup>
  11. import dayjs from 'dayjs'
  12. import { EChartsOption } from 'echarts'
  13. import * as MemberStatisticsApi from '@/api/mall/statistics/member'
  14. import { formatDate } from '@/utils/formatTime'
  15. import { CardTitle } from '@/components/Card'
  16. /** 会员用户统计卡片 */
  17. defineOptions({ name: 'MemberStatisticsCard' })
  18. const loading = ref(true) // 加载中
  19. /** 折线图配置 */
  20. const lineChartOptions = reactive<EChartsOption>({
  21. dataset: {
  22. dimensions: ['date', 'count'],
  23. source: []
  24. },
  25. grid: {
  26. left: 20,
  27. right: 20,
  28. bottom: 20,
  29. top: 80,
  30. containLabel: true
  31. },
  32. legend: {
  33. top: 50
  34. },
  35. series: [{ name: '注册量', type: 'line', smooth: true, areaStyle: {} }],
  36. toolbox: {
  37. feature: {
  38. // 数据区域缩放
  39. dataZoom: {
  40. yAxisIndex: false // Y轴不缩放
  41. },
  42. brush: {
  43. type: ['lineX', 'clear'] // 区域缩放按钮、还原按钮
  44. },
  45. saveAsImage: { show: true, name: '会员统计' } // 保存为图片
  46. }
  47. },
  48. tooltip: {
  49. trigger: 'axis',
  50. axisPointer: {
  51. type: 'cross'
  52. },
  53. padding: [5, 10]
  54. },
  55. xAxis: {
  56. type: 'category',
  57. boundaryGap: false,
  58. axisTick: {
  59. show: false
  60. },
  61. axisLabel: {
  62. formatter: (date: string) => formatDate(date, 'MM-DD')
  63. }
  64. },
  65. yAxis: {
  66. axisTick: {
  67. show: false
  68. }
  69. }
  70. }) as EChartsOption
  71. const getMemberRegisterCountList = async () => {
  72. loading.value = true
  73. // 查询最近一月数据
  74. const beginTime = dayjs().subtract(30, 'd').startOf('d')
  75. const endTime = dayjs().endOf('d')
  76. const list = await MemberStatisticsApi.getMemberRegisterCountList(beginTime, endTime)
  77. // 更新 Echarts 数据
  78. if (lineChartOptions.dataset && lineChartOptions.dataset['source']) {
  79. lineChartOptions.dataset['source'] = list
  80. }
  81. loading.value = false
  82. }
  83. /** 初始化 **/
  84. onMounted(() => {
  85. getMemberRegisterCountList()
  86. })
  87. </script>