diff --git a/frontend/src/pages/CompanyDetail.tsx b/frontend/src/pages/CompanyDetail.tsx index 6097c04..ceb5810 100644 --- a/frontend/src/pages/CompanyDetail.tsx +++ b/frontend/src/pages/CompanyDetail.tsx @@ -590,9 +590,9 @@ function TrendTooltip({ active, payload, label }: Record) { function TrendHistoryChart({ trends, latestTrends, ticker, marketPrices }: { trends: TrendSummary[]; latestTrends: TrendSummary[]; ticker: string; marketPrices: MarketPrice[] }) { const [selectedWindow, setSelectedWindow] = useState('7d'); - // Use history data for charts + // Use history data for charts — filter to selected window and time range const filtered = (trends ?? []) - .filter((t) => t.entity_id === ticker && t.window === selectedWindow) + .filter((t) => t.entity_id === ticker && t.window === selectedWindow && new Date(t.generated_at).getTime() >= cutoffTs) .sort((a, b) => new Date(a.generated_at).getTime() - new Date(b.generated_at).getTime()); // Build a price lookup — match by closest timestamp to each trend point @@ -600,11 +600,25 @@ function TrendHistoryChart({ trends, latestTrends, ticker, marketPrices }: { tre .filter((p) => p.bar_timestamp != null && p.close != null) .sort((a, b) => a.bar_timestamp - b.bar_timestamp); + // Determine the time range for the selected window to filter prices + const windowHours: Record = { + intraday: 12, + '1d': 24, + '7d': 7 * 24, + '30d': 30 * 24, + '90d': 90 * 24, + }; + const hoursBack = windowHours[selectedWindow] ?? 7 * 24; + const cutoffTs = Date.now() - hoursBack * 3600_000; + + // Filter prices to the selected window's time range + const windowPrices = sortedPrices.filter((p) => p.bar_timestamp >= cutoffTs); + function findClosestPrice(ts: number): number | undefined { - if (sortedPrices.length === 0) return undefined; - let best = sortedPrices[0]; + if (windowPrices.length === 0) return undefined; + let best = windowPrices[0]; let bestDiff = Math.abs(ts - best.bar_timestamp); - for (const p of sortedPrices) { + for (const p of windowPrices) { const diff = Math.abs(ts - p.bar_timestamp); if (diff < bestDiff) { best = p;