Skip to content

Commit

Permalink
Html and markup in axes (wizard) (#1971)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzmadom authored Jan 9, 2025
1 parent e4b2734 commit 8707b1f
Show file tree
Hide file tree
Showing 36 changed files with 461 additions and 202 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Feature} from '../../../../shared';
import {createFeatureConfig} from '../utils';

export default createFeatureConfig({
name: Feature.EscapeStringInWizard,
state: {
development: false,
production: false,
},
});
3 changes: 3 additions & 0 deletions src/server/modes/charts/plugins/datalens/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,15 @@ export const buildChartsConfigPrivate = (
}
} else if (visualizationId === 'pie' || visualizationId === 'donut') {
config.showPercentInTooltip = true;
config.manageTooltipConfig = ChartkitHandlers.WizardManageTooltipConfig;
} else if (visualizationId === 'metric') {
(config as MetricConfig).metricVersion = 2;
} else if (visualizationId === 'pivotTable') {
(config as TableConfig).settings = {
externalSort: true,
};
} else if (visualizationId === WizardVisualizationId.CombinedChart) {
config.manageTooltipConfig = ChartkitHandlers.WizardManageTooltipConfig;
}

const isTableWidget = (
Expand Down
38 changes: 23 additions & 15 deletions src/server/modes/charts/plugins/datalens/preparers/bar-x/d3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import type {
ChartKitWidgetData,
} from '@gravity-ui/chartkit/build/types/widget-data';

import type {SeriesExportSettings, ServerField} from '../../../../../../../shared';
import type {SeriesExportSettings, ServerField, WrappedMarkdown} from '../../../../../../../shared';
import {
AxisMode,
LabelsPositions,
PlaceholderId,
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 | WrappedMarkdown;
};

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
@@ -1,4 +1,5 @@
import _isEmpty from 'lodash/isEmpty';
import set from 'lodash/set';

import type {ServerPlaceholder, WizardVisualizationId} from '../../../../../../../shared';
import {
Expand All @@ -11,6 +12,7 @@ import {
getIsNavigatorEnabled,
getXAxisMode,
isDateField,
isHtmlField,
isMeasureNameOrValue,
} from '../../../../../../../shared';
import {getConfigWithActualFieldTypes} from '../../utils/config-helpers';
Expand Down Expand Up @@ -40,6 +42,7 @@ export function prepareHighchartsBarX(args: PrepareFunctionArgs) {
idToDataType,
visualizationId,
shared,
segments,
} = args;
const preparedData = prepareBarX(args);
const {graphs} = preparedData;
Expand Down Expand Up @@ -67,6 +70,8 @@ export function prepareHighchartsBarX(args: PrepareFunctionArgs) {

// Here we manage the highcharts settings depending on the parameters
if (ChartEditor) {
const customConfig: any = {xAxis: {}};

if (yFields.length) {
const isYSectionHasFloatItem = yFields.some((y) => y.data_type === 'float');
if (isYSectionHasFloatItem) {
Expand All @@ -75,7 +80,7 @@ export function prepareHighchartsBarX(args: PrepareFunctionArgs) {
});
}

const customConfig: any = {
Object.assign(customConfig, {
xAxis: {},
plotOptions: {},
axesFormatting: {xAxis: [], yAxis: []},
Expand All @@ -87,7 +92,7 @@ export function prepareHighchartsBarX(args: PrepareFunctionArgs) {
columnHeaderFormatter: ChartkitHandlers.WizardExportColumnNamesFormatter,
},
},
};
});

if (xPlaceholder?.settings?.axisFormatMode === AxisLabelFormatMode.ByField) {
customConfig.axesFormatting.xAxis.push(
Expand Down Expand Up @@ -179,22 +184,19 @@ export function prepareHighchartsBarX(args: PrepareFunctionArgs) {
isLegendEnabled(shared.extraSettings),
};

const {yAxisFormattings, yAxisSettings} = getSegmentsYAxis(
const {yAxisFormattings, yAxisSettings} = getSegmentsYAxis({
segment: segments[0],
segmentsMap,
{
placeholders: {
y: yPlaceholder as ServerPlaceholder,
y2: undefined as unknown as ServerPlaceholder,
},
visualizationId,
);
});
customConfig.yAxis = yAxisSettings;
customConfig.axesFormatting.yAxis = yAxisFormattings;
}

ChartEditor.updateHighchartsConfig(customConfig);
} else {
const customConfig: any = {xAxis: {}};

if (xIsDate || xIsNumber) {
customConfig.xAxis.reversed = isXAxisReversed(
x,
Expand All @@ -214,8 +216,13 @@ export function prepareHighchartsBarX(args: PrepareFunctionArgs) {
customConfig.xAxis.type = 'category';
}
}
}

ChartEditor.updateHighchartsConfig(customConfig);

ChartEditor.updateHighchartsConfig(customConfig);
const shouldUseHtmlForLegend = isHtmlField(colorItem);
if (shouldUseHtmlForLegend) {
set(customConfig, 'legend.useHTML', true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
ServerField,
ServerPlaceholder,
WizardVisualizationId,
WrappedHTML,
} from '../../../../../../../shared';
import {
AxisMode,
Expand All @@ -21,14 +22,16 @@ import {
isMeasureValue,
isPercentVisualization,
} from '../../../../../../../shared';
import {wrapHtml} from '../../../../../../../shared/utils/ui-sandbox';
import {mapAndColorizeGraphsByPalette} from '../../utils/color-helpers';
import {getConfigWithActualFieldTypes} from '../../utils/config-helpers';
import {PSEUDO} from '../../utils/constants';
import {
chartKitFormatNumberWrapper,
collator,
formatDate,
getCategoryFormatter,
getLabelValue,
getSeriesTitleFormatter,
getTimezoneOffsettedTime,
isGradientMode,
isNumericalDataType,
Expand Down Expand Up @@ -86,6 +89,7 @@ export function prepareBarX(args: PrepareFunctionArgs) {
const x2IsNumber = Boolean(x2DataType && isNumericalDataType(x2DataType));
const x2IsDate = Boolean(x2DataType && isDateField({data_type: x2DataType}));
const x2IsPseudo = Boolean(x2 && x2.type === PSEUDO);
const isHtmlX = isHtmlField(x);

const yPlaceholder: ServerPlaceholder = placeholders[1];

Expand All @@ -105,6 +109,7 @@ export function prepareBarX(args: PrepareFunctionArgs) {

const colorItem = colors[0];
const colorFieldDataType = colorItem ? idToDataType[colorItem.guid] : null;
const isHtmlColor = isHtmlField(colorItem);

const gradientMode =
colorItem &&
Expand All @@ -121,6 +126,8 @@ export function prepareBarX(args: PrepareFunctionArgs) {
const segmentIndexInOrder = getSegmentsIndexInOrder(order, segmentField, idToTitle);
const segmentsMap = getSegmentMap(args);
const isSegmentsExists = !_isEmpty(segmentsMap);
const isHtmlSegment = isHtmlField(segmentField);
const segmentTitleFormatter = getSeriesTitleFormatter({fields: [segmentField]});

const isShapeItemExist = false;
const isColorItemExist = Boolean(colorItem && colorItem.type !== 'PSEUDO');
Expand All @@ -144,6 +151,7 @@ export function prepareBarX(args: PrepareFunctionArgs) {
const nullsY1 = placeholders?.[1]?.settings?.nulls;

const categoriesMap = new Map<string | number, boolean>();
const seriesNameFormatter = getSeriesTitleFormatter({fields: [colorItem]});

if (mergedYSections.length) {
let categories: (string | number)[] = [];
Expand Down Expand Up @@ -332,7 +340,7 @@ export function prepareBarX(args: PrepareFunctionArgs) {

const graph: any = {
id: line.id,
title: line.title || 'Null',
title: seriesNameFormatter(line.title || 'Null'),
tooltip: line.tooltip,
dataLabels: {
...line.dataLabels,
Expand Down Expand Up @@ -368,7 +376,11 @@ export function prepareBarX(args: PrepareFunctionArgs) {

if (line.segmentNameKey) {
const currentSegment = segmentsMap[line.segmentNameKey];
const tooltipPointName = `${currentSegment.title}: ${line.title}`;
let tooltipPointName: string | WrappedHTML =
`${currentSegment.title}: ${line.title}`;
if (isHtmlSegment) {
tooltipPointName = wrapHtml(tooltipPointName);
}
point.custom = {
tooltipPointName,
};
Expand Down Expand Up @@ -423,7 +435,7 @@ export function prepareBarX(args: PrepareFunctionArgs) {
const currentSegment = segmentsMap[line.segmentNameKey];
graph.yAxis = currentSegment.index;

customSeriesData.segmentTitle = currentSegment.title;
customSeriesData.segmentTitle = segmentTitleFormatter(currentSegment.title);
} else if (lineKeysIndex === 0 || ySectionItems.length === 0) {
graph.yAxis = 0;
} else {
Expand Down Expand Up @@ -491,18 +503,18 @@ export function prepareBarX(args: PrepareFunctionArgs) {
ChartEditor.updateConfig({useMarkup: true});
}

if (isHtmlLabel) {
if (isHtmlLabel || isHtmlX || isHtmlColor || isHtmlSegment) {
ChartEditor.updateConfig({useHtml: true});
}

if (isXCategoryAxis) {
if (x && isXCategoryAxis) {
const categoriesFormatter = getCategoryFormatter({
field: {...x, data_type: xDataType ?? x.data_type},
});

return {
graphs,
categories: categories.map((value) => {
return xIsDate
? formatDate({valueType: xDataType!, value, format: x?.format, utc: true})
: value;
}),
categories: categories.map(categoriesFormatter),
};
} else {
return {graphs};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import set from 'lodash/set';

import type {ServerField, WizardVisualizationId} from '../../../../../../../shared';
import {
AxisLabelFormatMode,
Expand All @@ -11,6 +13,7 @@ import {
getXAxisMode,
isDateField,
isFloatField,
isHtmlField,
isMeasureNameOrValue,
} from '../../../../../../../shared';
import {getConfigWithActualFieldTypes} from '../../utils/config-helpers';
Expand Down Expand Up @@ -66,6 +69,7 @@ function getHighchartsConfig(args: PrepareFunctionArgs & {graphs: any[]}) {
isDateField(x) && isXDiscrete
? ChartkitHandlers.WizardXAxisFormatter
: undefined,
useHTML: isHtmlField(x),
},
},
axesFormatting: {
Expand Down Expand Up @@ -122,6 +126,11 @@ function getHighchartsConfig(args: PrepareFunctionArgs & {graphs: any[]}) {
}
}

const shouldUseHtmlForLegend = isHtmlField(colorItem);
if (shouldUseHtmlForLegend) {
set(customConfig, 'legend.useHTML', true);
}

return customConfig;
}

Expand Down
Loading

0 comments on commit 8707b1f

Please sign in to comment.