import { useState } from 'react' import { AreaChart, Area, XAxis, YAxis, Tooltip, BarChart, Bar } from 'recharts' import useDimensions from 'react-cool-dimensions' const Chart = ({ title, xAxis, yAxis, data, labelFormat, type }) => { const [mouseData, setMouseData] = useState(null) // @ts-ignore const { observe, width, height } = useDimensions() const handleMouseMove = (coords) => { if (coords.activePayload) { setMouseData(coords.activePayload[0].payload) } } const handleMouseLeave = () => { setMouseData(null) } return (
{title}
{mouseData ? ( <>
{labelFormat(mouseData[yAxis])}
{new Date(mouseData[xAxis]).toDateString()}
) : data.length > 0 && data[data.length - 1][yAxis] ? ( <>
{labelFormat(data[data.length - 1][yAxis])}
{new Date(data[data.length - 1][xAxis]).toDateString()}
) : ( <>
)}
{width > 0 && type === 'area' ? ( } /> ) : null} {width > 0 && type === 'bar' ? ( } /> ) : null}
) } export default Chart