|
| 1 | +import { useState } from 'react' |
| 2 | +import style from './ring.module.css' |
| 3 | + |
| 4 | +const SIZE = 232 |
| 5 | +const STROKE_WIDTH = 24 |
| 6 | +const STROKE_ARRAY = Math.round(SIZE * 2.7) |
| 7 | +console.log(STROKE_ARRAY) |
| 8 | + |
| 9 | +const ringStyles = (percentage: number, prior: number): string => { |
| 10 | + return ` |
| 11 | + stroke-dasharray: ${STROKE_ARRAY}; |
| 12 | + stroke-dashoffset: ${Math.round(STROKE_ARRAY * (percentage / 100))}; |
| 13 | + transform-origin: ${SIZE / 2}px ${SIZE / 2}px; |
| 14 | + transform: rotate(${-(360 * (prior / 100) + 90)}deg) |
| 15 | + ` |
| 16 | +} |
| 17 | + |
| 18 | +export const Ring: (props: { |
| 19 | + data: { label: string; value: number; color?: string }[] |
| 20 | +}) => any = ({ data }) => { |
| 21 | + let totalRotation = 0 |
| 22 | + |
| 23 | + return ( |
| 24 | + <div style={{ width: '100%' }}> |
| 25 | + <style> |
| 26 | + {data.map((ring, i) => { |
| 27 | + const elStyle = ` |
| 28 | + #${style.ring}-${i} { |
| 29 | + ${ringStyles(ring.value, totalRotation)} |
| 30 | + } |
| 31 | + ` |
| 32 | + |
| 33 | + totalRotation += ring.value |
| 34 | + return elStyle |
| 35 | + })} |
| 36 | + </style> |
| 37 | + |
| 38 | + <div style={{ position: 'relative' }}> |
| 39 | + {data.map((ring, i) => { |
| 40 | + const [hover, setHover] = useState(false) |
| 41 | + |
| 42 | + return ( |
| 43 | + <div |
| 44 | + style={{ position: 'absolute', pointerEvents: 'none' }} |
| 45 | + key={i} |
| 46 | + > |
| 47 | + <div className={style.dropdown}> |
| 48 | + <svg |
| 49 | + style={{ |
| 50 | + height: SIZE, |
| 51 | + width: SIZE, |
| 52 | + // margin: '0 auto', |
| 53 | + display: 'inline', |
| 54 | + }} |
| 55 | + > |
| 56 | + <g> |
| 57 | + <circle |
| 58 | + className={style.ring} |
| 59 | + id={`${style.ring}-${i}`} |
| 60 | + style={{ pointerEvents: 'auto' }} |
| 61 | + r={SIZE / 2 - STROKE_WIDTH} |
| 62 | + cy={SIZE / 2} |
| 63 | + cx={SIZE / 2} |
| 64 | + strokeWidth={STROKE_WIDTH} |
| 65 | + stroke={ring.color || '#69aff4'} |
| 66 | + fill="none" |
| 67 | + onMouseEnter={() => setHover(true)} |
| 68 | + onMouseLeave={() => setHover(false)} |
| 69 | + /> |
| 70 | + </g> |
| 71 | + </svg> |
| 72 | + <div |
| 73 | + className={style.dropdownContent} |
| 74 | + style={{ display: hover ? 'block' : 'none' }} |
| 75 | + > |
| 76 | + <p>{ring.label}</p> |
| 77 | + </div> |
| 78 | + </div> |
| 79 | + </div> |
| 80 | + ) |
| 81 | + })} |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + ) |
| 85 | +} |
0 commit comments