Skip to content

charts

Shared chart helper utilities for refinance visualizations.

build_linear_ticks(min_value, max_value, max_ticks=5)

Generate evenly spaced linear axis tick values.

Parameters:

Name Type Description Default
min_value float

Minimum value to include on the axis.

required
max_value float

Maximum value to include on the axis.

required
max_ticks int

Maximum number of ticks to produce.

5

Returns:

Type Description
list[float]

A list of axis values covering the requested range.

Source code in src/refi_calculator/core/charts.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def build_linear_ticks(min_value: float, max_value: float, max_ticks: int = 5) -> list[float]:
    """Generate evenly spaced linear axis tick values.

    Args:
        min_value: Minimum value to include on the axis.
        max_value: Maximum value to include on the axis.
        max_ticks: Maximum number of ticks to produce.

    Returns:
        A list of axis values covering the requested range.
    """
    if max_ticks < MIN_LINEAR_TICKS:
        return [min_value, max_value]

    span = max_value - min_value
    if span == 0:
        expansion = abs(max_value) or 1.0
        min_value -= expansion / 2
        max_value += expansion / 2
        span = max_value - min_value

    step = span / (max_ticks - 1)
    ticks = [min_value + step * i for i in range(max_ticks)]
    ticks[-1] = max_value
    return ticks

build_month_ticks(max_month, max_ticks=6)

Generate tick positions for month-based axes.

Parameters:

Name Type Description Default
max_month int

Latest month number to display on the axis.

required
max_ticks int

Maximum number of tick marks to emit.

6

Returns:

Type Description
list[int]

A list of month values to use for axis ticks.

Source code in src/refi_calculator/core/charts.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def build_month_ticks(max_month: int, max_ticks: int = 6) -> list[int]:
    """Generate tick positions for month-based axes.

    Args:
        max_month: Latest month number to display on the axis.
        max_ticks: Maximum number of tick marks to emit.

    Returns:
        A list of month values to use for axis ticks.
    """
    if max_month <= 0:
        return [0]

    tick_count = min(max_ticks, max_month + 1)
    if tick_count <= 1:
        return [max_month]

    interval = max_month / (tick_count - 1)
    ticks: list[int] = []
    last = -1
    for i in range(tick_count):
        tick = int(round(i * interval))
        if tick <= last:
            tick = last + 1
        tick = min(max_month, tick)
        ticks.append(tick)
        last = tick

    if ticks[-1] != max_month:
        ticks[-1] = max_month

    return ticks