getYAxisValues function

List<double> getYAxisValues(
  1. List<double> numbers
)

Returns a list of Y-axis values based on the given list of numbers.

This function takes a list of integers as input and generates a list of Y-axis values for a graph. The Y-axis values start from 0 and increment by a divider value up to the maximum number in the input list. The divider value is dynamically calculated based on the maximum number in the input list. If the maximum number is not a multiple of the divider, it is also included in the Y-axis values.

The function handles empty lists by returning a list with a single element, 0. If the generated Y-axis values list is empty, the function also returns a list with a single element, 0.

Example:

getYAxisValues([10, 20, 30, 40, 50]);  // Returns: [0, 10, 20, 30, 40, 50]
getYAxisValues([]);  // Returns: [0]
getYAxisValues([5]);  // Returns: [0, 1, 2, 3, 4, 5]
getYAxisValues([-100, -400, -533]); Returns [0];
getYAxisValues([-100, -400, -533]); Returns [0];
getYAxisValues([2000,5333,1003,3400]); Returns [0, 1000, 2000, 3000, 4000, 5000, 5333];

@param numbers The list of integers for which to generate Y-axis values. @return A list of integers representing the Y-axis values. TODO: Improve it to work with negative numbers. TODO: Improve it to work with fractions in 0-1.

Implementation

List<double> getYAxisValues(List<double> numbers) {
  if(numbers.isEmpty) return [0];

  double maxNumber = numbers.reduce((curr, next) => curr > next ? curr : next);
  int divider = 1;

  while (maxNumber ~/ divider > 10) {
    divider *= 10;
  }

  List<double> yAxisValues = [];
  for (double i = 0; i <= maxNumber; i += divider) {
    yAxisValues.add(i);
  }

  if (yAxisValues.isNotEmpty && yAxisValues.last != maxNumber) {
    yAxisValues.add(maxNumber);
  }

  if(yAxisValues.isEmpty) return [0];

  return yAxisValues;
}