Skip to content

Commit ba87e00

Browse files
committed
fix: Auto-scale Y-axis when filtering chart series
When a series is selected/filtered, the Y-axis now automatically adjusts to fit only the visible series data range instead of maintaining the original scale. This provides better visualization of the selected series without huge empty spaces or peaks.
1 parent 2e17bdd commit ba87e00

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

packages/app/src/HDXMultiSeriesTimeChart.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,44 @@ export const MemoChart = memo(function MemoChart({
489489
});
490490
}, [lineData, displayType, id, isHovered, selectedSeriesNames]);
491491

492+
const yAxisDomain = useMemo(() => {
493+
const hasSelection = selectedSeriesNames && selectedSeriesNames.size > 0;
494+
495+
if (!hasSelection) {
496+
// No selection, let Recharts auto-calculate based on all data
497+
return ['auto', 'auto'];
498+
}
499+
500+
// When series are selected, calculate domain based only on visible series
501+
let minValue = Infinity;
502+
let maxValue = -Infinity;
503+
504+
graphResults.forEach(dataPoint => {
505+
lineData.forEach(ld => {
506+
const seriesName = ld.displayName || ld.dataKey;
507+
// Only consider selected series
508+
if (selectedSeriesNames.has(seriesName)) {
509+
const value = dataPoint[ld.dataKey];
510+
if (typeof value === 'number' && !isNaN(value)) {
511+
minValue = Math.min(minValue, value);
512+
maxValue = Math.max(maxValue, value);
513+
}
514+
}
515+
});
516+
});
517+
518+
// If we found valid values, return them with some padding
519+
if (minValue !== Infinity && maxValue !== -Infinity) {
520+
const padding = (maxValue - minValue) * 0.1; // 10% padding
521+
return [
522+
Math.max(0, minValue - padding), // Don't go below 0
523+
maxValue + padding
524+
];
525+
}
526+
527+
return ['auto', 'auto'];
528+
}, [graphResults, lineData, selectedSeriesNames]);
529+
492530
const sizeRef = useRef<[number, number]>([0, 0]);
493531

494532
const formatTime = useFormatTime();
@@ -680,6 +718,7 @@ export const MemoChart = memo(function MemoChart({
680718
minTickGap={25}
681719
tickFormatter={tickFormatter}
682720
tick={{ fontSize: 11, fontFamily: 'IBM Plex Mono, monospace' }}
721+
domain={yAxisDomain}
683722
/>
684723
{lines}
685724
{isClickActive == null && (

0 commit comments

Comments
 (0)