Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored single subplot log scaling #633

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 16 additions & 36 deletions src/vis/scatter/useLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ export function useLayout({
axes[`xaxis${plotCounter > 0 ? plotCounter + 1 : ''}`] = {
...AXIS_TICK_STYLES,
range: toLogRange(config.xAxisScale!, pair.xDomain),
type: config.xAxisScale,
// Spread the previous layout to keep things like zoom
...(internalLayoutRef.current?.[`xaxis${plotCounter > 0 ? plotCounter + 1 : ''}` as 'xaxis'] || {}),
type: config.xAxisScale,
title: {
text: textMeasure.textEllipsis(pair.xTitle, xTitleSize),
standoff: 0,
text: subplots.xyPairs.length > 1 ? textMeasure.textEllipsis(pair.xTitle, xTitleSize) : pair.xTitle,
standoff: subplots.xyPairs.length > 1 ? 0 : undefined,
font: {
size: 12,
color: VIS_NEUTRAL_COLOR,
Expand All @@ -118,15 +118,15 @@ export function useLayout({
axes[`yaxis${plotCounter > 0 ? plotCounter + 1 : ''}`] = {
...AXIS_TICK_STYLES,
range: toLogRange(config.yAxisScale!, pair.yDomain),
type: config.yAxisScale,
// Spread the previous layout to keep things like zoom
...(internalLayoutRef.current?.[`yaxis${plotCounter > 0 ? plotCounter + 1 : ''}` as 'yaxis'] || {}),
type: config.yAxisScale,
title: {
font: {
size: 12,
color: VIS_NEUTRAL_COLOR,
},
text: textMeasure.textEllipsis(pair.yTitle, yTitleSize),
text: subplots.xyPairs.length > 1 ? textMeasure.textEllipsis(pair.yTitle, yTitleSize) : pair.yTitle,
},
};

Expand All @@ -149,37 +149,17 @@ export function useLayout({
});

// if we only find one facet (e.g., the categorical column only contains one value), we don't facet
const finalLayout: Partial<PlotlyTypes.Layout> =
subplots.xyPairs.length > 1
? {
...BASE_LAYOUT,
...(internalLayoutRef.current || {}),
grid: { rows: nRows, columns: nColumns, xgap: xGap, ygap: yGap, pattern: 'independent' },
...axes,
annotations: [...titleAnnotations, ...regressions.annotations],
shapes: regressions.shapes,
dragmode: config!.dragMode,
width,
height,
}
: {
...BASE_LAYOUT,
xaxis: {
...AXIS_TICK_STYLES,
...internalLayoutRef.current?.xaxis,
title: subplots.xyPairs[0]!.xTitle,
},
yaxis: {
...AXIS_TICK_STYLES,
...internalLayoutRef.current?.yaxis,
title: subplots.xyPairs[0]!.yTitle,
},
shapes: regressions.shapes,
annotations: [...regressions.annotations],
dragmode: config.dragMode,
width,
height,
};
const finalLayout: Partial<PlotlyTypes.Layout> = {
...BASE_LAYOUT,
...(internalLayoutRef.current || {}),
...(subplots.xyPairs.length > 1 ? { grid: { rows: nRows, columns: nColumns, xgap: xGap, ygap: yGap, pattern: 'independent' } } : {}),
...axes,
annotations: [...titleAnnotations, ...regressions.annotations],
shapes: regressions.shapes,
dragmode: config.dragMode,
width,
height,
};

return finalLayout;
}
Expand Down
39 changes: 39 additions & 0 deletions src/vis/stories/Vis/Scatter/ScatterIris.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ ControlledSubplots.args = {
title: 'Petal Length vs Petal Width',
},
],
xAxisScale: 'log',
yAxisScale: 'log',
facets: null,
numColorScaleType: ENumericalColorScaleType.SEQUENTIAL,
shape: null,
Expand All @@ -168,6 +170,43 @@ ControlledSubplots.args = {
},
};

export const ControlledSingleSubplot: typeof Template = Template.bind({}) as typeof Template;
ControlledSingleSubplot.args = {
externalConfig: {
type: ESupportedPlotlyVis.SCATTER,
color: null,
subplots: [
{
xColumn: {
description: '',
id: 'incompleteX',
name: 'Incomplete X',
},
yColumn: {
description: '',
id: 'incompleteY',
name: 'Incomplete Y',
},
title: 'Nicer Title',
},
],
xAxisScale: 'log',
yAxisScale: 'log',
facets: null,
numColorScaleType: ENumericalColorScaleType.SEQUENTIAL,
shape: null,
dragMode: EScatterSelectSettings.RECTANGLE,
alphaSliderVal: 1,
showLabels: ELabelingOptions.NEVER,
regressionLineOptions: {
type: ERegressionLineType.NONE,
},
} as IScatterConfig,
filterCallback: (option) => {
console.log({ option });
},
};

export const ColorByCategory: typeof Template = Template.bind({}) as typeof Template;
ColorByCategory.args = {
externalConfig: {
Expand Down
27 changes: 27 additions & 0 deletions src/vis/stories/fetchIrisData.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { EColumnTypes, VisColumn } from '../interfaces';
import { iris as dataPromise } from './irisData';

function randomNumberBetweenRange(min: number, max: number): number | null {
// Return null for some random values
if (Math.random() < 0.1) {
return null;
}

return Math.random() * (max - min) + min;
}

export function fetchIrisData(): VisColumn[] {
return [
{
Expand Down Expand Up @@ -73,5 +82,23 @@ export function fetchIrisData(): VisColumn[] {
type: EColumnTypes.CATEGORICAL,
values: () => dataPromise.map((r) => r.species).map((val, i) => ({ id: i.toString(), val: val ?? null })),
},
{
info: {
description: 'Incomplete X',
id: 'incompleteX',
name: 'Incomplete X',
},
type: EColumnTypes.NUMERICAL,
values: () => dataPromise.map((val, i) => ({ id: i.toString(), val: randomNumberBetweenRange(0.96499999997, 1.3850000003299998) })),
},
{
info: {
description: 'Incomplete Y',
id: 'incompleteY',
name: 'Incomplete Y',
},
type: EColumnTypes.NUMERICAL,
values: () => dataPromise.map((val, i) => ({ id: i.toString(), val: randomNumberBetweenRange(-1.5419997517300001, 28.96199726903) })),
},
];
}
Loading