AccountingNext/Schaad.Accounting.UI/Components/Pages/Analytics/ChartJsChart.razor.cs
Claudio Schaad afa07f8156 Analyse page: composition, year comparison and year table; fix year filter loop
Step 3 of the analytics plan:
- Zusammensetzung: treemap (chartjs-chart-treemap 4.2.2, vendored) with the
  queried node's children and, nested, their children; clicking a tile drills
  down to that level
- Jahresvergleich: one line per year over Jan-Dec, monthly or cumulative
- Jahre im Vergleich: category x year table, cells shaded by monthly average
  (so the running year compares fairly), change vs. the same months of the
  previous year, clickable rows; replaces both heatmap and table from the plan
- ExpenseAnalysis returns year profiles, the category x year table and the
  two-level composition; StackedBarChart becomes a generic ChartJsChart
- Click hints only show when something can be drilled into; the treemap
  cursor only changes over clickable tiles; zero cells show "-"

Fix: choosing a year in Von/Bis started an endless navigation loop (FluentSelect
re-reports its value on every re-render, before the new URL has been applied),
so the page stopped updating. The handlers now take over the year immediately.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 16:20:24 +02:00

86 lines
2.6 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace Schaad.Accounting.UI.Components.Pages.Analytics;
public partial class ChartJsChart : ComponentBase, IAsyncDisposable
{
private ElementReference canvas;
private IJSObjectReference? module;
private IJSObjectReference? chart;
private Task? creating;
private DotNetObjectReference<ChartJsChart>? selfReference;
private object? renderedData;
[Inject]
private IJSRuntime js { get; set; } = null!;
/// <summary>"stackedBar", "line" or "treemap"; see charts.js for the data each one takes.</summary>
[Parameter, EditorRequired]
public string Kind { get; set; } = "";
/// <summary>Serialized to JSON for charts.js. Pass a new object to update the chart.</summary>
[Parameter, EditorRequired]
public object Data { get; set; } = null!;
/// <summary>Called with the clicked element's key (see charts.js) when a clickable element is clicked.</summary>
[Parameter]
public EventCallback<string> OnClick { get; set; }
[Parameter]
public string Height { get; set; } = "420px";
[Parameter]
public string AriaLabel { get; set; } = "";
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (ReferenceEquals(Data, renderedData))
{
return;
}
var data = renderedData = Data;
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>("createChart", canvas, Kind, data, selfReference);
}
[JSInvokable]
public Task OnChartClicked(string key) => OnClick.InvokeAsync(key);
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();
}
}