Skip to content

Commit 66b68be

Browse files
authored
fix(BarChart/ColumnChart): display correct labels in stacked charts (#568)
1 parent 8acd221 commit 66b68be

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

packages/charts/src/components/BarChart/BarChart.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
BarChart as BarChartLib,
1414
Brush,
1515
CartesianGrid,
16+
LabelList,
1617
Legend,
1718
ReferenceLine,
1819
Tooltip,
@@ -163,6 +164,9 @@ const BarChart: FC<BarChartProps> = forwardRef((props: BarChartProps, ref: Ref<a
163164
const [width, legendPosition] = useLongestYAxisLabelBar(dataset, dimensions);
164165
const marginChart = useChartMargin(chartConfig.margin, chartConfig.zoomingTool);
165166
const [xAxisHeight] = useObserveXAxisHeights(chartRef, 1);
167+
const valueAccessor = (attribute) => ({ payload }) => {
168+
return payload[attribute];
169+
};
166170

167171
return (
168172
<ChartContainer
@@ -225,15 +229,19 @@ const BarChart: FC<BarChartProps> = forwardRef((props: BarChartProps, ref: Ref<a
225229
key={element.accessor}
226230
name={element.label ?? element.accessor}
227231
strokeOpacity={element.opacity}
228-
label={<ChartDataLabel config={element} chartType="bar" position="insideRight" />}
229232
type="monotone"
230233
dataKey={element.accessor}
231234
fill={element.color ?? `var(--sapChart_OrderedColor_${(index % 11) + 1})`}
232235
stroke={element.color ?? `var(--sapChart_OrderedColor_${(index % 11) + 1})`}
233236
barSize={element.width}
234237
onClick={onDataPointClickInternal}
235238
isAnimationActive={noAnimation === false}
236-
/>
239+
>
240+
<LabelList
241+
valueAccessor={valueAccessor(element.accessor)}
242+
content={<ChartDataLabel config={element} chartType="bar" position={'insideRight'} />}
243+
/>
244+
</Bar>
237245
);
238246
})}
239247
{!noLegend && (

packages/charts/src/components/ColumnChart/ColumnChart.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
BarChart as ColumnChartLib,
1414
Brush,
1515
CartesianGrid,
16+
LabelList,
1617
Legend,
1718
ReferenceLine,
1819
Tooltip,
@@ -168,6 +169,9 @@ const ColumnChart: FC<ColumnChartProps> = forwardRef((props: ColumnChartProps, r
168169

169170
const marginChart = useChartMargin(chartConfig.margin, chartConfig.zoomingTool);
170171
const xAxisHeights = useObserveXAxisHeights(chartRef, props.dimensions.length);
172+
const valueAccessor = (attribute) => ({ payload }) => {
173+
return payload[attribute];
174+
};
171175

172176
return (
173177
<ChartContainer
@@ -235,15 +239,19 @@ const ColumnChart: FC<ColumnChartProps> = forwardRef((props: ColumnChartProps, r
235239
key={element.accessor}
236240
name={element.label ?? element.accessor}
237241
strokeOpacity={element.opacity}
238-
label={<ChartDataLabel config={element} chartType="column" position={'insideTop'} />}
239242
type="monotone"
240243
dataKey={element.accessor}
241244
fill={element.color ?? `var(--sapChart_OrderedColor_${(index % 11) + 1})`}
242245
stroke={element.color ?? `var(--sapChart_OrderedColor_${(index % 11) + 1})`}
243246
barSize={element.width}
244247
onClick={onDataPointClickInternal}
245248
isAnimationActive={noAnimation === false}
246-
/>
249+
>
250+
<LabelList
251+
valueAccessor={valueAccessor(element.accessor)}
252+
content={<ChartDataLabel config={element} chartType="column" position={'insideTop'} />}
253+
/>
254+
</Column>
247255
);
248256
})}
249257
{!noLegend && (

0 commit comments

Comments
 (0)