# All files / transforms band.ts

55.55% Statements 160/288
62.03% Branches 67/108
69.23% Functions 9/13
55.19% Lines 154/279

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
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    4883x

    3x

    3x
    3x

    3x
    3x
    3x
    3x

    3x

    4x
    2x
    2x

    4x

    4x
    4x
    4x

    4x
    4x
    4x

    4x
    4x
    4x
    4x
    4x
    2x

    4x
    2x

    4x

    2x

    3x

    6x

    6x

    6x
    6x
    6x
    6x
    6x

    6x

    3x

    12x
    12x
    30x

    12x

    12x
    8x
    36x
    144x

    36x
    12x

    36x
    4x
    32x
    4x
    28x
    4x

    12x

    3x

    5x

    5x

    5x
    6x
    6x
    6x
    6x
    6x
    6x

    6x
    6x
    6x

    6x
    6x

    6x
    6x

    6x
    6x

    6x
    6x

    6x
    6x

    6x
    6x

    6x
    6x
    6x

    6x

    25x
    25x
    25x
    25x
    25x
    25x

    25x
    6x
    6x
    6x
    6x

    6x
    6x
    6x
    6x

    6x
    6x
    6x
    6x

    19x
    2x

    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x

    2x
    2x

    17x
    4x

    4x
    4x
    4x
    4x
    4x
    4x

    4x
    4x
    4x
    4x

    13x
    12x
    12x
    12x
    12x
    12x
    12x

    12x
    12x
    12x

    12x

    1x

    1x

    1x

    1x

    1x

    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x

    5x

    3x

     import {
      Band,
      BandBorder,
      BandLayerSpec,
      BandLineMap,
      ColumnGroupMap,
      DomainLabel,
      LatestIndexMap,
      LineData,
      Table,
    } from '../types'

    import {createGroupIDColumn, getBandColorScale} from './'

    import {FILL, LOWER, RESULT, TIME, UPPER} from '../constants/columnKeys'
    import {BAND_COLOR_SCALE_CONSTANT} from '../constants'

    import {isDefined} from '../utils/isDefined'
    import {isFiniteNumber} from '../utils/isFiniteNumber'
    import {isNumber} from '../utils/isNumber'
    import {createLatestBandIndices} from '../utils/legend/band'

    export const getBands = (
      lineData: LineData,
      bandLineMap: BandLineMap
    ): Band[] => {
      const {upperLines = [], rowLines = [], lowerLines = []} = bandLineMap
      const bands: Band[] = []
      rowLines.forEach((rowIndex, i) => {
        // Each band must have a "main column" which creates a row index
        // Any non-main column not associated with a "main" is not a band
        // Only bands get rendered
        Iif (!isFiniteNumber(rowIndex)) {
          return
        }
        const upperIndex = upperLines[i]
        const upperIsFinite = isFiniteNumber(upperIndex)
        const upper = upperIsFinite ? lineData[upperIndex] : {fill: ''}

        const lowerIndex = lowerLines[i]
        const lowerIsFinite = isFiniteNumber(lowerIndex)
        const lower = lowerIsFinite ? lineData[lowerIndex] : {fill: ''}

        const row = lineData[rowIndex]
        upper.fill = row.fill
        lower.fill = row.fill
        const result: Band = {...row}
        if (upperIsFinite) {
          result.upper = upper as BandBorder
        }
        if (lowerIsFinite) {
          result.lower = lower as BandBorder
        }
        bands.push(result)
      })

      return bands
    }

    export const getBandLineMap = (
      fillColumnMap: ColumnGroupMap,
      lowerColumnName: string,
      rowColumnName: string,
      upperColumnName: string
    ): BandLineMap => {
      const bandIndices = {
        rowLines: [],
        lowerLines: [],
        upperLines: [],
      }

      const bands = Object.values(
        groupLineIndicesIntoBands(
          fillColumnMap,
          lowerColumnName,
          rowColumnName,
          upperColumnName
        )
      )

      if (Array.isArray(bands)) {
        bands.forEach(band => {
          bandIndices.rowLines.push(isDefined(band.row) ? band.row : null)
          bandIndices.lowerLines.push(isDefined(band.lower) ? band.lower : null)
          bandIndices.upperLines.push(isDefined(band.upper) ? band.upper : null)
        })
      }

      return bandIndices
    }

    export const groupLineIndicesIntoBands = (
      fill: ColumnGroupMap,
      lowerColumnName: string,
      rowColumnName: string,
      upperColumnName: string
    ) => {
      const {columnKeys, mappings} = fill
      const columnKeysWithoutResult = Array.isArray(columnKeys)
        ? columnKeys.filter(key => key !== RESULT)
        : []
      const bandLineIndexMap = {}

      if (Array.isArray(mappings)) {
        mappings.forEach((line, index) => {
          const lineName = columnKeysWithoutResult.reduce((accum, current) => {
            return `${accum}${line[current]}`
          }, '')
          if (!bandLineIndexMap[lineName]) {
            bandLineIndexMap[lineName] = {
              lower: null,
              upper: null,
              row: null,
            }
          }
          if (line[RESULT] === lowerColumnName) {
            bandLineIndexMap[lineName][LOWER] = index
          } else if (line[RESULT] === upperColumnName) {
            bandLineIndexMap[lineName][UPPER] = index
          } else if (line[RESULT] === rowColumnName) {
            bandLineIndexMap[lineName].row = index
          }
        })
      }

      return bandLineIndexMap
    }

    export const alignMinMaxWithBand = (
      lineData: LineData,
      bandLineMap: BandLineMap
    ): LineData => {
      const {rowLines, upperLines, lowerLines} = bandLineMap

      const alignedData = {}

      for (let i = 0; i < rowLines.length; i += 1) {
        let bandXs = []
        let bandYs = []
        let upperXs = []
        let upperYs = []
        let lowerXs = []
        let lowerYs = []

        const bandId = rowLines[i]
        const upperId = upperLines[i]
        const lowerId = lowerLines[i]

        if (lineData[upperId]) {
          alignedData[upperId] = {
            fill: lineData[upperId].fill,
            xs: [],
            ys: [],
          }
          upperXs = lineData[upperId].xs
          upperYs = lineData[upperId].ys
        }

        if (lineData[lowerId]) {
          alignedData[lowerId] = {
            fill: lineData[lowerId].fill,
            xs: [],
            ys: [],
          }
          lowerXs = lineData[lowerId].xs
          lowerYs = lineData[lowerId].ys
        }

        if (lineData[bandId]) {
          alignedData[bandId] = {
            fill: lineData[bandId].fill,
            xs: [],
            ys: [],
          }
          bandXs = lineData[bandId].xs
          bandYs = lineData[bandId].ys
        }

        let bandIterator = 0
        let upperIterator = 0
        let lowerIterator = 0

        while (
          bandIterator < bandXs.length ||
          upperIterator < upperXs.length ||
          lowerIterator < lowerXs.length
        ) {
          const bandTime = bandXs[bandIterator]
          const bandValue = bandYs[bandIterator]
          const upperTime = upperXs[upperIterator]
          const upperValue = upperYs[upperIterator]
          const lowerTime = lowerXs[lowerIterator]
          const lowerValue = lowerYs[lowerIterator]

          // 1. All three are equal
          if (bandTime === upperTime && bandTime === lowerTime) {
            if (isDefined(bandTime)) {
              alignedData[bandId].xs.push(bandTime)
              alignedData[bandId].ys.push(bandValue)
              bandIterator += 1
            }
            if (isDefined(upperTime)) {
              alignedData[upperId].xs.push(upperTime)
              alignedData[upperId].ys.push(upperValue)
              upperIterator += 1
            }
            if (isDefined(lowerTime)) {
              alignedData[lowerId].xs.push(lowerTime)
              alignedData[lowerId].ys.push(lowerValue)
              lowerIterator += 1
            }
          }
          // 2. Lower is not equal to the other two
          else if (bandTime === upperTime) {
            Iif (bandTime > lowerTime || !isDefined(bandTime)) {
              Iif (isDefined(bandId)) {
                alignedData[bandId].xs.push(lowerTime)
                alignedData[bandId].ys.push(lowerValue)
              }
              Iif (isDefined(upperId)) {
                alignedData[upperId].xs.push(lowerTime)
                alignedData[upperId].ys.push(lowerValue)
              }
              alignedData[lowerId].xs.push(lowerTime)
              alignedData[lowerId].ys.push(lowerValue)
              lowerIterator += 1
            } else if (bandTime < lowerTime || !isDefined(lowerTime)) {
              alignedData[bandId].xs.push(bandTime)
              alignedData[bandId].ys.push(bandValue)
              alignedData[upperId].xs.push(upperTime)
              alignedData[upperId].ys.push(upperValue)
              if (isDefined(lowerId)) {
                alignedData[lowerId].xs.push(bandTime)
                alignedData[lowerId].ys.push(bandValue)
              }
              bandIterator += 1
              upperIterator += 1
            }
          }
          // 3. Upper is not equal to the other two
          else if (bandTime === lowerTime) {
            Iif (bandTime > upperTime || !isDefined(bandTime)) {
              Iif (isDefined(bandId)) {
                alignedData[bandId].xs.push(upperTime)
                alignedData[bandId].ys.push(upperValue)
              }
              Iif (isDefined(lowerId)) {
                alignedData[lowerId].xs.push(upperTime)
                alignedData[lowerId].ys.push(upperValue)
              }
              alignedData[upperId].xs.push(upperTime)
              alignedData[upperId].ys.push(upperValue)
              upperIterator += 1
            } else if (bandTime < upperTime || !isDefined(upperTime)) {
              alignedData[bandId].xs.push(bandTime)
              alignedData[bandId].ys.push(bandValue)
              if (isDefined(upperId)) {
                alignedData[upperId].xs.push(bandTime)
                alignedData[upperId].ys.push(bandValue)
              }
              alignedData[lowerId].xs.push(lowerTime)
              alignedData[lowerId].ys.push(lowerValue)
              bandIterator += 1
              lowerIterator += 1
            }
          }
          // 4. Band is not equal to the other two
          else if (upperTime === lowerTime) {
            if (upperTime > bandTime || !isDefined(upperTime)) {
              alignedData[bandId].xs.push(bandTime)
              alignedData[bandId].ys.push(bandValue)
              if (isDefined(upperId)) {
                alignedData[upperId].xs.push(bandTime)
                alignedData[upperId].ys.push(bandValue)
              }
              if (isDefined(lowerId)) {
                alignedData[lowerId].xs.push(bandTime)
                alignedData[lowerId].ys.push(bandValue)
              }
              bandIterator += 1
            } else IEif (upperTime < bandTime || !isDefined(bandTime)) {
              Iif (isDefined(bandId)) {
                alignedData[bandId].xs.push(upperTime)
                alignedData[bandId].ys.push(upperValue)
              }
              alignedData[upperId].xs.push(upperTime)
              alignedData[upperId].ys.push(upperValue)
              alignedData[lowerId].xs.push(lowerTime)
              alignedData[lowerId].ys.push(lowerValue)
              upperIterator += 1
              lowerIterator += 1
            }
          }
          // 5. They are all different
          else {
            Iif (!isDefined(bandTime)) {
              if (upperTime > lowerTime) {
                Iif (isDefined(bandId)) {
                  alignedData[bandId].xs.push(lowerTime)
                  alignedData[bandId].ys.push(lowerValue)
                }
                alignedData[upperId].xs.push(lowerTime)
                alignedData[upperId].ys.push(lowerValue)
                alignedData[lowerId].xs.push(lowerTime)
                alignedData[lowerId].ys.push(lowerValue)
                lowerIterator += 1
              } else {
                Iif (isDefined(bandId)) {
                  alignedData[bandId].xs.push(upperTime)
                  alignedData[bandId].ys.push(upperValue)
                }
                alignedData[upperId].xs.push(upperTime)
                alignedData[upperId].ys.push(upperValue)
                alignedData[lowerId].xs.push(upperTime)
                alignedData[lowerId].ys.push(upperValue)
                upperIterator += 1
              }
            } else Iif (!isDefined(upperTime)) {
              if (bandTime > lowerTime) {
                alignedData[bandId].xs.push(lowerTime)
                alignedData[bandId].ys.push(lowerValue)
                Iif (isDefined(upperId)) {
                  alignedData[upperId].xs.push(lowerTime)
                  alignedData[upperId].ys.push(lowerValue)
                }
                alignedData[lowerId].xs.push(lowerTime)
                alignedData[lowerId].ys.push(lowerValue)
                lowerIterator += 1
              } else {
                alignedData[bandId].xs.push(bandTime)
                alignedData[bandId].ys.push(bandValue)
                Iif (isDefined(upperId)) {
                  alignedData[upperId].xs.push(bandTime)
                  alignedData[upperId].ys.push(bandValue)
                }
                alignedData[lowerId].xs.push(bandTime)
                alignedData[lowerId].ys.push(bandValue)
                bandIterator += 1
              }
            } else Iif (!isDefined(lowerTime)) {
              if (bandTime > upperTime) {
                alignedData[bandId].xs.push(upperTime)
                alignedData[bandId].ys.push(upperValue)
                alignedData[upperId].xs.push(upperTime)
                alignedData[upperId].ys.push(upperValue)
                Iif (isDefined(lowerId)) {
                  alignedData[lowerId].xs.push(upperTime)
                  alignedData[lowerId].ys.push(upperValue)
                }
                upperIterator += 1
              } else {
                alignedData[bandId].xs.push(bandTime)
                alignedData[bandId].ys.push(bandValue)
                alignedData[upperId].xs.push(bandTime)
                alignedData[upperId].ys.push(bandValue)
                Iif (isDefined(lowerId)) {
                  alignedData[lowerId].xs.push(bandTime)
                  alignedData[lowerId].ys.push(bandValue)
                }
                bandIterator += 1
              }
            } else {
              const lowest = Math.min(bandTime, upperTime, lowerTime)

              Iif (lowest === lowerTime) {
                alignedData[bandId].xs.push(lowerTime)
                alignedData[bandId].ys.push(lowerValue)
                alignedData[upperId].xs.push(lowerTime)
                alignedData[upperId].ys.push(lowerValue)
                alignedData[lowerId].xs.push(lowerTime)
                alignedData[lowerId].ys.push(lowerValue)
                lowerIterator += 1
              } else Iif (lowest === upperTime) {
                alignedData[bandId].xs.push(upperTime)
                alignedData[bandId].ys.push(upperValue)
                alignedData[upperId].xs.push(upperTime)
                alignedData[upperId].ys.push(upperValue)
                alignedData[lowerId].xs.push(upperTime)
                alignedData[lowerId].ys.push(upperValue)
                upperIterator += 1
              } else {
                alignedData[bandId].xs.push(bandTime)
                alignedData[bandId].ys.push(bandValue)
                alignedData[upperId].xs.push(bandTime)
                alignedData[upperId].ys.push(bandValue)
                alignedData[lowerId].xs.push(bandTime)
                alignedData[lowerId].ys.push(bandValue)
                bandIterator += 1
              }
            }
          }
        }
      }

      return alignedData
    }

    export const bandTransform = (
      inputTable: Table,
      xColumnKey: string,
      yColumnKey: string,
      fillColKeys: string[],
      colors: string[],
      lowerColumnName: string,
      rowColumnName: string,
      upperColumnName: string
    ): BandLayerSpec => {
      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 bandLineMap = getBandLineMap(
        fillColumnMap,
        lowerColumnName || '',
        rowColumnName,
        upperColumnName || ''
      )
      const fillScale = range =>
        getBandColorScale(bandLineMap, colors)(range * BAND_COLOR_SCALE_CONSTANT)

      const lineData: LineData = {}

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

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

        Iif (!lineData[groupID]) {
          lineData[groupID] = {xs: [], ys: [], fill: ''}
          // 'fill' is set temporarily to no color
          //    it will be updated with another loop later,
          //    because it is faster to walk bandLineMap once
          //    than to search for the indices of many lines
        }

        lineData[groupID].xs.push(x)
        lineData[groupID].ys.push(y)

        xMin = Math.min(x, xMin)
        xMax = Math.max(x, xMax)
        yMin = Math.min(y, yMin)
        yMax = Math.max(y, yMax)
      }
      // remember the latest (most recent) index for each group
      const bandDimension = yColumnKey === TIME ? DomainLabel.Y : DomainLabel.X
      const latestIndices: LatestIndexMap = createLatestBandIndices(
        lineData,
        bandLineMap,
        bandDimension
      )

      Object.keys(bandLineMap).forEach(indexType => {
        bandLineMap[indexType].forEach((groupID, index) => {
          Iif (isNumber(groupID)) {
            lineData[groupID].fill = fillScale(index)
          }
        })
      })

      return {
        type: 'band',
        bandLineMap,
        bandName: rowColumnName,
        upperColumnName,
        lowerColumnName,
        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},
      }
    }