New page /analytics shows the current mandator's expenses across all years, with drill-down all -> subclass -> account -> booking-text group, year range and month/quarter/year filters, KPI tiles, a stacked bar chart and a category list. Filters and drill-down position live in the URL. - MultiYearRepository reads any mandator/year (read-only, shared cache) - ExpenseAnalysis flattens all years and answers queries in memory: net amounts (refunds count negative), accounts keyed by number with the latest name, bookings counted in the year of their books, year-over-year comparison over the same months - Chart.js 4.5.1 vendored in wwwroot/lib/chartjs, wrapped by wwwroot/js/charts.js and StackedBarChart.razor Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
94 lines
2.9 KiB
C#
94 lines
2.9 KiB
C#
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.JSInterop;
|
|
|
|
namespace Schaad.Accounting.UI.Components.Pages.Analytics;
|
|
|
|
public record ChartSeries(string Name, string Color, IReadOnlyList<decimal> Values, bool Clickable);
|
|
|
|
public partial class StackedBarChart : ComponentBase, IAsyncDisposable
|
|
{
|
|
private ElementReference canvas;
|
|
private IJSObjectReference? module;
|
|
private IJSObjectReference? chart;
|
|
private Task? creating;
|
|
private DotNetObjectReference<StackedBarChart>? selfReference;
|
|
private IReadOnlyList<string>? renderedLabels;
|
|
private IReadOnlyList<ChartSeries>? renderedSeries;
|
|
|
|
[Inject]
|
|
private IJSRuntime js { get; set; } = null!;
|
|
|
|
[Parameter, EditorRequired]
|
|
public IReadOnlyList<string> Labels { get; set; } = [];
|
|
|
|
[Parameter, EditorRequired]
|
|
public IReadOnlyList<ChartSeries> Series { get; set; } = [];
|
|
|
|
/// <summary>Called with the index into <see cref="Series"/> when a clickable segment is clicked.</summary>
|
|
[Parameter]
|
|
public EventCallback<int> OnSeriesClick { get; set; }
|
|
|
|
[Parameter]
|
|
public string Height { get; set; } = "420px";
|
|
|
|
[Parameter]
|
|
public string AriaLabel { get; set; } = "";
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
{
|
|
if (ReferenceEquals(Labels, renderedLabels) && ReferenceEquals(Series, renderedSeries))
|
|
{
|
|
return;
|
|
}
|
|
renderedLabels = Labels;
|
|
renderedSeries = Series;
|
|
|
|
var data = new
|
|
{
|
|
labels = Labels,
|
|
series = Series.Select(s => new { name = s.Name, color = s.Color, values = s.Values, clickable = s.Clickable })
|
|
};
|
|
|
|
if (creating is null)
|
|
{
|
|
creating = CreateAsync(data);
|
|
await creating;
|
|
return;
|
|
}
|
|
|
|
// New data may arrive while the first (async) creation is still running.
|
|
await creating;
|
|
await chart!.InvokeVoidAsync("update", data);
|
|
}
|
|
|
|
private async Task CreateAsync(object data)
|
|
{
|
|
module = await js.InvokeAsync<IJSObjectReference>("import", "./js/charts.js");
|
|
selfReference = DotNetObjectReference.Create(this);
|
|
chart = await module.InvokeAsync<IJSObjectReference>("createStackedBar", canvas, data, selfReference);
|
|
}
|
|
|
|
[JSInvokable]
|
|
public Task OnSeriesClicked(int seriesIndex) => OnSeriesClick.InvokeAsync(seriesIndex);
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
try
|
|
{
|
|
if (chart is not null)
|
|
{
|
|
await chart.InvokeVoidAsync("destroy");
|
|
await chart.DisposeAsync();
|
|
}
|
|
if (module is not null)
|
|
{
|
|
await module.DisposeAsync();
|
|
}
|
|
}
|
|
catch (JSDisconnectedException)
|
|
{
|
|
// Circuit already gone; the browser side is gone with it.
|
|
}
|
|
selfReference?.Dispose();
|
|
}
|
|
}
|