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

Add form checks for logarithmic axis type and fallbacks #1433

Merged
merged 5 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 19 additions & 2 deletions src/server/modes/charts/plugins/datalens/utils/axis-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,28 @@ export const applyPlaceholderSettingsToAxis = (
placeholder.settings.autoscale === false ||
(placeholder.settings.scale === 'auto' && placeholder.settings.scaleValue === '0-max')
) {
axis.min = 0;
if (placeholder.settings.type === 'logarithmic') {
// Fallback to null for incorrect case
axis.min = null;
} else {
axis.min = 0;
}
} else if (placeholder.settings.scale === 'manual') {
axis.endOnTick = false;
axis.min = Number((placeholder.settings.scaleValue as [string, string])[0]);
axis.max = Number((placeholder.settings.scaleValue as [string, string])[1]);

if (placeholder.settings.type === 'logarithmic') {
if (axis.min <= 0) {
// Fallback to null for incorrect case
axis.min = null;
}

if (axis.max <= 0) {
// Fallback to null for incorrect case
axis.max = null;
}
}
artemipanchuk marked this conversation as resolved.
Show resolved Hide resolved
}

const axisTitle = getAxisTitle(placeholder.settings, placeholder.items[0]);
Expand All @@ -75,7 +92,7 @@ export const applyPlaceholderSettingsToAxis = (
if (isDateField(placeholder.items[0])) {
axis.type = 'datetime';
} else {
axis.type = placeholder.settings?.type === 'logarithmic' ? 'logarithmic' : 'linear';
axis.type = placeholder.settings.type === 'logarithmic' ? 'logarithmic' : 'linear';
}

if (!isGridEnabled(placeholder.settings)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -762,10 +762,13 @@ class DialogPlaceholder extends React.PureComponent<Props, State> {
};

handleScaleRadioButtonsUpdate = (scaleMode: string) => {
const defaultManualValue: [string, string] =
this.state.settings.type === SETTINGS.TYPE.LOGARITHMIC
? ['0.001', '100']
: ['0', '100'];

const scaleValue =
scaleMode === SETTINGS.SCALE.MANUAL
? (['0', '100'] as [string, string])
: SETTINGS.SCALE_VALUE.MIN_MAX;
scaleMode === SETTINGS.SCALE.MANUAL ? defaultManualValue : SETTINGS.SCALE_VALUE.MIN_MAX;

this.setState({
settings: {
Expand All @@ -786,20 +789,38 @@ class DialogPlaceholder extends React.PureComponent<Props, State> {
},
};

if (
axisType === SETTINGS.TYPE.LOGARITHMIC &&
settings.scaleValue === SETTINGS.SCALE_VALUE.ZERO_MAX
) {
updatedState.settings = {
...updatedState.settings,
scaleValue: SETTINGS.SCALE_VALUE.MIN_MAX,
};
updatedState.tooltipType = 'scaleValue';
updatedState.tooltipContent = (
<span data-qa={DialogPlaceholderQa.TooltipZeroToMaxScale}>
{i18n('wizard', 'tooltip_zero-to-max-scale')}
</span>
);
updatedState.tooltipType = undefined;
updatedState.tooltipContent = '';

if (axisType === SETTINGS.TYPE.LOGARITHMIC) {
if (settings.scaleValue === SETTINGS.SCALE_VALUE.ZERO_MAX) {
updatedState.settings = {
...updatedState.settings,
scaleValue: SETTINGS.SCALE_VALUE.MIN_MAX,
};
updatedState.tooltipType = 'scaleValue';
updatedState.tooltipContent = (
<span data-qa={DialogPlaceholderQa.TooltipZeroToMaxScale}>
{i18n('wizard', 'tooltip_zero-to-max-scale')}
</span>
);
} else if (
settings.scale === SETTINGS.SCALE.MANUAL &&
(Number(settings?.scaleValue?.[0]) <= 0 || Number(settings?.scaleValue?.[1]) <= 0)
) {
updatedState.settings = {
...updatedState.settings,
scale: SETTINGS.SCALE.AUTO,
scaleValue: SETTINGS.SCALE_VALUE.MIN_MAX,
};

updatedState.tooltipType = 'scaleValue';
updatedState.tooltipContent = (
<span data-qa={DialogPlaceholderQa.TooltipZeroToMaxScale}>
{i18n('wizard', 'tooltip_zero-to-max-scale')}
artemipanchuk marked this conversation as resolved.
Show resolved Hide resolved
</span>
);
}
}

this.setState(
Expand All @@ -809,7 +830,7 @@ class DialogPlaceholder extends React.PureComponent<Props, State> {
};

handleScaleValueUpdate = (scaleValue: string) => {
const settings = this.state.settings;
const {settings} = this.state;

const updatedState: Partial<State> = {
settings: {
Expand All @@ -832,6 +853,9 @@ class DialogPlaceholder extends React.PureComponent<Props, State> {
{i18n('wizard', 'tooltip_logarithmic-axis')}
</span>
);
} else {
updatedState.tooltipType = undefined;
updatedState.tooltipContent = '';
}

this.setState(
Expand All @@ -846,20 +870,37 @@ class DialogPlaceholder extends React.PureComponent<Props, State> {

const [prevMin, prevMax] = scaleValue;

const updatedScaleValue = [];
const updatedState: {settings: State['settings']} & Partial<State> = {
settings: {
...this.state.settings,
},
tooltipType: undefined,
tooltipContent: '',
};

if (type === 'min') {
updatedScaleValue.push(value, prevMax);
updatedState.settings.scaleValue = [value, prevMax];
} else {
updatedScaleValue.push(prevMin, value);
updatedState.settings.scaleValue = [prevMin, value];
}

this.setState({
settings: {
...this.state.settings,
scaleValue: updatedScaleValue as [string, string],
},
});
// Here we need to check whether axis type is logarithmic and min value is 0 or negative
if (settings.type === SETTINGS.TYPE.LOGARITHMIC && Number(value) <= 0) {
// And if axis type was algorithmic, then we need to reset it to linear in that case
updatedState.settings.type = SETTINGS.TYPE.LINEAR;

updatedState.tooltipType = 'type';
updatedState.tooltipContent = (
<span data-qa={DialogPlaceholderQa.TooltipLogarithmicAxis}>
{i18n('wizard', 'tooltip_logarithmic-axis')}
</span>
);
}

this.setState(
(prevState: State) => ({...prevState, ...updatedState}),
() => this.tooltipRef.current && this.tooltipRef.current.openTooltip(),
);
};
}

Expand Down
Loading