diff --git a/packages/@mantine/charts/src/BarChart/BarChart.tsx b/packages/@mantine/charts/src/BarChart/BarChart.tsx index a06b2a47848..2883c6f6160 100644 --- a/packages/@mantine/charts/src/BarChart/BarChart.tsx +++ b/packages/@mantine/charts/src/BarChart/BarChart.tsx @@ -55,9 +55,9 @@ export type BarChartCssVariables = { export interface BarChartProps extends BoxProps, - GridChartBaseProps, - StylesApiProps, - ElementProps<'div'> { + GridChartBaseProps, + StylesApiProps, + ElementProps<'div'> { /** Data used to display chart. */ data: Record[]; @@ -81,8 +81,8 @@ export interface BarChartProps /** Props passed down to recharts `Bar` component */ barProps?: - | ((series: BarChartSeries) => Partial>) - | Partial>; + | ((series: BarChartSeries) => Partial>) + | Partial>; /** Determines whether a label with bar value should be displayed on top of each bar, incompatible with `type="stacked"` and `type="percent"`, `false` by default */ withBarValueLabel?: boolean; @@ -130,10 +130,23 @@ export function BarLabel({ parentViewBox, ...others }: Record) { + + function labelCoordinates(): { dx: number, dy: number } { + let coord: { dx: number, dy: number } = { dx: 0, dy: 0 } + if (others.props.h < 300) { + coord = { ...coord, dx: Math.ceil(others?.width + 10) } + } else { + coord.dx = Math.ceil(others?.width - others?.viewBox?.x / 2) + } + coord = { ...coord, dy: Math.ceil(others.props.h / 15) } + return coord; + } + return ( ((_props, ref) => { fillOpacity={dimmed ? 0.1 : fillOpacity} strokeOpacity={dimmed ? 0.2 : 0} stackId={stacked ? 'stack' : item.stackId || undefined} - label={withBarValueLabel ? : undefined} + label={withBarValueLabel ? : undefined} yAxisId={item.yAxisId || 'left'} minPointSize={minBarSize} {...(typeof barProps === 'function' ? barProps(item) : barProps)}