OpenDataVisualizer/client/src/watercraft/categories.ts
Claudio Schaad 39a62255af Add Thurgau watercraft map (#2)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claudio Schaad <c.schaad@pog.ch>
Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
2026-11-06 21:10:01 +00:00

61 lines
1.7 KiB
TypeScript

import type { Dimension, LocationSummary } from '../api/watercraft';
export interface CategoryGroup {
label: string;
color: string;
indices: number[];
}
const SERIES_COLORS = ['#2a78d6', '#eb6834', '#1baf7a', '#eda100', '#e87ba4', '#008300'];
const OTHER_COLOR = '#8a8984';
const ABSENT_COLOR = '#c9c8c2';
const ABSENT_CATEGORIES = new Set([
'Ohne Motor',
'Keine Angabe',
'Unbekannt',
'Ohne besondere Verwendung',
]);
const OTHER_LABEL = 'Übrige';
export function buildGroups(dimension: Dimension): CategoryGroup[] {
const named: CategoryGroup[] = [];
const absent: CategoryGroup[] = [];
const other: number[] = [];
dimension.categories.forEach((category, index) => {
if (ABSENT_CATEGORIES.has(category)) {
absent.push({ label: category, color: ABSENT_COLOR, indices: [index] });
} else if (named.length < SERIES_COLORS.length) {
named.push({ label: category, color: SERIES_COLORS[named.length], indices: [index] });
} else {
other.push(index);
}
});
if (other.length === 1 && named.length > 0) {
const last = named.pop()!;
other.unshift(...last.indices);
}
return [
...named,
...(other.length > 0 ? [{ label: OTHER_LABEL, color: OTHER_COLOR, indices: other }] : []),
...absent,
];
}
export function groupCounts(counts: number[] | undefined, groups: CategoryGroup[]): number[] {
return groups.map((group) => group.indices.reduce((sum, i) => sum + (counts?.[i] ?? 0), 0));
}
export function visibleCount(
location: LocationSummary,
dimension: Dimension,
groups: CategoryGroup[],
hidden: Set<string>,
): number {
return groupCounts(location.categories[dimension.key], groups).reduce(
(sum, count, i) => (hidden.has(groups[i].label) ? sum : sum + count),
0,
);
}