From e49eddc4761661f59e4b7850d36b44dfc104e732 Mon Sep 17 00:00:00 2001 From: Marcus Notheis Date: Tue, 19 May 2020 09:12:21 +0200 Subject: [PATCH] fix(Pie/DonutChart): respect measure formatter and custom data label (#525) --- .../src/components/PieChart/PieChart.tsx | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/packages/charts/src/components/PieChart/PieChart.tsx b/packages/charts/src/components/PieChart/PieChart.tsx index 50ec0cd550c..b65c0ec1b38 100644 --- a/packages/charts/src/components/PieChart/PieChart.tsx +++ b/packages/charts/src/components/PieChart/PieChart.tsx @@ -3,8 +3,8 @@ import { enrichEventWithDetails } from '@ui5/webcomponents-react-base/lib/Utils' import { ChartContainer } from '@ui5/webcomponents-react-charts/lib/components/ChartContainer'; import { PieChartPlaceholder } from '@ui5/webcomponents-react-charts/lib/PieChartPlaceholder'; import { useLegendItemClick } from '@ui5/webcomponents-react-charts/lib/useLegendItemClick'; -import React, { CSSProperties, FC, forwardRef, Ref, useCallback, useMemo } from 'react'; -import { Cell, Label, Legend, Pie, PieChart as PieChartLib, Tooltip } from 'recharts'; +import React, { CSSProperties, FC, forwardRef, Ref, useCallback, useMemo, isValidElement, cloneElement } from 'react'; +import { Cell, Label, Legend, Pie, PieChart as PieChartLib, Tooltip, Text } from 'recharts'; import { getValueByDataKey } from 'recharts/lib/util/ChartUtils'; import { IChartBaseProps } from '../../interfaces/IChartBaseProps'; import { IChartMeasure } from '../../interfaces/IChartMeasure'; @@ -101,14 +101,22 @@ const PieChart: FC = forwardRef((props: PieChartProps, ref: Ref { - if (measure.hideDataLabel) return null; - return { - position: 'outside', - content: measure.DataLabel, - formatter: measure.formatter - }; - }, [measure]); + const dataLabel = useCallback( + (props) => { + if (measure.hideDataLabel) return null; + + if (isValidElement(measure.DataLabel)) { + return cloneElement(measure.DataLabel, { ...props, config: measure }); + } + + return ( + + {measure.formatter(props.value)} + + ); + }, + [measure] + ); const tooltipValueFormatter = useCallback((value) => measure.formatter(value), [measure.formatter]); const chartRef = useConsolidatedRef(ref); @@ -158,7 +166,8 @@ const PieChart: FC = forwardRef((props: PieChartProps, ref: Ref {centerLabel && } {dataset &&