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

Component | Donut: Automatic label placement #8

Closed
wants to merge 1 commit into from

Conversation

rokotyan
Copy link

@rokotyan rokotyan commented Dec 9, 2024

No description provided.

Copy link

@curran curran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Author

@rokotyan rokotyan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@curran Left some comments to the code

image

packages/ts/src/components/donut/config.ts Outdated Show resolved Hide resolved
packages/ts/src/components/donut/config.ts Outdated Show resolved Hide resolved
packages/ts/src/components/donut/config.ts Outdated Show resolved Hide resolved
packages/ts/src/components/donut/index.ts Outdated Show resolved Hide resolved
packages/ts/src/components/donut/config.ts Outdated Show resolved Hide resolved
@curran
Copy link

curran commented Dec 9, 2024

Will do the following, as suggested in #5 (comment)

  • Leverage getCSSVariableValueInPixels to get font size based on --vis-donut-central-label-font-size and vis-donut-central-sub-label-font-size
  • Leverage estimateTextSize to approximate text width and height for alignment

@curran
Copy link

curran commented Dec 10, 2024

Maybe this other PR would be a better first step:

Then later, on top of those changes, we could add an option to automatically calculate the offsets based on measuring/estimating text size. What do you think?

@curran curran marked this pull request as draft December 10, 2024 18:16
@curran
Copy link

curran commented Dec 10, 2024

Converted this to draft PR because:

  • This other PR is cleaner: Component | Donut: Half Donut Label Placement #9
  • We can layer on top of chose changes some additional changes that focus on measuring/approximating text size, making the changes in this PR obsolete (there would be no longer any need for, say halfDonutLabelOffsetY, since it would be automatically computed)

@curran
Copy link

curran commented Dec 10, 2024

Current status:

  • managed to use estimateTextSize for centralLabel
  • solved left/right cases

Next steps:

  • solve top/bottom case
  • account for both centralLabel and centralSubLabel in all calculations (Math.max for width)

@curran
Copy link

curran commented Dec 11, 2024

I did the text measuring, but it only accounts for the main part, not the parts that stick up or down

image

@curran curran marked this pull request as ready for review December 11, 2024 15:11
@curran curran force-pushed the feature/half-donut-labels branch from 4b55775 to aadf850 Compare December 11, 2024 19:35
@curran curran changed the title Component | Donut: Handling half donut labels Component | Donut: Automatic label placement Dec 11, 2024
@curran
Copy link

curran commented Dec 11, 2024

Researching this more to get the right terminology...

image

The text size estimation appears to only account for the height as the distance between the baseline and the cap height. It does not account for the descender or ascender, resulting in those things getting cut off.

I wonder what the best solution here would be...

Perhaps we could bring back the idea of halfDonutLabelOffsetY as an additional offset to be applied in addition to the measured text size?

Or maybe we can modify the text height estimation to account for the descenders and ascenders? I'm not sure what that would look like. I don't think it's as straightforward as multiplying the size by some constant, because I would guess that various fonts have different values for the ratio of the cap height (which we know) to ascender height and descender height (both of which we don't know).

@curran
Copy link

curran commented Dec 11, 2024

Noting down the code that measured the x-height here for future reference, since we are squashing the commits and can't see the commit history:

      const fastEstimatesMode = true // Fast but inaccurate
      const fontWidthToHeightRatio = 0.52
      const centralLabelFontSize = getCSSVariableValueInPixels('var(--vis-donut-central-label-font-size)', this.element)
      const centralLabelSize = estimateTextSize(this.centralLabel, centralLabelFontSize, 0, fastEstimatesMode, fontWidthToHeightRatio)
      const centralSubLabelFontSize = getCSSVariableValueInPixels('var(--vis-donut-central-sub-label-font-size)', this.element)
      const centralSubLabelSize = estimateTextSize(this.centralSubLabel, centralSubLabelFontSize, 0, fastEstimatesMode, fontWidthToHeightRatio)

@curran curran force-pushed the feature/half-donut-labels branch from aadf850 to a07c96a Compare December 11, 2024 20:30
@curran
Copy link

curran commented Dec 11, 2024

Using getBoundingClientRect achieves the desired result.

image image image image

@rokotyan
Copy link
Author

@curran Nice!

You can keep working in your donut-label-offsets branch #9 overriding the history.

Feel free the modify the estimateTextSize function (it's current logic is pretty simple), or maybe just add some extra padding to take into account the imprecise nature of that function. It you think that won't solve the problem, you can use getBoundingClientRect. The downside of getBoundingClientRect is that it triggers a force reflow which is not good for performance.

Also please don't forget about the alignment
image

@curran
Copy link

curran commented Dec 12, 2024

Got it. Thanks for the feedback!

I made this mockup to clarify what you mean - that the sub-label should be aligned with the central label, and those should both be aligned with the edge of the half donut. I'm pretty sure this is what you mean.

image

Also, would we want to introduce some sort of "padding" option to apply in addition to the automatic offsets that do exact alignment? I could imagine cases where users would want the automatic alignment, but then would also want to add a tiny gap so the text is not exactly aligned.

@curran curran marked this pull request as draft December 12, 2024 17:55
@curran curran force-pushed the feature/half-donut-labels branch from a07c96a to e6ca4ec Compare December 12, 2024 18:02
@curran curran force-pushed the feature/half-donut-labels branch from e6ca4ec to c2ae7f7 Compare December 12, 2024 18:03
@curran
Copy link

curran commented Dec 13, 2024

Closing in favor of #9

@curran curran closed this Dec 13, 2024
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.

2 participants