Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Responsive Container it is very slow when resizing page #1767

Open
leorigon opened this issue Jun 7, 2019 · 38 comments
Open

Responsive Container it is very slow when resizing page #1767

leorigon opened this issue Jun 7, 2019 · 38 comments
Labels
bug General bug label

Comments

@leorigon
Copy link

leorigon commented Jun 7, 2019

Do you want to request a feature or report a bug?

Seems like a bug.

What is the current behavior?

When I resize the page, the animation caused by ResponsiveContainer is very slow creating an horizontal overflow. This is propagated for all components in the page environment. I have a page with 4 charts like this:

  <ResponsiveContainer width='99%' minHeight={ 275 }>
        <BarChart
            { ...chartProps }
            data={ props.values }>
            <XAxis
                { ...axisProps }
                hide
            />
            <YAxis
                { ...axisProps }
                interval={ 0 }
                dataKey='date'
                type='category'
                tick={ {
                    ...chartTextProps,
                    fontSize: 10
                } }
            />
            <Tooltip
                separator={ '' }
                formatter={ formatLabelTooltip }
                labelFormatter={ formatDateTooltip }
            />
            <Bar
                dataKey='value'
                fill={ GREEN_LABEL }
                maxBarSize={ 12 }
                fontSize={ 10 } >
                <LabelList
                    dataKey='value'
                    position='right'
                />
            </Bar>
        </BarChart>
    </ResponsiveContainer>

What is the expected behavior?

Resize more fast or not too slow.

Which versions of Recharts, and which browser / OS are affected by this issue? Did this work in previous versions of Recharts?

Recharts "1.6.2":
Ubuntu - Chrome 73.0.3683.103 (have same behaviour on all the OSs and browsers)

@stolaar
Copy link

stolaar commented Jul 22, 2019

I have the same problem. Please any feedback

@benlester
Copy link

I do experience this, for what it's worth it only seems to be an issue when decreasing in size, increase in size works as expected.

@aksperiod
Copy link

Same here

@ghost
Copy link

ghost commented Aug 24, 2019

