Files
WebApp/src/app/plot.ts
T

59 lines
1.7 KiB
TypeScript

import { Component, effect, ElementRef, input, OnDestroy, viewChild } from '@angular/core';
// The cartesian bundle: bars, lines, box and pie, as today's charts use.
import Plotly from 'plotly.js-cartesian-dist-min';
export type Figure = { data: Partial<Plotly.Data>[]; layout?: Partial<Plotly.Layout> };
/** The series colours today's charts use, in order. */
export const PALETTE = [
'#A8C243',
'#42C29C',
'#C24269',
'#9C42C2',
'#4269C2',
'#C29C42',
'#7A7A7A',
];
const LAYOUT: Partial<Plotly.Layout> = {
font: { family: 'Roboto, Arial, sans-serif', size: 14, color: '#17324d' },
colorway: PALETTE,
margin: { t: 24, r: 16, b: 48, l: 56 },
paper_bgcolor: 'rgba(0,0,0,0)',
plot_bgcolor: 'rgba(0,0,0,0)',
legend: { orientation: 'h', y: -0.2 },
};
const CONFIG: Partial<Plotly.Config> = {
responsive: true,
displaylogo: false,
modeBarButtonsToRemove: ['sendDataToCloud', 'lasso2d', 'select2d'],
};
/** One Plotly chart: the figure, drawn with the platform's layout and config. */
@Component({
selector: 'app-plot',
template: '<div #host class="plot" [style.height.px]="height()"></div>',
styles: ':host { display: block; } .plot { width: 100%; }',
})
export class Plot implements OnDestroy {
readonly figure = input.required<Figure>();
readonly height = input(320);
private readonly host = viewChild.required<ElementRef<HTMLDivElement>>('host');
constructor() {
effect(() => {
const { data, layout } = this.figure();
void Plotly.react(
this.host().nativeElement,
data as Plotly.Data[],
{ ...LAYOUT, ...layout },
CONFIG,
);
});
}
ngOnDestroy(): void {
Plotly.purge(this.host().nativeElement);
}
}