Skip to content

chart

Chart components for the refinance GUI.

AmortizationChart

Bases: Canvas

Chart showing remaining balances for current and new loans.

Attributes:

Name Type Description
width int

Canvas width.

height int

Canvas height.

padding dict[str, int]

Plot padding.

Source code in src/refi_calculator/gui/chart.py
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
class AmortizationChart(tk.Canvas):
    """Chart showing remaining balances for current and new loans.

    Attributes:
        width: Canvas width.
        height: Canvas height.
        padding: Plot padding.
    """

    width: int
    height: int
    padding: dict[str, int]

    def __init__(
        self,
        parent: tk.Misc,
        width: int = 400,
        height: int = 220,
    ):
        """Initialize AmortizationChart.

        Args:
            parent: Parent Tkinter widget (any widget subclass).
            width: Canvas width.
            height: Canvas height.
        """
        super().__init__(
            parent,
            width=width,
            height=height,
            bg="white",
            highlightthickness=1,
            highlightbackground="#ccc",
        )
        self.width = width
        self.height = height
        self.padding = {
            "left": 60,
            "right": 20,
            "top": 30,
            "bottom": 40,
        }

    def plot(
        self,
        current_schedule: list[dict],
        new_schedule: list[dict],
    ) -> None:
        """Plot remaining balances for both loans.

        Args:
            current_schedule: Monthly data for the current loan.
            new_schedule: Monthly data for the new loan.
        """
        self.delete("all")
        if not current_schedule or not new_schedule:
            return

        current_months = [row["month"] for row in current_schedule]
        current_balances = [row["balance"] for row in current_schedule]
        new_months = [row["month"] for row in new_schedule]
        new_balances = [row["balance"] for row in new_schedule]

        max_month = max(current_months[-1], new_months[-1])
        if max_month == 0:
            max_month = 1

        all_balances = [0.0]
        all_balances.extend(current_balances)
        all_balances.extend(new_balances)
        max_balance = max(all_balances)
        y_min = 0.0
        y_max = max_balance if max_balance > 0 else 1.0
        y_range = y_max - y_min if y_max != y_min else 1.0

        plot_w = self.width - self.padding["left"] - self.padding["right"]
        plot_h = self.height - self.padding["top"] - self.padding["bottom"]

        def to_canvas(month: int, balance: float) -> tuple[float, float]:
            x = self.padding["left"] + (month / max_month) * plot_w
            y = self.padding["top"] + (1 - (balance - y_min) / y_range) * plot_h
            return x, y

        current_points = [to_canvas(m, b) for m, b in zip(current_months, current_balances)]
        new_points = [to_canvas(m, b) for m, b in zip(new_months, new_balances)]

        if len(current_points) > 1:
            self.create_line(
                *[value for point in current_points for value in point],
                fill="#dc2626",
                width=2,
                smooth=True,
            )
        if len(new_points) > 1:
            self.create_line(
                *[value for point in new_points for value in point],
                fill="#2563eb",
                width=2,
                smooth=True,
            )

        left = self.padding["left"]
        bottom = self.height - self.padding["bottom"]
        right = self.width - self.padding["right"]

        self.create_line(left, self.padding["top"], left, bottom, fill="#333")
        self.create_line(left, bottom, right, bottom, fill="#333")

        for month in build_month_ticks(max_month):
            x = left + (month / max_month) * plot_w
            self.create_line(x, bottom, x, bottom + 5, fill="#999")
            self.create_text(
                x,
                bottom + 12,
                text=f"{month} mo",
                anchor=tk.N,
                font=("Segoe UI", 7),
                fill="#666",
            )

        for value in build_linear_ticks(y_min, y_max):
            y = self.padding["top"] + (1 - (value - y_min) / y_range) * plot_h
            self.create_line(left - 5, y, left, y, fill="#999")
            self.create_text(
                left - 8,
                y,
                text=f"${value / 1000:.0f}k",
                anchor=tk.E,
                font=("Segoe UI", 7),
                fill="#666",
            )

        self.create_text(
            self.padding["left"],
            self.padding["top"] - 6,
            text="Remaining Balance",
            anchor=tk.SW,
            font=("Segoe UI", 7),
            fill="#666",
        )
        self.create_text(
            self.width // 2,
            self.height - 8,
            text="Months",
            font=("Segoe UI", 8),
            fill="#666",
        )

        legend_x = self.width - self.padding["right"] - 90
        self.create_line(legend_x, 14, legend_x + 24, 14, fill="#dc2626", width=2)
        self.create_text(
            legend_x + 28,
            14,
            text="Current",
            anchor=tk.W,
            font=("Segoe UI", 7),
            fill="#666",
        )
        self.create_line(legend_x, 26, legend_x + 24, 26, fill="#2563eb", width=2)
        self.create_text(
            legend_x + 28,
            26,
            text="New",
            anchor=tk.W,
            font=("Segoe UI", 7),
            fill="#666",
        )

