|
| 1 | +import React, { Fragment, useState, useCallback } from "react" |
| 2 | +import useThemeColors from "../../hooks/use-colors" |
| 3 | + |
| 4 | +const HOV_SIZE = 50 |
| 5 | +export const Point = ({ x, y, value, t, color, width }) => { |
| 6 | + const [showPoint, setShowPoint] = useState(false) |
| 7 | + const onShow = useCallback(() => setShowPoint(true), [setShowPoint]) |
| 8 | + const onHide = useCallback(() => setShowPoint(false), [setShowPoint]) |
| 9 | + return ( |
| 10 | + <g onMouseEnter={onShow} onMouseLeave={onHide}> |
| 11 | + {showPoint && ( |
| 12 | + <Fragment> |
| 13 | + <text x={x + 10} y={y - 10} fill={color}> |
| 14 | + {value} |
| 15 | + </text> |
| 16 | + <circle cx={x} cy={y} r={5} fill={color} /> |
| 17 | + </Fragment> |
| 18 | + )} |
| 19 | + <rect |
| 20 | + x={x - width / 2} |
| 21 | + y={y - HOV_SIZE / 2} |
| 22 | + width={width} |
| 23 | + height={HOV_SIZE} |
| 24 | + fill="transparent" |
| 25 | + // If you're debugging the hover region, uncomment the following |
| 26 | + // fill="rgba(0,0,0,0.5)" |
| 27 | + ></rect> |
| 28 | + </g> |
| 29 | + ) |
| 30 | +} |
| 31 | + |
| 32 | +export const HighlightValueLabels = ({ |
| 33 | + visibleTransformedPointsOnCurves = [], |
| 34 | + curveColors = [], |
| 35 | +}) => { |
| 36 | + return ( |
| 37 | + <g> |
| 38 | + {visibleTransformedPointsOnCurves.flatMap((points, curveIndex) => |
| 39 | + points.map((p, i) => ( |
| 40 | + <Point |
| 41 | + key={`${curveIndex},${i}`} |
| 42 | + color={curveColors[curveIndex]} |
| 43 | + width={ |
| 44 | + i === 0 || i === points.length - 1 |
| 45 | + ? HOV_SIZE |
| 46 | + : (points[i + 1].x - points[i - 1].x) / 2 |
| 47 | + } |
| 48 | + {...p} |
| 49 | + /> |
| 50 | + )) |
| 51 | + )} |
| 52 | + </g> |
| 53 | + ) |
| 54 | +} |
| 55 | + |
| 56 | +export default HighlightValueLabels |
0 commit comments