Skip to content

Fix overlapping category labels in VirtualCFO pie charts#95835

Open
luacmartins wants to merge 18 commits into
mainfrom
cmartins-virtualcfo-pie-label-columns
Open

Fix overlapping category labels in VirtualCFO pie charts#95835
luacmartins wants to merge 18 commits into
mainfrom
cmartins-virtualcfo-pie-label-columns

Conversation

@luacmartins

@luacmartins luacmartins commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

"Top categories" pie chart labels overlapped when several small slices sat next to each other, since each label was placed by its own slice angle with only an even/odd radius alternation.

Replaced with a two-column layout: labels split left/right by ring side, stack top-to-bottom with collision-aware spacing, and connect via a single-bend leader line.

Fixed Issues

$ #94613

Tests

  1. Use the https://www.expensify.com.dev/_support/agentZeroSandbox tool
  2. Verify that the Top categories chart labels look correct.
  3. From server/victory-chart-renderer: bun test tests/render.test.ts — all golden PNGs pass, including new top-categories-crowded-slices fixture.
  4. Render any top-categories-*.xml fixture (standalone renderer or in-app) and confirm: two clean label columns, no overlaps, single-bend leader lines, no regression on well-separated charts.
  • Verify that no errors appear in the JS console

Offline tests

N/A

QA Steps

Same as tests

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Screenshot 2026-07-10 at 2 32 35 PM Screenshot 2026-07-10 at 2 32 10 PM Screenshot 2026-07-14 at 12 46 41 PM
Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari

Small adjacent slices in the "Top categories" donut (VirtualCFO/AnalyzeExpense)
crowded and overlapped their labels because each label was placed independently
along its own slice's angle, only alternating radius by index parity. Labels
now split into two fixed columns (left/right of the ring) and stack
top-to-bottom with collision-aware spacing, with leader lines bent once to the
resolved position.

Also fixes a pre-existing bug found while verifying the new layout: polar
chart containers clip their bottom ~10% (POLAR_CONTAINER_HEIGHT_RATIO), which
could silently cut off labels near 6 o'clock in the live app even though the
headless golden-PNG renderer's layout path never exercises that clipping.
@luacmartins luacmartins requested review from a team as code owners July 10, 2026 15:59
@melvin-bot melvin-bot Bot requested review from arosiclair and flaviadefaria and removed request for a team July 10, 2026 16:00
@melvin-bot

melvin-bot Bot commented Jul 10, 2026

Copy link
Copy Markdown

@arosiclair Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot melvin-bot Bot removed the request for review from a team July 10, 2026 16:00
@luacmartins luacmartins marked this pull request as draft July 10, 2026 16:00
@luacmartins

luacmartins commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

Not ready for review yet

@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.

Files with missing lines Coverage Δ
...ctoryChartContainer/VictoryChartContainerFixed.tsx 0.00% <ø> (ø)
.../components/VictoryChartContainer/index.native.tsx 9.09% <ø> (-3.96%) ⬇️
...yChartRenderer/components/VictoryChartPieLabel.tsx 0.00% <ø> (ø)
...er/HTMLRenderers/VictoryChartRenderer/constants.ts 100.00% <100.00%> (ø)
...ictoryChartRenderer/utils/computePieLabelLayout.ts 74.28% <74.28%> (ø)
...derer/components/VictoryChartPieLabelIndicator.tsx 3.84% <0.00%> (-3.85%) ⬇️
...ictoryChartRenderer/components/VictoryChartPie.tsx 7.54% <10.00%> (-1.83%) ⬇️
... and 91 files with indirect coverage changes

The leader line's second segment went straight from a slice's natural
elbow point to its (possibly collision-pushed) final position. Once a
label was pushed any real distance from its natural angle, that diagonal
could double back vertically relative to the first (radial) segment,
producing a visible zigzag.

