Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Charts: legends ensure user provided margin prop takes precedence
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ const BarChartInternal: FC< BarChartProps > = ( {
...defaultMargin,
...margin,
...( showLegend && legendPosition === 'top'
? { top: ( defaultMargin.top || 0 ) + legendHeight }
? { top: ( margin?.top ?? ( defaultMargin.top || 0 ) ) + legendHeight }
: {} ),
} }
xScale={ chartOptions.xScale }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,4 +677,52 @@ describe( 'BarChart', () => {
).toBeInTheDocument();
} );
} );

describe( 'Margin with Legend', () => {
test( 'respects user-provided margin.top when legend is positioned at top', () => {
const customMargin = { top: 50, right: 20, bottom: 20, left: 20 };
renderWithTheme( {
showLegend: true,
legendPosition: 'top',
margin: customMargin,
} );

// Verify chart renders properly with both legend at top and custom margin
const chart = screen.getByTestId( 'bar-chart' );
expect( chart ).toBeInTheDocument();

// Verify legend is visible
expect( screen.getByText( 'Series A' ) ).toBeInTheDocument();
} );

test( 'uses default margin.top when no custom margin provided and legend is at top', () => {
renderWithTheme( {
showLegend: true,
legendPosition: 'top',
} );

// Chart should still render with default margin plus legend height
const chart = screen.getByTestId( 'bar-chart' );
expect( chart ).toBeInTheDocument();

// Verify legend is visible
expect( screen.getByText( 'Series A' ) ).toBeInTheDocument();
} );

test( 'respects custom margin when legend is at bottom', () => {
const customMargin = { top: 50, right: 20, bottom: 20, left: 20 };
renderWithTheme( {
showLegend: true,
legendPosition: 'bottom',
margin: customMargin,
} );

// When legend is at bottom, custom margin.top should be used as-is
const chart = screen.getByTestId( 'bar-chart' );
expect( chart ).toBeInTheDocument();

// Verify legend is visible
expect( screen.getByText( 'Series A' ) ).toBeInTheDocument();
} );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ const LineChartInternal = forwardRef< SingleChartRef, LineChartProps >(
...defaultMargin,
...margin,
...( showLegend && legendPosition === 'top'
? { top: ( defaultMargin.top || 0 ) + legendHeight }
? { top: ( margin?.top ?? ( defaultMargin.top || 0 ) ) + legendHeight }
: {} ),
} }
// xScale and yScale could be set in Axis as well, but they are `scale` props there.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1209,4 +1209,52 @@ describe( 'LineChart', () => {
} );
} );
} );

describe( 'Margin with Legend', () => {
test( 'respects user-provided margin.top when legend is positioned at top', () => {
const customMargin = { top: 50, right: 20, bottom: 20, left: 20 };
renderWithTheme( {
showLegend: true,
legendPosition: 'top',
margin: customMargin,
} );

// Verify chart renders properly with both legend at top and custom margin
const chart = screen.getByTestId( 'line-chart' );
expect( chart ).toBeInTheDocument();

// Verify legend is visible
expect( screen.getByText( 'Series A' ) ).toBeInTheDocument();
} );

test( 'uses default margin.top when no custom margin provided and legend is at top', () => {
renderWithTheme( {
showLegend: true,
legendPosition: 'top',
} );

// Chart should still render with default margin plus legend height
const chart = screen.getByTestId( 'line-chart' );
expect( chart ).toBeInTheDocument();

// Verify legend is visible
expect( screen.getByText( 'Series A' ) ).toBeInTheDocument();
} );

test( 'respects custom margin when legend is at bottom', () => {
const customMargin = { top: 50, right: 20, bottom: 20, left: 20 };
renderWithTheme( {
showLegend: true,
legendPosition: 'bottom',
margin: customMargin,
} );

// When legend is at bottom, custom margin.top should be used as-is
const chart = screen.getByTestId( 'line-chart' );
expect( chart ).toBeInTheDocument();

// Verify legend is visible
expect( screen.getByText( 'Series A' ) ).toBeInTheDocument();
} );
} );
} );
Loading