change from d3.v2 to d3.v3

Also
replace d3.first by d3.min
replace d3.last by d3.max