Replaced with a horizontal run (at the slice's natural height) followed by
a vertical bend into the resolved row, so the line only ever moves toward
its target and never reverses direction. This also let a chunk of now-dead
labelRadius/labelIndicatorOuterOffset plumbing be removed from the pie
label components.
The previous elbow ran horizontal near the ring and vertical into the
label, which left the line meeting the text at an arbitrary height. Swap
the order: a short diagonal near the ring absorbs both the x and y offset
toward the resolved row, then a flat run carries the line the rest of the
way into the label at that row's own height.

labelIndicatorOuterOffset (previously removed as dead after the last
change) is reinstated with a new meaning: it now sizes the diagonal
segment's horizontal reach near the ring, instead of the old radial
elbow-point offset.
A 100%-value slice's start and end angle are the same point, so the
generic (start + end) / 2 "midpoint" used for every other slice lands
diametrically opposite the ring's seam — the bottom, given this codebase's
12-o'clock start angle — instead of a sensible spot. Anchor a lone slice's
label to a fixed 3-o'clock position instead.

The indicator line previously recomputed its own midAngle straight from
slice.startAngle/endAngle, disagreeing with the label's (now overridden)
angle and producing a line that still ran to the bottom before jumping to
the relocated label. Thread the resolved midAngle through instead, so both
the label and its line always agree.
ResolvedPieLabel was declared identically in both VictoryChartPieLabel.tsx
and VictoryChartPieLabelIndicator.tsx; moved it into the shared types.ts
module (alongside TextAnchor, which it already depends on) so both import
one definition instead of keeping two in sync by hand.

Also adds the missing header comment to computePieLabelLayout.ts
describing its purpose.
Neither type is imported anywhere outside this file — they're only used
internally by computePieLabelLayout itself, so exporting them was dead
surface area.
The title/subtitle are left-aligned at the same x the left column's
labels end at, so a label stacked right at the shared top clearance read
as cramped against them even without literally overlapping — the right
column doesn't share that horizontal space and didn't have the problem.

plotBounds is now computed per side instead of shared, with the left
column's top pushed down by an extra fixed buffer.
The diagonal segment's horizontal reach was a small fixed offset
regardless of how far a label had been pushed vertically. Giving the left
column extra top clearance increased how far labels near 12 o'clock get
pushed down, so that fixed-width diagonal turned into a near-vertical
stroke hugging the ring's x position for a long vertical run — reading as
a line cutting through the chart instead of a side connector.

The diagonal's horizontal reach now grows with the vertical distance it
covers (never steeper than 45°), falling back to the original fixed offset
when the push is small enough not to need it.
The elbow-radius approach from the last commit fixed the ring-overlap for
one case but introduced two new problems: extending along a slice's own
angle to a fixed radius overshoots well past the resolved row for slices
near the top/bottom seam (producing a stray peak above the ring), and can
still land inside the ring when there isn't enough room left before the
column for slices whose natural angle is already close to their column.

Replaced with a direct solve: since the final segment must be purely
horizontal at the resolved row's height (so the line meets the label text
at that height), the bend point's Y is fixed to resolvedLabel.y already —
so compute the X offset needed at that exact Y to clear the ring, clamped
between the ring touchpoint and the label itself. No separate elbow point,
no overshoot, and no possibility of landing inside the ring's radius.
The previous fix only guaranteed the bend point itself sat at a safe
radius from center. A straight line between two points that are each
individually outside the ring can still dip inside it if they sit at
sufficiently different angles — the chord bows toward center. This showed
up as a leader line cutting across a neighboring slice whenever several
tiny slices sat close together with very different resolved rows (the
ring touchpoint stays near the slice's own angle, but the bend point can
land at a very different effective angle once collision resolution has
moved the row).

Now finds the closest point on the ring-to-bend segment to the center and,
if it would dip inside the safe radius, projects that point outward onto
the safe circle instead of connecting the two points directly.
…bend

