import dayjs from 'dayjs' import { useState } from 'react' import { Area, AreaChart as RechartAreaChart, Tooltip, XAxis } from 'recharts' import { useChartHoverState } from './useChartHoverState' 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 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) 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) } setHover(e.activeTooltipIndex) }} onMouseLeave={() => { setFocusDataIndex(null) clearHover() }} > syncId && syncTooltip && hoveredIndex !== null ? (
{dayjs(data[hoveredIndex]?.[xAxisKey]).format(customDateFormat)}
{numberFormatter(Number(data[hoveredIndex]?.[yAxisKey]) || 0, valuePrecision)} {format}
) : null } />
{data && (
{dayjs(data[0][xAxisKey]).format(customDateFormat)} {dayjs(data[data?.length - 1]?.[xAxisKey]).format(customDateFormat)}
)}
) } export default AreaChart