From c23b3791a059b1e7609645b9657a8e51841fa756 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 89d3a9cde91..2ffdd072d3e 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/next/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 { IChartMeasure } from '../../interfaces/IChartMeasure'; import { IPolarChartConfig } from '../../interfaces/IPolarChartConfig'; @@ -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 &&