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

charts: add weight property to operational points #744

Merged
merged 2 commits into from
Jan 8, 2025

Conversation

Akctarus
Copy link
Contributor

@Akctarus Akctarus commented Dec 4, 2024

No description provided.

@Akctarus Akctarus requested a review from a team as a code owner December 4, 2024 10:44
@Akctarus Akctarus self-assigned this Dec 4, 2024
@Akctarus Akctarus requested review from Yohh and emersion December 4, 2024 10:49
Yohh
Yohh previously requested changes Dec 4, 2024
Copy link
Contributor

@Yohh Yohh left a 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

@Akctarus
Copy link
Contributor Author

Akctarus commented Dec 4, 2024

nice, but you have to update filterStops types in utils.ts

nice catch, fixed

@Yohh
Copy link
Contributor

Yohh commented Dec 4, 2024

sorry, but be carrefull to everywhere this new type is used, the utils for the story miss some changes, as reticle.ts, steps.ts... you should grep every occurrence of this obejct/type use

@Akctarus Akctarus force-pushed the tmn/chart/add-weight-property-to-OP branch from 1586b7e to fefeef0 Compare December 4, 2024 13:59
@Akctarus
Copy link
Contributor Author

Akctarus commented Dec 9, 2024

sorry, but be carrefull to everywhere this new type is used, the utils for the story miss some changes, as reticle.ts, steps.ts... you should grep every occurrence of this obejct/type use

Done !

@Akctarus Akctarus requested a review from Yohh December 9, 2024 14:02
@emersion
Copy link
Member

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?

@Akctarus
Copy link
Contributor Author

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.

@Akctarus Akctarus marked this pull request as draft December 16, 2024 09:16
@Akctarus Akctarus force-pushed the tmn/chart/add-weight-property-to-OP branch 4 times, most recently from 59bf4c0 to 99af508 Compare December 20, 2024 15:40
@Akctarus Akctarus marked this pull request as ready for review December 20, 2024 15:41
@Akctarus Akctarus requested a review from Yohh December 20, 2024 16:48
@Akctarus Akctarus dismissed Yohh’s stale review December 23, 2024 08:21

PR has evolved and this reviewer is on vacation

@Akctarus Akctarus requested review from kmer2016 and theocrsb and removed request for emersion and Yohh December 23, 2024 08:21
@theocrsb
Copy link
Contributor

I have a strange behavior with manchette-with-spacetimechart

image

ui-manchette-with-spacetimechart/src/helpers.ts Outdated Show resolved Hide resolved
ui-speedspacechart/src/components/utils.ts Outdated Show resolved Hide resolved
ui-speedspacechart/src/components/utils.ts Outdated Show resolved Hide resolved
ui-speedspacechart/src/components/utils.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@kmer2016 kmer2016 left a 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

ui-manchette-with-spacetimechart/src/helpers.ts Outdated Show resolved Hide resolved
ui-manchette-with-spacetimechart/src/helpers.ts Outdated Show resolved Hide resolved
@Akctarus Akctarus force-pushed the tmn/chart/add-weight-property-to-OP branch from 4f67852 to 551586a Compare January 6, 2025 10:20
Copy link
Contributor

@theocrsb theocrsb left a 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.

@Akctarus Akctarus force-pushed the tmn/chart/add-weight-property-to-OP branch 3 times, most recently from d53d31d to 526fbce Compare January 7, 2025 15:51
Copy link
Contributor

@kmer2016 kmer2016 left a 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

Copy link
Contributor

@kmer2016 kmer2016 left a 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

@Akctarus Akctarus force-pushed the tmn/chart/add-weight-property-to-OP branch from 72dc4ea to fcef10d Compare January 8, 2025 10:21
@theocrsb theocrsb added this pull request to the merge queue Jan 8, 2025
Merged via the queue into dev with commit f74d1fb Jan 8, 2025
6 checks passed
@theocrsb theocrsb deleted the tmn/chart/add-weight-property-to-OP branch January 8, 2025 12:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants