import { PlotlyFigure, Tema } from './indicadores.models'; import { C2_PALETTE, c2Theme } from './indicadores-c2-palette'; import { CeldaMapa, ComparacionAnalisis, DistribucionAnalisis, MapaAnalisis, TendenciaAnalisis } from './indicadores-analisis.service'; // The figures of the descriptive analysis. They draw what the answer publishes and nothing else: a suppressed or missing // value is left out of the drawing (a gap, never a zero) and named in the table beside it. const miles = (v: number) => v.toLocaleString('es-MX'); const pct = (v: number) => v.toLocaleString('es-MX', { minimumFractionDigits: 1, maximumFractionDigits: 1 }) + ' %'; const base = (tema: Tema, alto: number, margen: Record) => { const t = c2Theme(tema); return { autosize: true, height: alto, paper_bgcolor: t.background, plot_bgcolor: t.background, font: { color: t.text }, margin: margen, showlegend: false }; }; const config = { displayModeBar: false }; /** Share of the selection by category, with the Wilson interval of each published share as a horizontal bar. */ export function distribucionFigura(d: DistribucionAnalisis, tema: Tema): PlotlyFigure { const t = c2Theme(tema); const items = d.items.filter(i => !i.suprimido && (i.n ?? 0) > 0 && i.porcentaje !== null); return { data: [{ type: 'bar', orientation: 'h', y: items.map(i => i.etiqueta), x: items.map(i => i.porcentaje), customdata: items.map(i => i.codigo), marker: { color: C2_PALETTE.series[0] }, error_x: { type: 'data', symmetric: false, array: items.map(i => i.ic_alto! - i.porcentaje!), arrayminus: items.map(i => i.porcentaje! - i.ic_bajo!), color: t.text }, text: items.map(i => `${miles(i.n!)} (${pct(i.porcentaje!)})`), textposition: 'inside', insidetextanchor: 'start', textfont: { color: '#081839' }, cliponaxis: false, hovertemplate: items.map(i => `${i.etiqueta}
${miles(i.n!)} pacientes
${pct(i.porcentaje!)} (IC 95 %: ${pct(i.ic_bajo!)} a ${pct(i.ic_alto!)})`), }], layout: { ...base(tema, Math.max(220, 60 + 44 * items.length), { t: 24, l: 150, r: 90, b: 52 }), xaxis: { title: { text: 'Porcentaje de la selección' }, range: [0, 100], ticksuffix: ' %', gridcolor: t.grid, fixedrange: true }, yaxis: { autorange: 'reversed', automargin: true, fixedrange: true } }, config, }; } /** The share of cases per group as a point with its interval: groups that aren't published have no point. */ export function comparacionFigura(c: ComparacionAnalisis, tema: Tema, medida: string): PlotlyFigure { const t = c2Theme(tema); const grupos = c.grupos.filter(g => g.estado === 'disponible'); return { data: [{ type: 'scatter', mode: 'markers', y: grupos.map(g => g.etiqueta), x: grupos.map(g => g.valor), customdata: grupos.map(g => g.codigo), marker: { color: C2_PALETTE.series[1], size: 11 }, error_x: { type: 'data', symmetric: false, array: grupos.map(g => g.ic_alto! - g.valor!), arrayminus: grupos.map(g => g.valor! - g.ic_bajo!), color: t.text, thickness: 2, width: 6 }, hovertemplate: grupos.map(g => `${g.etiqueta}
${g.casos} de ${g.total} pacientes con resultado
${pct(g.valor!)} (IC 95 %: ${pct(g.ic_bajo!)} a ${pct(g.ic_alto!)})`), }], layout: { ...base(tema, Math.max(220, 80 + 46 * grupos.length), { t: 24, l: 150, r: 28, b: 56 }), xaxis: { title: { text: `${medida} entre pacientes con resultado de biopsia` }, range: [0, 100], ticksuffix: ' %', gridcolor: t.grid, fixedrange: true }, yaxis: { autorange: 'reversed', type: 'category', automargin: true, fixedrange: true } }, config, }; } const textoCelda = (c: CeldaMapa, medida: string) => c.suprimido ? 'No publicado' : medida === 'pacientes' ? `${miles(c.n!)} pacientes` : c.resultado.estado === 'disponible' ? `${pct(c.resultado.valor!)}
${c.resultado.casos} de ${c.resultado.total}` : c.resultado.estado === 'suprimido' ? 'No publicado' : 'Sin pacientes con resultado'; /** BI-RADS by age group: the number of patients or the share of cases in each cell; hidden cells are blank. */ export function mapaFigura(m: MapaAnalisis, tema: Tema, medida: 'pacientes' | 'resultado'): PlotlyFigure { const t = c2Theme(tema); const celdas = new Map(m.celdas.map(c => [c.fila + '\u0000' + c.columna, c])); const valor = (c: CeldaMapa | undefined) => !c || c.suprimido ? null : medida === 'pacientes' ? c.n : c.resultado.estado === 'disponible' ? c.resultado.valor : null; const z = m.filas.categorias.map(f => m.columnas.categorias.map(c => valor(celdas.get(f.codigo + '\u0000' + c.codigo)))); return { data: [{ type: 'heatmap', x: m.columnas.categorias.map(c => c.etiqueta), y: m.filas.categorias.map(f => f.etiqueta), z, customdata: m.filas.categorias.map(f => m.columnas.categorias.map(c => [f.codigo, c.codigo])), text: m.filas.categorias.map(f => m.columnas.categorias.map(c => textoCelda(celdas.get(f.codigo + '\u0000' + c.codigo)!, medida === 'pacientes' ? 'pacientes' : 'resultado'))), hovertemplate: '%{y}, %{x}
%{text}', hoverongaps: false, xgap: 2, ygap: 2, colorscale: [[0, '#E4EEC0'], [1, '#6A8000']], zmin: 0, zmax: medida === 'resultado' ? 100 : undefined, colorbar: { title: { text: medida === 'resultado' ? '% de casos' : 'Pacientes' } }, }], layout: { ...base(tema, Math.max(280, 90 + 40 * m.filas.categorias.length), { t: 24, l: 130, r: 28, b: 60 }), xaxis: { automargin: true, fixedrange: true, type: 'category' }, yaxis: { autorange: 'reversed', automargin: true, fixedrange: true, type: 'category' }, plot_bgcolor: t.grid }, config, }; } export interface FigurasTendencia { actividad: PlotlyFigure; proporcion: PlotlyFigure; } /** Reports per period (bars; a hidden total is a gap) and the share of malignant results with its interval. */ export function tendenciaFiguras(r: TendenciaAnalisis, tema: Tema, medida: string): FigurasTendencia { const t = c2Theme(tema); const x = r.periodos.map(p => p.periodo + (p.incompleto ? ' (en curso)' : '')); const disponibles = r.periodos.map((p, i) => ({ p, x: x[i] })).filter(({ p }) => p.maligno.estado === 'disponible'); return { actividad: { data: [ { type: 'bar', name: 'Reportes de mastografía', x, y: r.periodos.map(p => p.reportes_mastografia.total), marker: { color: C2_PALETTE.series[2] }, hovertemplate: '%{x}
%{y} reportes de mastografía' }, { type: 'bar', name: 'Reportes de biopsia', x, y: r.periodos.map(p => p.reportes_biopsia.total), marker: { color: C2_PALETTE.series[3] }, hovertemplate: '%{x}
%{y} reportes de biopsia' }, ], layout: { ...base(tema, 340, { t: 24, l: 56, r: 16, b: 80 }), showlegend: true, legend: { orientation: 'h', y: -0.28 }, barmode: 'group', xaxis: { type: 'category', automargin: true, fixedrange: true }, yaxis: { title: { text: 'Reportes' }, gridcolor: t.grid, rangemode: 'tozero', fixedrange: true } }, config, }, proporcion: { data: [{ type: 'scatter', mode: 'lines+markers', x: disponibles.map(d => d.x), y: disponibles.map(d => d.p.maligno.valor), connectgaps: false, line: { color: C2_PALETTE.series[1] }, marker: { color: C2_PALETTE.series[1], size: 9 }, error_y: { type: 'data', symmetric: false, array: disponibles.map(d => d.p.maligno.ic_alto! - d.p.maligno.valor!), arrayminus: disponibles.map(d => d.p.maligno.valor! - d.p.maligno.ic_bajo!), color: t.text }, hovertemplate: disponibles.map(d => `${d.x}
${d.p.maligno.casos} de ${d.p.maligno.total} reportes de biopsia con resultado
${pct(d.p.maligno.valor!)} (IC 95 %: ${pct(d.p.maligno.ic_bajo!)} a ${pct(d.p.maligno.ic_alto!)})`), }], layout: { ...base(tema, 340, { t: 24, l: 64, r: 16, b: 64 }), xaxis: { type: 'category', categoryorder: 'array', categoryarray: x, automargin: true, fixedrange: true }, yaxis: { title: { text: medida }, range: [0, 100], ticksuffix: ' %', gridcolor: t.grid, fixedrange: true } }, config, }, }; }