# All files / transforms index.ts

94% Statements 47/50
100% Branches 3/3
85.71% Functions 6/7
93.47% Lines 43/46

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
    11712x
    12x
    12x
    12x

    12x

    12x

    49x
    49x
    49x

    49x

    49x
    5013298x

    5013361x
    5013361x

    5013298x

    5013298x

    5013298x
    1475x

    1475x
    1475x

    1475x

    5013298x

    49x

    49x

    12x

    54x

    54x

    54x
    28x

    26x

    26x
    1468x

    54x

    54x

    12x

    44x

    12x

    10x

    12x

    1x
    1x

    1x

    12x

     import {range} from 'd3-array'
    import {scaleOrdinal} from 'd3-scale'
    import {interpolateRgbBasis} from 'd3-interpolate'
    import {scaleSequential} from 'd3-scale'

    import {
      Table,
      ColumnGroupMap,
      Scale,
      NumericColumnData,
      SymbolType,
      BandLineMap,
    } from '../types'
    import {ALL_SYMBOL_TYPES, BAND_COLOR_SCALE_CONSTANT} from '../constants'

    export const createGroupIDColumn = (
      table: Table,
      columnKeys: string[]
    ): [NumericColumnData, ColumnGroupMap] => {
      const groupIDColumn = new Float64Array(table.length)
      const mappings = []
      const groupIDs = {}

      let currentGroupID = 0

      for (let i = 0; i < table.length; i++) {
        const mapping = {}

        for (const k of columnKeys) {
          mapping[k] = table.getColumn(k)[i]
        }

        const hashedGroupValues = Object.values(mapping)
          .sort()
          .join('')

        let groupID = groupIDs[hashedGroupValues]

        if (groupID === undefined) {
          groupID = currentGroupID

          groupIDs[hashedGroupValues] = groupID
          mappings[groupID] = mapping

          currentGroupID++
        }

        groupIDColumn[i] = groupID
      }

      const columnGroupMap: ColumnGroupMap = {mappings, columnKeys}

      return [groupIDColumn, columnGroupMap]
    }

    const createNominalColorScale = (
      length: number,
      colors: string[]
    ): Scale<number, string> => {
      const domain = range(length)

      let scaleRange = []

      if (domain.length <= colors.length) {
        scaleRange = colors.slice(0, domain.length)
      } else {
        const interpolator = interpolateRgbBasis(colors)

        scaleRange = range(domain.length).map(k =>
          interpolator(k / (domain.length - 1))
        )
      }

      const scale = scaleOrdinal<number, string>()
        .domain(domain)
        .range(scaleRange)

      return scale
    }

    export const getNominalColorScale = (
      groupMap: ColumnGroupMap,
      colors: string[]
    ): Scale<number, string> =>
      createNominalColorScale(groupMap.mappings.length, colors)

    export const getBandColorScale = (
      bandLineMap: BandLineMap,
      colors: string[]
    ): Scale<number, string> =>
      createNominalColorScale(
        bandLineMap.rowLines.length * BAND_COLOR_SCALE_CONSTANT,
        colors
      )

    export const getContinuousColorScale = (
      domain: [number, number],
      colors: string[]
    ): Scale<number, string> => {
      const colorScheme = interpolateRgbBasis(colors)
      const scale = scaleSequential(colorScheme).domain(domain)

      return scale
    }

    export const getSymbolScale = (
      columnGroupMap: ColumnGroupMap
    ): Scale<number, SymbolType> => {
      const domain = range(columnGroupMap.mappings.length)

      const scale = scaleOrdinal<number, SymbolType>()
        .domain(domain)
        .range(ALL_SYMBOL_TYPES)

      return scale
    }