import React from 'react'; const TooltipUtil = (tooltipProps: any) => { const props = tooltipProps.graphProps; const [referenceLines, setReferenceLines] = React.useState(null); const handle = (e: any) => { e.persist(); const bbox = e.target.getBoundingClientRect(); const tickSize = 10; //TODO: should be tick size const relY = e.clientY - bbox.top; const relX = e.clientX - bbox.left; let points: any; //TODO speed up, calc bb of graph, if the cursor is not within bounds return switch (tooltipProps.mode) { case 'closest': points = props.formattedGraphicalItems.reduce( //TODO pass axis key to payload (acc: any, items: any) => acc.concat(items.props.points), [] ); break; default: case 'compare': points = props.formattedGraphicalItems.reduce( (acc: any, items: any) => { if (acc.length == 0) { return items.props.points; } else { return acc.map( (point: any, i: number) => Object.assign({}, point, { y: relY }) ); } }, [] ); break; } let minDist: number = Infinity; let index: any points && points.forEach((point: any, _index: number) => { if (point == null) return; //inactive const dist = Math.sqrt((relX-point.x)**2) + Math.sqrt((relY-point.y)**2); if (dist < minDist) { minDist = dist; index = _index; } }); if (Math.abs(minDist) > tickSize) { tooltipProps.onCoordChange(null); setReferenceLines(null); } else { //TODO use same api as recharts tooltipProps.onCoordChange(points[index]); setReferenceLines(points[index]); } } const leaveHandle = (e: any) => { tooltipProps.onCoordChange(null); setReferenceLines(null); } const referenceBox = 20; //TODO font size return ( {(referenceLines && tooltipProps.enableReferenceLines) && {referenceLines.value} {referenceLines.value} } ) } export default TooltipUtil;