@@ -38,10 +38,19 @@ const operationMap = {
3838 CUMSUM3D : "Cumulative Sum"
3939} ;
4040
41+ function Num2String ( value : number ) {
42+ if ( Math . abs ( value ) > 1e3 && Math . abs ( value ) < 1e6 || value === 0 ) {
43+ return value . toFixed ( 2 )
44+ } else {
45+ return value . toExponential ( 2 )
46+ }
47+ }
48+
4149const Colorbar = ( { units, metadata, valueScales} : { units : string , metadata : Record < string , any > , valueScales : { maxVal : number , minVal :number } } ) => {
42- const { colormap, variable} = useGlobalStore ( useShallow ( state => ( {
50+ const { colormap, variable, scalingFactor } = useGlobalStore ( useShallow ( state => ( {
4351 colormap : state . colormap ,
44- variable : state . variable
52+ variable : state . variable ,
53+ scalingFactor : state . scalingFactor
4554 } ) ) )
4655 const { cScale, cOffset, setCScale, setCOffset} = usePlotStore ( useShallow ( state => ( {
4756 cScale : state . cScale ,
@@ -61,8 +70,8 @@ const Colorbar = ({units, metadata, valueScales} : {units: string, metadata: Rec
6170 const scaling = useRef < boolean > ( false )
6271 const prevPos = useRef < { x : number | null ; y : number | null } > ( { x : null , y : null } ) ;
6372 const { origMin, origMax} = useMemo ( ( ) => ( {
64- origMin : TwoDecimals ( valueScales . minVal ) ,
65- origMax : TwoDecimals ( valueScales . maxVal )
73+ origMin : valueScales . minVal ,
74+ origMax : valueScales . maxVal
6675 } ) , [ valueScales ] )
6776 const range = origMax - origMin
6877 const zeroPoint = ( 0 - origMin ) / range ; // Used to account for shifting values
@@ -105,9 +114,8 @@ const Colorbar = ({units, metadata, valueScales} : {units: string, metadata: Rec
105114 const thisOffset = deltaX / 100
106115 const lastMin = prevVals . current . min
107116 const lastMax = prevVals . current . max
108- setNewMin ( TwoDecimals ( lastMin + ( range * thisOffset ) ) )
109- setNewMax ( TwoDecimals ( lastMax + ( range * thisOffset ) ) )
110-
117+ setNewMin ( lastMin + ( range * thisOffset ) )
118+ setNewMax ( lastMax + ( range * thisOffset ) )
111119 } ;
112120
113121 // Mouse up handler
@@ -175,7 +183,6 @@ const Colorbar = ({units, metadata, valueScales} : {units: string, metadata: Rec
175183 return `[${ units } ]`
176184 }
177185 } , [ analysisMode , execute ] )
178-
179186 return (
180187 < >
181188 < div className = 'colorbar' >
@@ -185,13 +192,13 @@ const Colorbar = ({units, metadata, valueScales} : {units: string, metadata: Rec
185192 left : `0%` ,
186193 top :'100%' ,
187194 position :'absolute' ,
188- width :`${ String ( newMin ) . length * 8 } px` ,
195+ width :`${ Num2String ( newMin * Math . pow ( 10 , scalingFactor ?? 0 ) ) . length * 8 } px` ,
189196 transform :'translateX(-50%)' ,
190197 textAlign :'right' ,
191198 minWidth :'30px'
192199 } }
193- value = { newMin }
194- onChange = { e => setNewMin ( TwoDecimals ( parseFloat ( e . target . value ) ) ) }
200+ value = { Num2String ( newMin * Math . pow ( 10 , scalingFactor ?? 0 ) ) }
201+ onChange = { e => setNewMin ( parseFloat ( e . target . value ) ) }
195202 />
196203 { Array . from ( { length : tickCount } ) . map ( ( _val , idx ) => {
197204 if ( idx == 0 || idx == tickCount - 1 ) {
@@ -205,7 +212,7 @@ const Colorbar = ({units, metadata, valueScales} : {units: string, metadata: Rec
205212 position :'absolute' ,
206213 transform :'translateX(-50%)' ,
207214 } }
208- > { vals [ idx ] . toFixed ( 2 ) }
215+ > { Num2String ( vals [ idx ] * Math . pow ( 10 , scalingFactor ?? 0 ) ) }
209216 </ p > ) }
210217 ) }
211218 < input type = "number"
@@ -214,13 +221,13 @@ const Colorbar = ({units, metadata, valueScales} : {units: string, metadata: Rec
214221 left : `100%` ,
215222 top :'100%' ,
216223 position :'absolute' ,
217- width :`${ String ( newMax ) . length * 9 + 1 } px` ,
224+ width :`${ Num2String ( newMax * Math . pow ( 10 , scalingFactor ?? 0 ) ) . length * 9 + 1 } px` ,
218225 transform :'translateX(-50%)' ,
219226 textAlign :'right' ,
220227 minWidth :'30px'
221228 } }
222- value = { newMax }
223- onChange = { e => setNewMax ( TwoDecimals ( parseFloat ( e . target . value ) ) ) }
229+ value = { Num2String ( newMax * Math . pow ( 10 , scalingFactor ?? 0 ) ) }
230+ onChange = { e => setNewMax ( parseFloat ( e . target . value ) ) }
224231 />
225232 < canvas id = "colorbar-canvas" ref = { canvasRef } width = { 512 } height = { 24 } onMouseDown = { handleMouseDown } />
226233 < p className = "colorbar-title"
@@ -250,7 +257,6 @@ const Colorbar = ({units, metadata, valueScales} : {units: string, metadata: Rec
250257 < FaPlus className = 'cursor-pointer' onClick = { ( ) => setTickCount ( Math . min ( tickCount + 1 , 10 ) ) } />
251258 </ div >
252259 </ div >
253-
254260 </ >
255261
256262 )
0 commit comments