1👍
✅
Add the following lines of code inside your forEach
loop, that will take care of the dataIndex
updating issue :
var isDatasetVisible = chartInstance.controller.isDatasetVisible(i);
if (!isDatasetVisible) return;
Here is the working example on JSFiddle
Source:stackexchange.com