Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Giving Heading to Legends. #2904

Open
Ishansinghlamba opened this issue Jul 17, 2022 · 5 comments
Open

Giving Heading to Legends. #2904

Ishansinghlamba opened this issue Jul 17, 2022 · 5 comments
Labels
enhancement Enhancement to a current API feature request Issues that are feature requests

Comments

@Ishansinghlamba
Copy link

Ishansinghlamba commented Jul 17, 2022

I want to add heading to the legends. Like I want to add the heading Score.
Screenshot 2022-07-18 at 2 19 07 AM

@TooMuchLagWillKillYou
Copy link

TooMuchLagWillKillYou commented Aug 8, 2022

You can render a custom Legend using the content prop in the Legend component. You can pass it a function (or a component, read the docs for more info) which will render the Legend.

This is an example:

const CustomLegend = (props: any) => {
    const { payload } = props;

    return (
      <>
        <p style={{ fontSize: "2rem", fontWeight: 800 }}>Scale</p>
        <ul>
          {payload.map((entry: any, index: number) => (
            <li key={index}>{entry.value}</li>
          ))}
        </ul>
      </>
    );
  };

You can find the full chart here: https://codesandbox.io/s/customized-legend-event-forked-b32o5t

@ckifer ckifer added enhancement Enhancement to a current API feature request Issues that are feature requests labels Jan 3, 2023
@ckifer
Copy link
Member

ckifer commented Jan 3, 2023

This issue has an answer, but providing the ability to give a legend header may be a nice idea in general.

Labeling a feature request for now to come back to eventually.

@bhatt40
Copy link

bhatt40 commented Jul 10, 2023

@ckifer Is there a way to access the default legend content function from within my custom legend content function? I want to add my header to the legend but I want the rest of the content to match the default legend. Something like:

return (
  <>
    <p>Header</p>
    {defaultContent(props)}
  </>
);

@ckifer
Copy link
Member

ckifer commented Jul 10, 2023

@bhatt40 yeah, DefaultLegendContent is exported now - https://github.com/recharts/recharts/blob/master/src/index.ts#L12

return (
  <>
    <p>Header</p>
    <DefaultLegendContent {...props} />
  </>
);

@bhatt40
Copy link

bhatt40 commented Jul 10, 2023

@ckifer Got it. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement to a current API feature request Issues that are feature requests
Projects
None yet
Development

No branches or pull requests

4 participants