Skip to content

Commit

Permalink
front: intervals editor, redraw the PR when the window is resized and…
Browse files Browse the repository at this point in the history
… display them above the main component
  • Loading branch information
clarani committed Aug 9, 2023
1 parent 2f3bc09 commit 33ec7ba
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 30 deletions.
5 changes: 3 additions & 2 deletions front/src/common/IntervalsDataViz/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ export function cropOperationPointsForDatavizViewbox(
wrapper: React.MutableRefObject<HTMLDivElement | null>,
fullLength: number
): Array<OperationalPoint & { positionInPx: number }> {
if (wrapper.current !== null) {
if (wrapper.current !== null && fullLength > 0) {
const wrapperWidth = wrapper.current.offsetWidth;
return (
[...operationalPoints]
Expand All @@ -634,7 +634,8 @@ export function cropOperationPointsForDatavizViewbox(
const lengthToDisplay = currentViewBox
? operationalPoint.position - currentViewBox[0]
: operationalPoint.position;
const positionInPx = (lengthToDisplay * wrapperWidth) / fullLength;
// subtract 2px for the display
const positionInPx = (lengthToDisplay * wrapperWidth) / fullLength - 2;
return {
...operationalPoint,
positionInPx,
Expand Down
95 changes: 67 additions & 28 deletions front/src/common/IntervalsDataViz/dataviz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,46 +123,61 @@ export const LinearMetadataDataviz = <T extends { [key: string]: any }>({
* When data change
* => we recompute the data for the viz
* => we recompute the full length of the displayed data.
*/
useEffect(() => {
const nData = cropForDatavizViewbox(data, viewBox);
setData4viz(nData);
setFullLength((last(nData)?.end || 0) - (head(nData)?.begin || 0));
}, [data]);

/**
* When operationalPoints or fullLength change
* => we recompute the operationalPoints4viz
*/
useEffect(() => {
const nData = cropForDatavizViewbox(data, viewBox);
const nFullLength = (last(nData)?.end || 0) - (head(nData)?.begin || 0);
const nOperationalPoints = cropOperationPointsForDatavizViewbox(
operationalPoints,
viewBox,
wrapper,
fullLength
nFullLength
);
setData4viz(nData);
setFullLength(nFullLength);
setOperationalPoints4viz(nOperationalPoints);
}, [data, viewBox]);

/**
* When operationalPoints change
* => we recompute the operationalPoints4viz
*/
useEffect(() => {
if (fullLength > 0) {
const nOperationalPoints = cropOperationPointsForDatavizViewbox(
operationalPoints,
viewBox,
wrapper,
fullLength
);
setOperationalPoints4viz(nOperationalPoints);
}
}, [operationalPoints, fullLength]);

/**
* When viewbox changes
* => we recompute the data for the viz
* => we recompute the full length of the displayed data.
* When the window is resized horizontally
* => we recompute the operationalPoints4viz
*/
useEffect(() => {
const nData = cropForDatavizViewbox(data, viewBox);
const nFullLength = (last(nData)?.end || 0) - (head(nData)?.begin || 0);
const nOperationalPoints = cropOperationPointsForDatavizViewbox(
operationalPoints,
viewBox,
wrapper,
nFullLength
);
setData4viz(nData);
setFullLength(nFullLength);
setOperationalPoints4viz(nOperationalPoints);
}, [viewBox]);
const debounceResize = () => {
let debounceTimeoutId;
clearTimeout(debounceTimeoutId);
debounceTimeoutId = setTimeout(() => {
const nOperationalPoints = cropOperationPointsForDatavizViewbox(
operationalPoints,
viewBox,
wrapper,
fullLength
);
setOperationalPoints4viz(nOperationalPoints);
}, 15);
};
window.addEventListener('resize', debounceResize);
return () => {
window.removeEventListener('resize', debounceResize);
};
}, [operationalPoints, viewBox, wrapper, fullLength]);

/**
* When the wrapper div change
Expand Down Expand Up @@ -265,6 +280,32 @@ export const LinearMetadataDataviz = <T extends { [key: string]: any }>({

return (
<div className={cx('linear-metadata-visualisation')}>
<div
className={cx(
'data',
viewBox !== null && draginStartAt && 'dragging',
resizing && 'resizing',
(viewBox === null || viewBox[0] === 0) && 'start-visible',
(viewBox === null || viewBox[1] === last(data)?.end) && 'end-visible'
)}
style={{ height: '30px' }}
>
{/* Display the operational points */}
{operationalPoints4viz.map((operationalPoint) => (
<div
key={`op-${operationalPoint.id}`}
className="operational-point"
style={{
position: 'absolute',
height: '50px',
left: `${operationalPoint.positionInPx}px`,
borderLeft: '2px dashed #a0a0a0',
}}
>
<p>{operationalPoint.name}</p>
</div>
))}
</div>
<div
id="linear-metadata-dataviz-content"
ref={wrapper}
Expand Down Expand Up @@ -328,9 +369,7 @@ export const LinearMetadataDataviz = <T extends { [key: string]: any }>({
left: `${operationalPoint.positionInPx}px`,
borderLeft: '2px dashed #a0a0a0',
}}
>
<p>{operationalPoint.name}</p>
</div>
/>
))}

{/* Create one div per item for the X axis */}
Expand Down

0 comments on commit 33ec7ba

Please sign in to comment.