question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chart component resizes width but not height when parent is resized

See original GitHub issue

I’ve tried every hack and stacktrace article offering a solution - none work so far. I’ve really got to figure out this showstopping problem 😦

I have a “Card” component which gets added into a draggable dashboard-style layout component (react-grid-layout). “CardPanel” is my wrapper around the responsive grad layout parent. The Card can resize nicely in the CardPanel parent. Into the Card I insert my “BarChart” (which is a wrapper class around “HorizontalBar”.

Here is how the card is added to the CardPanel…

          <CardPanel initialLayouts={initialLayouts}>
            <Card key="1" close>
              <BarChart fsInfo={fsInfo} style={{ width: '100%', height: '100%' }} />
            </Card>
            <Card key="2">
              <span className="text">2</span>
            </Card>
              ... etc etc
          </CardPanel>

Here is how the CardPanel wraps it’s Card children (to satisfy the react-grid-layout requirements and make the cards resizable and draggable)…

    <ResponsiveReactGridLayout
      className="layout"
      compactType="vertical"
      breakpoints={{ lg: 1200, md: 800, sm: 680, xs: 480, xxs: 0 }}
      cols={{ lg: 12, md: 10, sm: 6, xs: 4, xxs: 2 }}
      rowHeight={30}
      layouts={this.state.layouts}
      draggableHandle=".drag-handle"
      onLayoutChange={(layout, layouts) => this.onLayoutChange(layout, layouts)}
    >
      {
        React.Children.map(this.props.children, (child) => {
          if (!child.key) return;
          return (
            <div key={child.key} className={this.props.className} style={this.props.style}>
              {child}
            </div>
          );
        })
      }
    </ResponsiveReactGridLayout>

Here is how “Card” wraps it’s children (in this case, a BarChart)…

  <div id={id}>
    <div className="drag-handle">
      <hr />
      <hr />
    </div>
    {children}
    {close &&
    <i
      className={'close-button fa fa-fw fa-times-circle'}
      onClick={this.onClose}
      aria-hidden="true"
    />}
  </div>

…and finally, here is how BarChart wraps the ChartJS component…

  <div className="barChart" >
    <HorizontalBar data={data} options={options} />
  </div>

Here is the ChartJS settings of the bar chart (notice that maintainAspectRatio is false)…

const options = {
  legend: { display: true },
  scales: {
    xAxes: [{
      ticks: { min: 0,  max: 100, },
      stacked: true,
    }],
    yAxes: [{ barPercentage: 0.6, stacked: true, }],
  },
  elements: {
    rectangle: {
      borderWidth: 4,
      borderColor: 'rgb(255, 255, 0)',
    },
  },
  responsive: true,
  **maintainAspectRatio: false**,
};

The before and after screenshots show that the bar charts stretches it’s width nicely, but not the height. Also notice another Card has a simple < span > which resizes perfectly in both width and height.

Before: before

After: after

Friends, I am really stumped. I don’t want to have to go hunting for another Charts package, I like this one! Plz help me get past this tricky problem!

Thanks, /Mark

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14

github_iconTop GitHub Comments

5reactions
AdrianoMouracommented, Jul 4, 2018

What helped me was to add the option maintainAspectRatio and set as false

<Line data={data} options={{
 maintainAspectRatio: false
}}/>

And in my case I also need to add the redraw as true

<Line data={data} redraw={true} options={{
 maintainAspectRatio: false
}}/>
0reactions
Lucasgoh-cdgcommented, Feb 25, 2022

What helped me was to add the option maintainAspectRatio and set as false

<Line data={data} options={{
 maintainAspectRatio: false
}}/>

And in my case I also need to add the redraw as true

<Line data={data} redraw={true} options={{
 maintainAspectRatio: false
}}/>

Hi, even though I am using this, I am still having the same problem, what should I do ya? Thanks in advance

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chart component resizes width but not height when parent is ...
The Card can resize nicely in the CardPanel parent. Into the Card I insert my "BarChart" (which is a wrapper class around "HorizontalBar"....
Read more >
Responsive Charts - Chart.js
When it comes to changing the chart size based on the window size, a major limitation is that the canvas render size (...
Read more >
Chart not getting resize on parent <div> width change
Hi Team, Today we encountered issue with chart reflow when chart container changed in width. Please find the below attached image.
Read more >
Highcharts: Chart does not resize properly by making the ...
The problem is, I created a JSFiddle here, "running" the code if the output/result window is small, I see the 50%:50%. Resizing the...
Read more >
Resize issue with custom Chart component (SDK)
Thn the chart gets rendered (with 100% height and width) but it can't size properly since the underlaying container is not yet visible....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found