-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
129 additions
and
82 deletions.
There are no files selected for viewing
160 changes: 79 additions & 81 deletions
160
src/server/modes/charts/plugins/datalens/preparers/pie/d3.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
-1.8 KB
(94%)
...isualizations/pie/basic.test.ts/Wizard-Pie-chart-Auto-coloring-screenshot-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-16.6 KB
(71%)
...isualizations/pie/basic.test.ts/Wizard-Pie-chart-Auto-coloring-screenshot-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.