# All files / transforms mosaic.ts

100% Statements 86/86
95.65% Branches 22/23
100% Functions 11/11
100% Lines 82/82

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

    3x
    3x
    3x
    3x
    3x

    3x
    1x

    3x
    2x

    3x

    7x

    7x

    7x
    9x

    7x
    1x

    7x

    8x

    7x
    7x
    6x
    9x
    6x
    6x

    7x

    7x
    42x
    42x

    54x
    54x
    36x

    54x

    42x
    48x
    48x
    30x

    48x

    42x
    42x

    42x
    42x

    42x

    7x
    7x

    7x

    7x
    42x
    42x
    16x

    16x

    26x
    26x

    26x
    26x
    22x
    22x
    22x
    22x
    22x
    22x

    7x
    7x
    7x
    7x
    7x
    7x
    7x

    7x

    7x

    16x
    16x
    16x
    16x
    16x

    16x
    16x

    7x

    7x
    7x
    7x

    7x

     import memoizeOne from 'memoize-one'
    import {MosaicLayerSpec, Table} from '../types'
    import {DISPLAY_NAME, FILL, SERIES, X_MAX, X_MIN} from '../constants/columnKeys'
    import {isEqual} from '../utils/isEqual'
    import {newTable} from '../utils/newTable'
    import {resolveDomain} from '../utils/resolveDomain'
    import {createGroupIDColumn, getNominalColorScale} from './'

    const memoizedSortTimeStamps = memoizeOne(
      (timeStamps: Iterable<number>) => [...timeStamps].sort(),
      isEqual
    )

    const memoizedSortDataMapKeys = memoizeOne(
      (dataMapKeys: Iterable<string>) => [...dataMapKeys].sort(),
      isEqual
    )

    export const mosaicTransform = (
      inputTable: Table,
      xColumnKey: string,
      yColumnKeys: Array<string>,
      yLabelColumns: Array<string>,
      yLabelColumnSeparator: string,
      xDomain: Array<number>,
      fillColKeys: Array<string>,
      colors: Array<string>
    ): MosaicLayerSpec => {
      const [fillColumn, fillColumnMap] = createGroupIDColumn(
        inputTable,
        fillColKeys
      )
      const xInputCol = inputTable.getColumn(xColumnKey, 'number') || []

      let labelColumns = Array.isArray(yLabelColumns)
        ? yLabelColumns.filter(labelColumn => yColumnKeys.includes(labelColumn))
        : []
      if (labelColumns.length === 0 && Array.isArray(yColumnKeys)) {
        labelColumns = yColumnKeys
      }
      const yColumnsName = labelColumns.reduce(
        (combinedName, columnName) =>
          combinedName
            ? `${combinedName}${yLabelColumnSeparator}${columnName}`
            : columnName,
        ''
      )
      const yInputCols = new Map()
      if (Array.isArray(yColumnKeys)) {
        yColumnKeys.forEach(columnKey => {
          if (columnKey) {
            const column = inputTable.getColumn(columnKey, 'string')
            yInputCols.set(columnKey, column)
          }
        })
      }

      // Mosaic can have only one column as the fill value:
      // always the first fill column key
      const valueKey = fillColumnMap.columnKeys[0]

      const timeStampMap = new Map()
      for (let i = 0; i < inputTable.length; i++) {
        const yColumnTick = Array.isArray(yColumnKeys)
          ? yColumnKeys.reduce((combinedValue, key) => {
              let value = ''
              if (yInputCols.has(key) && Array.isArray(yInputCols.get(key))) {
                value = yInputCols.get(key)[i]
              }
              return `${combinedValue}${value}`
            }, '')
          : ''

        const yTickLabel = labelColumns.reduce((combinedValue, key) => {
          let value = ''
          if (yInputCols.has(key)) {
            value = yInputCols.get(key)[i]
          }
          return combinedValue
            ? `${combinedValue}${yLabelColumnSeparator}${value}`
            : value
        }, '')

        const currentX = xInputCol[i]
        const currentFillValue = fillColumnMap.mappings[fillColumn[i]][valueKey]

        if (!timeStampMap.has(currentX)) {
          timeStampMap.set(currentX, [])
        }
        timeStampMap.get(currentX).push({
          yTickLabel,
          yColumnTick,
          fill: currentFillValue,
        })
      }

      const sortedTimeStamps = memoizedSortTimeStamps([...timeStampMap.keys()])
      let tableLength = 0

      /*
        This is the structure used to create and update the data map below:

        dataMap[series] = {
          xMin: NumericColumnData,
          xMax: NumericColumnData,
          fill: NumericColumnData,
          series: string[],
          displayedColumns: string[],
          yTickLabel: string,
        }
      */
      const dataMap = new Map()

      sortedTimeStamps.forEach(timeStamp => {
        timeStampMap.get(timeStamp).forEach(data => {
          if (!dataMap.has(data.yColumnTick)) {
            dataMap.set(data.yColumnTick, {
              xMin: [timeStamp],
              xMax: [timeStamp],
              fill: [data.fill],
              series: [data.yColumnTick],
              displayedColumns: [data.yTickLabel],
              yTickLabel: data.yTickLabel,
            })
            tableLength += 1
          } else {
            const prevMaxIndex = dataMap.get(data.yColumnTick).xMax.length - 1
            const prevFill = dataMap.get(data.yColumnTick).fill[
              dataMap.get(data.yColumnTick).fill.length - 1
            ]

            dataMap.get(data.yColumnTick).xMax[prevMaxIndex] = timeStamp
            if (prevFill !== data.fill) {
              dataMap.get(data.yColumnTick).xMin.push(timeStamp)
              dataMap.get(data.yColumnTick).xMax.push(timeStamp)
              dataMap.get(data.yColumnTick).fill.push(data.fill)
              dataMap.get(data.yColumnTick).series.push(data.yColumnTick)
              dataMap.get(data.yColumnTick).displayedColumns.push(data.yTickLabel)
              tableLength += 1
            }
          }
        })
      })

      let xMinData = []
      let xMaxData = []
      let fillData = []
      let seriesData = []
      let displayedColumnsData = []
      const yTicks = []
      const ySeries = []

      const sortedDataMapKeys = memoizedSortDataMapKeys([...dataMap.keys()])

      sortedDataMapKeys.forEach(key => {
        // combine all series into the proper shape
        xMinData = xMinData.concat(dataMap.get(key).xMin)
        xMaxData = xMaxData.concat(dataMap.get(key).xMax)
        fillData = fillData.concat(dataMap.get(key).fill)
        seriesData = seriesData.concat(dataMap.get(key).series)
        displayedColumnsData = displayedColumnsData.concat(
          dataMap.get(key).displayedColumns
        )
        ySeries.push(key)
        yTicks.push(dataMap.get(key).yTickLabel)
      })

      const table = newTable(tableLength)
        .addColumn(X_MIN, 'system', 'number', xMinData)
        .addColumn(X_MAX, 'system', 'number', xMaxData)
        .addColumn(FILL, 'string', 'string', fillData)
        .addColumn(SERIES, 'string', 'string', seriesData)
        .addColumn(DISPLAY_NAME, 'string', 'string', displayedColumnsData)

      const resolvedXDomain = resolveDomain(xInputCol, xDomain)
      const resolvedYDomain = [0, yTicks.length]
      const fillScale = getNominalColorScale(fillColumnMap, colors)

      return {
        type: 'mosaic',
        inputTable,
        table,
        xDomain: resolvedXDomain,
        xColumnKey,
        xColumnType: inputTable.getColumnType(xColumnKey),
        yDomain: resolvedYDomain,
        yColumnType: 'string',
        yColumnsName,
        ySeries,
        yTicks,
        scales: {fill: fillScale},
        columnGroupMaps: {fill: fillColumnMap},
      }
    }