# All files / transforms line.ts

89.74% Statements 70/78
68.42% Branches 26/38
100% Functions 4/4
89.18% Lines 66/74

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
    178
    179
    180
    181
    182
    183
    184
    185
    186
    1871x

    1x
    1x
    1x
    1x

    1x

    4x

    4x
    4x
    56x
    14x

    56x

    56x
    56x
    140x
    56x

    56x

    4x

    1x

    56x

    56x

    1x

    16x

    8x

    8x
    8x
    8x
    8x
    8x
    8x
    8x

    8x
    4x
    4x

    8x
    8x
    8x
    8x

    8x
    112x
    112x
    112x

    112x
    32x

    32x

    112x
    56x
    56x

    112x
    112x

    112x
    32x
    80x

    80x

    80x

    112x
    8x

    112x
    28x

    112x
    12x

    112x
    30x

    8x
    4x

    8x

     import {
      ColumnGroupMap,
      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 = {}

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

          if (!cumulativeValues[time][maxGroup]) {
            for (let i = 0; i <= maxGroup; i += 1) {
              if (!cumulativeValues[time][i]) {
                cumulativeValues[time][i] = 0
              }
            }
          }
          cumulativeValues[time][maxGroup] =
            maxGroup >= 1
              ? 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,
      colorMapping: ColumnGroupMap = null
    ): 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') {
        if (yColumnKey === VALUE) {
          stackedValuesByTime = mapCumulativeValuesToTimeRange(
            xCol,
            yCol,
            fillColumn
          )
        } else IEif (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)}
          // override the fill with the saved colorMapping, when it exists
          Iif (colorMapping) {
            lineData[groupID].fill = colorMapping.mappings[groupID].color
          }
        }

        if (position === 'stacked') {
          if (yColumnKey === VALUE) {
            y = getCumulativeValueAtTime(stackedValuesByTime, xCol[i], groupID)
          } else IEif (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) {
          Iif (
            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,
          [...table.getColumn(FILL, 'number')],
          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,
      }
    }