diff --git a/src/App.vue b/src/App.vue index 748d2cb..6685f49 100644 --- a/src/App.vue +++ b/src/App.vue @@ -43,7 +43,7 @@ ({ + const { startDate } = getRecentMonthsRange(TOP_TWENTY_SUM_MONTHS) + const topTwentySum: TopTwentySum[] = (data.topTwentyPositionSum ?? []) + .filter((s) => s.tradeDate >= startDate) + .map((s) => ({ tradeDate: s.tradeDate, longSum: s.f009nSum, shortSum: s.f015nSum, - }), - ) + })) const updatedAt = list[0]?.tradeDate || sumLatest?.tradeDate || new Date().toISOString().slice(0, 10) diff --git a/src/components/position/PositionSumBar.vue b/src/components/position/PositionSumBar.vue index d6d954e..8117117 100644 --- a/src/components/position/PositionSumBar.vue +++ b/src/components/position/PositionSumBar.vue @@ -18,18 +18,23 @@ import { import type { EChartsOption } from 'echarts' import VChart from 'vue-echarts' import type { TopTwentySum } from '../../types' +import { getRecentMonthsRange } from '../../utils/tradingDate' use([CanvasRenderer, BarChart, GridComponent, TooltipComponent, LegendComponent]) +/** 仅展示最近 3 个月 */ +const DISPLAY_MONTHS = 3 + const props = defineProps<{ list: TopTwentySum[] }>() const option = computed((): EChartsOption => { - // 接口通常新→旧,图表按时间从左到右 - const sorted = [...props.list].sort((a, b) => - a.tradeDate.localeCompare(b.tradeDate), - ) + const { startDate } = getRecentMonthsRange(DISPLAY_MONTHS) + // 接口通常新→旧,图表按时间从左到右;仅保留最近 3 个月 + const sorted = [...props.list] + .filter((x) => x.tradeDate >= startDate) + .sort((a, b) => a.tradeDate.localeCompare(b.tradeDate)) const dates = sorted.map((x) => x.tradeDate) const longData = sorted.map((x) => x.longSum) const shortData = sorted.map((x) => x.shortSum) diff --git a/src/utils/tradingDate.ts b/src/utils/tradingDate.ts index 5c7a6dc..6e6cc53 100644 --- a/src/utils/tradingDate.ts +++ b/src/utils/tradingDate.ts @@ -51,6 +51,25 @@ export function extractVariety(contract: string): string { return m ? m[1] : contract } +/** + * 最近 N 个月日期区间(含起止日)。 + */ +export function getRecentMonthsRange( + months: number, + from: Date = new Date(), +): { + startDate: string + endDate: string +} { + const end = new Date(from.getFullYear(), from.getMonth(), from.getDate()) + const start = new Date(end) + start.setMonth(start.getMonth() - months) + return { + startDate: formatDateYmd(start), + endDate: formatDateYmd(end), + } +} + /** * 最近一个月日期区间(含起止日),用于机构盈利等按区间查询的接口。 */ @@ -58,11 +77,5 @@ export function getRecentMonthRange(from: Date = new Date()): { startDate: string endDate: string } { - const end = new Date(from.getFullYear(), from.getMonth(), from.getDate()) - const start = new Date(end) - start.setMonth(start.getMonth() - 1) - return { - startDate: formatDateYmd(start), - endDate: formatDateYmd(end), - } + return getRecentMonthsRange(1, from) }