87 lines
2.6 KiB
TypeScript
87 lines
2.6 KiB
TypeScript
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<string, Figure['data']>();
|
|
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<string>) => 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: `
|
|
<div class="choices">
|
|
@for (c of choices(); track c.id) {
|
|
<mat-checkbox [checked]="chosen().has(c.id)" (change)="toggle(c.id)">
|
|
{{ c.name }}
|
|
</mat-checkbox>
|
|
}
|
|
</div>
|
|
@if (chosen().size) {
|
|
@for (note of notes()(chosen()); track note) {
|
|
<p class="muted">{{ note }}</p>
|
|
}
|
|
<app-plot [figure]="figure()" [height]="340" [toolbar]="false" />
|
|
}
|
|
`,
|
|
styles: `
|
|
:host {
|
|
display: block;
|
|
}
|
|
.choices {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
gap: 0 16px;
|
|
margin-bottom: 8px;
|
|
}
|
|
`,
|
|
})
|
|
export class CurveChooser {
|
|
readonly choices = input.required<Choice[]>();
|
|
readonly draw = input.required<Draw>();
|
|
/** what the chosen curves assume, said under the choices */
|
|
readonly notes = input<(ids: Set<string>) => string[]>(() => []);
|
|
/** what the user toggled away from its default */
|
|
private readonly toggled = signal(new Set<string>());
|
|
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);
|
|
}
|
|
}
|