function resolveYLabelsForValues()

in src/YAxisLabels.js [22:75]


function resolveYLabelsForValues(
  scale,
  values,
  formats = [],
  style,
  force = true,
) {
  // given a set of Y-values to label, and a list of formatters to try,
  // find the first formatter that produces a set of labels which are distinct
  // since we currently do not support rotated axis value labels,
  // we do not check if they fit on the axis (unlike X labels), since all Y labels will have the same height
  // returns the formatter and the generated labels

  let labels;
  const attempts = [];
  const goodFormat = formats.find(format => {
    const testLabels = values.map((value, i) =>
      MeasuredValueLabel.getLabel({
        value,
        format,
        style: defaults(
          getValue(style.labelStyle, { value }, i),
          style.defaultStyle,
        ),
      }),
    );

    const areLabelsDistinct = checkLabelsDistinct(testLabels);
    if (!areLabelsDistinct) {
      attempts.push({ labels: testLabels, format, areLabelsDistinct });
      return false;
    }

    labels = testLabels;
    return true;
  });

  if (!isUndefined(goodFormat)) {
    // found labels which work, return them
    return {
      labels,
      format: goodFormat,
      areLabelsDistinct: true,
      collisionCount: 0,
    };
  }
  // none of the sets of labels are good
  // if we're not forced to decide, return all the labels we tried (let someone else decide)
  if (!force) return { attempts };

  // forced to decide, choose the least bad option
  // super bad, we don't have any label sets with distinct labels. return the last attempt.
  return last(attempts);
}