import { Component, computed, input, signal } from '@angular/core'; import { MatCheckboxModule } from '@angular/material/checkbox'; import { Figure, Plot } from '../plot'; export interface Choice { id: string; name: string; /** drawn before the user touches it */ on: boolean; } /** A figure's curves as choices, all on: a band has no name of its own and goes with the line * after it. */ export function figureChoices(figure: Figure): { choices: Choice[]; draw: Draw } { const groups = new Map(); let pending: Figure['data'] = []; for (const trace of figure.data) { pending.push(trace); const { name, showlegend } = trace as { name?: string; showlegend?: boolean }; if (name && showlegend !== false) { groups.set(name, pending); pending = []; } } return { choices: [...groups.keys()].map((name) => ({ id: name, name, on: true })), draw: (ids) => ({ ...figure, data: [...ids].flatMap((id) => groups.get(id) ?? []) }), }; } export type Draw = (ids: Set) => Figure; /** Curves chosen or not, to compare them on one plot: the record's risk and survival curves. */ @Component({ selector: 'app-curve-chooser', imports: [MatCheckboxModule, Plot], template: `
@for (c of choices(); track c.id) { {{ c.name }} }
@if (chosen().size) { @for (note of notes()(chosen()); track note) {

{{ note }}

} } `, styles: ` :host { display: block; } .choices { display: flex; flex-wrap: wrap; gap: 0 16px; margin-bottom: 8px; } `, }) export class CurveChooser { readonly choices = input.required(); readonly draw = input.required(); /** what the chosen curves assume, said under the choices */ readonly notes = input<(ids: Set) => string[]>(() => []); /** what the user toggled away from its default */ private readonly toggled = signal(new Set()); protected readonly chosen = computed( () => new Set( this.choices() .filter((c) => c.on !== this.toggled().has(c.id)) .map((c) => c.id), ), ); protected readonly figure = computed(() => this.draw()(this.chosen())); protected toggle(id: string) { const next = new Set(this.toggled()); if (!next.delete(id)) next.add(id); this.toggled.set(next); } }