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>
124 lines
5 KiB
JavaScript
124 lines
5 KiB
JavaScript
// Thin Chart.js wrapper for Blazor (see StackedBarChart.razor). Chart.js is loaded on first use
|
||
// from wwwroot/lib/chartjs, so pages without charts don't pay for it.
|
||
|
||
let chartJsLoading;
|
||
|
||
function loadChartJs() {
|
||
if (window.Chart) {
|
||
return Promise.resolve();
|
||
}
|
||
chartJsLoading ??= new Promise((resolve, reject) => {
|
||
const script = document.createElement('script');
|
||
script.src = 'lib/chartjs/chart.umd.min.js';
|
||
script.onload = resolve;
|
||
script.onerror = () => reject(new Error('Chart.js could not be loaded'));
|
||
document.head.appendChild(script);
|
||
});
|
||
return chartJsLoading;
|
||
}
|
||
|
||
// Same look as ChartTheme.cs.
|
||
const theme = {
|
||
font: "'Helvetica Neue', Helvetica, Arial, sans-serif",
|
||
ink: '#0b0b0b',
|
||
muted: '#52514e',
|
||
grid: '#e1e0d9',
|
||
surface: '#fcfcfb'
|
||
};
|
||
|
||
// 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, "'");
|
||
|
||
function toDatasets(series) {
|
||
return series.map(s => ({
|
||
label: s.name,
|
||
data: s.values,
|
||
backgroundColor: s.color,
|
||
hoverBackgroundColor: s.color,
|
||
borderColor: theme.surface,
|
||
borderWidth: { top: 1 },
|
||
borderRadius: 3,
|
||
stack: 'total',
|
||
clickable: s.clickable
|
||
}));
|
||
}
|
||
|
||
/**
|
||
* Creates a stacked bar chart on the canvas and returns a handle with update/destroy.
|
||
* data: { labels: string[], series: { name, color, values: number[], clickable }[] }
|
||
* Clicking a bar segment of a clickable series calls dotNetRef.OnSeriesClicked(seriesIndex).
|
||
*/
|
||
export async function createStackedBar(canvas, data, dotNetRef) {
|
||
await loadChartJs();
|
||
window.Chart.defaults.font.family = theme.font;
|
||
|
||
const chart = new window.Chart(canvas, {
|
||
type: 'bar',
|
||
data: { labels: data.labels, datasets: toDatasets(data.series) },
|
||
options: {
|
||
responsive: true,
|
||
maintainAspectRatio: false,
|
||
animation: { duration: 250 },
|
||
interaction: { mode: 'index', intersect: false },
|
||
plugins: {
|
||
legend: {
|
||
position: 'bottom',
|
||
labels: { usePointStyle: true, pointStyle: 'rectRounded', boxWidth: 10, color: theme.muted, font: { family: theme.font, size: 12 } }
|
||
},
|
||
tooltip: {
|
||
backgroundColor: '#ffffff',
|
||
borderColor: theme.grid,
|
||
borderWidth: 1,
|
||
titleColor: theme.ink,
|
||
bodyColor: theme.ink,
|
||
footerColor: theme.ink,
|
||
titleFont: { family: theme.font, weight: 'bold' },
|
||
bodyFont: { family: theme.font },
|
||
footerFont: { family: theme.font, weight: 'bold' },
|
||
padding: 10,
|
||
usePointStyle: true,
|
||
filter: item => item.parsed.y !== 0,
|
||
itemSort: (a, b) => b.parsed.y - a.parsed.y,
|
||
callbacks: {
|
||
label: item => ` ${item.dataset.label}: ${formatChf(item.parsed.y)} CHF`,
|
||
footer: items => `Total: ${formatChf(items.reduce((sum, i) => sum + i.parsed.y, 0))} CHF`
|
||
}
|
||
}
|
||
},
|
||
scales: {
|
||
x: { stacked: true, grid: { display: false }, ticks: { color: theme.muted, font: { family: theme.font } } },
|
||
y: {
|
||
stacked: true,
|
||
grid: { color: theme.grid },
|
||
border: { display: false },
|
||
title: { display: true, text: 'CHF', color: theme.muted, font: { family: theme.font } },
|
||
ticks: { color: theme.muted, font: { family: theme.font }, callback: value => formatAxis(value) }
|
||
}
|
||
},
|
||
onHover: (event, _, chart) => {
|
||
const hit = chart.getElementsAtEventForMode(event, 'nearest', { intersect: true }, false)[0];
|
||
event.native.target.style.cursor = hit && chart.data.datasets[hit.datasetIndex].clickable ? 'pointer' : 'default';
|
||
},
|
||
onClick: (event, _, chart) => {
|
||
const hit = chart.getElementsAtEventForMode(event, 'nearest', { intersect: true }, false)[0];
|
||
if (hit && chart.data.datasets[hit.datasetIndex].clickable) {
|
||
dotNetRef.invokeMethodAsync('OnSeriesClicked', hit.datasetIndex);
|
||
}
|
||
}
|
||
}
|
||
});
|
||
|
||
return {
|
||
update(newData) {
|
||
chart.data.labels = newData.labels;
|
||
chart.data.datasets = toDatasets(newData.series);
|
||
chart.update();
|
||
},
|
||
destroy() {
|
||
chart.destroy();
|
||
}
|
||
};
|
||
}
|