Draw a card's credit limit as a line on its balance chart

JA
2d ago
+439-11

Fixes jaysbeekay/sure#900 (with the slice below, #1020)
Upstream issue: we-promise/sure#4356
Upstream PR: https://github.com/we-promise/sure/pull/5989 (we-promise/sure#5989, draft, position 4 of the native stack https://github.com/we-promise/sure/stack/5995); same branch, same commits.

Stack: base up/card-credit-limit (jaysbeekay/sure#1020) ← up/card-daily-interest (jaysbeekay/sure#1006). Next slice: up/redbark-card-limit.

Summary

A card with a credit limit now shows the limit on its balance chart: a solid line that joins the chart's y-domain (so it is always on the plot), and, once the balance reaches 80% of the limit, a tinted band between the 80% level and the line. The card chart gets a legend. Every other account, and a card without a limit, gets exactly the chart payload it had before.

What changed

  • UI::Account::Chart#chart_data: the series JSON, plus reference_line: { value, threshold, tinted, token, band_opacity } only for a CreditCard with a positive credit_limit. tinted compares the last plotted balance with limit × CARD_LIMIT_ALERT_SHARE (0.8) directly, so 4,799.99 of 6,000 (79.9998%, which rounds to 80.0) does not tint and 4,800 does. The band describes the line it sits on, so a past range tints from its own last point, not from today's balance.
  • CARD_LIMIT_TOKEN (--color-warning) and CARD_LIMIT_BAND_OPACITY (0.12). The line uses warning, not destructive, because the balance line itself turns red as debt grows (screenshot below). The chart's band and the legend swatch share the one opacity. Solid, because the limit is a fact (dashed stays "forecast", per LOAN_SERIES_STYLES).
  • chart.html.erb: the time-series mount reads chart_data. Under a card chart that has a limit there is a legend: the balance swatch in the trend colour, the limit line, and the band only when tinted. The legend shows only when the series has two or more points, which is when the controller draws the line. The loan and card legends share two component-local helpers, legend_line_item and legend_swatch_item. They are not a DS::* primitive, because only this component uses them. The band swatch has a full-strength border, and its fill is at the band's opacity.
  • time_series_chart_controller.js: _referenceLine from the payload. _d3YScale includes the limit in the domain, and the scale is built once per draw (memoised, cleared at the start of _draw). _drawReferenceLine draws the band (when tinted) and then the line, before the trendline. Flatness is still judged from the balance points only, so an unchanged balance keeps its faint hairline with the limit above it.
  • components/en.yml: legend labels; the band label is "%{share} or more of the limit", with the share taken from CARD_LIMIT_ALERT_SHARE.

Tests

test/components/UI/account/chart_test.rb (red on #1020: 2 errors, undefined method '[]' for nil — no reference_line)

  • 533.10 of 6,000 → reference_line value 6000.0, threshold 4800.0, tinted false; legend lists "Credit limit"
  • 4,800 → tinted; 4,799.99 → not tinted (balances set on the series)
  • no limit and limit 0 → no reference_line key, no legend; a depository's payload is byte-identical to series.as_json

Added after the Gatekeeper review (each failed first as noted):

  • "a card whose series has one point shows no limit legend": before, expected not to find ul[aria-label='Chart series'], found 1
  • "the band is tinted from the last plotted balance, not the account balance": before, Expected false, Actual true
  • "the band legend names the alert share it is tinted from": with the share swapped to 0.9 the legend reads 90%; before, it still read 80%
  • "a card with a limit and no balance points is not tinted": before, NoMethodError on a nil last value

test/system/credit_card_limit_chart_test.rb (new, browser)

  • limit far above the balance: the line's y is inside the plot (≥ 0, < height) and above the balance line; no band
  • balance at 80% → band drawn, its top equal to the line's y1 (within 1px), its height positive, its fill the line's stroke
  • new "a balance at the limit meets the limit line": the line sits on the trendline's top (within 1px)
  • new "the legend swatch tints its fill like the band and borders it like the line": opacity 1, border = the line's stroke, fill alpha = the band opacity
  • new "an unchanged balance under a limit stays a hairline, with the limit on the chart": stroke-opacity 0.4, limit inside the plot and above the balance
  • new "the chart builds its y-scale once per draw" and "a draw builds its own y-scale without a teardown first"
  • no limit → no reference elements

Mutations (each alone)

#MutationCaught by
M1tint on > not >=80% boundary
M2tint on rounded utilization_percent >= 8080% boundary (4,799.99)
M3reference_line always merged (nil)no-limit/depository payload
M4non-positive limit acceptedlimit 0
M5legend removedlegend test
M6threshold 85%threshold + boundary tests
J1limit not in the y-domainsystem: line inside the plot (y was negative)
J2band always drawnsystem: no band below 80%
J3reference never drawnsystem: both line tests
J4line drawn at ref.threshold instead of ref.valuesystem: band top = line, balance at the limit
R1legend guard back to series.any?one-point legend test
R2tint from account.balancelast-plotted-balance test
R3flatness judged with the limit includedhairline test
R4limit dropped from the y-domain4 system tests
R5y-scale memo not cleared in _drawboth y-scale tests
R6swatch at element opacity, no border, or half the fillswatch test
R7share hard-coded as "80%" (method or view)alert-share test
R8nil-safe last value removedno-points test

Verification

  • Head 9e00509e9 changes only reference_line_geometry in test/system/credit_card_limit_chart_test.rb, which now checks the line against the plot height from the controller. That file ran 8 runs, 0 failures on it. The previous head, 7100fedfa, was a merge of base 787be900 that changed no file this PR owns, and its CI was green. CI on 9e00509e9: all 12 runs pass.
  • Full unit suite (beastpc) on b304ea7b2: 19502 runs, 0 failures, 8 errors, 9 skips (the 8 ReleaseNotesWorkflowTest errors, no node). After merging the updated jaysbeekay/sure#1020 and jaysbeekay/sure#1006 (7b72b7b0d), the targeted card and chart files ran 508 runs, 0 failures, 0 errors.
  • System tests: credit_card_limit_chart_test, loan_payoff_chart_test, account_chart_drag_select_test, dashboard_chart_drag_select_test, chart_sidebar_resize_test, chart_touch_test: 54 runs, 0 failures.
  • bin/rubocop: no offenses. erb_lint: no errors. biome lint (chart controller): clean. bin/brakeman: 0 warnings.

Screenshots

Seeded card: balance $5,100 of a $6,000 limit (85%, so the band is tinted). Before = #1020.

ViewBeforeAfter
Card page (chart + Overview) — light, desktopbeforeafter
Card page (chart + Overview) — light, phonebeforeafter
Card page (chart + Overview) — dark, desktopbeforeafter
Card page (chart + Overview) — dark, phonebeforeafter

After the Gatekeeper review fixes (b304ea7b2). "High" is 85% of the limit (band tinted; the swatch is bordered like the line and filled like the band). "Low" is 8.9% of the limit, showing how the limit in the y-range lowers a small balance. "Flat" is an unchanged balance, which keeps its hairline under the limit.

ViewLightDark
High, desktophighhigh
High, phonehighhigh
Low, desktoplowlow
Low, phonelowlow
Flat, desktopflatflat

Design system

The legend's line and swatch items are defined once in this diff: the loan legend's two inline <li> shapes (upstream chart.html.erb:95 and :101) become legend_line_item and legend_swatch_item, and the card legend calls the same helpers. The same line-swatch-beside-a-label shape is also hand-built upstream in app/components/goals/lifecycle_panel_component.html.erb:134-150. A DS::ChartLegend primitive (line and swatch items) covering the account chart and the goals panel is offered as a follow-up proposal upstream. It is not part of this PR.

Migration notes

No migration.

Blast radius

  • chart.html.erb's time-series mount is shared by every non-loan account. chart_data returns series.as_json unchanged unless the account is a card with a positive limit (pinned by the depository byte-equality test); series.to_json and series.as_json.to_json are the same string.
  • The limit stays in the y-domain on purpose: a line that slips off the top of the chart would be worse than a compressed low balance (see the "Low" screenshots).
  • time_series_chart_controller.js also draws sidebar sparklines, the dashboard net-worth chart and investment charts. _referenceLine is null for all of them, so _d3YScale and _drawChart take their old paths (chart system tests above green).
  • Overlap: open upstream PRs #5297, #5624 (investment net contributions), #3973, #5270, #3709 touch UI::Account::Chart; textual only.