The chord-dip check compared against safeRadius (slice.radius + outer
offset), but the ring attachment point itself always sits at the smaller
innerRadius by construction. Since innerRadius < safeRadius always, the
"closest point" on almost every line was the attachment point itself,
which the check then flagged as unsafe and pushed outward — adding an
unnecessary extra bend to lines that were never actually at risk of
crossing the ring (visible as a double-elbow zigzag on well-separated
labels like large slices sharing a column with a small cluster).

Check against innerRadius instead — the radius the attachment point is
actually guaranteed to sit at — so the correction only fires when a line
would truly dip inside the ring.
@luacmartins luacmartins requested a review from a team July 14, 2026 19:01
@luacmartins luacmartins added the InternalQA This pull request required internal QA label Jul 14, 2026
@luacmartins luacmartins marked this pull request as ready for review July 14, 2026 19:02
@situchan

Copy link
Copy Markdown
Contributor

Can you please share test chart data?

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 08bb0b5df7

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@luacmartins

Copy link
Copy Markdown
Contributor Author

@situchan this is one of the charts from the screenshot

<victorychart width="680" height="530" domainpadding="0" style="{parent: {backgroundColor: '#F8F4F0', borderRadius: 16, width: '100%', maxWidth: 680}}"> <victoryaxis style="{axis: {stroke: 'transparent'}, ticks: {stroke: 'transparent'}, grid: {stroke: 'transparent'}, tickLabels: {fill: 'transparent'}}" /> <victoryaxis dependentaxis style="{axis: {stroke: 'transparent'}, ticks: {stroke: 'transparent'}, grid: {stroke: 'transparent'}, tickLabels: {fill: 'transparent'}}" /> <victorylabel x="32" y="40" text="Top categories" style="{fill: '#002E22', fontSize: 17, fontWeight: 700, fontFamily: 'Expensify Neue'}" /> <victorylabel x="32" y="62" text="As of: Jul 14, 2026 at 06:45 PM" style="{fill: '#76847E', fontSize: 11, fontWeight: 400, fontFamily: 'Expensify Neue'}" /> <victorypie standalone="false" width="680" height="550" padding="{top: 0, bottom: 0, left: 0, right: 0}" innerradius="125" padangle="0.5" radius="145" labelindicator="&lt;ShiftedLineSegment dy=&quot;0&quot; style=&quot;{ stroke: &#39;#E6E1DA&#39;, strokeWidth: 1 }&quot; /&gt;" labelindicatorinneroffset="12" labelindicatorouteroffset="24" labelradius="215" data="[{x: 'Travel', y: 3359}, {x: 'Meals and Entertainment', y: 1271}, {x: 'Uncategorized', y: 742}, {x: 'Other', y: 336}, {x: 'Utilities', y: 203}, {x: 'Benefits', y: 203}, {x: 'Office Supplies', y: 17}, {x: 'Car', y: 2}]" labels="['Travel\n$3,359', 'Meals and Entertain…\n$1,271', 'Uncategorized\n$742', 'Other\n$336', 'Utilities\n$203', 'Benefits\n$203', 'Office Supplies\n$17', 'Car\n$2']" labelcomponent="&lt;victorylabel lineheight=&quot;[1.2, 1.4]&quot; style=&quot;[{fill: '#002E22', fontSize: 11, fontWeight: 700, fontFamily: 'Expensify Neue'}, {fill: '#76847E', fontSize: 11, fontWeight: 400, fontFamily: 'Expensify Neue'}]&quot; /&gt;" colorscale="['#FED607', '#FF7101', '#F68DFE', '#03D47C', '#50EEF6', '#0185FF', '#D18000', '#BF3013']" style="{data: {stroke: '#F8F4F0'}}" /> <victorylabel x="340" y="265" text="'Total\n$6,133'" textanchor="middle" verticalanchor="middle" lineheight="[1.23, 1.27]" style="[{fill: '#76847E', fontSize: 13, fontFamily: 'Expensify Neue'}, {fill: '#002E22', fontSize: 26, fontFamily: 'Expensify New Kansas'}]" /> </victorychart>

@luacmartins luacmartins requested a review from situchan July 14, 2026 20:16
@shawnborton

