# All files / transforms annotation.ts

100% Statements 31/31
89.47% Branches 17/19
100% Functions 1/1
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    2x
    2x

    2x

    5x
    5x
    5x
    5x

    5x
    5x
    5x
    5x

    5x
    15x
    5x

    15x
    15x

    15x
    5x

    15x
    15x

    5x
    5x
    2x
    12x
    12x

    8x

    5x

    5x

     import {AnnotationLayerSpec, AnnotationMark, Table} from '../types'
    import {FILL} from '../constants/columnKeys'
    import {createGroupIDColumn} from './'

    export const annotationTransform = (
      inputTable: Table,
      inputAnnotations: AnnotationMark[],
      xColumnKey: string,
      yColumnKey: string,
      fillColKeys: string[]
    ): AnnotationLayerSpec => {
      const [fillColumn] = createGroupIDColumn(inputTable, fillColKeys)
      const table = inputTable.addColumn(FILL, 'system', 'number', fillColumn)
      const xCol = table.getColumn(xColumnKey, 'number')
      const yCol = table.getColumn(yColumnKey, 'number')

      let xMin = Infinity
      let xMax = -Infinity
      let yMin = Infinity
      let yMax = -Infinity

      for (let i = 0; i < table.length; i += 1) {
        if (xCol[i] < xMin) {
          xMin = xCol[i]
        }

        Eif (xCol[i] > xMax) {
          xMax = xCol[i]
        }

        if (yCol[i] < yMin) {
          yMin = yCol[i]
        }

        Eif (yCol[i] > yMax) {
          yMax = yCol[i]
        }
      }

      // A hash of all input annotations is needed to:
      // - keep only the first occurence of an annotation with the same
      //   dimension, startValue, stopValue
      // - ensure 'y' dimension annotations are within yDomain
      // - ensure 'x' dimension annotations are within xDomain
      const annotationMap = {}
      if (Array.isArray(inputAnnotations)) {
        for (let i = 0; i < inputAnnotations.length; i += 1) {
          const uniqueKey = `${inputAnnotations[i].dimension}-${inputAnnotations[i].startValue}-${inputAnnotations[i].stopValue}`
          if (
            !annotationMap[uniqueKey] &&
            ((inputAnnotations[i].dimension === 'y' &&
              yMin <= inputAnnotations[i].startValue &&
              inputAnnotations[i].stopValue <= yMax) ||
              (inputAnnotations[i].dimension === 'x' &&
                xMin <= inputAnnotations[i].startValue &&
                inputAnnotations[i].stopValue <= xMax))
          ) {
            annotationMap[uniqueKey] = inputAnnotations[i]
          }
        }
      }
      const annotationData: AnnotationMark[] = Object.values(annotationMap)

      return {
        type: 'annotation',
        table,
        annotationData,
        xColumnKey,
        yColumnKey,
        xColumnType: table.getColumnType(xColumnKey),
        yColumnType: table.getColumnType(yColumnKey),
        xDomain: [xMin, xMax],
        yDomain: [yMin, yMax],
      }
    }