mirror of
https://github.com/apache/superset.git
synced 2026-04-25 19:14:27 +00:00
feat(echarts-pie): add string template support for labels (#28774)
This commit is contained in:
@@ -123,11 +123,31 @@ const config: ControlPanelConfig = {
|
||||
['key_percent', t('Category and Percentage')],
|
||||
['key_value_percent', t('Category, Value and Percentage')],
|
||||
['value_percent', t('Value and Percentage')],
|
||||
['template', t('Template')],
|
||||
],
|
||||
description: t('What should be shown on the label?'),
|
||||
},
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
name: 'label_template',
|
||||
config: {
|
||||
type: 'TextControl',
|
||||
label: t('Label Template'),
|
||||
renderTrigger: true,
|
||||
description: t(
|
||||
'Format data labels. ' +
|
||||
'Use variables: {name}, {value}, {percent}. ' +
|
||||
'\\n represents a new line. ' +
|
||||
'ECharts compatibility:\n' +
|
||||
'{a} (series), {b} (name), {c} (value), {d} (percentage)',
|
||||
),
|
||||
visibility: ({ controls }: ControlPanelsContainerProps) =>
|
||||
controls?.label_type?.value === 'template',
|
||||
},
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
name: 'number_format',
|
||||
|
||||
@@ -143,6 +143,7 @@ export default function transformProps(
|
||||
labelsOutside,
|
||||
labelLine,
|
||||
labelType,
|
||||
labelTemplate,
|
||||
legendMargin,
|
||||
legendOrientation,
|
||||
legendType,
|
||||
@@ -242,6 +243,38 @@ export default function transformProps(
|
||||
{},
|
||||
);
|
||||
|
||||
const formatTemplate = (
|
||||
template: string,
|
||||
formattedParams: {
|
||||
name: string;
|
||||
value: string;
|
||||
percent: string;
|
||||
},
|
||||
rawParams: CallbackDataParams,
|
||||
) => {
|
||||
// This function supports two forms of template variables:
|
||||
// 1. {name}, {value}, {percent}, for values formatted by number formatter.
|
||||
// 2. {a}, {b}, {c}, {d}, compatible with ECharts formatter.
|
||||
//
|
||||
// \n is supported to represent a new line.
|
||||
|
||||
const items = {
|
||||
'{name}': formattedParams.name,
|
||||
'{value}': formattedParams.value,
|
||||
'{percent}': formattedParams.percent,
|
||||
'{a}': rawParams.seriesName || '',
|
||||
'{b}': rawParams.name,
|
||||
'{c}': `${rawParams.value}`,
|
||||
'{d}': `${rawParams.percent}`,
|
||||
'\\n': '\n',
|
||||
};
|
||||
|
||||
return Object.entries(items).reduce(
|
||||
(acc, [key, value]) => acc.replaceAll(key, value),
|
||||
template,
|
||||
);
|
||||
};
|
||||
|
||||
const formatter = (params: CallbackDataParams) => {
|
||||
const [name, formattedValue, formattedPercent] = parseParams({
|
||||
params,
|
||||
@@ -262,6 +295,19 @@ export default function transformProps(
|
||||
return `${name}: ${formattedPercent}`;
|
||||
case EchartsPieLabelType.ValuePercent:
|
||||
return `${formattedValue} (${formattedPercent})`;
|
||||
case EchartsPieLabelType.Template:
|
||||
if (!labelTemplate) {
|
||||
return '';
|
||||
}
|
||||
return formatTemplate(
|
||||
labelTemplate,
|
||||
{
|
||||
name,
|
||||
value: formattedValue,
|
||||
percent: formattedPercent,
|
||||
},
|
||||
params,
|
||||
);
|
||||
default:
|
||||
return name;
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ export type EchartsPieFormData = QueryFormData &
|
||||
innerRadius: number;
|
||||
labelLine: boolean;
|
||||
labelType: EchartsPieLabelType;
|
||||
labelTemplate: string | null;
|
||||
labelsOutside: boolean;
|
||||
metric?: string;
|
||||
outerRadius: number;
|
||||
@@ -56,6 +57,7 @@ export enum EchartsPieLabelType {
|
||||
KeyPercent = 'key_percent',
|
||||
KeyValuePercent = 'key_value_percent',
|
||||
ValuePercent = 'value_percent',
|
||||
Template = 'template',
|
||||
}
|
||||
|
||||
export interface EchartsPieChartProps
|
||||
|
||||
Reference in New Issue
Block a user