Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TOI zoom behavior #908

Merged
merged 5 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/scripts/components/exploration/atoms/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export const zoomTransformAtom = atom<ZoomTransformPlain>({
k: 1
});


// Atom to zoom TOI when analysis is run
type ZoomTOIFunction = (newX: number, newK: number) => void;
export const onTOIZoomAtom = atom<ZoomTOIFunction | null>(null);

// Width of the whole timeline item. Set via a size observer and then used to
// compute the different element sizes.
export const timelineWidthAtom = atom<number | undefined>(undefined);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import {
} from '@devseed-ui/collecticons';
import { timelineDatasetsAtom } from '../../atoms/datasets';
import { selectedIntervalAtom } from '../../atoms/dates';

import { useScales } from '../../hooks/scales-hooks';
import useMaps from '$components/common/map/hooks/use-maps';
import { useOnTOIZoom } from '$components/exploration/hooks/use-toi-zoom';
import {
timelineWidthAtom
} from '$components/exploration/atoms/timeline';

import useAois from '$components/common/map/controls/hooks/use-aois';
import { calcFeatCollArea } from '$components/common/aoi/utils';
Expand All @@ -21,6 +27,7 @@ import useThemedControl from '$components/common/map/controls/hooks/use-themed-c
import { getZoomFromBbox } from '$components/common/map/utils';
import { AoIFeature } from '$components/common/map/types';
import { ShortcutCode } from '$styles/shortcut-code';
import { RIGHT_AXIS_SPACE, HEADER_COLUMN_WIDTH } from '$components/exploration/constants';

const AnalysisMessageWrapper = styled.div.attrs({
'data-tour': 'analysis-message'
Expand Down Expand Up @@ -90,8 +97,13 @@ export function AnalysisMessage({ mainMap }: { mainMap: MapRef | undefined }) {
const datasets = useAtomValue(timelineDatasetsAtom);
const datasetIds = datasets.map((d) => d.data.id);

const timelineWidth = useAtomValue(timelineWidthAtom);
const { main } = useScales();
const { onTOIZoom } = useOnTOIZoom();

const { features } = useAois();
const selectedInterval = useAtomValue(selectedIntervalAtom);

const dateLabel =
selectedInterval &&
formatDateRange(selectedInterval.start, selectedInterval.end);
Expand All @@ -102,7 +114,10 @@ export function AnalysisMessage({ mainMap }: { mainMap: MapRef | undefined }) {
setObsolete();
}, [setObsolete, features]);



const analysisCallback = useCallback(() => {
// Fit AOI
const bboxToFit = bbox({
type: 'FeatureCollection',
features: selectedFeatures
Expand All @@ -112,7 +127,18 @@ export function AnalysisMessage({ mainMap }: { mainMap: MapRef | undefined }) {
center:[ (bboxToFit[2] + bboxToFit[0])/2, (bboxToFit[3] + bboxToFit[1])/2],
zoom
});
}, [selectedFeatures, mainMap]);

// Fit TOI
if (!main || !timelineWidth || !selectedInterval?.start ) return;

const widthToFit = (timelineWidth - RIGHT_AXIS_SPACE - HEADER_COLUMN_WIDTH) * 0.9;
const startPoint = 0;
const new_k = widthToFit/(main(selectedInterval.end) - main(selectedInterval.start));
const new_x = startPoint - new_k * main(selectedInterval.start);

onTOIZoom(new_x, new_k);

}, [selectedFeatures, mainMap, main, timelineWidth, onTOIZoom, selectedInterval]);

if (isAnalyzing) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
useScaleFactors,
useScales
} from '$components/exploration/hooks/scales-hooks';
import { useOnTOIZoom } from '$components/exploration/hooks/use-toi-zoom';
import {
TimelineDatasetStatus,
TimelineDatasetSuccess,
Expand Down Expand Up @@ -200,7 +201,7 @@ export default function Timeline(props: TimelineProps) {
const [selectedInterval, setSelectedInterval] = useAtom(selectedIntervalAtom);

const { setObsolete, runAnalysis, isAnalyzing } = useAnalysisController();

const [zoomTransform, setZoomTransform] = useAtom(zoomTransformAtom);
const { features } = useAois();

useEffect(() => {
Expand All @@ -216,8 +217,6 @@ export default function Timeline(props: TimelineProps) {
[width]
);

const [zoomTransform, setZoomTransform] = useAtom(zoomTransformAtom);

// Calculate min and max scale factors, such has each day has a minimum of 2px
// and a maximum of 100px.
const { k0, k1 } = useScaleFactors();
Expand Down Expand Up @@ -371,7 +370,17 @@ export default function Timeline(props: TimelineProps) {
applyTransform(zoomBehavior, select(interactionRef.current), 0, 0, k0);
}, [prevSuccessDatasetsCount, successDatasetsCount, k0, zoomBehavior]);

const onControlsZoom = useCallback(
const { initializeTOIZoom } = useOnTOIZoom();

useEffect(() => {
// Set TOIZoom functionality in atom so it can be used in analysis component
// Ensure zoomBehavior and interactionRef are defined before initializing
if (zoomBehavior && interactionRef.current) {
initializeTOIZoom(zoomBehavior, interactionRef);
}
}, [initializeTOIZoom, zoomBehavior, interactionRef]);

const onControlsZoom = useCallback(
(zoomV) => {
if (!interactionRef.current || !xMain || !xScaled || !selectedDay) return;

Expand Down
33 changes: 33 additions & 0 deletions app/scripts/components/exploration/hooks/use-toi-zoom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useCallback } from 'react';
import { useAtom } from 'jotai';
import { select, ZoomBehavior } from 'd3';
import { onTOIZoomAtom } from '../atoms/timeline';
import { applyTransform } from '$components/exploration/components/timeline/timeline-utils';

export function useOnTOIZoom() {
const [onTOIZoom, setOnTOIZoom] = useAtom(onTOIZoomAtom);

const initialize = useCallback((zoomBehavior: ZoomBehavior<Element, unknown>, interactionRef: React.RefObject<HTMLElement>) => {
setOnTOIZoom(() => (newX: number, newK: number) => {
if (!newX || !newK) return;
const { current: interactionElement } = interactionRef;
if (!interactionElement) return;

applyTransform(
zoomBehavior,
select(interactionElement),
newX,
0,
newK
);
});
}, [setOnTOIZoom]);

const safeOnTOIZoom = (newX: number, newK: number) => {
if (onTOIZoom) {
onTOIZoom(newX, newK);
}
};

return { initializeTOIZoom: initialize, onTOIZoom: safeOnTOIZoom };
}
Loading