Copy link
Copy Markdown
Contributor

Screenshots are looking good to me 👍

@situchan

Copy link
Copy Markdown
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: HybridApp
Android: mWeb Chrome
iOS: HybridApp
iOS: mWeb Safari
MacOS: Chrome / Safari

@situchan

Copy link
Copy Markdown
Contributor

@situchan this is one of the charts from the screenshot

<victorychart width="680" height="530" domainpadding="0" style="{parent: {backgroundColor: '#F8F4F0', borderRadius: 16, width: '100%', maxWidth: 680}}"> <victoryaxis style="{axis: {stroke: 'transparent'}, ticks: {stroke: 'transparent'}, grid: {stroke: 'transparent'}, tickLabels: {fill: 'transparent'}}" /> <victoryaxis dependentaxis style="{axis: {stroke: 'transparent'}, ticks: {stroke: 'transparent'}, grid: {stroke: 'transparent'}, tickLabels: {fill: 'transparent'}}" /> <victorylabel x="32" y="40" text="Top categories" style="{fill: '#002E22', fontSize: 17, fontWeight: 700, fontFamily: 'Expensify Neue'}" /> <victorylabel x="32" y="62" text="As of: Jul 14, 2026 at 06:45 PM" style="{fill: '#76847E', fontSize: 11, fontWeight: 400, fontFamily: 'Expensify Neue'}" /> <victorypie standalone="false" width="680" height="550" padding="{top: 0, bottom: 0, left: 0, right: 0}" innerradius="125" padangle="0.5" radius="145" labelindicator="&lt;ShiftedLineSegment dy=&quot;0&quot; style=&quot;{ stroke: &#39;#E6E1DA&#39;, strokeWidth: 1 }&quot; /&gt;" labelindicatorinneroffset="12" labelindicatorouteroffset="24" labelradius="215" data="[{x: 'Travel', y: 3359}, {x: 'Meals and Entertainment', y: 1271}, {x: 'Uncategorized', y: 742}, {x: 'Other', y: 336}, {x: 'Utilities', y: 203}, {x: 'Benefits', y: 203}, {x: 'Office Supplies', y: 17}, {x: 'Car', y: 2}]" labels="['Travel\n$3,359', 'Meals and Entertain…\n$1,271', 'Uncategorized\n$742', 'Other\n$336', 'Utilities\n$203', 'Benefits\n$203', 'Office Supplies\n$17', 'Car\n$2']" labelcomponent="&lt;victorylabel lineheight=&quot;[1.2, 1.4]&quot; style=&quot;[{fill: '#002E22', fontSize: 11, fontWeight: 700, fontFamily: 'Expensify Neue'}, {fill: '#76847E', fontSize: 11, fontWeight: 400, fontFamily: 'Expensify Neue'}]&quot; /&gt;" colorscale="['#FED607', '#FF7101', '#F68DFE', '#03D47C', '#50EEF6', '#0185FF', '#D18000', '#BF3013']" style="{data: {stroke: '#F8F4F0'}}" /> <victorylabel x="340" y="265" text="'Total\n$6,133'" textanchor="middle" verticalanchor="middle" lineheight="[1.23, 1.27]" style="[{fill: '#76847E', fontSize: 13, fontFamily: 'Expensify Neue'}, {fill: '#002E22', fontSize: 26, fontFamily: 'Expensify New Kansas'}]" /> </victorychart>

I am not seeing amount with this data.

Screenshot 2026-07-15 at 7 57 41 PM Screenshot 2026-07-15 at 7 58 30 PM

There's no padding at the edge

The bottom and left/right edge-padding fix changes rendered pixel
output for these fixtures, so their golden regression snapshots need
regenerating to match.
@luacmartins

Copy link
Copy Markdown
Contributor Author

I am not seeing amount with this data.

Oh weird since I think that's the data that I used to generate the chart in the OP. It seems like you figured it out though.

There's no padding at the edge

@situchan should be fixed now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

InternalQA This pull request required internal QA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants