Skip to content

Commit

Permalink
Add nullMode to GradientColorConfig & Up @gravity-ui/uikit 6.37.0 -> …
Browse files Browse the repository at this point in the history
…6.39.0 (#1937)
  • Loading branch information
DaryaLari authored Dec 24, 2024
1 parent 1aeec00 commit a142fb5
Show file tree
Hide file tree
Showing 25 changed files with 294 additions and 160 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"@gravity-ui/stylelint-config": "^4.0.1",
"@gravity-ui/tsconfig": "^1.0.0",
"@gravity-ui/ui-logger": "^1.1.0",
"@gravity-ui/uikit": "^6.37.0",
"@gravity-ui/uikit": "^6.39.0",
"@jest/types": "^29.6.3",
"@microsoft/fetch-event-source": "^2.0.1",
"@playwright/test": "^1.48.2",
Expand Down
2 changes: 2 additions & 0 deletions src/i18n-keysets/wizard/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@
"label_overlap": "Labels overlap",
"label_overriden-from-dashboard": "overriden from dashboard",
"label_pagination": "Pagination",
"label_painting-as-0": "Paint as 0",
"label_painting-ignore": "Do not paint",
"label_palette": "Palette",
"label_part": "Date part",
"label_percent": "Percentage",
Expand Down
2 changes: 2 additions & 0 deletions src/i18n-keysets/wizard/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@
"label_overlap": "Перекрытие подписей",
"label_overriden-from-dashboard": "перезаписан с дашборда",
"label_pagination": "Пагинация",
"label_painting-as-0": "Окрашивать как 0",
"label_painting-ignore": "Не окрашивать",
"label_palette": "Палитра",
"label_part": "Часть даты",
"label_percent": "В процентах",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
} from '../../../../../../../../shared';
import {
ApiV2Annotations,
GradientNullModes,
PseudoFieldTitle,
getFakeTitleOrTitle,
} from '../../../../../../../../shared';
Expand Down Expand Up @@ -231,8 +232,19 @@ export const prepareBackgroundColorSettings = (args: PrepareBackgroundColorSetti
const fieldColorValues = Array.from(colorValues);

continuousColorsByField[guid] = {};
const nilValue =
backgroundSettings.settings.gradientState.nullMode === GradientNullModes.AsZero
? 0
: null;

const colorValuesWithoutNull = fieldColorValues.reduce<number[]>((acc, cv) => {
const colorValue = cv === null ? nilValue : cv;
if (colorValue !== null) {
acc.push(Number(colorValue));
}

const colorValuesWithoutNull = fieldColorValues.filter((cv): cv is number => cv !== null);
return acc;
}, []);

const min = Math.min(...colorValuesWithoutNull);
const max = Math.max(...colorValuesWithoutNull);
Expand All @@ -250,12 +262,15 @@ export const prepareBackgroundColorSettings = (args: PrepareBackgroundColorSetti

fieldColorValues.forEach((value) => {
const colorValue = getContinuousColorValue(value);
if (colorValue === null) {
if (
colorValue === null &&
backgroundSettings.settings.gradientState.nullMode !== GradientNullModes.AsZero
) {
return;
}

const color = colorizePivotTableCell(colorValue, chartColorsConfig, [min, max]);
continuousColorsByField[guid][String(value)] = color?.backgroundColor || null;
continuousColorsByField[guid][String(colorValue)] = color?.backgroundColor || null;
});
});

Expand Down Expand Up @@ -317,7 +332,10 @@ export const colorizePivotTableByFieldBackgroundSettings = (
const {settings, colorFieldGuid} = backgroundColorSettings;
const colorKey = cell.colorKey;

if (!colorKey) {
if (
!colorKey &&
backgroundColorSettings.settings.gradientState.nullMode !== GradientNullModes.AsZero
) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {ServerColor} from '../../../../../../../../shared';
import {ApiV2Annotations, isMeasureName} from '../../../../../../../../shared';
import {ApiV2Annotations, GradientNullModes, isMeasureName} from '../../../../../../../../shared';
import type {ChartColorsConfig} from '../../../types';
import {colorizePivotTableCell} from '../../../utils/color-helpers';
import type {AnnotationsMap, PivotDataCellValue, PivotDataRows} from '../types';
Expand Down Expand Up @@ -110,11 +110,13 @@ export const colorizePivotTableByColorField = (args: ColorizeByColorFieldArgs) =
}

const {colorValues, min, max} = colorSettings;
const nilValue = colorsConfig.nullMode === GradientNullModes.AsZero ? 0 : null;

rows.forEach((row, rowIndex) => {
for (let i = rowHeaderLength; i < row.cells.length; i++) {
const cell = row.cells[i];
const colorValue = colorValues[rowIndex][i - rowHeaderLength];
const rawColorValue = colorValues[rowIndex][i - rowHeaderLength];
const colorValue = rawColorValue === null ? nilValue : rawColorValue;

const isInvalidColorValue = colorValue === null;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import isNil from 'lodash/isNil';
import isNumber from 'lodash/isNumber';

import type {
RGBColor,
ServerField,
TableFieldBackgroundSettings,
import {
GradientNullModes,
type RGBColor,
type ServerField,
type TableFieldBackgroundSettings,
} from '../../../../../../../../../shared';
import type {ChartColorsConfig} from '../../../../types';
import {
Expand Down Expand Up @@ -46,15 +47,13 @@ export function colorizeFlatTableColumn({
index: number;
colorsConfig: ChartColorsConfig;
}) {
const colorValues = data.reduce(
(acc, row) => {
const rowValue = row[index];
const parsedRowValue = isNil(rowValue) ? null : parseFloat(rowValue);

return [...acc, parsedRowValue];
},
[] as (number | null)[],
);
const nilValue = colorsConfig.nullMode === GradientNullModes.AsZero ? 0 : null;
const colorValues = data.reduce<(number | null)[]>((acc, row) => {
const rowValue = row[index];
const parsedRowValue = isNil(rowValue) ? nilValue : parseFloat(rowValue);
acc.push(parsedRowValue);
return acc;
}, []);

const {min, mid, max} = getThresholdValues(colorsConfig, colorValues.filter(isNumber));
const currentGradient = getCurrentGradient(colorsConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
} from '../../../../../../../shared';
import {
DATASET_FIELD_TYPES,
GradientNullModes,
IS_NULL_FILTER_TEMPLATE,
MINIMUM_FRACTION_DIGITS,
isDateField,
Expand Down Expand Up @@ -277,7 +278,7 @@ function prepareFlatTable({

if (colors.length) {
const valueColor = values[iColor];
if (valueColor !== null) {
if (valueColor !== null || colorsConfig.nullMode === GradientNullModes.AsZero) {
cell.color = Number(valueColor);
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/shared/constants/gradients/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {ColorPalette} from '../../types/color-palettes';
import type {ValueOf} from '../../types/utility-types';

import {
THREE_POINT_DEFAULT_GRADIENT,
Expand Down Expand Up @@ -27,6 +28,13 @@ export interface Gradient {
colors: string[];
}

export const GradientNullModes = {
Ignore: 'ignore',
AsZero: 'as-0',
};

export type GradientNullMode = ValueOf<typeof GradientNullModes>;

export interface RGBColor {
red: number;
green: number;
Expand Down
3 changes: 2 additions & 1 deletion src/shared/types/config/wizard/v12.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {MarkupType, WidgetSizeType} from '../../..';
import type {GradientNullMode, MarkupType, WidgetSizeType} from '../../..';
import type {ColorMode} from '../../../constants';
import type {DatasetFieldCalcMode, ParameterDefaultValue} from '../../dataset';
import type {
Expand Down Expand Up @@ -294,6 +294,7 @@ export type V12ColorsConfig = {
coloredByMeasure?: boolean;
palette?: string;
colorMode?: ColorMode;
nullMode?: GradientNullMode;
};

export type V12ShapesConfig = {
Expand Down
3 changes: 2 additions & 1 deletion src/shared/types/wizard/background-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ type GradientFields =
| 'rightThreshold'
| 'gradientPalette'
| 'gradientMode'
| 'reversed';
| 'reversed'
| 'nullMode';

export interface TableFieldBackgroundSettings {
enabled: boolean;
Expand Down
2 changes: 2 additions & 0 deletions src/shared/types/wizard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
} from '../';
import type {
ColorMode,
GradientNullMode,
GradientType,
NavigatorLinesMode,
NavigatorPeriod,
Expand Down Expand Up @@ -75,6 +76,7 @@ export interface ColorsConfig {
coloredByMeasure?: boolean;
palette?: string;
colorMode?: ColorMode;
nullMode?: GradientNullMode;
}

export enum LabelsPositions {
Expand Down
1 change: 1 addition & 0 deletions src/ui/units/wizard/actions/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ export function openDialogColors({item, onApply, colorSectionFields}: OpenDialog
}
},
colorsConfig,
canSetNullMode: true,
}),
);
}
Expand Down
4 changes: 3 additions & 1 deletion src/ui/units/wizard/actions/dialogColor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {selectAvailableClientGradients} from 'constants/common';

import type {ColorsConfig, Field, GradientType, PartialBy} from 'shared';
import type {ColorsConfig, Field, GradientNullMode, GradientType, PartialBy} from 'shared';
import {closeDialog, openDialog} from 'store/actions/dialog';
import type {DatalensGlobalState} from 'ui';

Expand Down Expand Up @@ -42,6 +42,7 @@ export interface GradientState {
rightThreshold?: string;
gradientPalette: string;
validationStatus?: ValidationStatus;
nullMode?: GradientNullMode;
}

export const RESET_DIALOG_COLOR_STATE = Symbol('wizard/dialogColor/RESET_DIALOG_COLOR_STATE');
Expand Down Expand Up @@ -141,6 +142,7 @@ export function prepareDialogColorState(props: {
middleThreshold: colorsConfig?.middleThreshold || defaultGradientState.middleThreshold,
rightThreshold: colorsConfig?.rightThreshold || defaultGradientState.rightThreshold,
gradientPalette: colorsConfig?.gradientPalette || defaultGradientState.gradientPalette,
nullMode: colorsConfig?.nullMode || defaultGradientState.nullMode,
};

const gradients = selectAvailableClientGradients(getState(), gradientState.gradientMode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type OwnProps = {
onColorModeChange: (value: ColorMode) => void;
isColorModeChangeAvailable: boolean;
colorSectionFields?: Field[];
canSetNullMode?: boolean;
};

type StateProps = ReturnType<typeof mapStateToProps>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}

&_gradient-mode {
height: 370px;
height: 380px;

.color-settings-container {
&__row {
Expand Down
27 changes: 24 additions & 3 deletions src/ui/units/wizard/components/Dialogs/DialogColor/DialogColor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ import {bindActionCreators} from 'redux';
import type {ColorsConfig, Field} from 'shared';
import {ColorMode, isMeasureValue} from 'shared';
import type {DatalensGlobalState} from 'ui';
import {ALLOWED_FOR_NULL_MODE_VISUALIZATIONS} from 'ui/units/wizard/constants/dialogColor';
import {setDialogColorPaletteState} from 'units/wizard/actions/dialogColor';
import {selectDataset, selectParameters} from 'units/wizard/selectors/dataset';
import {selectUpdates} from 'units/wizard/selectors/preview';
import {selectDashboardParameters, selectFilters} from 'units/wizard/selectors/visualization';
import {
selectDashboardParameters,
selectFilters,
selectVisualization,
} from 'units/wizard/selectors/visualization';

import {
isGradientDialog,
Expand Down Expand Up @@ -46,6 +51,7 @@ interface OwnProps {
colorsConfig: ColorsConfig;
extra?: ExtraSettings;
isColorModeChangeAvailable: boolean;
canSetNullMode?: boolean;
}

type StateProps = ReturnType<typeof mapStateToProps>;
Expand Down Expand Up @@ -87,15 +93,26 @@ class DialogColorComponent extends React.Component<Props, State> {
}

render() {
const {item, items, dataset, isColorModeChangeAvailable, colorSectionFields} = this.props;
const {
item,
items,
dataset,
isColorModeChangeAvailable,
colorSectionFields,
visualization,
} = this.props;
const {mountedColors = {}} = this.props.paletteState;
const {validationStatus} = this.props.gradientState;
const {colorMode} = this.state;

if (!item || !dataset) {
if (!item || !dataset || !visualization) {
return null;
}

const canSetNullMode =
this.props.canSetNullMode &&
(ALLOWED_FOR_NULL_MODE_VISUALIZATIONS as string[]).includes(visualization.id);

return (
<Dialog open={true} onClose={this.onClose} disableFocusTrap={true}>
<div className={b({[`${colorMode}-mode`]: true})}>
Expand All @@ -117,6 +134,7 @@ class DialogColorComponent extends React.Component<Props, State> {
colorMode={this.state.colorMode}
isColorModeChangeAvailable={isColorModeChangeAvailable}
onColorModeChange={this.onColorModeChange}
canSetNullMode={canSetNullMode}
/>
</Dialog.Body>
<Dialog.Footer
Expand Down Expand Up @@ -183,6 +201,7 @@ class DialogColorComponent extends React.Component<Props, State> {
leftThreshold,
middleThreshold,
rightThreshold,
nullMode,
} = this.props.gradientState;

config = {
Expand All @@ -195,6 +214,7 @@ class DialogColorComponent extends React.Component<Props, State> {
middleThreshold,
rightThreshold,
colorMode,
nullMode,
};

return config;
Expand Down Expand Up @@ -228,6 +248,7 @@ const mapStateToProps = (state: DatalensGlobalState) => {
dataset: selectDataset(state),
gradientState: selectDialogColorGradientState(state),
paletteState: selectDialogColorPaletteState(state),
visualization: selectVisualization(state),
};
};

Expand Down
Loading

0 comments on commit a142fb5

Please sign in to comment.