Well the problem was the poinstcontainer was not getting anything in path so from var pointscontainer = figurecontainer.querySelector(".scatterlayer .trace:last-of-type g");
I changed to this var pointscontainer = figurecontainer.querySelector(".scatterlayer .trace:last-of-type .points");
and its working fine now.