# All files / transforms line.ts

90.54% Statements 67/74
72.55% Branches 37/51
100% Functions 4/4
90% Lines 63/70

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
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    1787x

    7x
    7x
    7x
    7x

    7x

    8x

    8x
    5003238x
    5052x

    5003238x

    5003238x
    25052x
    2x
    2x

    5003238x

    8x

    7x

    5003220x

    5003220x

    7x

    14x

    14x
    14x
    14x
    14x
    14x
    14x
    14x

    14x
    6x
    6x

    14x
    14x
    14x
    14x

    14x
    5004279x
    5004279x
    5004279x

    5004279x
    634x

    5004279x
    5003220x
    5003220x

    5004279x
    5004279x

    5004279x
    634x
    5003645x

    5003645x

    1003629x

    5004279x
    14x

    5004279x
    5083x

    5004279x
    206x

    5004279x
    560x

    14x
    6x

    14x

     import {
      CumulativeValuesByTime,
      DomainLabel,
      LatestIndexMap,
      LineData,
      LineLayerSpec,
      LinePosition,
      NumericColumnData,
      Table,
    } from '../types'
    import {FILL, TIME, VALUE} from '../constants/columnKeys'
    import {createGroupIDColumn, getNominalColorScale} from './'
    import {getDomainDataFromLines} from '../utils/lineData'
    import {isDefined} from '../utils/isDefined'

    export const mapCumulativeValuesToTimeRange = (
      timesCol: NumericColumnData,
      valuesCol: NumericColumnData,
      fillCol: NumericColumnData
    ): CumulativeValuesByTime => {
      const cumulativeValues: CumulativeValuesByTime = {}

      timesCol.forEach((time, index) => {
        if (!cumulativeValues[time]) {
          cumulativeValues[time] = {}
        }
        const maxGroup = fillCol[index]

        if (!cumulativeValues[time][maxGroup - 1]) {
          for (let i = 0; i < maxGroup; i += 1) {
            Eif (!cumulativeValues[time][i]) {
              cumulativeValues[time][i] = 0
            }
          }
        }
        cumulativeValues[time][maxGroup] =
          maxGroup - 1 >= 0
            ? cumulativeValues[time][maxGroup - 1] + valuesCol[index]
            : valuesCol[index]
      })
      return cumulativeValues
    }

    const getCumulativeValueAtTime = (
      cumulativeValues: CumulativeValuesByTime,
      time: number,
      groupID: number
    ): number => {
      Iif (
        !cumulativeValues ||
        !cumulativeValues[time] ||
        !cumulativeValues[time][groupID]
      ) {
        return 0
      }
      return cumulativeValues[time][groupID]
    }

    export const lineTransform = (
      inputTable: Table,
      xColumnKey: string,
      yColumnKey: string,
      fillColKeys: string[],
      colors: string[],
      position: LinePosition
    ): LineLayerSpec => {
      const [fillColumn, fillColumnMap] = createGroupIDColumn(
        inputTable,
        fillColKeys
      )

      const table = inputTable.addColumn(FILL, 'system', 'number', fillColumn)
      const xCol = table.getColumn(xColumnKey, 'number')
      const yCol = table.getColumn(yColumnKey, 'number')
      const fillScale = getNominalColorScale(fillColumnMap, colors)
      const lineData: LineData = {}
      let stackedValuesByTime: CumulativeValuesByTime = {}
      const latestIndices: LatestIndexMap = {}

      if (position === 'stacked') {
        Eif (yColumnKey === VALUE) {
          stackedValuesByTime = mapCumulativeValuesToTimeRange(
            xCol,
            yCol,
            fillColumn
          )
        } else if (xColumnKey === VALUE) {
          stackedValuesByTime = mapCumulativeValuesToTimeRange(
            yCol,
            xCol,
            fillColumn
          )
        }
      }

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

      for (let i = 0; i < table.length; i++) {
        const groupID = fillColumn[i]
        let x = xCol[i]
        let y = yCol[i]

        if (!lineData[groupID]) {
          lineData[groupID] = {xs: [], ys: [], fill: fillScale(groupID)}
        }

        if (position === 'stacked') {
          Eif (yColumnKey === VALUE) {
            y = getCumulativeValueAtTime(stackedValuesByTime, xCol[i], groupID)
          } else if (xColumnKey === VALUE) {
            x = getCumulativeValueAtTime(stackedValuesByTime, yCol[i], groupID)
          }
        }
        lineData[groupID].xs.push(x)
        lineData[groupID].ys.push(y)

        // remember the latest (most recent) index for each group
        if (!isDefined(latestIndices[groupID])) {
          latestIndices[groupID] = i
        } else Iif (yColumnKey === TIME) {
          if (
            y > yCol[latestIndices[groupID]] ||
            !isDefined(yCol[latestIndices[groupID]])
          ) {
            latestIndices[groupID] = i
          }
        } else if (
          x > xCol[latestIndices[groupID]] ||
          !isDefined(xCol[latestIndices[groupID]])
        ) {
          latestIndices[groupID] = i
        }

        if (x < xMin) {
          xMin = x
        }

        if (x > xMax) {
          xMax = x
        }

        if (y < yMin) {
          yMin = y
        }

        if (y > yMax) {
          yMax = y
        }
      }

      let stackedDomainValueColumn: NumericColumnData
      if (position === 'stacked') {
        stackedDomainValueColumn = getDomainDataFromLines(
          lineData,
          yColumnKey === VALUE ? DomainLabel.Y : DomainLabel.X
        )
      }

      return {
        type: 'line',
        inputTable,
        table,
        lineData,
        xDomain: [xMin, xMax],
        yDomain: [yMin, yMax],
        xColumnKey,
        yColumnKey,
        xColumnType: table.getColumnType(xColumnKey),
        yColumnType: table.getColumnType(yColumnKey),
        scales: {fill: fillScale},
        columnGroupMaps: {fill: fillColumnMap, latestIndices},
        stackedDomainValueColumn,
      }
    }