Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to conditionally render label to avoid labels overlapping in recharts?

I'm having an issue similar to this using Recharts library with NextJS.

I'm using a ComposedChart that renders Bar and Line components.

However, the Labels are overlapping on each other if the value is not big enough between them.

Bar chart

Is there any way we can conditionally render out the Label inside LabelList if the data's value is higher than a specific value? (Ex: Hide the Label when the data's value is lower than 5)

like image 453
KYin Avatar asked Oct 26 '25 02:10

KYin


1 Answers

You can use formatter property to control the content that gets displayed and simply return null if you want to hide your label:

<LabelList
    dataKey={yourKey}
    fill={fill}
    position="top"
    formatter={label => {                                              
        return label > 5 ? label : null;
    }}
/>
like image 93
mickl Avatar answered Oct 27 '25 15:10

mickl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!