import dayjs from 'dayjs' import { useState } from 'react' import { Area, AreaChart as RechartAreaChart, Tooltip, XAxis } from 'recharts' import { CHART_COLORS, DateTimeFormats } from 'components/ui/Charts/Charts.constants' import ChartHeader from './ChartHeader' import type { CommonChartProps, Datum } from './Charts.types' import { numberFormatter, useChartSize } from './Charts.utils' import NoDataPlaceholder from './NoDataPlaceholder' export interface AreaChartProps extends CommonChartProps { yAxisKey: string xAxisKey: string format?: string customDateFormat?: string displayDateInUtc?: boolean } const AreaChart = ({ data, yAxisKey, xAxisKey, format, customDateFormat = DateTimeFormats.FULL, title, highlightedValue, highlightedLabel, displayDateInUtc, minimalHeader, className = '', valuePrecision, size = 'normal', }: AreaChartProps) => { const { Container } = useChartSize(size) const [focusDataIndex, setFocusDataIndex] = useState(null) const day = (value: number | string) => (displayDateInUtc ? dayjs(value).utc() : dayjs(value)) const resolvedHighlightedLabel = (focusDataIndex !== null && data && data[focusDataIndex] !== undefined && day(data[focusDataIndex][xAxisKey]).format(customDateFormat)) || highlightedLabel const resolvedHighlightedValue = focusDataIndex !== null ? data[focusDataIndex]?.[yAxisKey] : highlightedValue if (data.length === 0) { return ( ) } return (
{ if (e.activeTooltipIndex !== focusDataIndex) { setFocusDataIndex(e.activeTooltipIndex) } }} onMouseLeave={() => setFocusDataIndex(null)} > null} /> {data && (
{dayjs(data[0][xAxisKey]).format(customDateFormat)} {dayjs(data[data?.length - 1]?.[xAxisKey]).format(customDateFormat)}
)}
) } export default AreaChart