Skip to content

Commit

Permalink
feat: improve inset
Browse files Browse the repository at this point in the history
  • Loading branch information
hamed-musallam committed Jan 29, 2025
1 parent 285c9cb commit e553083
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 51 deletions.
9 changes: 8 additions & 1 deletion src/component/1d/ApodizationLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import useXYReduce, { XYReducerDomainAxis } from '../hooks/useXYReduce.js';
import type { ApodizationOptions } from '../panels/filtersPanel/Filters/hooks/useApodization.js';
import { PathBuilder } from '../utility/PathBuilder.js';

import { useIsInset } from './inset/InsetProvider.js';
import { getYScale } from './utilities/scale.js';

const emptyData = { data: {}, info: {} };
Expand All @@ -36,6 +37,8 @@ export function ApodizationLine() {
const {
toolOptions: { selectedTool },
} = useChartData();
const isInset = useIsInset();

const activeSpectrum = useActiveSpectrum();
const { scaleX } = useScaleChecked();
const spectrum = useSpectrum({ emptyData }) as Spectrum1D;
Expand All @@ -44,7 +47,11 @@ export function ApodizationLine() {
const { sharedFilterOptions: externalApodizationOptions } =
useFilterSyncOptions<ApodizationOptions>();

if (!activeSpectrum?.id || selectedTool !== Filters1D.apodization.name) {
if (
!activeSpectrum?.id ||
selectedTool !== Filters1D.apodization.name ||
!isInset
) {
return null;
}

Expand Down
20 changes: 16 additions & 4 deletions src/component/1d/LinesSeries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,25 @@ const Rect = styled.rect`
}
`;

function LinesSeries() {
function useSpectra() {
const { xDomains, data } = useChartData();
const activeSpectra = useActiveSpectra();
const spectra = (data?.filter(

const inset = useInsetOptions();

if (inset) {
return data?.filter((d) => isSpectrum1D(d) && d.id === inset.spectrumKey);
}

return data?.filter(
(d) => isSpectrum1D(d) && d.display.isVisible && xDomains[d.id],
) || []) as Spectrum1D[];
);
}

function LinesSeries() {
const activeSpectra = useActiveSpectra();
const spectra = useSpectra() as Spectrum1D[];
const { id: insetKey = 'primary' } = useInsetOptions() || {};

return (
<g className="spectra">
{spectra.map((d, i) => (
Expand Down
4 changes: 3 additions & 1 deletion src/component/1d/SpectraLegends.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { SVGGroup } from '../elements/SVGGroup.js';
import { useActiveSpectra } from '../hooks/useActiveSpectra.js';
import { usePanelPreferences } from '../hooks/usePanelPreferences.js';
import { convertPathArrayToString } from '../utility/convertPathArrayToString.js';

Check warning on line 19 in src/component/1d/SpectraLegends.tsx

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

There should be at least one empty line between import groups
import { useIsInset } from './inset/InsetProvider.js';

const styles: Record<'text' | 'colorIndicator', CSSProperties> = {
text: {
Expand Down Expand Up @@ -125,6 +126,7 @@ function SpectraLegends() {
},
xDomains,
} = useChartData();
const isInset = useIsInset();

const { legendsFields } = usePanelPreferences(
'multipleSpectraAnalysis',
Expand All @@ -136,7 +138,7 @@ function SpectraLegends() {
selectedSpectra.map((spectrum) => spectrum.id),
);

if (!showLegend) return null;
if (!showLegend || !isInset) return null;

const spectra = data.filter(
(spectrum) =>
Expand Down
22 changes: 17 additions & 5 deletions src/component/1d/inset/DraggableInset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import type { ActionsButtonsPopoverProps } from '../../elements/ActionsButtonsPo
import { Viewer1D } from '../Viewer1D.js';

import { InsetProvider } from './InsetProvider.js';

import type { Inset } from './index.js';
import type { Inset } from './SpectraInsets.js';

interface InsetBounding {
x: number;
Expand All @@ -34,9 +33,15 @@ const ReactRnd = styled(Rnd)`
}
`;

export function DraggableInset(props: Pick<Inset, 'id' | 'bounding'>) {
export function DraggableInset(props: Inset) {
const dispatch = useDispatch();
const { id, bounding: externalBounding } = props;
const {
id,
bounding: externalBounding,
xDomain,
yDomain,
spectrumKey,
} = props;
const { viewerRef } = useGlobal();
const [bounding, setBounding] = useState<InsetBounding>(externalBounding);
const [isMoveActive, setIsMoveActive] = useState(false);
Expand Down Expand Up @@ -142,7 +147,14 @@ export function DraggableInset(props: Pick<Inset, 'id' | 'bounding'>) {
},
}}
>
<InsetProvider id={id} width={width} height={height}>
<InsetProvider
id={id}
width={width}
height={height}
xDomain={xDomain}
yDomain={yDomain}
spectrumKey={spectrumKey}
>
<Viewer1D />
</InsetProvider>
</ActionsButtonsPopover>
Expand Down
38 changes: 33 additions & 5 deletions src/component/1d/inset/InsetProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,49 @@
import type { ReactNode } from 'react';
import { createContext, useContext, useMemo } from 'react';

export interface InsetPagContextProps {
import type { Margin } from '../../reducer/Reducer.js';

import type { Inset } from './SpectraInsets.js';

interface BaseInsetPagContextProps
extends Pick<Inset, 'xDomain' | 'yDomain' | 'id' | 'spectrumKey'> {
width: number;
height: number;
id: string;
}

interface InsetPagContextProps extends BaseInsetPagContextProps {
margin: Margin;
}

const InsetContext = createContext<InsetPagContextProps | null>(null);

export function isInsetNotNull(
inset: InsetPagContextProps | null,
): inset is InsetPagContextProps {
return !!inset;
}

export function useInsetOptions() {
return useContext(InsetContext);
}
export function useIsInset() {
return !!useContext(InsetContext);
}

interface InsetProviderProps extends InsetPagContextProps {
interface InsetProviderProps extends BaseInsetPagContextProps {
children: ReactNode;
}

export function InsetProvider(props: InsetProviderProps) {
const { children, width, height, id = 'primary' } = props;
const {
children,
width,
height,
xDomain,
yDomain,
spectrumKey,
id = 'primary',
} = props;

const state = useMemo(() => {
return {
Expand All @@ -31,8 +56,11 @@ export function InsetProvider(props: InsetProviderProps) {
left: 10,
},
id,
xDomain,
yDomain,
spectrumKey,
};
}, [height, id, width]);
}, [height, id, spectrumKey, width, xDomain, yDomain]);

return (
<InsetContext.Provider value={state}>{children}</InsetContext.Provider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ export function SpectraInsets() {
return (
<>
{insets.map((inset) => {
return (
<DraggableInset
key={inset.id}
id={inset.id}
bounding={inset.bounding}
/>
);
return <DraggableInset key={inset.id} {...inset} />;
})}
</>
);
Expand Down
4 changes: 4 additions & 0 deletions src/component/1d/tool/BaseLineZones.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useScaleChecked } from '../../context/ScaleContext.js';
import { ResizerWithScale } from '../../elements/ResizerWithScale.js';
import { useHighlight } from '../../highlight/index.js';
import { useResizerStatus } from '../../hooks/useResizerStatus.js';
import { useIsInset } from '../inset/InsetProvider.js';

function BaseLineZones() {
const {
Expand All @@ -14,6 +15,9 @@ function BaseLineZones() {
data: { baselineCorrection },
},
} = useChartData();
const isInset = useIsInset();

if (isInset) return null;

const baseLineZones = baselineCorrection.zones;

Expand Down
75 changes: 56 additions & 19 deletions src/component/1d/utilities/scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,46 @@ import type {
SpectraDirection,
VerticalAlignment,
} from '../../reducer/Reducer.js';
import { useIsInset } from '../inset/InsetProvider.js';

interface ScaleXOptions {
export const SPECTRA_BOTTOM_MARGIN = 40;

interface ScaleInsetXOptions {
width: number;
margin: Margin;
xDomains: Domains;
xDomain: number[];
mode: SpectraDirection;
}
interface ScaleXOptions extends ScaleInsetXOptions {
xDomains: Domains;
}

interface InsetYScaleOptions {
height: number;
margin: Pick<Margin, 'top' | 'bottom'>;
yDomain: number[];
}

interface ScaleYOptions extends InsetYScaleOptions {
yDomains: Domains;
verticalAlign: VerticalAlignment;
}

function getInsetXScale(options: ScaleInsetXOptions) {
const { width, margin, xDomain, mode } = options;
const range =
mode === 'RTL'
? [width - margin.right, margin.left]
: [margin.left, width - margin.right];
return scaleLinear(xDomain, range);
}

function getInsetYScale(options: InsetYScaleOptions) {
const { height, margin, yDomain } = options;
const innerHeight = height - margin.bottom - SPECTRA_BOTTOM_MARGIN;
return scaleLinear(yDomain, [innerHeight, margin.top]);
}

function getXScale(
options: ScaleXOptions,
spectrumId: number | null | string = null,
Expand All @@ -29,16 +61,6 @@ function getXScale(
return scaleLinear(spectrumId ? xDomains[spectrumId] : xDomain, range);
}

interface ScaleYOptions {
height: number;
margin: Pick<Margin, 'top' | 'bottom'>;
yDomains: Domains;
yDomain: number[];
verticalAlign: VerticalAlignment;
}

export const SPECTRA_BOTTOM_MARGIN = 40;

function getYScale(
options: ScaleYOptions,
spectrumId: number | null | string = null,
Expand Down Expand Up @@ -100,23 +122,36 @@ function reScaleY(scale: number, { domain, height, margin }) {

function useScaleX() {
const { margin, mode, width, xDomain, xDomains } = useChartData();
const isInset = useIsInset();

return useCallback(
(spectrumId = null) =>
getXScale({ margin, mode, width, xDomain, xDomains }, spectrumId),
[margin, mode, width, xDomain, xDomains],
(spectrumId = null) => {
if (isInset) {
return getInsetXScale({ margin, mode, width, xDomain });
}

return getXScale({ margin, mode, width, xDomain, xDomains }, spectrumId);
},
[isInset, margin, mode, width, xDomain, xDomains],
);
}
function useScaleY() {
const { margin, height, yDomain, yDomains } = useChartData();
const verticalAlign = useVerticalAlign();
const isInset = useIsInset();

return useCallback(
(spectrumId = null) =>
getYScale(
(spectrumId = null) => {
if (isInset) {
return getInsetYScale({ margin, height, yDomain });
}

return getYScale(
{ margin, height, verticalAlign, yDomain, yDomains },
spectrumId,
),
[margin, height, verticalAlign, yDomain, yDomains],
);
},
[isInset, margin, height, verticalAlign, yDomain, yDomains],
);
}

Expand All @@ -128,4 +163,6 @@ export {
getIntegralYScale,
reScaleY,
getYScaleWithRation,
getInsetXScale,
getInsetYScale,
};
4 changes: 3 additions & 1 deletion src/component/context/ChartContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ export function useChartData() {

if (!viewportSize) return data;

const { width, height, margin } = viewportSize;
const { width, height, margin, xDomain, yDomain } = viewportSize;
const updatedData = produce(data, (draft) => {
draft.width = width;
draft.height = height;
draft.margin = margin ?? draft.margin;
draft.xDomain = xDomain ?? draft.xDomain;
draft.yDomain = yDomain ?? draft.yDomain;
});

return updatedData;
Expand Down
Loading

0 comments on commit e553083

Please sign in to comment.