From ec0bf7d6022b3c9e0a4d12c965179429a7318764 Mon Sep 17 00:00:00 2001 From: Claudio Schaad Date: Mon, 26 Oct 2026 16:27:17 +0100 Subject: [PATCH] Split the CHF/kWh chart by Netz/PV and fix its axis scale The price-per-kWh line only showed the blended total; add GridChfPerKwh and PvChfPerKwh (null when there was no draw/consumption of that kind, so the chart shows a gap instead of a misleading 0) and plot Netz, PV and Total as separate lines. Also fixes the y-axis: ticks used a fixed 0-decimal formatter, so CHF/kWh values (well under 1) all rounded to "0". charts.js now takes an optional data.axisDecimals (defaults to 0, so the Analyse page is unaffected); the Strom page passes 2 for its CHF/kWh chart. Co-Authored-By: Claude Sonnet 5 --- .../Models/ElectricityBill.cs | 10 ++++++ .../ElectricityBillTestShould.cs | 32 +++++++++++++++++++ .../Components/Pages/Electricity.razor | 2 +- .../Components/Pages/Electricity.razor.cs | 7 ++-- Schaad.Accounting.UI/wwwroot/js/charts.js | 24 ++++++++++---- 5 files changed, 65 insertions(+), 10 deletions(-) diff --git a/Schaad.Accounting.Common/Models/ElectricityBill.cs b/Schaad.Accounting.Common/Models/ElectricityBill.cs index 6311756..fd1d8de 100644 --- a/Schaad.Accounting.Common/Models/ElectricityBill.cs +++ b/Schaad.Accounting.Common/Models/ElectricityBill.cs @@ -63,5 +63,15 @@ namespace Schaad.Accounting.Models [JsonIgnore] [Display(Name = "CHF/kWh")] public decimal ChfPerKwh => TotalKwh == 0 ? 0 : TotalChf / TotalKwh; + + /// Price per kWh of grid electricity; null if there was no grid draw. + [JsonIgnore] + [Display(Name = "CHF/kWh Netz")] + public decimal? GridChfPerKwh => GridKwh == 0 ? null : GridChf / GridKwh; + + /// Price per kWh of PV electricity; null if there was no PV consumption. + [JsonIgnore] + [Display(Name = "CHF/kWh PV")] + public decimal? PvChfPerKwh => PvKwh == 0 ? null : PvChf / PvKwh; } } diff --git a/Schaad.Accounting.Tests/ElectricityBillTestShould.cs b/Schaad.Accounting.Tests/ElectricityBillTestShould.cs index b335ea9..2119756 100644 --- a/Schaad.Accounting.Tests/ElectricityBillTestShould.cs +++ b/Schaad.Accounting.Tests/ElectricityBillTestShould.cs @@ -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(); + } } diff --git a/Schaad.Accounting.UI/Components/Pages/Electricity.razor b/Schaad.Accounting.UI/Components/Pages/Electricity.razor index 3f49484..d8cfcf7 100644 --- a/Schaad.Accounting.UI/Components/Pages/Electricity.razor +++ b/Schaad.Accounting.UI/Components/Pages/Electricity.razor @@ -50,7 +50,7 @@
Preis pro kWh
+ AriaLabel="Preis pro kWh für Netz, PV und Total über die Abrechnungsperioden" />
} diff --git a/Schaad.Accounting.UI/Components/Pages/Electricity.razor.cs b/Schaad.Accounting.UI/Components/Pages/Electricity.razor.cs index 479ff26..f5d799c 100644 --- a/Schaad.Accounting.UI/Components/Pages/Electricity.razor.cs +++ b/Schaad.Accounting.UI/Components/Pages/Electricity.razor.cs @@ -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 }; } diff --git a/Schaad.Accounting.UI/wwwroot/js/charts.js b/Schaad.Accounting.UI/wwwroot/js/charts.js index 0fa4ce8..463d12a 100644 --- a/Schaad.Accounting.UI/wwwroot/js/charts.js +++ b/Schaad.Accounting.UI/wwwroot/js/charts.js @@ -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 },