function buildDefaultCoordinates()

in client/src/components/special/timeline-chart/renderer/coordinates/index.js [238:354]


function buildDefaultCoordinates (options) {
  const {
    axis,
    valueShift = 0,
    stepPixelSize = 40,
    minimumStepPixelSize = 5.0,
    includeStart = true,
    includeEnd = true,
    includeZero = true
  } = options;
  if (!axis || !axis.initialized || axis.pixelsSize <= 0) {
    return [];
  }
  const result = [];
  const {
    getLabelSize,
    releaseContext
  } = getLabelSizer(options);
  const createConfig = (base) => {
    const step = 10 ** base;
    const roundBasePow = base - 2;
    const negative = roundBasePow < 0;
    const roundBase = negative ? (10 ** Math.abs(roundBasePow)) : (10 ** roundBasePow);
    const round = (value) => negative
      ? (Math.round(value * roundBase) / roundBase)
      : (Math.round(value / roundBase) * roundBase);
    return {
      step,
      base,
      roundBase,
      getLabel: (value) => `${round(value)}`,
      getNextStep: (currentStep, variation = 1) => round(currentStep + step * variation),
      getNearest: (value) => round(value),
      variations: [1, 1.25, 2, 2.5, 5]
        .sort((a, b) => a - b)
        .map((variation) => ({
          variation,
          stepPxSize: axis.getPixelSizeForValueSize(step * variation)
        })),
      stepPxSize: axis.getPixelSizeForValueSize(step),
      labelSize: getLabelSize(`${step}`)
    };
  };
  const minValue = axis.correctActualValue(axis.from);
  const maxValue = axis.correctActualValue(axis.to);
  const getBase = (value) => Math.floor(Math.log10(Math.abs(value) || 10));
  let base = getBase(maxValue);
  let mainConfig = createConfig(base);
  while (mainConfig.stepPxSize > minimumStepPixelSize) {
    base -= 1;
    mainConfig = createConfig(base);
  }
  mainConfig = createConfig(base + 1);
  mainConfig.main = true;
  base -= 1;
  const smallConfig = createConfig(base);
  const addTick = (tickValue, tickOptions) => {
    const {
      config,
      start = false,
      end = false
    } = tickOptions || {};
    if (
      (!start && !end && tickValue - valueShift < minValue) ||
      result.find((tick) => tick.value === tickValue - valueShift)
    ) {
      return;
    }
    const {
      main = start || end,
      getLabel = ((o) => `${o}`)
    } = config || {};
    let label = getLabel(tickValue);
    const size = getLabelSize(label);
    result.push({
      value: tickValue - valueShift,
      main,
      start,
      end,
      label,
      size
    });
  };
  if (includeStart) {
    addTick(minValue + valueShift, {start: true});
  }
  if (includeEnd) {
    addTick(maxValue + valueShift, {end: true});
  }
  if (includeZero) {
    addTick(0, {main: true});
  }
  const iterateWithConfig = (config) => {
    if (config) {
      const {
        variation,
        stepPxSize
      } = config.variations
        .find((aVariation) => aVariation.stepPxSize >= stepPixelSize) ||
      config.variations[config.variations.length - 1];
      if (stepPxSize < minimumStepPixelSize) {
        return;
      }
      let tick = config.getNearest(axis.correctActualValue(axis.from) + valueShift);
      let iteration = 0;
      while (tick <= axis.correctActualValue(axis.to) + valueShift && iteration < 500) {
        iteration += 1;
        addTick(tick, {config});
        tick = config.getNextStep(tick, variation);
      }
    }
  };
  iterateWithConfig(mainConfig, true);
  iterateWithConfig(smallConfig, false);
  releaseContext();
  return result.sort(ticksSorter);
}