From 8610ff163ebc3c2b6c31b4c9910797382d1f9c84 Mon Sep 17 00:00:00 2001 From: Jose Quintas Date: Fri, 3 May 2024 17:37:46 +0200 Subject: [PATCH] Adjust min/max size --- .../x-charts/src/BarChart/BarElementLabel.tsx | 18 +++++++++++++----- packages/x-charts/src/BarChart/BarPlot.tsx | 10 ++++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/x-charts/src/BarChart/BarElementLabel.tsx b/packages/x-charts/src/BarChart/BarElementLabel.tsx index f53429e8a0bbc..283102364e8a1 100644 --- a/packages/x-charts/src/BarChart/BarElementLabel.tsx +++ b/packages/x-charts/src/BarChart/BarElementLabel.tsx @@ -56,13 +56,14 @@ export const BarElementLabelRoot = styled(animated.text, { opacity: (ownerState.isFaded && 0.3) || 1, textAnchor: 'middle', textAlign: 'center', - dominantBaseline: 'middle', + dominantBaseline: 'central', pointerEvents: 'none', })); export type BarLabelProps = Omit, 'id'> & { - highlightScope?: Partial; ownerState: BarElementLabelOwnerState; + minWidth?: number; + minHeight?: number; }; export interface BarElementLabelSlots { @@ -78,8 +79,7 @@ export interface BarElementLabelSlotProps { } export type BarElementLabelProps = Omit & - Pick & { - barLabel?: Partial; + Pick & { /** * The props used for each component slot. * @default {} @@ -91,6 +91,10 @@ export type BarElementLabelProps = Omit; + height?: number; + width?: number; + layout?: 'vertical' | 'horizontal'; }; function BarElementLabel(props: BarElementLabelProps) { @@ -104,6 +108,10 @@ function BarElementLabel(props: BarElementLabelProps) { labelText, slots, slotProps, + minHeight, + minWidth, + height, + width, ...other } = props; const { item } = React.useContext(InteractionContext); @@ -139,7 +147,7 @@ function BarElementLabel(props: BarElementLabelProps) { ownerState, }); - if (!labelText) { + if (!labelText || (height ?? 0) < (minHeight ?? 0) || (width ?? 0) < (minWidth ?? 0)) { return null; } diff --git a/packages/x-charts/src/BarChart/BarPlot.tsx b/packages/x-charts/src/BarChart/BarPlot.tsx index 7102c0e253880..3155b2c368e2c 100644 --- a/packages/x-charts/src/BarChart/BarPlot.tsx +++ b/packages/x-charts/src/BarChart/BarPlot.tsx @@ -66,7 +66,7 @@ export interface BarPlotProps extends BarPlotSlotProps { ) => void; } -interface CompletedBarData { +export interface CompletedBarData { seriesId: SeriesId; dataIndex: number; layout: BarSeriesType['layout']; @@ -284,17 +284,19 @@ function BarPlot(props: BarPlotProps) { style={style} /> ))} - {barLabelTransition((style, { seriesId, dataIndex, color, value }) => ( + {barLabelTransition((style, { seriesId, dataIndex, color, value, width, height }) => ( (x ?? 0) + width / 2), - y: to([(style as any).y, (style as any).height], (x, width) => (x ?? 0) + width / 2), + x: to([(style as any).x, (style as any).width], (x, w) => (x ?? 0) + w / 2), + y: to([(style as any).y, (style as any).height], (y, w) => (y ?? 0) + w / 2), } as any } labelText={value ? value.toString() : null}