__init__(parent, width=400, height=220)

Initialize AmortizationChart.

Parameters:

Name Type Description Default
parent Misc

Parent Tkinter widget (any widget subclass).

required
width int

Canvas width.

400
height int

Canvas height.

220
Source code in src/refi_calculator/gui/chart.py
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
def __init__(
    self,
    parent: tk.Misc,
    width: int = 400,
    height: int = 220,
):
    """Initialize AmortizationChart.

    Args:
        parent: Parent Tkinter widget (any widget subclass).
        width: Canvas width.
        height: Canvas height.
    """
    super().__init__(
        parent,
        width=width,
        height=height,
        bg="white",
        highlightthickness=1,
        highlightbackground="#ccc",
    )
    self.width = width
    self.height = height
    self.padding = {
        "left": 60,
        "right": 20,
        "top": 30,
        "bottom": 40,
    }

plot(current_schedule, new_schedule)

Plot remaining balances for both loans.

Parameters:

Name Type Description Default
current_schedule list[dict]

Monthly data for the current loan.

required
new_schedule list[dict]

Monthly data for the new loan.

required
Source code in src/refi_calculator/gui/chart.py
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
def plot(
    self,
    current_schedule: list[dict],
    new_schedule: list[dict],
) -> None:
    """Plot remaining balances for both loans.

    Args:
        current_schedule: Monthly data for the current loan.
        new_schedule: Monthly data for the new loan.
    """
    self.delete("all")
    if not current_schedule or not new_schedule:
        return

    current_months = [row["month"] for row in current_schedule]
    current_balances = [row["balance"] for row in current_schedule]
    new_months = [row["month"] for row in new_schedule]
    new_balances = [row["balance"] for row in new_schedule]

    max_month = max(current_months[-1], new_months[-1])
    if max_month == 0:
        max_month = 1

    all_balances = [0.0]
    all_balances.extend(current_balances)
    all_balances.extend(new_balances)
    max_balance = max(all_balances)
    y_min = 0.0
    y_max = max_balance if max_balance > 0 else 1.0
    y_range = y_max - y_min if y_max != y_min else 1.0

    plot_w = self.width - self.padding["left"] - self.padding["right"]
    plot_h = self.height - self.padding["top"] - self.padding["bottom"]

    def to_canvas(month: int, balance: float) -> tuple[float, float]:
        x = self.padding["left"] + (month / max_month) * plot_w
        y = self.padding["top"] + (1 - (balance - y_min) / y_range) * plot_h
        return x, y

    current_points = [to_canvas(m, b) for m, b in zip(current_months, current_balances)]
    new_points = [to_canvas(m, b) for m, b in zip(new_months, new_balances)]

    if len(current_points) > 1:
        self.create_line(
            *[value for point in current_points for value in point],
            fill="#dc2626",
            width=2,
            smooth=True,
        )
    if len(new_points) > 1:
        self.create_line(
            *[value for point in new_points for value in point],
            fill="#2563eb",
            width=2,
            smooth=True,
        )

    left = self.padding["left"]
    bottom = self.height - self.padding["bottom"]
    right = self.width - self.padding["right"]

    self.create_line(left, self.padding["top"], left, bottom, fill="#333")
    self.create_line(left, bottom, right, bottom, fill="#333")

    for month in build_month_ticks(max_month):
        x = left + (month / max_month) * plot_w
        self.create_line(x, bottom, x, bottom + 5, fill="#999")
        self.create_text(
            x,
            bottom + 12,
            text=f"{month} mo",
            anchor=tk.N,
            font=("Segoe UI", 7),
            fill="#666",
        )

    for value in build_linear_ticks(y_min, y_max):
        y = self.padding["top"] + (1 - (value - y_min) / y_range) * plot_h
        self.create_line(left - 5, y, left, y, fill="#999")
        self.create_text(
            left - 8,
            y,
            text=f"${value / 1000:.0f}k",
            anchor=tk.E,
            font=("Segoe UI", 7),
            fill="#666",
        )

    self.create_text(
        self.padding["left"],
        self.padding["top"] - 6,
        text="Remaining Balance",
        anchor=tk.SW,
        font=("Segoe UI", 7),
        fill="#666",
    )
    self.create_text(
        self.width // 2,
        self.height - 8,
        text="Months",
        font=("Segoe UI", 8),
        fill="#666",
    )

    legend_x = self.width - self.padding["right"] - 90
    self.create_line(legend_x, 14, legend_x + 24, 14, fill="#dc2626", width=2)
    self.create_text(
        legend_x + 28,
        14,
        text="Current",
        anchor=tk.W,
        font=("Segoe UI", 7),
        fill="#666",
    )
    self.create_line(legend_x, 26, legend_x + 24, 26, fill="#2563eb", width=2)
    self.create_text(
        legend_x + 28,
        26,
        text="New",
        anchor=tk.W,
        font=("Segoe UI", 7),
        fill="#666",
    )

