8

I'm using recharts to create pie charts. When look at this example, http://recharts.org/#/en-US/examples/PieResponsiveContainer the pie chart can be centered using ResponsiveContainer component.

But my attempt failed : https://codesandbox.io/s/ll68p643xl. Try to change the size of the viewport to a larger width, the pie isn't center anymore. I wonder what's wrong here.

Cecilia Chan
  • 679
  • 2
  • 8
  • 17

2 Answers2

3

ResponsiveContainer just makes the pie take the size of its container. If you inspect the html, you will see that the container is in fact the size of its parent. If you want to center it, you can make the container not take the whole parent, and use css. Something like this:

.pie-row .pie-wrap > div {
    background: red;
    margin: 0 auto;
}

And then the container:

<ResponsiveContainer className="container" height={70} width='30%'>
Mario F
  • 45,569
  • 6
  • 37
  • 38
2

I know this is a late answer, but for anyone who is trying to center <PieChart> with width and height, pass cx prop to half the width of <PieChart>.

Like,

  <PieChart width={this.props.width} height={this.props.height}>
    <Pie
      cx={this.props.width / 2}
      cy={200}
      label
      outerRadius={this.props.pieData.radius}
      data={this.props.pieData.data}
      dataKey={this.props.pieData.dataKey}>
    </Pie>
  </PieChart>
Kaung Myat Lwin
  • 1,049
  • 1
  • 9
  • 23