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

recharts responsive pie chart with legend #4487

Open
MiguelG97 opened this issue May 5, 2024 · 2 comments
Open

recharts responsive pie chart with legend #4487

MiguelG97 opened this issue May 5, 2024 · 2 comments
Labels
enhancement Enhancement to a current API

Comments

@MiguelG97
Copy link

I have a problem when showing the Pie and the legend. Apparently, if the legend is to wide, it tends to remove space from the pie chart. Shouldn't the responsive container calculate the values to set an appropriate overall width??

<div
    className="flex flex-row justify-between items-center
  h-full w-full"
  >
    <ResponsiveContainer
    >
      <PieChart>
        <Pie
          data={questionnaire?.chartData}
          paddingAngle={5}
          label
          nameKey={"name"}
          dataKey={"value"}
          innerRadius={60}
          outerRadius={80}
        >
          {questionnaire?.chartData.map(
            (x, index) => (
              <Cell
                stroke="0"
                key={`cell-${index}`}
                fill={`${COLORS[index]}`}
              />
            )
          )}
        </Pie>
        <Legend
          layout="vertical"
          align="right"
          verticalAlign="middle"
        />
        <Tooltip />
      </PieChart>
    </ResponsiveContainer>
  </div>

image

@ckifer
Copy link
Member

ckifer commented May 5, 2024

Nothing magic in ResponsiveContainer - if your content is too large it will begin to overlap. SVGs require absolute positioning and so make things like true responsiveness, text wrapping, etc. difficult.

Edit pie-chart-of-straight-angle (forked)

So yeah this could be an enhancement, but in general this is how recharts has worked for a long time.

Potential enhancements: collision detection, making legend relatively positioned (this would introduce regressions, probably not the right thing to do), scaling on resize (this also maybe unwanted behavior depending on the use-case)

A lot of things to consider in order to get to a place where this is nicer unfortunately

@ckifer ckifer added the enhancement Enhancement to a current API label May 5, 2024
@MiguelG97
Copy link
Author

Great, thanks for the clarifications!

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
Projects
None yet
Development

No branches or pull requests

2 participants