# All files / transforms line.ts

63.08% Statements 41/65
30.77% Branches 12/39
25% Functions 1/4
62.3% Lines 38/61

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
    1581x

    1x
    1x
    1x

    1x

    1x

    1x

    3x

    3x
    3x
    3x
    3x
    3x
    3x

    3x

    3x
    3x
    3x
    3x

    3x
    9x
    9x
    9x

    9x
    3x

    9x

    9x
    9x

    9x
    3x

    9x
    9x

    9x
    3x

    9x
    6x

    3x

    3x

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

    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) {
            if (!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 => {
      if (
        !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 = {}

      Iif (position === 'stacked') {
        if (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)}
        }

        Iif (position === 'stacked') {
          if (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)

        if (x < xMin) {
          xMin = x
        }

        Eif (x > xMax) {
          xMax = x
        }

        if (y < yMin) {
          yMin = y
        }

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

      let stackedDomainValueColumn: NumericColumnData
      Iif (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},
        stackedDomainValueColumn,
      }
    }