import React, { useCallback, useRef } from 'react'; import { Area, CartesianGrid, ComposedChart, Line, ReferenceLine, Tooltip, TooltipContentProps, XAxis, YAxis, } from 'recharts'; import styled, { useTheme } from 'styled-components'; import { fontSize } from '../../../style/theme'; import { ChartHeader, StyledResponsiveContainer } from '../common/SharedComponents'; import { formatTickValue, getTicks } from '../common/chartUtils'; import { formatXAxisLabel } from './LineTimeSerieChart.utils'; import { LineChartProps, CHART_PRESETS } from './LineTimeSerieChart.types'; import { LineTimeSerieChartTooltip } from './LineTimeSerieChartTooltip'; import { useChartHover } from './useChartHover'; import { useChartData } from './useChartData'; const LineTemporalChartWrapper = styled.div` display: flex; flex-direction: column; justify-content: flex-start; `; /** * LineTimeSerieChart - A time series line chart component * * @param series - The data series to display (can be symmetrical with above/below) * @param title - The title of the chart * @param height - The height of the chart in pixels * @param startingTimeStamp - Starting timestamp in seconds * @param interval - Interval between data points in seconds * @param duration - Total duration of the chart in seconds * @param unitRange - Configuration for automatic unit scaling * @param syncId - ID to synchronize multiple charts * @param isLoading - Whether to show loading state * @param yAxisType - Type of Y-axis: 'default', 'percentage', or 'symmetrical' * @param yAxisTitle - Label for the Y-axis * @param helpText - Help text shown as tooltip * @param renderTooltip - Custom tooltip renderer */ export function LineTimeSerieChart({ series, title, height, startingTimeStamp, interval, duration, unitRange, isLoading = false, yAxisType = 'default', yAxisTitle, helpText, rightTitle, displayPreset = 'default', displayOptions, syncId, renderTooltip, }: LineChartProps) { const presetOptions = CHART_PRESETS[displayPreset]; const resolvedNoBackground = displayOptions?.noBackground ?? presetOptions.noBackground; const resolvedShowHorizontalGridLines = displayOptions?.showHorizontalGridLines ?? presetOptions.showHorizontalGridLines; const resolvedNoHeader = displayOptions?.noHeader ?? presetOptions.noHeader; const resolvedNoYAxisLine = displayOptions?.noYAxisLine ?? presetOptions.noYAxisLine; const resolvedNoTickLine = displayOptions?.noTickLine ?? presetOptions.noTickLine; const theme = useTheme(); const chartRef = useRef(null); // Hover state management for tooltip display const { handleMouseEnter, handleMouseLeave, chartId } = useChartHover(); // Process chart data const { rechartsData, topDomain, topValue, unitLabel, xAxisTicks, linesToRender, belowSeriesLabels, } = useChartData({ series, startingTimeStamp, duration, interval, yAxisType, unitRange, }); // Format X-axis labels based on duration const formatXAxisLabelCallback = useCallback( (timestamp: number) => formatXAxisLabel(timestamp, duration), [duration], ); // Format Y-axis tick values const tickFormatter = useCallback( (value: number) => formatTickValue(value, topValue), [topValue], ); return ( {!resolvedNoHeader && ( )} {/* Gradient definitions for series with withGradient */} {linesToRender.filter((l) => l.withGradient).map((line) => ( ))} ) => ( )} /> {/* Horizontal reference line at y=0 for symmetrical charts */} {yAxisType === 'symmetrical' && ( )} {/* Chart lines — Area for gradient series, Line otherwise */} {linesToRender.map((line) => line.withGradient ? ( ) : ( ) )} ); }