copying whole function from nvd3 source