react-chartjs icon indicating copy to clipboard operation
react-chartjs copied to clipboard

Chart height creeps up as window is resized

Open paulkarpenko opened this issue 9 years ago • 8 comments

Hi, I love the chart, but I'm seeing an issue with responsiveness. I've got <ChartJS.Line data={data} options={options} height={100} /> with responsive: true, maintainAspectRatio: false and though the chart renders fine and resizes with the window, as the window is resized, the height of the chart creeps higher and higher. If I resize the window back and forth in place, the chart eventually grows to the height of the screen.

paulkarpenko avatar Apr 07 '16 00:04 paulkarpenko

Hi, I have noticed this behaviour too and it's quite annoying, for the time being I "solved" it by setting in options:

responsive: false, maintainAspectRatio: true

It doesn't really solve the issue, but at least it's better than having the chart change its size for every window resize.

RamonBeast avatar Apr 11 '16 15:04 RamonBeast

@jhudson8 Can you provide any insight here? Thanks.

paulkarpenko avatar Apr 11 '16 17:04 paulkarpenko

Setting display: block to the canvas fixes this issue as well.

ashwinurao avatar May 15 '16 16:05 ashwinurao

yea, this is pretty brutal, and the display block doesn't seem to work for me :\

jtmkrueger avatar Jun 13 '18 17:06 jtmkrueger

So I figured out what I needed to do here. Just wrap the ChartComponent in an surrounding div and give it the styles you want.

              <div className="chart-container">
                <ChartComponent
                  type='horizontalBar'
                  data={this.state.chartData}
                  height={14}
                  options={options}
                />
              </div>

then in your css file:

  .chart-container {
    position: relative;
    height:90px;
    width:100%;
  }

This seemed to get rid of all my issues! The key was setting the position: relative; on the surrounding div.

jtmkrueger avatar Jun 13 '18 17:06 jtmkrueger

Having the same issue, any news on this?

aslaugsollilja avatar May 15 '19 17:05 aslaugsollilja

Wrapping the chart around <div> worked for me. Also set

options=
{
maintainAspectRatio:false,
responsive:true
}

And then set the height={200} :smile:

YashMeh avatar May 30 '19 04:05 YashMeh

I created a wrapper div with

  height: 400px;
  width:100%;
  position: relative;

and I set

responsive: true,
maintainAspectRatio: false

however, the chart only expands in width. If I increase the width of the browser window, the chart width increases as well. If I decrease the width, the chart stays the same.

Any ideas?

armandn avatar Jun 21 '19 16:06 armandn