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[]; layout?: Partial }; /** The series colours today's charts use, in order. */ export const PALETTE = [ '#A8C243', '#42C29C', '#C24269', '#9C42C2', '#4269C2', '#C29C42', '#7A7A7A', ]; const LAYOUT: Partial = { 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 = { 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: '
', styles: ':host { display: block; } .plot { width: 100%; }', }) export class Plot implements OnDestroy { readonly figure = input.required
(); readonly height = input(320); private readonly host = viewChild.required>('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); } }