I have the same issue. When I resize the window. all animation is very slow not smoothly. any idea to fix it :(((

@chrisw90uk
Copy link

+1

@kalbert312
Copy link

kalbert312 commented Oct 30, 2019

Also facing this.
EDIT: I can always reproduce it if at the element containing the ResponsiveContainer shrinks due to another element growing in size (ex. one flex item in a single-row flex container changing width)

@BaldBit
Copy link

BaldBit commented Nov 7, 2019

+1 I'm also experiencing the same issue. Appreciate if someone can help me.

@kalbert312
Copy link

I made a code sandbox that sort of highlights it: https://codesandbox.io/s/gifted-elion-x3ilm?fontsize=14 It might just be a general performance problem if there's more than a few charts on the page. When the chart space grows, it is smooth. When it shrinks, it's not smooth unless there's only a couple charts. It happens even with a debounce value on the responsive containers. It would likely be noticeable on something like a dashboard.

@pchisholm
Copy link

+1

@lukas1994
Copy link

Any updates? Is there a way to only disable resize animations but keep the others?

@cbaker
Copy link

cbaker commented Feb 3, 2020

+1

@stolaar
Copy link

stolaar commented Feb 4, 2020

In my case, this was happening where there was a lot of data in the graphs. I'm representing the graphs in a flex box container, so I just added width: 0 property to the flex item and it solved the problem.

@eli9000
Copy link

eli9000 commented Feb 14, 2020

@stolaar can you please elaborate a bit on your solution? Perhaps share your code? Much appreciated!

@stolaar
Copy link

stolaar commented Feb 14, 2020

Sorry for my unclear solution above. So this was the solution in my case.
I have the following graphs:

JSX:

<div className="dashboard>
   <div className="chart-container">
      <ResponsiveContainer width="99%" aspect={2}>
        {/* Graph components */ }
      </ResponsiveContainer>
    </div>
{  /* 5 more graphs here */ }
</div>

CSS:

.dashboard {
display: flex;
justify-content: space-evenly;
}

.chart-container {
flex: 0 1 500px;
width: 0;
}

width: 0 on the chart-container solves my problem.

@m3h0w
Copy link

m3h0w commented Feb 19, 2020

Having the same issue: trying to have a drawer component that forces the graphs to stretch and shrink. @stolaar's solution doesn't fix it for me.

@VacaSan
Copy link

VacaSan commented Mar 13, 2020

this happens because responsive container has width: 100%, and its child, that is svg element, has fixed width, equal to containers width in px. that means the containers actual width is equal to its child width. so when you resize down, container doesn't change dimensions, and resize observer callback never gets called.

my workaround is to position absolute ResponsiveContainer inside a 'responsive' div, and use that as a replacement.

import { ResponsiveContainer } from 'recharts';

function CustomResponsiveContainer(props) {
  return (
    <div style={{ width: '100%', height: '100%', position: 'relative' }}>
      <div
        style={{
          width: '100%',
          height: '100%',
          position: 'absolute',
          top: 0,
          left: 0
        }}
      >
        <ResponsiveContainer {...props} />
      </div>
    </div>
  );
}

@xile611 xile611 closed this as completed Mar 17, 2020
@lukas1994
Copy link

@VacaSan your fix made things slightly better but I'm still getting these laggy animations when resizing the window: https://share.getcloudapp.com/NQuD98GY

Any ideas on how to fix this? Is there a way to disable animations when resizing the window?

@felipe-perestrelo
Copy link

I had the same problem. I fixed it by adding "overflow-y: hidden;" to the ResponsiveContainer container.

My code is something similar to:

<div className="chart-wrapper">
  <p className="chart-title">
    Chart Title
  </p>
  <div className="recharts-container">
    <ResponsiveContainer>
      <AllOfRechartsCode />
    </ResponsiveContainer>
  </div>
</div>

With CSS:

.chart-wrapper {
  display: flex;
  flex-direction: column;
}

.recharts-container {
  flex-grow: 1;
  overflow-y: hidden;
}

I have a few similar components that use that same structure. Before I got to fix it, I had all of them with flex-wrap: wrap; instead of flex-direction: column; on .chart-wrapper. None of them had the overflow-y setting.

Then I changed flex-wrap to flex-direction, and just one of them started working properly. By comparing it to others, the only relevant difference was the overflow-y setting. So I just tested on the others components and they all worked.

I don't quite understand why that worked, though. Based on VacaSan's response, saying that observer callback never gets called on down resizing, I'd guess that for every resize step overflow would get recalculated or something, thus delaying a proper resize.

But I'm not even sure if that makes much sense. I'm not a very experienced developer, it was quite a struck of luck.

Anyways... Hope this helps.

@chuckponzi
Copy link

I see that this has been closed but I have yet to be able to remedy this issue in my app. I've tried every suggested css approach and nothing speeds up the size reduction animation. Where has this been conclusively resolved?

@carlocuevas
Copy link

I see that this has been closed but I have yet to be able to remedy this issue in my app. I've tried every suggested css approach and nothing speeds up the size reduction animation. Where has this been conclusively resolved?

I've encountered this issue maybe using debounce props on ResponsiveContainer will resolve the lag transition on the charts

@dayyad
Copy link

dayyad commented Nov 8, 2021

Hey all, I had been struggling with the same problem for ages. I used the '99%' width trick and it helped take maybe the edge off but with large data sets its still a very noticeable problem.
I highly suggest using a hook similar to this one I made

import { useState, useEffect, useCallback } from 'react';
import lodash from 'lodash';
// A custom hook that detects if the window is currently being resized
// Returns a single value 'isBeingResized'
// the method is debounced to 200ms after which it returns to being false.

export const useIsResizing = () => {
  const [isBeingResized, setIsBeingResized] = useState(false);

  const reset = useCallback(
    lodash.debounce(() => {
      setIsBeingResized(false);
    }, 200),
    [],
  );

  useEffect(() => {
    const handleResize = () => {
      setIsBeingResized(true);
      reset();
    };

    window.addEventListener('resize', handleResize);

    return () => {
      window.removeEventListener('resize', handleResize);
    };
  }, [setIsBeingResized]);

  return isBeingResized;
};

Then where ever you are rendering the graph simply remove it from the DOM while isBeingResized is true or show some placeholder text / component. This allows the layout to still resize in real time then the graph kind of pops back in when resizing ends.

Hope this helps someone!

@Zyntos
Copy link

Zyntos commented Jul 28, 2022

I see that this issue has been closed, even though it has not been fixed yet, and there are only workarounds stated. Currently in our application, I used AutoSizer instead of the responsive Container, but this should not be the solution for a highly used Framework like this, as its also not fixing the issue 100%

@BeHustle
Copy link

Using flex-basis instead of width will help solve the problem

@ckifer
Copy link
Member

ckifer commented Jun 14, 2023

Comments give me visibility into these closed issues, thanks. Reopening.

@ckifer ckifer reopened this Jun 14, 2023
@hywook4
Copy link

hywook4 commented Aug 18, 2023

I was suffering from similar issue and found that there's "debounce" props in ResponsiveContainer.

Try using the debounce props.

@ablondel19
Copy link

ablondel19 commented Aug 19, 2023

I'm not really proud of that solution but there it is haha, i'm pausing with a spinner to give the impression that it's not a bug and the resizing is as fast as usual. (DashboardBox is the component that wraps all the responsive containers i'm using)

I hope this helps.

import { Box } from "@mui/material";
import { styled } from "@mui/system";
import Spinner from "./Spinner";
import { useEffect, useState } from "react";

const DashboardBox = styled(Box)(({ theme }) => ({
  backgroundColor: theme.palette.background.light,
  borderRadius: "1rem",
  boxShadow: "0.15rem 0.2rem 0.15rem 0.1rem rgba(0, 0, 0, 0.8)",
}));

const ResizableBox = ({ children, gridArea }) => {
  const [isResizing, setIsResizing] = useState(false);

  let resizeTimeout;

  const handleResizeStart = () => {
    clearTimeout(resizeTimeout);
    setIsResizing(!isResizing);
  };

  const handleResizeEnd = () => {
    resizeTimeout = setTimeout(() => {
      setIsResizing(false);
    }, 150);
  };

  useEffect(() => {
    window.addEventListener("resize", handleResizeStart);
    window.addEventListener("resize", handleResizeEnd);

    return () => {
      window.removeEventListener("resize", handleResizeStart);
      window.removeEventListener("resize", handleResizeEnd);
    };
  }, []);

  return (
    <DashboardBox gridArea={gridArea}>
      {isResizing ? <Spinner /> : children}
    </DashboardBox>
  );
};

export default ResizableBox;

@mamo-muj
Copy link

Hey everyone, so what's the ideal solution here? I've been trying to figure it out but no luck 🙏

@ckifer
Copy link
Member

ckifer commented Oct 26, 2023

We've refactored responsive container for the next version which is directly using resize detector. The debounce prop is there and maybe next release we'll add a sane default

@mamo-muj
Copy link

@ckifer would you know what specific version has this update? I ask because I'm on version 2.0.6 and when I upgraded to 2.9.0, it throws me this error (something to do with the tsconfig.json)

So maybe if I update it to a specific version then hopefully I won't face this issue

image

@ckifer
Copy link
Member

ckifer commented Oct 27, 2023

@mamo-muj can you create a separate issue for this? (please include webpack/bundler config, package.json, etc.) victory-vendor was added in 2.3 (but this issue we're in isn't fixed anywhere yet)

@mamo-muj
Copy link

sure @ckifer thank you. Also, do we have an ETA on when the next release will be out?

@ckifer
Copy link
Member

ckifer commented Oct 27, 2023

Just released 2 weeks ago - there are a few small patches to get out sooner, but otherwise another 2-4 weeks

@mamo-muj
Copy link

Got it and the 2.9.0 version doesn't solve the slowness when resizing correct? That'll be in the next version?

@ckifer
Copy link
Member

ckifer commented Oct 27, 2023

Right - hopefully some sort of improvement will be in yeah - the refactoring alone didn't solve anything but this PR might #3908

ckifer pushed a commit that referenced this issue Nov 1, 2023
<!--- Provide a general summary of your changes in the Title above -->

## Description

rendering improvement

This is a follow-up PR for #3908.

## Related Issue

#3908
#1767

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an
issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps
to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

## How Has This Been Tested?

<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran
to -->
<!--- see how your change affects other areas of the code, etc. -->

## Screenshots (if appropriate):

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->

- [x] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have added tests to cover my changes.
- [ ] I have added a storybook story or extended an existing story to
show my changes
- [x] All new and existing tests passed.
GMer78 pushed a commit to GMer78/recharts-1 that referenced this issue Nov 24, 2023
<!--- Provide a general summary of your changes in the Title above -->

## Description

rendering improvement

This is a follow-up PR for recharts#3908.

## Related Issue

recharts#3908
recharts#1767

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an
issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps
to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

## How Has This Been Tested?

<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran
to -->
<!--- see how your change affects other areas of the code, etc. -->

## Screenshots (if appropriate):

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->

- [x] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have added tests to cover my changes.
- [ ] I have added a storybook story or extended an existing story to
show my changes
- [x] All new and existing tests passed.
@levii96-rafa
Copy link

to fix this problem just add the debounce prop to the responsive container

@TaiDoTMA
Copy link

TaiDoTMA commented Feb 28, 2024

I only use debounce={number} for fixed

<ResponsiveContainer debounce={200} width={width} height={height}>
    <Chart here/>
 </ResponsiveContainer>

@ArthurRuxton-DY
Copy link

ArthurRuxton-DY commented Sep 13, 2024

Hi all, if you're still experiencing this issue I recommend the following steps before resorting to removing charts from the dom whilst resizing. This solution is not perfect but it does significantly reduce the sluggish visual effect.

  • upgrade to recharts latest version (for me this was 2.12.7
  • give your ResponsiveContainer's the debounce prop: debounce={num}
  • play with num to tune the delay, for me the best I could get was 200.

@ckifer
Copy link
Member

ckifer commented Sep 13, 2024

If someone can make a reproduction that contains the issue I'm willing to take another stab at this, but I think there are too many scenarios for it to be perfect.

We need absolute height and width to feed the svg no matter what. That is expensive and means potentially slow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug General bug label
Projects
None yet
Development

No branches or pull requests