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

Causes chart to prematurely stop rendering #44

Open
avindra opened this issue Jan 14, 2021 · 5 comments
Open

Causes chart to prematurely stop rendering #44

avindra opened this issue Jan 14, 2021 · 5 comments

Comments

@avindra
Copy link

avindra commented Jan 14, 2021

There are some indications from users that Animations from react-smooth are causing the graph to render partially / in an incomplete state. Users have been using isAnimationActive={false} to disable animations to workaround the bug. See recharts issues:

recharts/recharts#1426
recharts/recharts#1821

@incraigulous
Copy link

incraigulous commented Oct 14, 2021

The issue seems to be related to the frequency in which your parent component needs to rerender, or possibly how often your data array reference changes. The workaround seems to be to do two things:

  • Force your chart to rerender when your parent component re-renders
  • Ensure your parent component does not rerender during the animation if possible. For me, I found that a parent component issue was causing my chart to rerender too often. That fix is not shown here.

I set a unique key on the Chart or ResponsiveContainer that forces the chart to rerender when the parent component rerenders. This can lead to issues with your chart reanimating on each rerender, which may not be your desired effect. Here is how I worked that out. Note that I'm turning off animations after a timeout to prevent duplicate animations after the key change. You would think that I could simply turn the animation off at onAnimationEnd but that callback seems to be called before the animation actually ends for some reason. It's also possible that your component will never actually finish the animation if this bug does happen to occur during the animation. I had better luck with the following approach:

const RingChart = ({data, colors, angle = 140}) => {
    let [animate, setAnimate] = useState(true)
    const onAnimationStart = useCallback(() => {
        setTimeout(() => {
            setAnimate(false)
        }, 2000)
    }, [])

    return (<div>
            <div  ref={ref} className={"absolute w-grid-7 h-grid-1 l-grid-7 t-grid-7"}/>
            <ResponsiveContainer key={uniqid()}>
                <PieChart>
                    <Pie
                        isAnimationActive={animate}
                        data={data}
                        startAngle={angle}
                        endAngle={angle + 360}
                        cx={'50%'}
                        cy={'50%'}
                        outerRadius={'70%'}
                        innerRadius={'47%'}
                        fill="#8884d8"
                        dataKey="value"
                        paddingAngle={0}
                        labelLine={<LabelLine/>}
                        label={<Label/>}
                        onAnimationStart={onAnimationStart}
                    >
                        {data.map((entry, index) => (
                            <Cell key={`cell-${index}`} fill={colors[index % colors.length]} stroke="none"/>
                        ))}
                    </Pie>
                </PieChart>
            </ResponsiveContainer>
        </div>
    )
}

@nightwolf-041
Copy link

any updates ?

@pl12133
Copy link

pl12133 commented Dec 21, 2021

It's worth noting that when the Animate component unmounts, it does not call onAnimationEnd. This makes it impossible for recharts to clean up any outstanding animations when Animate unmounts.

Perhaps there could be an onAnimationStopped prop to address cases where the AnimateManager is stopped abruptly.

@semoal
Copy link

semoal commented Dec 22, 2021

We would love a contribution from your side @pl12133 , calling the onAnimationEnd on unmount

@ckifer
Copy link
Member

ckifer commented Aug 29, 2023

Merged #80 which calls onAnimationEnd on unmount - this should solve part of this

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

No branches or pull requests

6 participants