1- import type { Spectrum1D } from '@zakodium/nmrium-core' ;
21import { xFindClosestIndex } from 'ml-spectra-processing' ;
32import { Fragment , useMemo } from 'react' ;
43import { MF } from 'react-mf' ;
@@ -16,13 +15,14 @@ import { useActiveSpectrum } from '../hooks/useActiveSpectrum.js';
1615import { useFormatNumberByNucleus } from '../hooks/useFormatNumberByNucleus.js' ;
1716import { options } from '../toolbar/ToolTypes.js' ;
1817
18+ import type { Spectrum1DTraces } from './useTracesSpectra.ts' ;
1919import type { Get2DDimensionLayoutReturn } from './utilities/DimensionLayout.js' ;
2020import { LAYOUT , getLayoutID } from './utilities/DimensionLayout.js' ;
2121import { get1DYScale , get2DXScale , get2DYScale } from './utilities/scale.js' ;
2222
2323interface FooterBannerProps {
2424 layout : Get2DDimensionLayoutReturn ;
25- data1D : Spectrum1D [ ] ;
25+ data1D : Spectrum1DTraces ;
2626}
2727
2828export default function FooterBanner ( props : FooterBannerProps ) {
@@ -54,153 +54,128 @@ export default function FooterBanner(props: FooterBannerProps) {
5454
5555 const nuclei = activeTab . split ( ',' ) ;
5656 const [ formatX , formatY ] = useFormatNumberByNucleus ( nuclei ) ;
57+ const hasTraces = data1D . x || data1D . y ;
5758
5859 const scaleX = useMemo ( ( ) => {
59- if ( data1D . length === 0 ) {
60- return get2DXScale ( { width , margin , xDomain , mode } ) ;
60+ if ( selectedTool === options . slicing . id || ! trackID ) {
61+ return null ;
6162 }
62- if ( selectedTool !== options . slicing . id ) {
63- switch ( trackID ) {
64- case LAYOUT . top :
65- case LAYOUT . main : {
66- return get2DXScale ( { width, margin, xDomain, mode } ) ;
67- }
68- case LAYOUT . left : {
69- return get2DYScale ( { height, margin, yDomain } ) ;
70- }
71- default :
72- return null ;
73- }
63+
64+ if ( ! hasTraces || trackID === 'MAIN' || trackID === 'TOP' ) {
65+ return get2DXScale ( { width, margin, xDomain, mode } ) ;
7466 }
75- return null ;
67+
68+ return get2DYScale ( { height, margin, yDomain } ) ;
7669 } , [
77- data1D ,
78- height ,
79- margin ,
70+ hasTraces ,
8071 selectedTool ,
81- trackID ,
8272 width ,
73+ margin ,
8374 xDomain ,
84- yDomain ,
8575 mode ,
76+ trackID ,
77+ height ,
78+ yDomain ,
8679 ] ) ;
8780
8881 const scaleY = useMemo ( ( ) => {
89- if ( data1D . length === 0 ) {
82+ if ( selectedTool === options . slicing . id || ! trackID ) {
83+ return null ;
84+ }
85+
86+ if ( ! hasTraces || trackID === 'MAIN' ) {
9087 return get2DYScale ( { height, margin, yDomain } ) ;
9188 }
92- if ( selectedTool !== options . slicing . id ) {
93- switch ( trackID ) {
94- case LAYOUT . main : {
95- return get2DYScale ( { height, margin, yDomain } ) ;
96- }
97- case LAYOUT . top : {
98- return data1D [ 0 ]
99- ? get1DYScale ( yDomains [ data1D [ 0 ] . id ] , margin . top )
100- : null ;
101- }
102- case LAYOUT . left : {
103- return data1D [ 1 ]
104- ? get1DYScale ( yDomains [ data1D [ 1 ] . id ] , margin . left )
105- : null ;
106- }
107- default :
108- return null ;
109- }
89+
90+ if ( trackID === 'TOP' ) {
91+ return data1D . x ? get1DYScale ( yDomains [ data1D . x . id ] , margin . top ) : null ;
11092 }
111- return null ;
112- } , [ data1D , height , margin , selectedTool , trackID , yDomain , yDomains ] ) ;
11393
94+ return data1D . y ? get1DYScale ( yDomains [ data1D . y . id ] , margin . left ) : null ;
95+ } , [
96+ data1D . x ,
97+ data1D . y ,
98+ hasTraces ,
99+ height ,
100+ margin ,
101+ selectedTool ,
102+ trackID ,
103+ yDomain ,
104+ yDomains ,
105+ ] ) ;
114106 if (
115107 ! activeSpectrum ||
116108 ! position ||
117109 position . y < 10 ||
118110 position . x < 10 ||
119111 position . x > width - margin . right ||
120- position . y > height - margin . bottom ||
121- ! data1D
112+ position . y > height - margin . bottom
122113 ) {
123114 return < FooterContainer /> ;
124115 }
125116 const getRealYValue = ( coordinate : any ) => {
126- let index : number | null = null ;
117+ let axis : 'x' | 'y' | null = null ;
127118 if ( trackID === LAYOUT . top ) {
128- index = 0 ;
119+ axis = 'x' ;
129120 } else if ( trackID === LAYOUT . left ) {
130- index = 1 ;
121+ axis = 'y' ;
131122 }
132- if ( index != null && scaleX != null && data1D [ index ] ) {
133- const datum = get1DDataXY ( data1D [ index ] ) ;
134- const xIndex = xFindClosestIndex ( datum . x , scaleX . invert ( coordinate ) ) ;
135- return datum . y [ xIndex ] ;
123+ const spectrum = axis && data1D [ axis ] ;
124+
125+ if ( ! scaleX || ! spectrum ) {
126+ return 1 ;
136127 }
137- return 1 ;
128+
129+ const datum = get1DDataXY ( spectrum ) ;
130+ const xIndex = xFindClosestIndex ( datum . x , scaleX . invert ( coordinate ) ) ;
131+ return datum . y [ xIndex ] ;
138132 } ;
139133
140134 const getXValue = ( x : number | null = null ) => {
141- if ( scaleX != null ) {
142- switch ( trackID ) {
143- case LAYOUT . main :
144- case LAYOUT . top : {
145- return scaleX . invert ( x || position . x ) ;
146- }
147- case LAYOUT . left : {
148- return scaleX . invert ( x || position . y ) ;
149- }
150- default :
151- return 0 ;
152- }
135+ if ( ! scaleX || ! trackID ) return 0 ;
136+
137+ if ( trackID === 'MAIN' ) {
138+ return scaleX . invert ( x || position . y ) ;
153139 }
154- return 0 ;
140+ // return if the trackID = "MAIN" | "TOP"
141+ return scaleX . invert ( x || position . x ) ;
155142 } ;
156143
157144 const getYValue = ( ) => {
158- if ( scaleY != null ) {
159- switch ( trackID ) {
160- case LAYOUT . main :
161- case LAYOUT . top : {
162- return scaleY . invert ( position . y ) ;
163- }
164- case LAYOUT . left : {
165- return scaleY . invert ( position . x ) ;
166- }
167- default :
168- return 0 ;
169- }
145+ if ( ! scaleY || ! trackID ) return 0 ;
146+
147+ if ( trackID === 'LEFT' ) {
148+ return scaleY . invert ( position . x ) ;
170149 }
171- return 0 ;
150+
151+ return scaleY . invert ( position . y ) ;
172152 } ;
173153
174154 const getRation = ( ) => {
175- switch ( trackID ) {
176- case LAYOUT . top : {
177- return (
178- ( getRealYValue ( startX ) / ( getRealYValue ( endX ) || Number . MIN_VALUE ) ) *
179- 100
180- ) . toFixed ( 2 ) ;
181- }
182- case LAYOUT . left : {
183- return (
184- ( getRealYValue ( startY ) / ( getRealYValue ( endY ) || Number . MIN_VALUE ) ) *
185- 100
186- ) . toFixed ( 2 ) ;
187- }
188- default :
189- return 0 ;
155+ if ( ! trackID || trackID === 'MAIN' ) return 0 ;
156+
157+ if ( trackID === 'TOP' ) {
158+ return (
159+ ( getRealYValue ( startX ) / ( getRealYValue ( endX ) || Number . MIN_VALUE ) ) *
160+ 100
161+ ) . toFixed ( 2 ) ;
190162 }
163+
164+ // tracKId = "LEFT"
165+ return (
166+ ( getRealYValue ( startY ) / ( getRealYValue ( endY ) || Number . MIN_VALUE ) ) *
167+ 100
168+ ) . toFixed ( 2 ) ;
191169 } ;
192170
193171 const getDeltaX = ( ) => {
194- switch ( trackID ) {
195- case LAYOUT . top : {
196- return ( getXValue ( startX ) - getXValue ( endX ) ) . toPrecision ( 6 ) ;
197- }
198- case LAYOUT . left : {
199- return ( getXValue ( startY ) - getXValue ( endY ) ) . toPrecision ( 6 ) ;
200- }
201- default :
202- return 0 ;
172+ if ( ! trackID || trackID === 'MAIN' ) return 0 ;
173+
174+ if ( trackID === 'TOP' ) {
175+ return ( getXValue ( startX ) - getXValue ( endX ) ) . toPrecision ( 6 ) ;
203176 }
177+
178+ return ( getXValue ( startY ) - getXValue ( endY ) ) . toPrecision ( 6 ) ;
204179 } ;
205180
206181 const getLabel = ( label2d : any , label1d : any , nucleus : any ) => {
0 commit comments