1+ import * as React from 'react' ;
2+ import { Gauge , gaugeClasses } from '@mui/x-charts/Gauge' ;
3+ import { Box } from '@mui/material' ;
4+
5+ interface ScoreGaugeProps {
6+ score : number ;
7+ width ?: number ;
8+ height ?: number ;
9+ }
10+
11+ const ScoreGauge : React . FC < ScoreGaugeProps > = ( { score, width = 200 , height = 200 } ) => {
12+ const getGradientColor = ( value : number ) => {
13+ // Clamp value between 0 and 100
14+ value = Math . max ( 0 , Math . min ( 100 , value ) ) ;
15+
16+ let r , g , b ;
17+
18+ if ( value <= 50 ) {
19+ // Red (255, 0, 0) to Yellow (255, 215, 0)
20+ const ratio = value / 50 ;
21+ r = 255 ;
22+ g = Math . round ( 215 * ratio ) ;
23+ b = 0 ;
24+ } else {
25+ // Yellow (255, 215, 0) to Green (82, 178, 2)
26+ const ratio = ( value - 50 ) / 50 ;
27+ r = Math . round ( 255 + ( 82 - 255 ) * ratio ) ; // Decrease red
28+ g = Math . round ( 215 + ( 178 - 215 ) * ratio ) ; // Shift green
29+ b = Math . round ( 0 + ( 2 - 0 ) * ratio ) ; // Increase blue a little
30+ }
31+
32+ return `rgb(${ r } , ${ g } , ${ b } )` ;
33+ } ;
34+
35+ return (
36+ < Box sx = { { display : 'flex' , justifyContent : 'center' } } >
37+ < Gauge
38+ width = { width }
39+ height = { height }
40+ value = { score }
41+ cornerRadius = "50%"
42+ sx = { ( theme ) => ( {
43+ [ `& .${ gaugeClasses . valueText } ` ] : {
44+ fontSize : 30 ,
45+ fontWeight : 'bold' ,
46+ } ,
47+ [ `& .${ gaugeClasses . valueArc } ` ] : {
48+ fill : getGradientColor ( score ) ,
49+ transition : 'fill 1s ease-in-out' ,
50+ } ,
51+ [ `& .${ gaugeClasses . referenceArc } ` ] : {
52+ fill : theme . palette . text . disabled ,
53+ } ,
54+ } ) }
55+ />
56+ </ Box >
57+ ) ;
58+ } ;
59+
60+ export default ScoreGauge ;
0 commit comments