Skip to content

Commit

Permalink
fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzmadom committed Dec 26, 2024
1 parent 4691994 commit 8b36807
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 33 deletions.
36 changes: 22 additions & 14 deletions src/server/modes/charts/plugins/datalens/preparers/bar-x/d3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getFakeTitleOrTitle,
getXAxisMode,
} from '../../../../../../../shared';
import type {WrappedHTML} from '../../../../../../../shared/types/charts';
import {getFormattedLabel} from '../../d3/utils/dataLabels';
import {getConfigWithActualFieldTypes} from '../../utils/config-helpers';
import {getExportColumnSettings} from '../../utils/export-helpers';
Expand All @@ -27,14 +28,19 @@ type OldBarXDataItem = {
custom?: any;
} | null;

type ExtendedBarXSeries = BarXSeries & {
type ExtendedBaXrSeriesData = Omit<BarXSeriesData, 'x'> & {
x?: BarXSeriesData['x'] | WrappedHTML;
};

type ExtendedBarXSeries = Omit<BarXSeries, 'data'> & {
custom?: {
exportSettings?: SeriesExportSettings;
colorValue?: string;
};
data: ExtendedBaXrSeriesData[];
};

export function prepareD3BarX(args: PrepareFunctionArgs): ChartKitWidgetData {
export function prepareD3BarX(args: PrepareFunctionArgs) {
const {
shared,
labels,
Expand Down Expand Up @@ -97,8 +103,8 @@ export function prepareD3BarX(args: PrepareFunctionArgs): ChartKitWidgetData {
stackId: graph.stack,
stacking: 'normal',
data: graph.data.reduce(
(acc: BarXSeriesData[], item: OldBarXDataItem, index: number) => {
const dataItem: BarXSeriesData = {
(acc: ExtendedBaXrSeriesData[], item: OldBarXDataItem, index: number) => {
const dataItem: ExtendedBaXrSeriesData = {
y: item?.y || 0,
custom: item?.custom,
};
Expand Down Expand Up @@ -130,22 +136,24 @@ export function prepareD3BarX(args: PrepareFunctionArgs): ChartKitWidgetData {
};
});

const config: ChartKitWidgetData = {
series: {
data: seriesData,
},
};

if (config.series.data.length <= 1) {
config.legend = {enabled: false};
let legend: ChartKitWidgetData['legend'];
if (seriesData.length <= 1) {
legend = {enabled: false};
}

let xAxis: ChartKitWidgetData['xAxis'];
if (isCategoriesXAxis) {
config.xAxis = {
xAxis = {
type: 'category',
categories: xCategories?.map(String),
};
}

return config;
return {
series: {
data: seriesData,
},
legend,
xAxis,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import type {SegmentsMap} from '../segments/types';

describe('getSegmentsYAxis', () => {
it('The position of a segment(top) depends on its index', () => {
const segmentMap: SegmentsMap = {
const segmentsMap: SegmentsMap = {
a: {index: 1, title: 'a', isOpposite: false},
b: {index: 0, title: 'b', isOpposite: false},
};
const actual = getSegmentsYAxis(
segmentMap,
{},
WizardVisualizationId.Column,
).yAxisSettings.map((s) => ({
const actual = getSegmentsYAxis({
segmentsMap,
placeholders: {},
visualizationId: WizardVisualizationId.Column,
}).yAxisSettings.map((s) => ({
top: (s as Highcharts.YAxisOptions).top,
title: s.title?.text,
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
isPseudoField,
markupToRawString,
} from '../../../../../../../../../shared';
import type {WrappedHTML} from '../../../../../../../../../shared/types/charts';
import {
findIndexInOrder,
getFormatOptionsFromFieldFormatting,
Expand Down Expand Up @@ -170,6 +171,10 @@ const extendLineWithSegmentsData = (args: ExtendLineWithSegmentDataArgs): LineTe
};
};

function getSeriesId(...str: (string | WrappedHTML)[]) {
return str.find((s) => typeof s === 'string') ?? '';
}

export const prepareLines = (args: PrepareLinesArgs) => {
const {
ySectionItems,
Expand Down Expand Up @@ -354,7 +359,7 @@ export const prepareLines = (args: PrepareLinesArgs) => {
});
}

lines[keys.key].id = currentLine.legendTitle || currentLine.title;
lines[keys.key].id = getSeriesId(currentLine.legendTitle, currentLine.title);

if (keys.pointConflict) {
lines[keys.key].pointConflict = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import type {Highcharts} from '@gravity-ui/chartkit/highcharts';
import sortBy from 'lodash/sortBy';

import type {ServerField, ServerPlaceholder, WrappedHTML} from '../../../../../../../../../shared';
import type {ServerField, ServerPlaceholder} from '../../../../../../../../../shared';
import {AxisLabelFormatMode, isHtmlField} from '../../../../../../../../../shared';
import {wrapHtml} from '../../../../../../../../../shared/utils/ui-sandbox';
import type {AxisOptions} from '../../../../types';
import {applyPlaceholderSettingsToAxis} from '../../../../utils/axis-helpers';
import {getAxisFormattingByField} from '../axis/getAxisFormattingByField';

import type {SegmentsMap} from './types';

const DEFAULT_SPACE_BETWEEN_SEGMENTS = 4;

type AxisOptions = {
title?: Omit<Highcharts.AxisOptions['title'], 'text'> & {
text?: string | WrappedHTML;
};
} & Highcharts.AxisOptions;

export const getSegmentsYAxis = (args: {
segment: ServerField | undefined;
segment?: ServerField;
segmentsMap: SegmentsMap;
placeholders: {y?: ServerPlaceholder; y2?: ServerPlaceholder};
visualizationId: string;
Expand Down Expand Up @@ -52,7 +46,7 @@ export const getSegmentsYAxis = (args: {
segmentIndex = leftAxisSegment;
}

const segmentTitle = isHtmlSegment ? wrapHtml(segment.title) : segment.title;
const segmentTitle = isHtmlSegment ? wrapHtml(segment.title) : String(segment.title);

const axis: AxisOptions = {
top: `${DEFAULT_SPACE_BETWEEN_SEGMENTS * segmentIndex + segmentsSpace * segmentIndex}%`,
Expand Down
27 changes: 27 additions & 0 deletions src/server/modes/charts/plugins/datalens/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
TableBarsSettings,
Update,
} from '../../../../../shared';
import type {WrappedHTML} from '../../../../../shared/types/charts';
import type {TableFieldBackgroundSettings} from '../../../../../shared/types/wizard/background-settings';

export type PayloadParameter = {
Expand Down Expand Up @@ -69,3 +70,29 @@ export interface ChartColorsConfig extends ServerColorsConfig {
loadedColorPalettes: Record<string, ColorPalette>;
availablePalettes: Record<string, Palette>;
}

// Extended Highcharts.AxisOptions
export type AxisOptions = {
type?: string;
visible?: boolean;
opposite?: boolean;
top?: string;
height?: string;
offset?: number;
min?: number;
max?: number;
endOnTick?: boolean;
tickPixelInterval?: number;
lineWidth?: number;
gridLineWidth?: number;
minorGridLineWidth?: number;
title?: {
text: string | WrappedHTML;
align?: string;
textAlign?: string;
offset?: number;
rotation?: number;
style?: React.CSSProperties;
};
labels?: any[];
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
ServerPlaceholderSettings,
} from '../../../../../../shared';
import {isDateField} from '../../../../../../shared';
import type {AxisOptions} from '../types';

import {getOriginalTitleOrTitle} from './misc-helpers';

Expand Down Expand Up @@ -49,7 +50,7 @@ export function getTickPixelInterval(placeholderSettings: ServerPlaceholderSetti
// eslint-disable-next-line complexity
export const applyPlaceholderSettingsToAxis = (
placeholder: ServerPlaceholder | undefined,
axis: Highcharts.AxisOptions,
axis: Highcharts.AxisOptions | AxisOptions,
ignore: IgnoreProps,
) => {
if (placeholder && placeholder.settings) {
Expand Down
10 changes: 9 additions & 1 deletion src/shared/utils/ui-sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import {WRAPPED_HTML_KEY} from '../constants';
import type {ChartKitHtmlItem} from '../types';
import type {ChartKitHtmlItem, WrappedHTML} from '../types';

export function wrapHtml(value: ChartKitHtmlItem | string) {
return {
[WRAPPED_HTML_KEY]: value,
};
}

export function isWrappedHTML(value: unknown): value is WrappedHTML {
if (!value || typeof value !== 'object') {
return false;
}

return WRAPPED_HTML_KEY in value;
}

0 comments on commit 8b36807

Please sign in to comment.