function generateChartGapMap(data, maxGapSize) {
  let gaps = [];
  if (data.length > 1) {
    let currentOffset = 0;
    let previousOffset = 0;
    let previousX = data[0][0];

    for (let i = 1; i < data.length; i++) {
      if (data[i][0] != null && data[i][1] != null) {
        let currentX = data[i][0];
        if (currentX - previousX > maxGapSize) { //New gap
          previousOffset = currentOffset;
          currentOffset += currentX - (previousX + maxGapSize);
          let gap = {
            unmappedGapStart: previousX,
            unmappedGapEnd: currentX,
            mappedGapStart: previousX - previousOffset,
            mappedGapEnd: currentX - currentOffset,
            startOffset: previousOffset,
            endOffset: currentOffset
          };
          gaps.push(gap);
        }
        previousX = currentX;
      }
    }
  }
  return gaps;
}
function mapChartXValue(unmappedValue, gapMap) {
  if (gapMap.length > 0) {
    let i = 0;
    let gap = gapMap[i];
    while (unmappedValue > gap.unmappedGapEnd && i < gapMap.length - 1) {
      i++;
      gap = gapMap[i];
    }
    if (unmappedValue >= gap.unmappedGapEnd) { //unmappedValue is after this gap
      return unmappedValue - gap.endOffset;
    } else {
      if (unmappedValue <= gap.unmappedGapStart) { //unmappedValue is before this gap
        return unmappedValue - gap.startOffset;
      } else { //unmappedValue is within this gap
        let unmappedGapSize = gap.unmappedGapEnd - gap.unmappedGapStart;
        let mappedGapSize = gap.mappedGapEnd - gap.mappedGapStart;
        let position = (unmappedValue - gap.unmappedGapStart) / unmappedGapSize;
        return gap.mappedGapStart + (position * mappedGapSize);
      }
    }
  } else {
    return unmappedValue;
  }
}
function unmapChartXValue(mappedValue, gapMap) {
  if (gapMap.length > 0) {
    let i = 0;
    let gap = gapMap[i];
    while (mappedValue > gap.mappedGapEnd && i < gapMap.length - 1) {
      i++;
      gap = gapMap[i];
    }
    if (mappedValue >= gap.mappedGapEnd) { //mappedValue is after this gap
      return mappedValue + gap.endOffset;
    } else {
      if (mappedValue <= gap.mappedGapStart) { //mappedValue is before this gap
        return mappedValue + gap.startOffset;
      } else { //mappedValue is within this gap
        let unmappedGapSize = gap.unmappedGapEnd - gap.unmappedGapStart;
        let mappedGapSize = gap.mappedGapEnd - gap.mappedGapStart;
        let position = mappedValue - gap.mappedGapStart;
        return gap.unmappedGapStart + (position / mappedGapSize * unmappedGapSize);
      }
    }
  } else {
    return mappedValue;
  }
}
