fix: company charts X-axis now adjusts to selected window time range
The trend history chart was showing all historical data regardless of which window was selected — only filtering by window name but not by time range. Now filters both trend data and price data to the time range matching the selected window (e.g., 7d shows last 7 days only, 30d shows last 30 days).
This commit is contained in:
@@ -590,9 +590,9 @@ function TrendTooltip({ active, payload, label }: Record<string, unknown>) {
|
|||||||
function TrendHistoryChart({ trends, latestTrends, ticker, marketPrices }: { trends: TrendSummary[]; latestTrends: TrendSummary[]; ticker: string; marketPrices: MarketPrice[] }) {
|
function TrendHistoryChart({ trends, latestTrends, ticker, marketPrices }: { trends: TrendSummary[]; latestTrends: TrendSummary[]; ticker: string; marketPrices: MarketPrice[] }) {
|
||||||
const [selectedWindow, setSelectedWindow] = useState('7d');
|
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 ?? [])
|
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());
|
.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
|
// 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)
|
.filter((p) => p.bar_timestamp != null && p.close != null)
|
||||||
.sort((a, b) => a.bar_timestamp - b.bar_timestamp);
|
.sort((a, b) => a.bar_timestamp - b.bar_timestamp);
|
||||||
|
|
||||||
|
// Determine the time range for the selected window to filter prices
|
||||||
|
const windowHours: Record<string, number> = {
|
||||||
|
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 {
|
function findClosestPrice(ts: number): number | undefined {
|
||||||
if (sortedPrices.length === 0) return undefined;
|
if (windowPrices.length === 0) return undefined;
|
||||||
let best = sortedPrices[0];
|
let best = windowPrices[0];
|
||||||
let bestDiff = Math.abs(ts - best.bar_timestamp);
|
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);
|
const diff = Math.abs(ts - p.bar_timestamp);
|
||||||
if (diff < bestDiff) {
|
if (diff < bestDiff) {
|
||||||
best = p;
|
best = p;
|
||||||
|
|||||||
Reference in New Issue
Block a user