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)', // Above the plot, left-aligned: Plotly reserves its room, so it never meets an axis title. legend: { orientation: 'h', x: 0, xanchor: 'left', y: 1.02, yanchor: 'bottom' }, hoverlabel: { font: { family: 'Roboto, Arial, sans-serif', size: 13 } }, }; // Axes make room for their labels and titles; a title keeps clear of the tick labels. // (`ticklabelstandoff` is Plotly's, newer than its published types.) const AXIS = { automargin: true, title: { standoff: 12 }, ticklabelstandoff: 4, // the lowest y label and the first x label never touch } as Partial; /** The platform's layout under a figure's own: legend, margins and axes merged key by key, * so a figure that sets one of their keys keeps the others. */ export function layoutOf(layout: Partial = {}): Partial { const axes = Object.fromEntries( Object.entries(layout) .filter(([k]) => /^[xy]axis\d*$/.test(k)) .map(([k, v]) => { const axis = (v ?? {}) as Partial; const title = typeof axis.title === 'string' ? { text: axis.title } : axis.title; return [k, { ...AXIS, ...axis, title: { ...AXIS.title, ...title } }]; }), ); return { ...LAYOUT, xaxis: AXIS, yaxis: AXIS, ...layout, ...axes, legend: { ...LAYOUT.legend, ...layout.legend }, margin: { ...LAYOUT.margin, ...layout.margin }, }; } /** Pie labels that sit outside take the room they need instead of being clipped. */ function withRoom(data: Partial[]): Plotly.Data[] { return data.map((d) => (d.type === 'pie' ? { automargin: true, ...d } : d)) as Plotly.Data[]; } 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); /** Plotly's buttons over the chart; a record's curves go without them */ readonly toolbar = input(true); private readonly host = viewChild.required>('host'); constructor() { effect(() => { const { data, layout } = this.figure(); void Plotly.react(this.host().nativeElement, withRoom(data), layoutOf(layout), { ...CONFIG, ...(this.toolbar() ? {} : { displayModeBar: false }), }); }); } ngOnDestroy(): void { Plotly.purge(this.host().nativeElement); } }