import { useState } from 'react' import { Area, AreaChart as RechartAreaChart, Tooltip, XAxis } from 'recharts' import { ChartHeader } from './ChartHeader' import type { CommonChartProps, Datum } from './Charts.types' import { numberFormatter, useChartSize } from './Charts.utils' import NoDataPlaceholder from './NoDataPlaceholder' import { useChartHoverState } from './useChartHoverState' import { CHART_COLORS, DateTimeFormats } from '@/components/ui/Charts/Charts.constants' import { formatDateTime, useFormatDateTime } from '@/lib/datetime' export interface AreaChartProps extends CommonChartProps { yAxisKey: string xAxisKey: string format?: string customDateFormat?: string displayDateInUtc?: boolean syncId?: string } const AreaChart = ({ data, yAxisKey, xAxisKey, format, customDateFormat = DateTimeFormats.FULL, title, highlightedValue, highlightedLabel, displayDateInUtc, minimalHeader, className = '', valuePrecision, size = 'normal', syncId, }: AreaChartProps) => { const { Container } = useChartSize(size) const { hoveredIndex, syncTooltip, setHover, clearHover } = useChartHoverState( syncId || 'default' ) const [focusDataIndex, setFocusDataIndex] = useState(null) // When `displayDateInUtc` is set the chart explicitly wants UTC labels (used // by views that display server time). Otherwise honour the user's selected // timezone via the picker, which `useFormatDateTime` reads from context. const formatPickerDate = useFormatDateTime() const formatChartDate = (value: number | string) => displayDateInUtc ? formatDateTime(value, { tz: 'UTC', format: customDateFormat }) : formatPickerDate(value, customDateFormat) const resolvedHighlightedLabel = (focusDataIndex !== null && data && data[focusDataIndex] !== undefined && formatChartDate(data[focusDataIndex][xAxisKey])) || highlightedLabel const resolvedHighlightedValue = focusDataIndex !== null ? data[focusDataIndex]?.[yAxisKey] : highlightedValue if (data.length === 0) { return ( ) } return (
{ if (e.activeTooltipIndex !== focusDataIndex) { setFocusDataIndex(e.activeTooltipIndex) } setHover(e.activeTooltipIndex) }} onMouseLeave={() => { setFocusDataIndex(null) clearHover() }} > syncId && syncTooltip && hoveredIndex !== null ? (
{formatChartDate(data[hoveredIndex]?.[xAxisKey] as number | string)}
{numberFormatter(Number(data[hoveredIndex]?.[yAxisKey]) || 0, valuePrecision)} {format}
) : null } />
{data && (
{formatChartDate(data[0][xAxisKey] as number | string)} {formatChartDate(data[data?.length - 1]?.[xAxisKey] as number | string)}
)}
) } export default AreaChart