0

I'm using a visualization library React-vis and have found that while a line graph or chart can shrink and grow normally when using the FlexibleXYPlot component, once it's placed in a table cell with a width of 40% of the row it's in, it grows but does not shrink back down when I resize the window. I was wondering how I can get the shrinking behavior that I get outside a table in a table. The code is here, which demonstrates the two contexts: https://codesandbox.io/s/wk6kpxl3jk

The below is not my graph per se, but shows the same behavior that I'm getting and would like to avoid. enter image description here

reactor
  • 1,722
  • 1
  • 14
  • 34

3 Answers3

0

Try using vw instead of %. Like this

.class {
    width: 100vw
}

You might need to use a smaller number than 100 though

Chris
  • 85
  • 1
  • 4
  • 16
0

I do it with

import ReactResizeDetector from 'react-resize-detector';

...

const handleResize = () => {
   const element = window.document.querySelector('.mainWrapper');
   this.setState({ width: element.offsetWidth });
}

...

<ReactResizeDetector handleWidth handleHeight onResize={handleResize} />

Then add the width as prop to your chart component.

szokrika
  • 41
  • 2
  • 8
-1

Probably this is a better solution. There is actual responsive support in the library:

import {
  FlexibleXYPlot,
  FlexibleWidthXYPlot,
  FlexibleHeightXYPlot
} from 'react-vis';

FlexibleWidthXYPlot modifies XYPlot so that it no longer requires a width, since it will take all the with in its container div. Likewise, FlexibleHeightXYPlot modifies XYPlot so that is no longer requires a height, and its height will be that of its container div. Finally, FlexibleXYPlot modifies XYPlot so that it no longer requires either a width and a height, its dimensions will be that of its container.

These components can be used exactly as XYPlot:

<FlexibleWidthXYPlot height={100}>
  <VerticalBarSeries data={data} />
</FlexibleWidthXYPlot>

<FlexibleHeightXYPlot width={100}>
  <VerticalBarSeries data={data} />
</FlexibleHeightXYPlot>

<FlexibleXYPlot>
  <VerticalBarSeries data={data} />
</FlexibleXYPlot>

Read more:

https://github.com/uber/react-vis/blob/master/docs/flexible-plots.md

szokrika
  • 41
  • 2
  • 8
  • 3
    Please don't just post some tool or library as an answer. At least demonstrate [how it solves the problem](http://meta.stackoverflow.com/a/251605) in the answer itself. – Matthew Jun 10 '19 at 14:18
  • 1
    I believe I'm using it as you described in the original post and am unsure what changes you're suggesting I make to it based on your post – reactor Jul 22 '19 at 20:47