|
| 1 | +<template> |
| 2 | + <div class="wd-content-body"> |
| 3 | + <h4 class="chart-title">{{ config.title }}</h4> |
| 4 | + <div class="date-range"> |
| 5 | + <div class="mr16 my-date-picker"> |
| 6 | + <FDatePicker type="daterange" v-model="dateRange" @change="updateDateRange" /> |
| 7 | + </div> |
| 8 | + <FButton key="btn-1" class="mr16" :class="{ 'my-btn': true, active: days === 7 }" @click="updateDays(7)"> |
| 9 | + 最近7天 |
| 10 | + </FButton> |
| 11 | + <FButton key="btn-2" :class="{ 'my-btn': true, active: days === 30 }" @click="updateDays(30)"> |
| 12 | + 最近30天 |
| 13 | + </FButton> |
| 14 | + </div> |
| 15 | + <div :id="chartId" class="chart-container"> |
| 16 | + </div> |
| 17 | + </div> |
| 18 | +</template> |
| 19 | + |
| 20 | +<script setup lang="ts"> |
| 21 | +import { ref, computed } from 'vue'; |
| 22 | +import { FDatePicker, FButton } from '@fesjs/fes-design'; |
| 23 | +import { useChart, type ChartConfig } from './useChart'; |
| 24 | +import { |
| 25 | + getYear, getMonth, getDate, subDays, differenceInDays, |
| 26 | +} from 'date-fns'; |
| 27 | +
|
| 28 | +
|
| 29 | +
|
| 30 | +interface Props { |
| 31 | + chartId: string; |
| 32 | + config: ChartConfig; |
| 33 | + initialDays?: number; |
| 34 | +} |
| 35 | +
|
| 36 | +const props = defineProps<Props>(); |
| 37 | +
|
| 38 | +// 日期范围相关 |
| 39 | +const days = ref(props.initialDays || 7); |
| 40 | +const initialEndDate = new Date().getTime(); |
| 41 | +const initialStartDate = subDays(new Date(initialEndDate), days.value - 1).getTime(); |
| 42 | +const endDate = ref(initialEndDate); |
| 43 | +const startDate = ref(initialStartDate); |
| 44 | +
|
| 45 | +// 初始化时,开始日期在前,结束日期在后 |
| 46 | +const dateRange = ref([initialStartDate, initialEndDate]); |
| 47 | +
|
| 48 | +const updateDateRange = (range: number[]) => { |
| 49 | + const [startStamp, endStamp] = range; |
| 50 | + startDate.value = startStamp; |
| 51 | + endDate.value = endStamp; |
| 52 | + const now = Date.now(); |
| 53 | + if (startStamp && endStamp) { |
| 54 | + const isToday = getYear(now) === getYear(endStamp) |
| 55 | + && getMonth(now) === getMonth(endStamp) |
| 56 | + && getDate(now) === getDate(endStamp); |
| 57 | + const daysDiff = differenceInDays(endStamp, startStamp) + 1; |
| 58 | + if (isToday && [7, 30].includes(daysDiff)) { |
| 59 | + days.value = daysDiff; |
| 60 | + } else { |
| 61 | + days.value = 0; |
| 62 | + } |
| 63 | + } |
| 64 | +}; |
| 65 | +
|
| 66 | +const updateDays = (newDays: number) => { |
| 67 | + const _endDate = new Date(); |
| 68 | + const _startDate = subDays(_endDate, newDays - 1); |
| 69 | + dateRange.value = [_startDate.getTime(), _endDate.getTime()]; |
| 70 | + days.value = newDays; |
| 71 | + startDate.value = _startDate.getTime(); |
| 72 | + endDate.value = _endDate.getTime(); |
| 73 | +}; |
| 74 | +
|
| 75 | +// 图表相关 |
| 76 | +const { loading } = useChart( |
| 77 | + props.chartId, |
| 78 | + props.config, |
| 79 | + startDate, |
| 80 | + endDate |
| 81 | +); |
| 82 | +</script> |
0 commit comments