Plot axes are built fresh for every chart
The module-level axis defaults were shared by every chart, and Plotly writes its detected axis type, range and autorange back into the layout it is given, so one chart's category axis and range leaked into the others (count axes typed as categories, percent ranges ignored). Each layout now gets its own axis objects.
This commit is contained in:
@@ -5,7 +5,12 @@ import type { Catalog, Category, HistoryPanel } from './pages';
|
|||||||
const OLIVE = '#A8C243';
|
const OLIVE = '#A8C243';
|
||||||
const ROSE = '#C24269';
|
const ROSE = '#C24269';
|
||||||
export const SHORT = '.1~f';
|
export const SHORT = '.1~f';
|
||||||
export const percentAxis = { ticksuffix: ' %', tickformat: '.0f', hoverformat: SHORT };
|
export const percentAxis = {
|
||||||
|
type: 'linear' as const,
|
||||||
|
ticksuffix: ' %',
|
||||||
|
tickformat: '.0f',
|
||||||
|
hoverformat: SHORT,
|
||||||
|
};
|
||||||
|
|
||||||
const count = (n: number) => n.toLocaleString('es-MX');
|
const count = (n: number) => n.toLocaleString('es-MX');
|
||||||
const percent = (n: number) => (Math.round(n * 10) / 10).toLocaleString('es-MX');
|
const percent = (n: number) => (Math.round(n * 10) / 10).toLocaleString('es-MX');
|
||||||
|
|||||||
+6
-5
@@ -23,11 +23,11 @@ const LAYOUT: Partial<Plotly.Layout> = {
|
|||||||
legend: { orientation: 'h', x: 0, xanchor: 'left', y: 1.02, yanchor: 'bottom' },
|
legend: { orientation: 'h', x: 0, xanchor: 'left', y: 1.02, yanchor: 'bottom' },
|
||||||
hoverlabel: { font: { family: 'Roboto, Arial, sans-serif', size: 13 } },
|
hoverlabel: { font: { family: 'Roboto, Arial, sans-serif', size: 13 } },
|
||||||
};
|
};
|
||||||
const AXIS = {
|
const axisDefaults = (): Partial<Plotly.LayoutAxis> => ({
|
||||||
automargin: true,
|
automargin: true,
|
||||||
title: { standoff: 12 },
|
title: { standoff: 12 },
|
||||||
ticklabelstandoff: 4,
|
ticklabelstandoff: 4,
|
||||||
} as Partial<Plotly.LayoutAxis>;
|
});
|
||||||
|
|
||||||
function layoutOf(layout: Partial<Plotly.Layout> = {}): Partial<Plotly.Layout> {
|
function layoutOf(layout: Partial<Plotly.Layout> = {}): Partial<Plotly.Layout> {
|
||||||
const axes = Object.fromEntries(
|
const axes = Object.fromEntries(
|
||||||
@@ -36,13 +36,14 @@ function layoutOf(layout: Partial<Plotly.Layout> = {}): Partial<Plotly.Layout> {
|
|||||||
.map(([k, v]) => {
|
.map(([k, v]) => {
|
||||||
const axis = (v ?? {}) as Partial<Plotly.LayoutAxis>;
|
const axis = (v ?? {}) as Partial<Plotly.LayoutAxis>;
|
||||||
const title = typeof axis.title === 'string' ? { text: axis.title } : axis.title;
|
const title = typeof axis.title === 'string' ? { text: axis.title } : axis.title;
|
||||||
return [k, { ...AXIS, ...axis, title: { ...AXIS.title, ...title } }];
|
const base = axisDefaults();
|
||||||
|
return [k, { ...base, ...axis, title: { ...base.title, ...title } }];
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
...LAYOUT,
|
...LAYOUT,
|
||||||
xaxis: AXIS,
|
xaxis: axisDefaults(),
|
||||||
yaxis: AXIS,
|
yaxis: axisDefaults(),
|
||||||
...layout,
|
...layout,
|
||||||
...axes,
|
...axes,
|
||||||
legend: { ...LAYOUT.legend, ...layout.legend },
|
legend: { ...LAYOUT.legend, ...layout.legend },
|
||||||
|
|||||||
Reference in New Issue
Block a user