SavingsChart

Bases: Canvas

Canvas that draws cumulative savings / NPV trends.

Attributes:

Name Type Description
width int

Canvas width.

height int

Canvas height.

padding dict[str, int]

Padding around the plot area.

Source code in src/refi_calculator/gui/chart.py
 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
 40
 41
 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
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
class SavingsChart(tk.Canvas):
    """Canvas that draws cumulative savings / NPV trends.

    Attributes:
        width: Canvas width.
        height: Canvas height.
        padding: Padding around the plot area.
    """

    width: int
    height: int
    padding: dict[str, int]

    def __init__(
        self,
        parent: tk.Misc,
        width: int = 400,
        height: int = 200,
    ):
        """Initialize SavingsChart.

        Args:
            parent: Parent Tkinter widget (any widget subclass).
            width: Canvas width.
            height: Canvas height.
        """
        super().__init__(
            parent,
            width=width,
            height=height,
            bg="white",
            highlightthickness=1,
            highlightbackground="#ccc",
        )
        self.width = width
        self.height = height
        self.padding = {
            "left": 60,
            "right": 20,
            "top": 20,
            "bottom": 40,
        }

    def plot(
        self,
        data: list[tuple[int, float, float]],
        breakeven: int | None,
    ) -> None:
        """Plot cumulative savings tuples and optional breakeven marker.

        Args:
            data: List of (month, nominal savings, NPV savings) tuples.
            breakeven: Optional breakeven month to mark on the chart.
        """
        self.delete("all")
        min_number_of_data_points = 2
        if len(data) < min_number_of_data_points:
            return

        months = [d[0] for d in data]
        nominal = [d[1] for d in data]
        npv = [d[2] for d in data]

        all_values = nominal + npv
        y_min, y_max = min(all_values), max(all_values)
        if y_min == y_max:
            expansion = abs(y_max) or 1.0
            y_min -= expansion / 2
            y_max += expansion / 2
        y_range = y_max - y_min
        if y_range == 0:
            y_range = 1.0

        plot_w = self.width - self.padding["left"] - self.padding["right"]
        plot_h = self.height - self.padding["top"] - self.padding["bottom"]

        max_month = max(months)
        if max_month == 0:
            max_month = 1

        def to_canvas(month: int, value: float) -> tuple[float, float]:
            x = self.padding["left"] + (month / max_month) * plot_w
            y = self.padding["top"] + (1 - (value - y_min) / y_range) * plot_h
            return x, y

        if y_min < 0 < y_max:
            self._draw_zero_reference(to_canvas)

        if breakeven and breakeven <= max_month:
            self._draw_breakeven_line(breakeven, to_canvas)

        nominal_points = self._canvas_points(months, nominal, to_canvas)
        npv_points = self._canvas_points(months, npv, to_canvas)

        self._render_series(nominal_points, "#2563eb")
        self._render_series(npv_points, "#16a34a")

        left = self.padding["left"]
        right = self.width - self.padding["right"]
        bottom = self.height - self.padding["bottom"]

        self._draw_axis_lines(left, bottom, right)
        self._draw_month_ticks(left, bottom, plot_w, max_month)
        self._draw_value_ticks(left, plot_h, bottom, y_min, y_max, y_range)
        self._draw_range_labels(left, bottom, y_min, y_max)
        self._draw_legend()

    def _draw_zero_reference(
        self,
        to_canvas: Callable[[int, float], tuple[float, float]],
    ) -> None:
        _, zero_y = to_canvas(0, 0)
        self.create_line(
            self.padding["left"],
            zero_y,
            self.width - self.padding["right"],
            zero_y,
            fill="#ccc",
            dash=(4, 2),
        )
        self.create_text(
            self.padding["left"] + 4,
            zero_y - 5,
            text="0 cumulative savings",
            anchor=tk.SW,
            font=("Segoe UI", 7),
            fill="#666",
        )

    def _draw_breakeven_line(
        self,
        breakeven: int,
        to_canvas: Callable[[int, float], tuple[float, float]],
    ) -> None:
        be_x, _ = to_canvas(breakeven, 0)
        self.create_line(
            be_x,
            self.padding["top"],
            be_x,
            self.height - self.padding["bottom"],
            fill="#888",
            dash=(2, 2),
        )
        self.create_text(
            be_x,
            self.padding["top"] - 5,
            text=f"BE: {breakeven}mo",
            font=("Segoe UI", 7),
            fill="#666",
        )

    def _canvas_points(
        self,
        months: list[int],
        values: list[float],
        to_canvas: Callable[[int, float], tuple[float, float]],
    ) -> list[tuple[float, float]]:
        return [to_canvas(month, value) for month, value in zip(months, values)]

    def _render_series(self, points: list[tuple[float, float]], color: str) -> None:
        if len(points) > 1:
            self.create_line(
                *[coord for point in points for coord in point],
                fill=color,
                width=2,
                smooth=True,
            )

    def _draw_axis_lines(self, left: float, bottom: float, right: float) -> None:
        self.create_line(
            left,
            self.padding["top"],
            left,
            bottom,
            fill="#333",
        )
        self.create_line(
            left,
            bottom,
            right,
            bottom,
            fill="#333",
        )

    def _draw_month_ticks(
        self,
        left: float,
        bottom: float,
        plot_width: float,
        max_month: int,
    ) -> None:
        for month in build_month_ticks(max_month):
            x = left + (month / max_month) * plot_width
            self.create_line(x, bottom, x, bottom + 5, fill="#999")
            self.create_text(
                x,
                bottom + 12,
                text=f"{month} mo",
                anchor=tk.N,
                font=("Segoe UI", 7),
                fill="#666",
            )

    def _draw_value_ticks(
        self,
        left: float,
        plot_height: float,
        bottom: float,
        y_min: float,
        y_max: float,
        y_range: float,
    ) -> None:
        top = self.padding["top"]
        for value in build_linear_ticks(y_min, y_max):
            y = top + (1 - (value - y_min) / y_range) * plot_height
            self.create_line(left - 5, y, left, y, fill="#999")
            self.create_text(
                left - 8,
                y,
                text=f"${value / 1000:.0f}k",
                anchor=tk.E,
                font=("Segoe UI", 7),
                fill="#666",
            )

    def _draw_range_labels(self, left: float, bottom: float, y_min: float, y_max: float) -> None:
        self.create_text(
            self.width // 2,
            self.height - 8,
            text="Months",
            font=("Segoe UI", 8),
            fill="#666",
        )
        self.create_text(
            left - 5,
            self.padding["top"],
            text=f"${y_max / 1000:.0f}k",
            anchor=tk.E,
            font=("Segoe UI", 7),
            fill="#666",
        )
        self.create_text(
            left - 5,
            bottom,
            text=f"${y_min / 1000:.0f}k",
            anchor=tk.E,
            font=("Segoe UI", 7),
            fill="#666",
        )

    def _draw_legend(self) -> None:
        legend_x = self.width - self.padding["right"] - 90
        self.create_line(legend_x, 14, legend_x + 24, 14, fill="#2563eb", width=2)
        self.create_text(
            legend_x + 28,
            14,
            text="Nominal",
            anchor=tk.W,
            font=("Segoe UI", 7),
            fill="#666",
        )
        self.create_line(legend_x, 26, legend_x + 24, 26, fill="#16a34a", width=2)
        self.create_text(
            legend_x + 28,
            26,
            text="NPV",
            anchor=tk.W,
            font=("Segoe UI", 7),
            fill="#666",
        )

