minMaxScale function

List<double> minMaxScale(
  1. List<num> values, {
  2. double low = 0.0,
  3. double high = 1.0,
})

Min-max scale to low, high. Returns new list.

Implementation

List<double> minMaxScale(List<num> values, {double low = 0.0, double high = 1.0}) {
  if (values.isEmpty) return <double>[];
  final doubles = values.map((num x) => x.toDouble()).toList();
  final double minV = doubles.fold(double.infinity, (a, b) => a < b ? a : b);
  final double maxV = doubles.fold(double.negativeInfinity, (a, b) => a > b ? a : b);
  // All values equal: the (x-min)/(max-min) ratio would divide by zero, so map
  // every element to the midpoint of the target range instead.
  if (maxV == minV) return List.filled(values.length, (low + high) / 2);
  return values.map((num x) => low + (x.toDouble() - minV) / (maxV - minV) * (high - low)).toList();
}