-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: preview spectrum from database (#1561)
* feat: add new option to database preferences * feat: customize loading text * refactor: replace deprecated function * feat: load jcamp from database * feat: preference group component * feat: database spectrum preferences define the bottom margin and color for the preview spectrum / resurrect spectra from ranges close #1563 * feat: add spectrum from jcamp if jampURL is exists
- Loading branch information
1 parent
2ebdeb1
commit 24174cc
Showing
12 changed files
with
231 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { HighlightedSource, useHighlightData } from '../../highlight'; | ||
import { usePanelPreferences } from '../../hooks/usePanelPreferences'; | ||
|
||
import DatabaseSpectrum from './DatabaseSpectrum'; | ||
import ResurrectedDatabaseRanges from './ResurrectedDatabaseRanges'; | ||
|
||
function DatabaseElements() { | ||
const { highlight } = useHighlightData(); | ||
const { previewJcamp } = usePanelPreferences('database'); | ||
const { jcampURL } = highlight?.sourceData?.extra || {}; | ||
if (highlight.sourceData?.type !== HighlightedSource.DATABASE) { | ||
return null; | ||
} | ||
|
||
if (previewJcamp && jcampURL) { | ||
return <DatabaseSpectrum />; | ||
} | ||
|
||
return <ResurrectedDatabaseRanges />; | ||
} | ||
|
||
export default DatabaseElements; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { useCallback, useContext, useEffect, useState } from 'react'; | ||
|
||
import { addJcamp } from '../../../data/SpectraManager'; | ||
import { Datum1D } from '../../../data/types/data1d'; | ||
import { useChartData } from '../../context/ChartContext'; | ||
import { useScaleChecked } from '../../context/ScaleContext'; | ||
import { useAlert } from '../../elements/popup/Alert'; | ||
import { HighlightedSource, useHighlightData } from '../../highlight'; | ||
import { usePanelPreferences } from '../../hooks/usePanelPreferences'; | ||
import { spinnerContext } from '../../loader/SpinnerContext'; | ||
import { loadFile } from '../../utility/FileUtility'; | ||
import { getYScale } from '../utilities/scale'; | ||
|
||
function DatabaseSpectrum() { | ||
const { displayerKey, height, verticalAlign, yDomain, margin } = | ||
useChartData(); | ||
const [path, setPath] = useState<string>(); | ||
const [isLoading, setLoading] = useState<boolean>(false); | ||
const { highlight } = useHighlightData(); | ||
const { scaleX } = useScaleChecked(); | ||
const alert = useAlert(); | ||
const { color, marginBottom } = usePanelPreferences('database'); | ||
const { jcampURL: jcampRelativeURL, baseURL } = | ||
highlight?.sourceData?.extra || []; | ||
const getSpinner = useContext(spinnerContext); | ||
|
||
const scaleY = useCallback( | ||
() => | ||
getYScale({ | ||
height: height, | ||
margin: { top: margin.top, bottom: margin.bottom + marginBottom }, | ||
verticalAlign, | ||
yDomain, | ||
}), | ||
[height, margin, marginBottom, verticalAlign, yDomain], | ||
); | ||
|
||
useEffect(() => { | ||
void (async () => { | ||
try { | ||
setLoading(true); | ||
|
||
const jcampURL = new URL(jcampRelativeURL, baseURL); | ||
const result = await loadFile(jcampURL); | ||
const spectra = []; | ||
addJcamp(spectra, result, {}, {}); | ||
setLoading(false); | ||
const spectrum = spectra?.[0] || null; | ||
if (spectrum) { | ||
const { x, re: y } = (spectrum as Datum1D)?.data; | ||
let path = `M ${scaleX()(x[0])} ${scaleY()(y[0])} `; | ||
path += x.slice(1).reduce((accumulator, point, i) => { | ||
accumulator += ` L ${scaleX()(point)} ${scaleY()(y[i + 1])}`; | ||
return accumulator; | ||
}, ''); | ||
setPath(path); | ||
} | ||
} catch (e) { | ||
alert.error('Failed to Load spectrum'); | ||
} | ||
})(); | ||
}, [alert, baseURL, jcampRelativeURL, scaleX, scaleY]); | ||
|
||
if (highlight.sourceData?.type !== HighlightedSource.DATABASE) { | ||
return null; | ||
} | ||
|
||
return isLoading ? ( | ||
<foreignObject width="100%" height="100%"> | ||
{getSpinner('Load Jcamp ....')} | ||
</foreignObject> | ||
) : ( | ||
<g | ||
clipPath={`url(#${displayerKey}clip-chart-1d)`} | ||
className="database-spectrum" | ||
> | ||
<path stroke={color} fill="none" d={path} /> | ||
</g> | ||
); | ||
} | ||
|
||
export default DatabaseSpectrum; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.