Giter Site home page Giter Site logo

Comments (5)

incraigulous avatar incraigulous commented on June 11, 2024 8

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>
    )
}

from react-smooth.

pl12133 avatar pl12133 commented on June 11, 2024 1

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.

from react-smooth.

ckifer avatar ckifer commented on June 11, 2024 1

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

from react-smooth.

nightwolf-041 avatar nightwolf-041 commented on June 11, 2024

any updates ?

from react-smooth.

semoal avatar semoal commented on June 11, 2024

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

from react-smooth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.