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 until the user says otherwise */ on: boolean; } export type Draw = (ids: Set) => Figure; /** A figure's named curves as choices, all on; a trace with no legend of its own (a band) goes * with the named one 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) ?? []) }), }; } /** Curves to compare on one plot, each chosen or not, with what the chosen ones assume. */ @Component({ selector: 'app-curve-chooser', imports: [MatCheckboxModule, Plot], template: `
@for (c of choices(); track c.id) { {{ c.name }} }
@if (chosen().size) { @for (note of said(); 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(); readonly notes = input<(ids: Set) => string[]>(() => []); /** the choices the user turned from their default */ private readonly flipped = signal(new Set()); protected readonly chosen = computed( () => new Set( this.choices() .filter((c) => c.on !== this.flipped().has(c.id)) .map((c) => c.id), ), ); protected readonly figure = computed(() => this.draw()(this.chosen())); protected readonly said = computed(() => this.notes()(this.chosen())); protected toggle(id: string) { const next = new Set(this.flipped()); if (!next.delete(id)) next.add(id); this.flipped.set(next); } }