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

Width is calculated from the container element but height not

Open mosheKenshoo opened this issue 9 years ago • 14 comments

Hi @kirjs Thank you for a great component :)

As the title implies i am having a problem with the chart height. In highchart documentation it says that if width and height are not set in the config, then the chart takes the parent container size.

In the example here It works well on pure highchart http://jsfiddle.net/o1pnxfnv/

But when i use react-highchart, the height seems fixed and does not stretch to the parent div height. Example (can be edited thorugh gist if clicking on the button on the top-right) http://output.jsbin.com/gobukah

Any idea?

mosheKenshoo avatar Dec 13 '16 14:12 mosheKenshoo

@omerKenshoo @omriavigdor @charliehavak

mosheKenshoo avatar Dec 13 '16 14:12 mosheKenshoo

@mosheKenshoo I noticed this as well and was going to file a bug report when I got around to it. Another example of height not adjusting - https://jsfiddle.net/ktppqv0g/1/.

In the meantime, I use the following for a work around.

const parentHeight = <get via https://github.com/STRML/react-resizable or similar lib>;
const config = {
  chart: {
    ...
    height: parentHeight
  }
  ...
};
<ReactHighstock config={config} />

JakeHP avatar Dec 13 '16 23:12 JakeHP

@Jakehp thanks!

mosheKenshoo avatar Dec 14 '16 07:12 mosheKenshoo

This has to do with the offsetHeight of a block element inside a block:

<div style="height: 200px">
  <div class="highcharts-root">
    <div class="highcharts-container"><!-- rendered by highcharts --></div>
  </div>
</div>

The offsetHeight of .highcharts-root is 0, so according to the documentation, chart.height gets automatically set to 400px.

Note that automatic resize on container resize is outside the scope of highcharts.

STRML avatar Dec 30 '16 22:12 STRML

Feel free to reopen (also if anyone wants to send a docs PR, would be great, maybe just mention height and link here?

kirjs avatar Mar 18 '17 16:03 kirjs

does somebody can give me a solution for this problem? can't solve it...

rawbknalb avatar May 02 '17 21:05 rawbknalb

I added an event listener on page resize that updates a height local slice of state which is used by my ReactHighStock component to determine the height. This solved the problem for me.

` componentDidMount() { window.addEventListener("resize", this.updateDimensions); this.updateDimensions(); }

updateDimensions() {
    let height = this.getDims();
    this.setState({
        height: height
    });
}

getDims() {
    let navBarHeight = document.querySelector('.navbar-container').clientHeight;
    let height = window.innerHeight - navBarHeight;
    return(height);
}`

Jstuff36 avatar Sep 24 '17 18:09 Jstuff36

@STRML How to get the onResize event on GridItem as mentioned in STRML/ResizableBox

 onResize = (event, {element, size}) => {
    this.setState({width: size.width, height: size.height});
  };

I am expecting this event in my code so, that I can re flow the highcharts in child component.

<ResponsiveReactGridLayout
                    breakpoints={{lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0}}
                    cols={{lg: 18, md: 10, sm: 6, xs: 4, xxs: 2}}
                    rowHeight={20}
                    autoSize={false}
                    draggableCancel="input,textarea"
                    useCSSTransforms={true}
                    // onLayoutChange={this.onLayoutChange}
                    // onBreakpointChange={this.onBreakpointChange}
                    containerPadding={[5, 5]}
                    margin={[10, 10]}
                    {...this.props}
                >
                {_.map((this.dashboardStore.currentDashboard.layout), el => this.createElement(el))}
                </ResponsiveReactGridLayout>

In child component of I use,

                   <div className="visualTile" ref={(tileContainerEl) => this.tileContainerEl = tileContainerEl} >
                            {/* <pre>{JSON.stringify(this.props.grid, null, 1)}</pre> */}
                            <ReactHighcharts
                                config={config}
                                callback={this.getChart}
                            />
                        </div>
componentDidMount() {
        var height = this.tileContainerEl.clientHeight; 
        var width = this.tileContainerEl.clientWidth; 
        this.chart.setSize(width, height);
        this.chart.reflow();
    }

Thank you.

abelkbil avatar Mar 01 '18 10:03 abelkbil

Hey @abelkbil , How did you prevent your "ResponsiveReactGridLayout" to trigger "onLayoutChange" when user click on grid?

nywooz avatar Mar 09 '18 16:03 nywooz

@nywooz Click will not trigger, I confuse your click might have made some changes to click either by css or any.

abelkbil avatar Mar 23 '18 04:03 abelkbil

Anyone can give me a solution for this problem? can't solve it!

wsYdd avatar Oct 30 '18 03:10 wsYdd

I finally find a solution with the help of react-sizeme library.

  1. npm install react-sizeme See: https://github.com/ctrlplusb/react-sizeme
  2. Make a wrapper component ReactHighstockWrapper.
import cloneDeep from 'lodash/cloneDeep';
import React, { PureComponent } from 'react';
import { withSize } from 'react-sizeme';
import ReactHighstock from 'react-highcharts/ReactHighstock.src';

class ReactHighstockWrapper extends PureComponent {
  render() {
    const {
      size: { width, height },
      config,
      ...otherProps
    } = this.props;
    let newConfig = cloneDeep(config);
    newConfig.chart = {
      width,
      height,
    };

    return (
      <div style={{ height: '100%' }}>
        <ReactHighstock config={newConfig} {...otherProps} />
      </div>
    );
  }
}
  1. ReactHighstockWrapper component will automatically extend to 100% height of its container's div.

huangzhuolin avatar Nov 19 '18 06:11 huangzhuolin

@huangzhuolin very thanks

Carrie999 avatar Dec 24 '18 07:12 Carrie999

solution is Config.chart = { width:'200', height:'200' }; changed hight

Carrie999 avatar Dec 24 '18 08:12 Carrie999