Draw net contributions beside total value on the account chart

Draft
JA
2mo ago
+236-2

Part of #326

Upstream pre-flight for the chart half of we-promise/sure#4008, stacked on jaysbeekay/sure#380. It replaces the pre-flight jaysbeekay/sure#344 (we-promise/sure#4029), which stays open until the owner decides how to retire it. Base: up/net-contributions-series (jaysbeekay/sure#380).

The upstream PR is NOT YET OPENED (awaiting owner approval). When it is: same branch, same commit.


Supersedes #4029. Same chart, now drawing the series from SERIES_PR (which supersedes #4028) instead of #4028's. #4029 will be closed in favour of this PR.

This PR is the last of a stack:

  1. #3557 adds the flow classifier.
  2. DAILY_RETURNS_PR adds Portfolio::DailyReturns; JOURNAL_FLOWS_PR values a journalled security at its position.
  3. SERIES_PR adds the net contributions series, read from those daily returns.
  4. This PR draws the series. It is built on SERIES_PR's branch (up/net-contributions-series).

Until the parents merge, GitHub shows their commits here too. Only one commit is this PR's:

  • 30b570dea Draw net contributions beside total value on the account chart

It is #4029's commit (64899409d), cherry-picked without conflicts. The only edits are the understated note's wording and one comment, because the rule for "understated" changed with the model (see below). After each parent merges I'll rebase this branch onto main, so only its own commit is left.

Closes #4008

Fork pre-flight PR: jaysbeekay/sure#381 (pull/381), same branch, same commit

Summary

On an investment account, and on a Crypto exchange account, the Total value chart now draws a second, dashed line: Net contributions. That is the money put in, net of the money taken out. The gap between the two lines is how the account has actually performed, and because the line is now built from the same daily returns as the portfolio's return figures, the gap agrees with them. The tooltip adds net contributions and the signed difference, with the difference as a percentage of net contributions. A legend under the chart names both lines.

What changed

  • UI::Account::Chart passes the model PR's series to time_series_chart_controller as a new optional comparison value. Each point carries its difference from total value. The figures are rounded the same way as the value line's points, so the three numbers in the tooltip agree.
  • time_series_chart_controller.js:
    • When comparison is present, the controller draws a dashed text-secondary line on the same scales. Both lines' points set the y domain.
    • It adds two rows to the tooltip: Net contributions and Difference. The rows are built as DOM nodes with d3's .text(), not as an HTML string, so no label or amount is parsed as markup. When net contributions are zero the percentage is left off rather than shown as infinity.
    • Without comparison, the controller takes exactly the path it took before.
  • The legend follows the shape the loan chart's legend already uses: a ul with an aria-label and aria-hidden swatches. The lines differ in style (solid and dashed) as well as colour.
  • When a flow the line counts could not be valued (a currency with no rate at all, or a journalled position with no price that day), a note under the legend says net contributions are understated from that date.
  • Four strings added to config/locales/views/components/en.yml under UI.account.chart.net_contributions.

Unchanged: the Holdings, Cash and Gains views, accounts that don't hold trades, and every other caller of the controller.

Changed from #4029: the understated note now reads "Some money moved in or out could not be valued, so net contributions are understated from that date." #4029's wording ("has no exchange rate for its date") no longer describes the rule. The model now carries the last known rate to a date with none, and also flags an unpriced journal. Nothing else in the diff changed.

How flows are valued (from the model PR):

  • A security moved in or out counts at the position's value that day.
  • A flow in another currency converts at the previous day's rate, as every start-of-day flow does in the daily returns.
  • A flow that cannot be valued counts as nothing, never at 1:1, and the understated note appears.

Demo data

No demo data change needed. From the generator's code, the demo Fidelity HSA Investment account's quarterly transfers from checking should step the line up, and the other demo investment accounts, which only record buys, should keep it flat at the opening value. I haven't checked this on a running demo instance.

Screenshots

These are #4029's screenshots. The test account has only same-currency cash flows and no journals, and for those the two valuations give the same figures. I haven't re-taken them on this branch.

The test account has these values:

  • opened with $10,000
  • $5,000 deposited about four months ago
  • a $150 dividend
  • $2,000 withdrawn

The dividend lifts only the value line. The withdrawal lowers both lines.

LightDark
LightDark
LightDark
Tooltip, lightTooltip, dark

Migration notes

No migration.

Verification

Tests in test/components/UI/account/chart_test.rb, unchanged from #4029, now running against the daily-returns model:

  • the Total value view of an investment account carries net contributions and a legend. The test checks the serialized difference: $400, +2.7% of $15,000.
  • the line isn't drawn on the Holdings, Cash or Gains views
  • the line is drawn only for accounts that hold trades: an exchange Crypto account does, a wallet Crypto account and a Depository account don't
  • the chart says when net contributions are understated

I broke each part on purpose and reran the test file. A test caught every break:

BreakageCaught by
Component drops the supports_trades? checkaccount-type test, and the existing build-once test
Component drops the balance view checkviews test
Difference computed the wrong way roundserialized-difference test
Understated note never shownunderstated test
Understated note always shownunderstated test + total value test
Comparison drawn from the value line's own seriestotal value test
Legend removedtotal value test + understated test

The controller has no JavaScript test harness in this repository, so the drawing and tooltip code is covered by Biome and by #4029's screenshots, not by automated tests.

Commands, on 30b570dea:

  • bin/rails test: 10,707 runs, 0 failures, 0 errors, 33 skips
  • bin/rubocop on the changed Ruby: no offenses
  • bundle exec erb_lint app/components/UI/account/chart.html.erb: no errors
  • biome check on the controller: clean
  • bin/brakeman: no warnings

Blast radius

Who else hits these lines, and what do they pass?

  • time_series_chart_controller is shared by the dashboard net worth chart, the sparkline partial and the onboarding preferences preview, as well as the account chart. The new comparison value is optional. When it is absent, _comparisonPoints is empty, so:

    • no extra line is drawn
    • the tooltip has no extra rows
    • the y-scale domain is computed from the same points as before (an empty array is concatenated)

    Only UI::Account::Chart passes the value (grep -rn "comparison-value" app), and only for the Total value view of an account that holds trades.

  • UI::Account::Chart: the series is still built once per render, and the existing "builds its balance series once per render" test passes. The net contributions series is a second call, made only when the line is shown.

What else reads what this changes the reading of? The understated note's string is read only by this template. The model PR lists the other "net contributions" measures (InvestmentStatement#net_contributions, Goal#net_contributed_for), which this chart doesn't use.

When this is rebased onto main: #3906 rewrote the body of _d3YScale after this branch's base. This PR changes only that method's first two lines, so both lines' points set the domain. The rebase will keep that change on top of the new scale logic.