Skip to content

Commit

Permalink
Some pie chart improvements (#1832)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzmadom authored Dec 5, 2024
1 parent 21e4795 commit 4447f3b
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 82 deletions.
160 changes: 79 additions & 81 deletions src/server/modes/charts/plugins/datalens/preparers/pie/d3.ts
Original file line number Diff line number Diff line change
@@ -1,110 +1,108 @@
import type {
ChartKitWidgetData,
PieSeries,
PieSeriesData,
} from '@gravity-ui/chartkit/build/types/widget-data';
import type {PieSeries, PieSeriesData} from '@gravity-ui/chartkit';

import type {SeriesExportSettings, ServerField} from '../../../../../../../shared';
import type {SeriesExportSettings} from '../../../../../../../shared';
import {WizardVisualizationId, formatNumber, getFormatOptions} from '../../../../../../../shared';
import {getFormattedLabel} from '../../d3/utils/dataLabels';
import {getFakeTitleOrTitle} from '../../../../../../../shared/modules/fields';
import {isMarkdownField, isMarkupField} from '../../../../../../../shared/types/index';
import {getExportColumnSettings} from '../../utils/export-helpers';
import type {PrepareFunctionArgs} from '../types';
import type {PiePoint, PrepareFunctionArgs} from '../types';

import type {PieConfig} from './prepare-pie-data';
import preparePieData from './prepare-pie-data';
import {getFormattedValue, isColoringByMeasure} from './utils';

type MapPieSeriesArgs = {
graph: PieConfig;
isLabelsEnabled?: boolean;
labelField?: ServerField;
measureField?: ServerField;
visualizationId: WizardVisualizationId;
totals?: string | null;
ChartEditor: PrepareFunctionArgs['ChartEditor'];
};

type ExtendedPieSeriesData = PieSeriesData & {
type ExtendedPieSeriesData = Omit<PieSeriesData, 'label'> & {
drillDownFilterValue?: string;
formattedValue: string | null;
percentage: number;
label?: PiePoint['label'];
};

type ExtendedPieSeries = PieSeries & {
type ExtendedPieSeries = Omit<PieSeries, 'data'> & {
custom?: {
totals?: string;
exportSettings?: SeriesExportSettings;
};
data: ExtendedPieSeriesData[];
};

function mapPieSeries(args: MapPieSeriesArgs) {
const {graph, isLabelsEnabled, measureField, labelField, visualizationId, totals, ChartEditor} =
args;
export function prepareD3Pie(args: PrepareFunctionArgs) {
const {labels, visualizationId, ChartEditor, colorsConfig, idToDataType} = args;
const {graphs, label, measure, totals} = preparePieData(args);
const isLabelsEnabled = Boolean(labels?.length && label && measure?.hideLabelMode !== 'hide');
const isMarkdownLabel = isMarkdownField(label);
const isMarkupLabel = isMarkupField(label);

const seriesConfig: ExtendedPieSeries = {
type: 'pie',
dataLabels: {
enabled: isLabelsEnabled,
},
data:
graph.data?.map<PieSeriesData>((item) => {
const dataItem: ExtendedPieSeriesData = {
value: item.y,
color: String(item.color),
name: item.name,
custom: item.custom,
drillDownFilterValue: item.drillDownFilterValue,
};
let data: ExtendedPieSeries[] = [];

if (isLabelsEnabled) {
dataItem.label = getFormattedLabel(item.label as string | number, labelField);
}
if (measure && graphs.length > 0) {
const graph = graphs[0];
const total = graph.data?.reduce((sum, d) => sum + (d.y || 0), 0) ?? 0;
const seriesConfig: ExtendedPieSeries = {
type: 'pie',
dataLabels: {
enabled: isLabelsEnabled,
html: isMarkdownLabel || isMarkupLabel,
},
data:
graph.data?.map((item) => {
return {
...item,
value: item.y,
color: String(item.color),
formattedValue: getFormattedValue(String(item.y), {
...measure,
data_type: idToDataType[measure.guid],
}),
percentage: item.y / total,
};
}) ?? [],
};

return dataItem;
}) || [],
};
seriesConfig.custom = {
exportSettings: {
columns: [
{
name: ChartEditor.getTranslation('chartkit.data-provider', 'categories'),
field: 'name',
},
getExportColumnSettings({path: 'value', field: measure}),
],
},
};

seriesConfig.custom = {
exportSettings: {
columns: [
{
name: ChartEditor.getTranslation('chartkit.data-provider', 'categories'),
field: 'name',
},
getExportColumnSettings({path: 'value', field: measureField}),
],
},
};
if (visualizationId === WizardVisualizationId.DonutD3) {
seriesConfig.innerRadius = '50%';

if (visualizationId === WizardVisualizationId.DonutD3) {
seriesConfig.innerRadius = '50%';

if (measureField && totals) {
seriesConfig.custom = {
...seriesConfig.custom,
totals: formatNumber(Number(totals), getFormatOptions(measureField)),
};
if (measure && totals) {
seriesConfig.custom = {
...seriesConfig.custom,
totals: formatNumber(Number(totals), getFormatOptions(measure)),
};
}
}
}

return seriesConfig;
}
data.push(seriesConfig);
} else {
data = [];
}

export function prepareD3Pie(args: PrepareFunctionArgs): ChartKitWidgetData {
const {labels, visualizationId, ChartEditor} = args;
const {graphs, label, measure, totals} = preparePieData(args);
const isLabelsEnabled = Boolean(labels?.length && label && measure?.hideLabelMode !== 'hide');
let legend;
if (graphs.length && isColoringByMeasure(args)) {
legend = {
enabled: true,
type: 'continuous',
title: {text: getFakeTitleOrTitle(measure)},
colorScale: {
colors: colorsConfig.gradientColors,
stops: colorsConfig.gradientColors.length === 2 ? [0, 1] : [0, 0.5, 1],
},
};
}

return {
series: {
data: graphs.map((graph) =>
mapPieSeries({
graph,
labelField: label,
isLabelsEnabled,
visualizationId: visualizationId as WizardVisualizationId,
totals,
measureField: measure,
ChartEditor,
}),
),
data,
},
legend,
};
}
20 changes: 20 additions & 0 deletions src/ui/libs/DatalensChartkit/ChartKit/ChartKit.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,24 @@
&__content {
font-size: 12px;
}

&__row {
display: flex;
}

&__color {
border-radius: 1px;
display: inline-block;
height: 6px;
width: 12px;
margin-bottom: 2px;
}

&__block {
padding: 0 4px;
max-width: 400px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
30 changes: 29 additions & 1 deletion src/ui/libs/DatalensChartkit/ChartKit/helpers/tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React from 'react';

import type {ChartKitWidgetData, TreemapSeriesData} from '@gravity-ui/chartkit';
import type {ChartKitWidgetData, PieSeriesData, TreemapSeriesData} from '@gravity-ui/chartkit';
import type {
ChartKitWidgetTooltip,
ScatterSeries,
ScatterSeriesData,
} from '@gravity-ui/chartkit/build/types/widget-data';
import block from 'bem-cn-lite';
import get from 'lodash/get';
import {formatNumber} from 'shared/modules/format-units/index';

import type {PointCustomData, ScatterSeriesCustomData} from '../../../../../shared/types/chartkit';

Expand Down Expand Up @@ -92,6 +93,29 @@ function treemapTooltipRenderer(
);
}

function pieTooltipRenderer(_widgetData: ChartKitWidgetData, data: Parameters<TooltipRenderer>[0]) {
const point = data?.hovered[0]?.data as PieSeriesData | undefined;

if (!point) {
return null;
}

const value = get(point, 'formattedValue', point.value);
let percentage = get(point, 'percentage');
percentage = percentage ? formatNumber(percentage, {precision: 1, format: 'percent'}) : null;

return (
<div className={b('row')}>
<div className={b('block')}>
<span className={b('color')} style={{backgroundColor: point.color}} />
</div>
<div className={b('block')}>{point.name}</div>
<div className={b('block')}>{percentage}</div>
<div className={b('block')}>{value}</div>
</div>
);
}

export const getTooltipRenderer = (widgetData: ChartKitWidgetData): TooltipRenderer | undefined => {
const seriesTypes = (widgetData?.series?.data || []).map((s) => s.type);

Expand All @@ -105,6 +129,10 @@ export const getTooltipRenderer = (widgetData: ChartKitWidgetData): TooltipRende
return (data: Parameters<TooltipRenderer>[0]) =>
treemapTooltipRenderer(widgetData, data);
}
case 'pie': {
return (data: Parameters<TooltipRenderer>[0]) =>
pieTooltipRenderer(widgetData, data);
}
default: {
return undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ async function unwrapMarkup(args: {config: Widget['config']; data: Widget['data'

try {
unwrapItem(get(data, 'graphs', []));
unwrapItem(get(data, 'series.data', []));
unwrapItem(get(data, 'categories', []));
} catch (e) {
console.error(e);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4447f3b

Please sign in to comment.