-
Notifications
You must be signed in to change notification settings - Fork 5
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
charts: add weight property to operational points #744
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice, but you have to update filterStops
types in utils.ts
nice catch, fixed |
sorry, but be carrefull to everywhere this new type is used, the |
1586b7e
to
fefeef0
Compare
Done ! |
So this only adds the new property but doesn't use it anywhere, right? Is this still a TODO, or will this come in a future PR? |
A future PR on the back side will add the “weight” field for OPs, here I'm preparing the GET and GEV for this new property. |
59bf4c0
to
99af508
Compare
PR has evolved and this reviewer is on vacation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactoring calcWaypointsToDisplay and filterStops proposal:
The functions calcWaypointsToDisplay
and filterStops
share similar logic. Both aim to:
- Sort elements by weight to prioritize the most important ones.
- Check if there is enough space between elements before displaying them.
- Reorder the selected elements based on their original position for final output.
This similarity suggests an opportunity to create a reusable function that handles the shared logic, making the code cleaner and easier to maintain.
Proposed Refactor
introduce a generic function called filterVisibleElements
. This function handles:
- Sorting elements by weight.
- Calculating positions.
- Checking spacing constraints to avoid overlaps.
- Returning elements in the correct order for display.
type VisibilityFilterOptions<T> = {
elements: T[];
getPosition: (element: T) => number;
getWeight: (element: T) => number | undefined;
minSpace: number;
};
const filterVisibleElements = <T>(
{ elements, getPosition, getWeight, minSpace }: VisibilityFilterOptions<T>
): T[] => {
const sortedElements = elements.toSorted((a, b) => (getWeight(b) ?? 0) - (getWeight(a) ?? 0));
const displayedElements: { element: T; position: number }[] = [];
for (const element of sortedElements) {
const position = getPosition(element);
const hasSpace = !displayedElements.some(
(displayed) => Math.abs(position - displayed.position) < minSpace
);
if (hasSpace) {
displayedElements.push({ element, position });
}
}
return displayedElements.sort((a, b) => getPosition(a.element) - getPosition(b.element)).map(({ element }) => element);
};
Both calcWaypointsToDisplay
and filterStops
now use filterVisibleElements
. Their logic is simplified to focus on the specific details of their contexts.
Refactored calcWaypointsToDisplay
export const calcWaypointsToDisplay = (
waypoints: Waypoint[],
{ height, isProportional, yZoom }: WaypointsOptions
): InteractiveWaypoint[] => {
if (!isProportional || waypoints.length === 0) {
return waypoints.map((waypoint) => ({ ...waypoint, display: true }));
}
const totalDistance = calcTotalDistance(waypoints);
const heightWithoutFinalWaypoint = getHeightWithoutLastWaypoint(height);
const minSpace = BASE_WAYPOINT_HEIGHT / yZoom;
const displayedWaypoints = filterVisibleElements({
elements: waypoints,
getPosition: (waypoint) => (waypoint.position / totalDistance) * heightWithoutFinalWaypoint,
getWeight: (waypoint) => waypoint.weight,
minSpace,
});
return displayedWaypoints.map((waypoint) => ({ ...waypoint, display: true }));
};
Refactored filterStops
:
export const filterStops = (
stops: LayerData<OperationalPoints>[],
ratioX: number,
width: number,
maxPosition: number,
minSpace = 8
): LayerData<OperationalPoints>[] => {
const displayedStops = filterVisibleElements({
elements: stops,
getPosition: (stop) => positionToPosX(stop.position.start, maxPosition, width, ratioX),
getWeight: (stop) => stop.value.weight,
minSpace,
});
return displayedStops;
};
Suggestions for Naming
I also suggest renaming the functions for clarity:
- calcWaypointsToDisplay → getDisplayedWaypoints
- filterStops → getDisplayedStops
4f67852
to
551586a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM and tested
I think it will be possible to make a small refacto to specify the type of the array of elements
in filterVisibleElements
.
d53d31d
to
526fbce
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice ! OK for me, juste small suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice ! OK for me, just small suggestion
Signed-off-by: Theo Macron <[email protected]>
Signed-off-by: Theo Macron <[email protected]>
72dc4ea
to
fcef10d
Compare
No description provided.