Skip to content

Instantly share code, notes, and snippets.

@fiftin
Created December 27, 2016 03:19
Show Gist options
  • Save fiftin/b0a217b5e4e8e8d1018ac7175358262e to your computer and use it in GitHub Desktop.
Save fiftin/b0a217b5e4e8e8d1018ac7175358262e to your computer and use it in GitHub Desktop.
Best ticks for Y axis
function bestTick(largest, mostticks) {
var minimum = largest / mostticks;
var magnitude = Math.pow(10, Math.floor(Math.log(minimum) / Math.log(10)));
var residual = minimum / magnitude;
var tick;
if (residual > 5) {
tick = 10 * magnitude;
} else if (residual > 2) {
tick = 5 * magnitude;
} else if (residual > 1) {
tick = 2 * magnitude;
} else {
tick = magnitude;
}
return tick;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment