0

I want to create a component where the size of the chart automatically adjusts with resize events of the container holding the graph. It would be great if someone can help me with an example. Here si the link to the library. react-rnd library

E.x somehting like this but using highcharts codebox react-rnd with googlecharts

const Highchart = () => {
 
return (

<rnd>
<div>
<HightchartsReact highcharts={Highcharts} constructorType={'chart'} options={option} /> 
</div>
</rnd>
)
}
Ibra
  • 912
  • 1
  • 12
  • 31

1 Answers1

1

You need to call chart.reflow method in onResizeStop event callback function.

return (
  <Rnd
    ...
    onResizeStop={(e, direction, ref, delta, position) => {
      const chart = this.chartComponent.current?.chart;
      if (chart) {
        chart.reflow();
      }
      ...
    }}
  >
    ...
  </Rnd>
);

From Highcharts API:

reflow( [e])

Reflows the chart to its container. By default, the chart reflows automatically to its container following a window.resize event, as per the chart.reflow option. However, there are no reliable events for div resize, so if the container is resized without a window resize event, this must be called explicitly.


Live demo: https://codesandbox.io/s/cranky-hoover-cz-czw5k?file=/src/index.js

API Reference: https://api.highcharts.com/class-reference/Highcharts.Chart#reflow

ppotaczek
  • 36,341
  • 2
  • 14
  • 24