niceNumber function

double niceNumber(
  1. double x, {
  2. bool round = false,
})

Nice number calculation for axis ticks.

Implementation

double niceNumber(double x, {bool round = false}) {
  if (x == 0) return 0;

  final sign = x < 0 ? -1 : 1;
  x = x.abs();

  final exp = (x == 0 ? 0 : (math.log(x.abs()) / math.log(10.0)).floor());
  final f = x / _pow10(exp);

  double nf;
  if (round) {
    if (f < 1.5) {
      nf = 1;
    } else if (f < 3) {
      nf = 2;
    } else if (f < 7) {
      nf = 5;
    } else {
      nf = 10;
    }
  } else {
    if (f <= 1) {
      nf = 1;
    } else if (f <= 2) {
      nf = 2;
    } else if (f <= 5) {
      nf = 5;
    } else {
      nf = 10;
    }
  }

  return sign * nf * _pow10(exp);
}