react-google-charts icon indicating copy to clipboard operation
react-google-charts copied to clipboard

Chart height does not change dynamically

Open mvgian opened this issue 4 years ago • 4 comments

I have requirement to change a chart height dynamically based on parent div's size. I've used a boolean isHalf to determine if the chart height should be at 50% or 90%. The initial value for isHalf is true, therefore the chart component initially renders with height 50%. However when isHalf is changed to false, the height remains at 50% when the value is passed to the component as 90%

const report = { chart_configuration: {
   height:  isHalf ? "50%" : "90%";
}}
<Chart
     chartType={report.chart_configuration.type}
     loader={<div>Loading Chart</div>}
    data={report.chart_configuration.data}
    options={report.chart_configuration.options}
    width={report.chart_configuration.width}
    height={report.chart_configuration.height}
    legendToggle
/>

mvgian avatar Nov 22 '20 08:11 mvgian

I have found this issue too, very bad experience

YazhouMiao avatar Dec 03 '20 03:12 YazhouMiao

This creates 4 divs around the SVG (using the gantt chart as an example.) If the height is updated in the javascript, the height of the SVG is updated, but the height on the outermost two are not. That is causing some headaches for me.

  • div height style (original)px
  • div height style (original)px
  • div height style (updated)px
  • div height style 100%
  • svg height attr (updated)

I did find a way around this by wrapping the component in another div with a class name to get surgical with some CSS tweaks. SCSS:

.cl_chart_container { & > div { height: auto !important; & > .cl_chart { height: auto !important; } } }

Not ideal but it helps.

stgermaniac avatar Jan 28 '22 23:01 stgermaniac

I found the root cause https://github.com/rakannimer/react-google-charts/blob/master/src/components/GoogleChart.tsx#L219

shouldComponentUpdate(nextProps: Props, nextState: State) {
    return (
      this.state.isReady !== nextState.isReady ||
      nextProps.controls !== this.props.controls
    );
  }

the component will only re-render if controls prop is changed

jtg-gg avatar Feb 09 '22 07:02 jtg-gg

I found a "good" workaround

<div style={{height:dynamicHeight}}>
  <Chart
    ...
    height="100%"
    options={{
      chartArea:{height:dynamicHeight}
    }}
  />
</div>

jtg-gg avatar Feb 10 '22 02:02 jtg-gg