__init__(parent, width=400, height=200)

Initialize SavingsChart.

Parameters:

Name Type Description Default
parent Misc

Parent Tkinter widget (any widget subclass).

required
width int

Canvas width.

400
height int

Canvas height.

200
Source code in src/refi_calculator/gui/chart.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def __init__(
    self,
    parent: tk.Misc,
    width: int = 400,
    height: int = 200,
):
    """Initialize SavingsChart.

    Args:
        parent: Parent Tkinter widget (any widget subclass).
        width: Canvas width.
        height: Canvas height.
    """
    super().__init__(
        parent,
        width=width,
        height=height,
        bg="white",
        highlightthickness=1,
        highlightbackground="#ccc",
    )
    self.width = width
    self.height = height
    self.padding = {
        "left": 60,
        "right": 20,
        "top": 20,
        "bottom": 40,
    }

plot(data, breakeven)

Plot cumulative savings tuples and optional breakeven marker.

Parameters:

Name Type Description Default
data list[tuple[int, float, float]]

List of (month, nominal savings, NPV savings) tuples.

required
breakeven int | None

Optional breakeven month to mark on the chart.

required
Source code in src/refi_calculator/gui/chart.py
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def plot(
    self,
    data: list[tuple[int, float, float]],
    breakeven: int | None,
) -> None:
    """Plot cumulative savings tuples and optional breakeven marker.

    Args:
        data: List of (month, nominal savings, NPV savings) tuples.
        breakeven: Optional breakeven month to mark on the chart.
    """
    self.delete("all")
    min_number_of_data_points = 2
    if len(data) < min_number_of_data_points:
        return

    months = [d[0] for d in data]
    nominal = [d[1] for d in data]
    npv = [d[2] for d in data]

    all_values = nominal + npv
    y_min, y_max = min(all_values), max(all_values)
    if y_min == y_max:
        expansion = abs(y_max) or 1.0
        y_min -= expansion / 2
        y_max += expansion / 2
    y_range = y_max - y_min
    if y_range == 0:
        y_range = 1.0

    plot_w = self.width - self.padding["left"] - self.padding["right"]
    plot_h = self.height - self.padding["top"] - self.padding["bottom"]

    max_month = max(months)
    if max_month == 0:
        max_month = 1

    def to_canvas(month: int, value: float) -> tuple[float, float]:
        x = self.padding["left"] + (month / max_month) * plot_w
        y = self.padding["top"] + (1 - (value - y_min) / y_range) * plot_h
        return x, y

    if y_min < 0 < y_max:
        self._draw_zero_reference(to_canvas)

    if breakeven and breakeven <= max_month:
        self._draw_breakeven_line(breakeven, to_canvas)

    nominal_points = self._canvas_points(months, nominal, to_canvas)
    npv_points = self._canvas_points(months, npv, to_canvas)

    self._render_series(nominal_points, "#2563eb")
    self._render_series(npv_points, "#16a34a")

    left = self.padding["left"]
    right = self.width - self.padding["right"]
    bottom = self.height - self.padding["bottom"]

    self._draw_axis_lines(left, bottom, right)
    self._draw_month_ticks(left, bottom, plot_w, max_month)
    self._draw_value_ticks(left, plot_h, bottom, y_min, y_max, y_range)
    self._draw_range_labels(left, bottom, y_min, y_max)
    self._draw_legend()