Chart.js icon indicating copy to clipboard operation
Chart.js copied to clipboard

Apply render optimizations when animation duration is 0

Open alex-statsig opened this issue 1 year ago • 3 comments

Feature Proposal

My goal is to allow the same performance optimization that are applied when animations are disabled (i.e. with options.animation = false) or when using mode="none" (i.e. chart.update('none')) to animations with a duration of 0 (i.e. options.animation.duration = 0).

Example context

Sometimes its desirable to only enable animations for certain situations. For example, you may want to generally disable most animations upon updates, but keep around animations for things like zooming (where the animation is more important to preserve context). As suggested in the docs, you can accomplish this with a setup like this:

const options = {
  animations: { duration: 0 },
  transitions: { zoom: { duration: 500 } },
}

Note that you cannot set options.animations = false or else all animations are disabled (including zoom).

In this situation, any chart update with a mode other than 'zoom' can equivalently run with mode='none' since the animations are disabled via duration=0. However, currently chartjs will still use the animation-based render, preventing certain performance optimizations in updaters (such as dataset direct update and line controller direct update). These performance optimizations can significantly speed up renders for large datasets (ex. 200ms -> 30ms).

Possible Implementation

One options could be to detect in chart.update() if an update mode effectively has its animations disabled (via duration=0). In this event, mode could automatically be remapped to 'none' so downstream updates don't need to change their logic (ex. dataset and line controller both already support optimizing mode='none').

alex-statsig avatar Dec 15 '24 01:12 alex-statsig

@LeeLenaleee I saw you were contributing more actively to chartjs lately if you have any thoughts here. I'd be happy to help contribute for this improvement, but would like some guidance / validation

alex-statsig avatar Jan 07 '25 22:01 alex-statsig

It sounds like a good idea, but I don't know this part well enough to help you any further on it.

LeeLenaleee avatar Jan 08 '25 10:01 LeeLenaleee

@alex-statsig it might be as simple as updating https://github.com/chartjs/Chart.js/blob/master/src/core/core.controller.js#L498 but it would require some testing for sure

etimberg avatar Jan 08 '25 13:01 etimberg