1👍
This is my understanding of the whole process after diving into the code base quite a bit. I’ve tried to be as detailed as possible with links to the exact line of code I am talking about. Hope this helps:
-
Beginning with the code snippet you shared:
export const Radar = /* #__PURE__ */ createTypedChart('radar', RadarController);
-
If you follow the RadarController via the import statement, you see that it is fetched from
chart.js
-
Now we move to the Chart.js code and look for this controller RadarController. It is found in a file called src/controllers/controller.radar.js.
-
Within that file, you see an update function
-
This function then calls updateElements with the points information
-
This function gets the new point position which is then set in properties and passed into the updateElement function
-
This updateElement function directly takes us to the core.datasetController
-
Here you see a condition to check if the chart is in directUpdateMode. If not, it calls a function to _resolveAnimations
-
Within this function, you will see the new Animations(args) object
-
This eventually brings us to the core.animations file which consists of all the animation related information and processing.
-
One interesting bit I found here was: this is what seems to be making the beautiful movement of points to the changed location.
-
You can explore this Animations class further for more detailed understanding
So yeah essentially, it is the js part under the hood that facilitates the smooth transitions and this is how it does it. React code is essentially just like a wrapper of Chart.js calling this update method with the new values.
2👍
As you explained the canvas element does not get changed so it gets reused. To animate the chart chart.js itself has an update method. React-chartjs-2 uses a useeffect function that checks if the data you pass it has changed. If this is the case it calls the update function from chart.js itself and they handle the animations and updates itself:
useEffect(() => {
if (!chartRef.current) return;
if (redraw) {
destroyChart();
setTimeout(renderChart);
} else {
chartRef.current.update();
}
}, [redraw, options, data.labels, data.datasets]);
1👍
You can see here: https://github.com/reactchartjs/react-chartjs-2/blob/master/src/chart.tsx
The react-chartjs-2 library creates a component that adds a canvas and when the props update the component creates/updates an internal Chart object that uses the rendered canvas.
From what I saw the animation starts when the props are changed.
The path is props->react-chartjs-2 component->chart object->animation