mirror of
https://github.com/we-promise/sure.git
synced 2026-09-05 14:51:15 +00:00
* fix(charts): round chart values to the currency's display precision Charts are drawn from the serialized amounts, not from the formatted strings, so a sub-unit residue plotted as a visible move between two points that both read $0.00, and the trend between them reported a change. Round in `Series#as_json` so the series values stay exact for insights, goals and the assistant. Also hide the percentage when the previous value is zero, since that makes it infinite. * fix(charts): round the two payloads that bypass Series#as_json `NetWorthBreakdownSeriesBuilder` builds its payload by hand, so the reports chart never went through the rounding added in `Series#as_json` and still plotted raw amounts: adjacent points printing the same value rendered a visible move, and the tooltip it inherits reported a change between them. `Series#trend` had the same gap on the server side. It is rendered right above the chart by `UI::Account::Chart` and by the reports summary, so an account going from 0 to a sub-cent residue showed a coloured $0.00 change next to a flat line. Rounding the trend can make a previously finite percentage infinite, so guard the three views that render `percent_formatted` without checking, as `shared/_trend_change` already does. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(charts): address net worth review comments * fix(charts): tighten rounded trend handling * fix(reports): restore positive sign in print trend --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: sure-admin <sure-admin@splashblot.com>
101 lines
3.0 KiB
Ruby
101 lines
3.0 KiB
Ruby
class Series
|
|
# Behave like an Array whose elements are the `Value` structs stored in `values`
|
|
include Enumerable
|
|
|
|
# Forward any undefined method calls (e.g. `first`, `[]`, `map`) to `values`
|
|
delegate_missing_to :values
|
|
|
|
# Enumerable needs `#each`
|
|
def each(&block)
|
|
values.each(&block)
|
|
end
|
|
|
|
attr_reader :start_date, :end_date, :interval, :trend, :values, :favorable_direction
|
|
|
|
Value = Struct.new(
|
|
:date,
|
|
:date_formatted,
|
|
:value,
|
|
:trend,
|
|
keyword_init: true
|
|
)
|
|
|
|
class << self
|
|
def from_raw_values(values, interval: "1 day")
|
|
raise ArgumentError, "Must be an array of at least 2 values" unless values.size >= 2
|
|
raise ArgumentError, "Must have date and value properties" unless values.all? { |value| value.has_key?(:date) && value.has_key?(:value) }
|
|
|
|
ordered = values.sort_by { |value| value[:date] }
|
|
start_date = ordered.first[:date]
|
|
end_date = ordered.last[:date]
|
|
|
|
new(
|
|
start_date: start_date,
|
|
end_date: end_date,
|
|
interval: interval,
|
|
values: [ nil, *ordered ].each_cons(2).map do |prev_value, curr_value|
|
|
Value.new(
|
|
date: curr_value[:date],
|
|
date_formatted: I18n.l(curr_value[:date], format: :long),
|
|
value: curr_value[:value],
|
|
trend: Trend.new(
|
|
current: curr_value[:value],
|
|
previous: prev_value&.[](:value)
|
|
)
|
|
)
|
|
end
|
|
)
|
|
end
|
|
end
|
|
|
|
def initialize(start_date:, end_date:, interval:, values:, favorable_direction: "up")
|
|
@start_date = start_date
|
|
@end_date = end_date
|
|
@interval = interval
|
|
@values = values
|
|
@favorable_direction = favorable_direction
|
|
end
|
|
|
|
# Rounded like the serialized values: this trend is only ever rendered next to
|
|
# them, so a change between two amounts that print identically has to read as
|
|
# no change here too.
|
|
def trend
|
|
return nil if values.blank?
|
|
@trend ||= Trend.new(
|
|
current: display_amount(values.last&.value),
|
|
previous: display_amount(values.first&.value),
|
|
favorable_direction: favorable_direction
|
|
)
|
|
end
|
|
|
|
def as_json
|
|
{
|
|
start_date: start_date,
|
|
end_date: end_date,
|
|
interval: interval,
|
|
trend: trend,
|
|
values: values.map do |v|
|
|
{ date: v.date, date_formatted: v.date_formatted, value: display_amount(v.value), trend: display_trend(v.trend) }
|
|
end
|
|
}
|
|
end
|
|
|
|
private
|
|
# A chart is drawn from these amounts, not from the formatted strings, so a
|
|
# sub-unit residue would plot as a visible move between two points that both
|
|
# print as the same value, and a change between them would read as non-zero.
|
|
def display_trend(trend)
|
|
return trend unless trend.is_a?(Trend)
|
|
|
|
Trend.new(
|
|
current: display_amount(trend.current),
|
|
previous: display_amount(trend.previous),
|
|
favorable_direction: trend.favorable_direction
|
|
)
|
|
end
|
|
|
|
def display_amount(amount)
|
|
amount.is_a?(Money) ? amount.for_display : amount
|
|
end
|
|
end
|