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 <noreply@anthropic.com>
This commit is contained in:
Claudio Schaad 2026-10-26 16:27:17 +01:00
parent 7d7bd6073c
commit ec0bf7d602
5 changed files with 65 additions and 10 deletions

View file

@ -63,5 +63,15 @@ namespace Schaad.Accounting.Models
[JsonIgnore] [JsonIgnore]
[Display(Name = "CHF/kWh")] [Display(Name = "CHF/kWh")]
public decimal ChfPerKwh => TotalKwh == 0 ? 0 : TotalChf / TotalKwh; 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); 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"> <FluentCard class="chart-card">
<div class="card-title">Preis pro kWh</div> <div class="card-title">Preis pro kWh</div>
<ChartJsChart Kind="line" Data="@lineData" Height="360px" <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> </FluentCard>
</div> </div>
} }

View file

@ -63,9 +63,12 @@ public partial class Electricity : ComponentBase
labels, labels,
series = new object[] 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. // 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 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 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 // 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. // 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 } } 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. // unit/decimals default to CHF/0, so existing callers that don't pass data.unit/axisDecimals
const amountAxis = (unit = 'CHF') => ({ // keep their current axis.
const amountAxis = (unit = 'CHF', decimals = 0) => ({
grid: { color: theme.grid }, grid: { color: theme.grid },
border: { display: false }, border: { display: false },
title: { display: true, text: unit, color: theme.muted }, 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 } }; 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) => { hit: (chart, event) => {
const hit = chart.getElementsAtEventForMode(event, 'nearest', { intersect: true }, false)[0]; 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'}` } 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 hit: () => null
}, },