Add "Strom" page for the yearly NeoVac electricity bills #9

Merged
clayschaad merged 6 commits from add-electricity-page into main 2026-10-27 18:40:53 +00:00
5 changed files with 65 additions and 10 deletions
Showing only changes of commit ec0bf7d602 - Show all commits

View file

@ -63,5 +63,15 @@ namespace Schaad.Accounting.Models
[JsonIgnore]
[Display(Name = "CHF/kWh")]
public decimal ChfPerKwh => TotalKwh == 0 ? 0 : TotalChf / TotalKwh;
/// <summary>Price per kWh of grid electricity; null if there was no grid draw.</summary>
[JsonIgnore]
[Display(Name = "CHF/kWh Netz")]
public decimal? GridChfPerKwh => GridKwh == 0 ? null : GridChf / GridKwh;
/// <summary>Price per kWh of PV electricity; null if there was no PV consumption.</summary>
[JsonIgnore]
[Display(Name = "CHF/kWh PV")]
public decimal? PvChfPerKwh => PvKwh == 0 ? null : PvChf / PvKwh;
}
}

View file

@ -69,4 +69,36 @@ public class ElectricityBillTestShould
bill.ChfPerKwh.ShouldBe(0m);
}
[Fact]
public void DivideGridChfByGridKwhWhenCalculatingGridChfPerKwh()
{
var bill = new ElectricityBill { GridKwh = 600m, GridChf = 180m };
bill.GridChfPerKwh.ShouldBe(0.3m);
}
[Fact]
public void ReturnNullGridChfPerKwhWhenThereWasNoGridDraw()
{
var bill = new ElectricityBill { GridKwh = 0m, GridChf = 0m };
bill.GridChfPerKwh.ShouldBeNull();
}
[Fact]
public void DividePvChfByPvKwhWhenCalculatingPvChfPerKwh()
{
var bill = new ElectricityBill { PvKwh = 400m, PvChf = 100m };
bill.PvChfPerKwh.ShouldBe(0.25m);
}
[Fact]
public void ReturnNullPvChfPerKwhWhenThereWasNoPvConsumption()
{
var bill = new ElectricityBill { PvKwh = 0m, PvChf = 0m };
bill.PvChfPerKwh.ShouldBeNull();
}
}

View file

@ -50,7 +50,7 @@
<FluentCard class="chart-card">
<div class="card-title">Preis pro kWh</div>
<ChartJsChart Kind="line" Data="@lineData" Height="360px"
AriaLabel="Preis pro kWh über die Abrechnungsperioden" />
AriaLabel="Preis pro kWh für Netz, PV und Total über die Abrechnungsperioden" />
</FluentCard>
</div>
}

View file

@ -63,9 +63,12 @@ public partial class Electricity : ComponentBase
labels,
series = new object[]
{
new { name = "CHF/kWh", color = ChartTheme.ColorAt(0), values = source.Select(b => b.ChfPerKwh).ToList(), width = 2 }
new { name = "Netz", color = ChartTheme.ColorAt(0), values = source.Select(b => b.GridChfPerKwh).ToList(), width = 2 },
new { name = "PV", color = ChartTheme.ColorAt(1), values = source.Select(b => b.PvChfPerKwh).ToList(), width = 2 },
new { name = "Total", color = ChartTheme.OtherColor, values = source.Select(b => (decimal?)b.ChfPerKwh).ToList(), width = 2 }
},
unit = "CHF/kWh"
unit = "CHF/kWh",
axisDecimals = 2
};
}

View file

@ -36,9 +36,18 @@ const theme = {
// de-CH uses a typographic apostrophe; the rest of the app (Formatting.cs) uses a plain one.
const amount = new Intl.NumberFormat('de-CH', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
const axisAmount = new Intl.NumberFormat('de-CH', { maximumFractionDigits: 0 });
const formatChf = value => amount.format(value).replace(/’/g, "'");
const formatAxis = value => axisAmount.format(value).replace(/’/g, "'");
// Axis ticks default to whole numbers (CHF amounts in the hundreds/thousands); series with small
// values (e.g. CHF/kWh, well under 1) pass data.axisDecimals to avoid every tick rounding to "0".
const axisAmountFormatters = new Map();
function axisAmount(decimals) {
if (!axisAmountFormatters.has(decimals)) {
axisAmountFormatters.set(decimals, new Intl.NumberFormat('de-CH', { minimumFractionDigits: decimals, maximumFractionDigits: decimals }));
}
return axisAmountFormatters.get(decimals);
}
const formatAxis = (value, decimals = 0) => axisAmount(decimals).format(value).replace(/’/g, "'");
// Treemap tile helpers: level 0 tiles are parents (their group value is the parent id); level 1
// tiles keep the original tree items, which carry the parent id.
@ -68,12 +77,13 @@ const legend = {
labels: { usePointStyle: true, pointStyle: 'rectRounded', boxWidth: 10, color: theme.muted, font: { size: 12 } }
};
// unit defaults to CHF, so existing callers that don't pass data.unit keep their current axis.
const amountAxis = (unit = 'CHF') => ({
// unit/decimals default to CHF/0, so existing callers that don't pass data.unit/axisDecimals
// keep their current axis.
const amountAxis = (unit = 'CHF', decimals = 0) => ({
grid: { color: theme.grid },
border: { display: false },
title: { display: true, text: unit, color: theme.muted },
ticks: { color: theme.muted, callback: value => formatAxis(value) }
ticks: { color: theme.muted, callback: value => formatAxis(value, decimals) }
});
const categoryAxis = { grid: { display: false }, ticks: { color: theme.muted } };
@ -130,7 +140,7 @@ const kinds = {
}
}
},
scales: { x: { ...categoryAxis, stacked: true }, y: { ...amountAxis(state().unit ?? 'CHF'), stacked: true } }
scales: { x: { ...categoryAxis, stacked: true }, y: { ...amountAxis(state().unit ?? 'CHF', state().axisDecimals ?? 0), stacked: true } }
}),
hit: (chart, event) => {
const hit = chart.getElementsAtEventForMode(event, 'nearest', { intersect: true }, false)[0];
@ -166,7 +176,7 @@ const kinds = {
callbacks: { label: item => ` ${item.dataset.label}: ${formatChf(item.parsed.y)} ${state().unit ?? 'CHF'}` }
}
},
scales: { x: categoryAxis, y: amountAxis(state().unit ?? 'CHF') }
scales: { x: categoryAxis, y: amountAxis(state().unit ?? 'CHF', state().axisDecimals ?? 0) }
}),
hit: () => null
},