There is a d3.mouse(this) statement in the dragged() function. This returns the x and y coordinates of the current event relative to the specified Container (ref). Your container for mouse(<container>) is this and means the dragged <g> the element. But it should be the <svg>. Therefore you should use d3.mouse(svg.node()) instead.
But be careful, since you haven't taken the offset inside the <g> into account (it's hard to describe). Maybe you have to add the coordinates of the cursor position inside the <g>.
IMHO I would anyway prefere to use d3.event over d3.mouse example.
Edit: New drag functions with correct coordinates
// New: Coordinates inside <g>
var coordG = 0;
function dragstarted(d) {
// New: Read out coordinates inside <g>
coordG = d3.mouse(this)[0];
d3.select(this).raise().classed("active", true);
}
function dragged(d) {
d3.select(this).attr("transform", function(d,i){
var coordinates = d3.mouse(svg.node());
var mx = coordinates[0] - coordG;
console.log( coordG);
return "translate(" + [mx,0] + ")";
});
}
function dragended(d) {
d3.select(this).classed("active", false);
}