|
| 1 | +import React from 'react' |
| 2 | +import styles from './ArcMetric.module.css' |
| 3 | + |
| 4 | +/** |
| 5 | + * ArcMetric component |
| 6 | + * @param {Object} props |
| 7 | + * @param {number} props.value - The current value to display in the arc |
| 8 | + * @param {number} props.max - The maximum value for the arc (for progress calculation) |
| 9 | + * @param {string} props.unit - The unit of measure (e.g., 'MB') |
| 10 | + * @param {string} props.title - The title to display at the top |
| 11 | + * @param {React.ReactNode} props.helper - The helper text (can be ReactNode) |
| 12 | + */ |
| 13 | +export default function ArcMetric ({ value, max, unit, title, helper }) { |
| 14 | + // Arc settings |
| 15 | + const radius = 150 |
| 16 | + const progress = Math.max(0, Math.min(1, value / max)) |
| 17 | + |
| 18 | + return ( |
| 19 | + <div className={styles.container}> |
| 20 | + <div className={styles.headerRow}> |
| 21 | + <span className={styles.title}>{title}</span> |
| 22 | + <span className={styles.infoIcon}>ⓘ</span> |
| 23 | + </div> |
| 24 | + <div className={styles.arcRow}> |
| 25 | + <SemiCircleProgress |
| 26 | + percentage={progress * 100} |
| 27 | + strokeWidth={10} |
| 28 | + diameter={radius * 2} |
| 29 | + orientation='up' |
| 30 | + direction='right' |
| 31 | + showPercentValue |
| 32 | + value={value} |
| 33 | + unit={unit} |
| 34 | + /> |
| 35 | + <div className={styles.helperBox}>{helper}</div> |
| 36 | + </div> |
| 37 | + </div> |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +export function SemiCircleProgress ({ |
| 42 | + stroke = '#fff', |
| 43 | + strokeWidth = 10, |
| 44 | + background = '#33373C', |
| 45 | + diameter = 200, |
| 46 | + orientation = 'up', |
| 47 | + direction = 'right', |
| 48 | + showPercentValue = false, |
| 49 | + percentage, |
| 50 | + value, |
| 51 | + unit |
| 52 | +}) { |
| 53 | + const coordinateForCircle = diameter / 2 |
| 54 | + const radius = (diameter - 2 * strokeWidth) / 2 |
| 55 | + const circumference = Math.PI * radius |
| 56 | + |
| 57 | + let percentageValue |
| 58 | + if (percentage > 100) { |
| 59 | + percentageValue = 100 |
| 60 | + } else if (percentage < 0) { |
| 61 | + percentageValue = 0 |
| 62 | + } else { |
| 63 | + percentageValue = percentage |
| 64 | + } |
| 65 | + const semiCirclePercentage = percentageValue * (circumference / 100) |
| 66 | + |
| 67 | + let rotation |
| 68 | + if (orientation === 'down') { |
| 69 | + if (direction === 'left') { |
| 70 | + rotation = 'rotate(180deg) rotateY(180deg)' |
| 71 | + } else { |
| 72 | + rotation = 'rotate(180deg)' |
| 73 | + } |
| 74 | + } else { |
| 75 | + if (direction === 'right') { |
| 76 | + rotation = 'rotateY(180deg)' |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return ( |
| 81 | + <div className='semicircle-container' style={{ position: 'relative' }}> |
| 82 | + <svg |
| 83 | + width={diameter} |
| 84 | + height={diameter / 2} |
| 85 | + style={{ transform: rotation, overflow: 'hidden' }} |
| 86 | + > |
| 87 | + <circle |
| 88 | + cx={coordinateForCircle} |
| 89 | + cy={coordinateForCircle} |
| 90 | + r={radius} |
| 91 | + fill='none' |
| 92 | + stroke={background} |
| 93 | + strokeWidth={strokeWidth} |
| 94 | + strokeDasharray={circumference} |
| 95 | + style={{ |
| 96 | + strokeDashoffset: circumference |
| 97 | + }} |
| 98 | + /> |
| 99 | + <circle |
| 100 | + cx={coordinateForCircle} |
| 101 | + cy={coordinateForCircle} |
| 102 | + r={radius} |
| 103 | + fill='none' |
| 104 | + stroke={stroke} |
| 105 | + strokeWidth={strokeWidth} |
| 106 | + strokeDasharray={circumference} |
| 107 | + style={{ |
| 108 | + strokeDashoffset: semiCirclePercentage, |
| 109 | + transition: |
| 110 | + 'stroke-dashoffset .3s ease 0s, stroke-dasharray .3s ease 0s, stroke .3s' |
| 111 | + }} |
| 112 | + /> |
| 113 | + </svg> |
| 114 | + {showPercentValue && ( |
| 115 | + <div className='flex items-center justify-center gap-2 absolute bottom-0 left-0 right-0 text-white text-[2.3rem] font-bold'> |
| 116 | + <span>{value}</span> |
| 117 | + <span className='text-white/70 text-[1.2rem] font-normal'>{unit}</span> |
| 118 | + </div> |
| 119 | + |
| 120 | + )} |
| 121 | + </div> |
| 122 | + ) |
| 123 | +} |
| 124 | + |
| 125 | +// function percentageValidation (isRequired) { |
| 126 | +// return function (props, propName, componentName) { |
| 127 | +// const prop = props[propName] |
| 128 | +// if (prop == null) { |
| 129 | +// if (isRequired) { |
| 130 | +// throw new Error('Percentage is a required prop.') |
| 131 | +// } |
| 132 | +// } else { |
| 133 | +// if (typeof prop !== 'number') { |
| 134 | +// return new Error( |
| 135 | +// 'Invalid percentage. Must be a number between 0 and 100.' |
| 136 | +// ) |
| 137 | +// } |
| 138 | +// if (props[propName] < 0 || props[propName] > 100) { |
| 139 | +// return new Error( |
| 140 | +// 'Invalid percentage. Must be a number between 0 and 100.' |
| 141 | +// ) |
| 142 | +// } |
| 143 | +// } |
| 144 | +// } |
| 145 | +// } |
0 commit comments