## Summary A new **Analyse** page (Charts → Analyse, `/analytics`) shows the current mandator's **expenses over all years**, with drill-down **all expenses → subclass → account → booking text** (first word, like the existing charts). This covers steps 1 and 2 of the analytics plan; more views (composition, year comparison, heatmap, table) follow in later PRs. The page **replaces the old "Ausgaben" and "Zeitverlauf" charts**, which are removed (see below). "Vermögen" stays until the Analyse page covers assets. ### Page - **Filters:** from/to year, "Alle Jahre", Monat / Quartal / Jahr. The default is monthly for a single year and quarterly for several. - **Breadcrumb** to jump back up the drill-down. - **Tiles:** total, average per month, change versus the previous year over the same months (e.g. 2026 vs. 2025, Jan–Okt; more spending red, less green), largest item with its share. - **Stacked bar chart** (Chart.js) with sorted tooltips including a total. Clicking a segment drills down. Beyond 7 categories the tail is grouped as "Andere", like the other charts. - **Category list** with amount, share and share bar, in the chart's colours. Entries are links that drill down. - **URL state** (`?from=&to=&period=&sub=&acc=&text=`): back/forward and bookmarks work. Data is loaded once per visit; every change is an in-memory query. ### Data - `MultiYearRepository` (`IMultiYearRepository`) reads Accounts / SubClasses / Transactions of any mandator and year. It is read-only (never creates folders or copies files) and shares the per-session cache with the other repositories, so a save in the current year is reflected immediately. - `ExpenseAnalysis` flattens all years into one row per month / account / booking-text group: - **Net amounts:** bookings to an expense account count positive, bookings from one (refunds, corrections) negative; moves between expense accounts cancel out at the higher levels. - **Accounts are keyed by number** across years and shown with the latest name. - **Bookings count in the year of the books they are in**, like the annual reports. The few bookings dated outside that year (late-December payments booked in the next year) go to its first or last month. - The running year stops at the last month with data; the year-over-year comparison uses the same months of both years. - `TransactionDataset.GetTextGroup` gets a static overload so the grouping can be used without a transaction view. - `BaseRepository` can load a cached list from any directory. ### Chart.js - Chart.js **4.5.1** (MIT) is vendored in `wwwroot/lib/chartjs` with its licence, checked against the npm registry checksum. There is no CDN dependency, so the app works offline. - `wwwroot/js/charts.js` loads it only when the page opens and styles it like `ChartTheme` (palette, font, de-CH numbers with `'`). `StackedBarChart.razor` creates the chart once and then updates it in place. - Plotly stays for "Vermögen" and the booking-text charts on the Transactions page. ### Removed - The pages **Ausgaben** (`/spendings`, donut) and **Zeitverlauf** (`/SpendingsOverTime`, stacked bars) and their menu entries. Bookmarks to them now show "not found". - `ChartService.GetExpensesPerMonthAsync` with its two helpers and its 6 tests; nothing else used them. `ChartService` now only serves the booking-text charts. ## Test plan - [x] `dotnet build --no-incremental`: no warnings - [x] `dotnet test`: 75/75 pass (6 tests removed with `GetExpensesPerMonthAsync`), including 15 new `ExpenseAnalysisTestShould` tests (subclass totals, refunds, non-expense bookings, transfers, account renames across years, booking-text level, breadcrumb, book-year attribution, running year, quarters/years, year range, year-over-year comparison, no previous year, no data) and 4 `MultiYearRepositoryTestShould` tests - [x] Against a copy of the real data: every year's total matches an independent Python recomputation from the JSON files, and the 2026 total (77'006.07) equals "Total Ausgaben" in the profit-and-loss report - [x] In headless Chrome: chart renders, clicking a bar drills to the subclass, a list entry drills to the account, browser back returns, and the period buttons switch. No console errors. - [ ] Narrow-screen layout checked by hand (only covered by CSS so far) ## Known limitations - A partial last period (e.g. Q4 26 with only October) is not marked as incomplete yet. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Reviewed-on: #4 Co-authored-by: Claudio Schaad <c.schaad@pog.ch> Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
152 lines
6.2 KiB
Text
152 lines
6.2 KiB
Text
@page "/analytics"
|
||
@rendermode InteractiveServer
|
||
@using Schaad.Accounting.Datasets.Analytics
|
||
|
||
<PageTitle>Analyse</PageTitle>
|
||
|
||
@if (analysis is null)
|
||
{
|
||
<Loading/>
|
||
return;
|
||
}
|
||
|
||
@if (analysis.Years.Count == 0 || result is null)
|
||
{
|
||
<FluentMessageBar Intent="MessageIntent.Info" AllowDismiss="false">Für @mandator gibt es noch keine Daten.</FluentMessageBar>
|
||
return;
|
||
}
|
||
|
||
<div class="analytics">
|
||
<div class="toolbar">
|
||
<FluentBreadcrumb>
|
||
@for (var i = 0; i < result.Breadcrumb.Count; i++)
|
||
{
|
||
var crumb = result.Breadcrumb[i];
|
||
if (i < result.Breadcrumb.Count - 1)
|
||
{
|
||
<FluentBreadcrumbItem Href="@NodeUrl(crumb.Node)">@crumb.Name</FluentBreadcrumbItem>
|
||
}
|
||
else
|
||
{
|
||
<FluentBreadcrumbItem>@crumb.Name</FluentBreadcrumbItem>
|
||
}
|
||
}
|
||
</FluentBreadcrumb>
|
||
|
||
<div class="filters">
|
||
<FluentSelect TOption="int" Label="Von" Width="110px"
|
||
Items="@analysis.Years"
|
||
OptionText="@(y => y.ToString())"
|
||
OptionValue="@(y => y.ToString())"
|
||
SelectedOption="@fromYear"
|
||
SelectedOptionChanged="@OnFromChanged"/>
|
||
<FluentSelect TOption="int" Label="Bis" Width="110px"
|
||
Items="@analysis.Years"
|
||
OptionText="@(y => y.ToString())"
|
||
OptionValue="@(y => y.ToString())"
|
||
SelectedOption="@toYear"
|
||
SelectedOptionChanged="@OnToChanged"/>
|
||
<FluentButton Appearance="Appearance.Lightweight" OnClick="ShowAllYears"
|
||
Disabled="@(fromYear == analysis.Years[0] && toYear == analysis.Years[^1])">Alle Jahre</FluentButton>
|
||
|
||
<div class="segmented" role="group" aria-label="Zeitraster">
|
||
@foreach (var p in Periods)
|
||
{
|
||
<FluentButton Appearance="@(p == period ? Appearance.Accent : Appearance.Neutral)"
|
||
aria-pressed="@(p == period ? "true" : "false")"
|
||
OnClick="@(() => SetPeriod(p))">@PeriodName(p)</FluentButton>
|
||
}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="kpis">
|
||
<FluentCard class="kpi">
|
||
<div class="kpi-label">Total @RangeLabel</div>
|
||
<div class="kpi-value">@Chf(result.Total)</div>
|
||
<div class="kpi-sub ellipsis" title="@CurrentName">@CurrentName</div>
|
||
</FluentCard>
|
||
<FluentCard class="kpi">
|
||
<div class="kpi-label">Ø pro Monat</div>
|
||
<div class="kpi-value">@Chf(result.AveragePerMonth)</div>
|
||
<div class="kpi-sub">über @result.Months Monate</div>
|
||
</FluentCard>
|
||
<FluentCard class="kpi">
|
||
@if (result.Comparison is { } comparison)
|
||
{
|
||
<div class="kpi-label">@ComparisonLabel(comparison)</div>
|
||
<div class="kpi-value @ChangeClass(comparison)">@ChangeLabel(comparison)</div>
|
||
<div class="kpi-sub">@Chf(comparison.Current) statt @Chf(comparison.Previous)</div>
|
||
}
|
||
else
|
||
{
|
||
<div class="kpi-label">Vergleich zum Vorjahr</div>
|
||
<div class="kpi-value">–</div>
|
||
<div class="kpi-sub">keine Daten für @(toYear - 1)</div>
|
||
}
|
||
</FluentCard>
|
||
<FluentCard class="kpi">
|
||
<div class="kpi-label">Grösster Posten</div>
|
||
@if (result.Categories.Count > 0)
|
||
{
|
||
var top = result.Categories[0];
|
||
<div class="kpi-value kpi-name" title="@top.Name">@top.Name</div>
|
||
<div class="kpi-sub">@Chf(top.Total), @Percent(top.Share)</div>
|
||
}
|
||
else
|
||
{
|
||
<div class="kpi-value">–</div>
|
||
<div class="kpi-sub">keine Ausgaben im Zeitraum</div>
|
||
}
|
||
</FluentCard>
|
||
</div>
|
||
|
||
<div class="main">
|
||
<FluentCard class="chart-card">
|
||
<div class="card-title">@CurrentName im Verlauf</div>
|
||
@if (result.Categories.Count > 0)
|
||
{
|
||
<StackedBarChart Labels="@chartLabels" Series="@chartSeries" OnSeriesClick="@OnSeriesClick"
|
||
AriaLabel="@($"{CurrentName} {RangeLabel} nach {ChildLevelName}, gestapelte Balken")"/>
|
||
@if (node.Level != AnalyticsLevel.Text)
|
||
{
|
||
<div class="hint">Klick auf einen Balken oder einen Eintrag rechts zeigt die Details.</div>
|
||
}
|
||
}
|
||
else
|
||
{
|
||
<div class="empty">Keine Ausgaben im gewählten Zeitraum.</div>
|
||
}
|
||
</FluentCard>
|
||
|
||
<FluentCard class="list-card">
|
||
<div class="card-title">@ChildLevelName</div>
|
||
<table class="categories">
|
||
<tbody>
|
||
@for (var i = 0; i < result.Categories.Count; i++)
|
||
{
|
||
var category = result.Categories[i];
|
||
<tr @key="category.Node">
|
||
<td class="swatch-cell"><span class="swatch" style="background: @ColorFor(i)"></span></td>
|
||
<td class="name">
|
||
@if (category.CanDrillDown)
|
||
{
|
||
<a href="@NodeUrl(category.Node)">@category.Name</a>
|
||
}
|
||
else
|
||
{
|
||
@category.Name
|
||
}
|
||
<div class="share-bar"><div style="width: @ShareWidth(category.Share); background: @ColorFor(i)"></div></div>
|
||
</td>
|
||
<td class="amount">
|
||
@Chf(category.Total)
|
||
<div class="share">@Percent(category.Share)</div>
|
||
</td>
|
||
</tr>
|
||
}
|
||
</tbody>
|
||
</table>
|
||
</FluentCard>
|
||
</div>
|
||
</div>
|