20261118 Pastilla Fix

This commit is contained in:
Manuel Aurrecoechea Castillo
2026-11-18 16:53:56 -06:00
parent a217ddae38
commit fefa28ae7a
7 changed files with 123 additions and 40 deletions
@@ -280,6 +280,10 @@ describe('PlotlyController transiciones opcionales', () => {
expect(dimmed.data[1]['values']).toEqual([4, 6]); expect(dimmed.data[1]['text']).toEqual(['4', '6']);
expect((dimmed.data[1]['marker'] as { colors: string[] }).colors).toEqual(['rgba(10,20,30,0.65)', 'rgba(10,20,30,0.65)']);
expect(pieFrame(pie([4, 6]), pie([4, 6]), .5).data[1]).toEqual(pie([4, 6]).data[1]);
const grown = pieFrame({ ...pie([4, 6]), data: pie([4, 6]).data.map(t => ({ ...t, pull: [0, 0] })) },
{ ...pie([4, 6]), data: pie([4, 6]).data.map(t => ({ ...t, pull: [0, 0.12] })) }, .5);
expect(grown.data[1]['pull']).toEqual([0, 0.06]);
expect(grown.data[1]['text']).toEqual(['4', '6']);
});
it('la dona interpola en varios react sin ocultar el host ni pasar por carga, y termina en la figura exacta', async () => {
const c = controller();
+25 -2
View File
@@ -27,6 +27,25 @@ function mixColor(from: unknown, to: unknown, t: number): unknown {
return `rgba(${v.slice(0, 3).map(Math.round).join(',')},${+v[3].toFixed(3)})`;
}
const numbers = (v: unknown): v is number[] => Array.isArray(v) && v.every(n => typeof n === 'number' && Number.isFinite(n));
function mixDomain(from: unknown, to: unknown, t: number): { x: number[]; y: number[] } | undefined {
const read = (value: unknown) => {
if (!value || typeof value !== 'object') return undefined;
const x = (value as { x?: unknown }).x, y = (value as { y?: unknown }).y;
if (!numbers(x) || !numbers(y) || x.length !== 2 || y.length !== 2) return undefined;
return { x, y };
};
const start = read(from), end = read(to);
if (!start || !end) return undefined;
if (start.x.every((v, i) => v === end.x[i]) && start.y.every((v, i) => v === end.y[i])) return undefined;
const mix = (a: number[], b: number[]) => b.map((v, i) => a[i] + (v - a[i]) * t);
return { x: mix(start.x, end.x), y: mix(start.y, end.y) };
}
function mixPull(from: unknown, to: unknown, length: number, t: number): number[] | undefined {
const start = from === undefined ? Array<number>(length).fill(0) : from;
const end = to === undefined ? Array<number>(length).fill(0) : to;
if (!numbers(start) || !numbers(end) || start.length !== length || end.length !== length || start.every((v, i) => v === end[i])) return undefined;
return end.map((v, i) => start[i] + (v - start[i]) * t);
}
/** Pies cannot use layout.transition: both figures must have the same pies and value shapes. */
export function piesInterpolable(from: PlotlyFigure, to: PlotlyFigure): boolean {
return to.data.length > 0 && from.data.length === to.data.length && to.data.every((trace, i) => {
@@ -46,12 +65,16 @@ export function pieFrame(from: PlotlyFigure, to: PlotlyFigure, t: number): Plotl
const marker = trace['marker'] as Record<string, unknown> | undefined;
const oldColors = oldMarker?.['colors'], colors = marker?.['colors'];
const sameValues = a.every((v, j) => v === b[j]);
const clear = (list: unknown) => Array.isArray(list) && list.some(c => typeof c === 'string' && /,\s*0\)$/.test(c));
const mixColors = !!marker && Array.isArray(colors) && Array.isArray(oldColors) && oldColors.length === colors.length
&& colors.some((c, j) => c !== oldColors[j]);
if (sameValues && !mixColors && !moving) return trace;
&& !clear(colors) && !clear(oldColors) && colors.some((c, j) => c !== oldColors[j]);
const pull = mixPull(from.data[i]['pull'], trace['pull'], b.length, t);
const domain = mixDomain(from.data[i]['domain'], trace['domain'], t);
if (sameValues && !mixColors && !moving && !pull && !domain) return trace;
// Geometry is illustrative until the exact target render. Hide all clinical
// readouts (including the centre) and never attach counts to moving slices.
return { ...trace, ...(sameValues ? {} : { values: b.map((v, j) => a[j] + (v - a[j]) * t) }),
...(pull ? { pull } : {}), ...(domain ? { domain } : {}),
...(moving ? { text: b.map(() => ''), textinfo: 'none', texttemplate: '',
hoverinfo: 'skip', hovertemplate: null, hovertext: [], customdata: [] } : {}),
...(mixColors ? { marker: { ...marker, colors: (colors as unknown[]).map((c, j) => mixColor((oldColors as unknown[])[j], c, t)) } } : {}) };