# All files / utils/legend band.ts

82.6% Statements 38/46
64.28% Branches 9/14
62.5% Functions 5/8
81.81% Lines 36/44

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

    6x
    6x
    6x

    6x

    6x

    2x
    2x

    2x
    2x
    12x
    12x

    2x

    2x
    4x

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

    4x

    4x
    4x
    4x
    18x

    4x

    4x
    4x

    4x
    4x
    4x
    18x

    4x

    4x
    4x

    2x

     import {
      Table,
      LegendColumn,
      LineData,
      BandLineMap,
      DomainLabel,
      LatestIndexMap,
    } from '../../types'
    import {RESULT} from '../../constants/columnKeys'
    import {isVoid} from '../isVoid'
    import {isNumber} from '../isNumber'

    export const getTooltipBandGroupColumns = (
      table: Table,
      rowIndices: number[],
      groupColKeys: string[],
      getValueFormatter: (colKey: string) => (x: any) => string,
      rowColors: string[] | null
    ): LegendColumn[] => {
      return groupColKeys.reduce((accum, key) => {
        Iif (key === RESULT) {
          return accum
        }
        const colData = table.getColumn(key)
        const formatter = getValueFormatter(key)

        accum.push({
          key,
          name: table.getColumnName(key),
          type: table.getColumnType(key),
          colors: rowColors,
          values: rowIndices.map(i =>
            !isVoid(colData[i]) ? formatter(colData[i]) : null
          ),
        })
        return accum
      }, [])
    }

    export const createLatestBandIndices = (
      lineData: LineData,
      bandLineMap: BandLineMap,
      bandDimension: DomainLabel,
      hoveredLines?: LatestIndexMap
    ): LatestIndexMap => {
      const latestIndices: LatestIndexMap = {}
      const lineDataLastIndices: LatestIndexMap = {}

      let counter = -1
      Object.keys(lineData).forEach(lineNumber => {
        counter += lineData[lineNumber][bandDimension].length
        lineDataLastIndices[lineNumber] = counter
      })
      const {upperLines, rowLines, lowerLines} = bandLineMap

      rowLines.forEach((line, position) => {
        const targetValues = lineData[line][bandDimension]

        let lastIndex = lineDataLastIndices[line]
        let targetBandValue = targetValues[targetValues.length - 1]
        if (hoveredLines) {
          const offset = line === 0 ? 0 : lineDataLastIndices[line - 1] + 1
          lastIndex = hoveredLines[line]
          targetBandValue = targetValues[lastIndex - offset]
        }
        latestIndices[line] = lastIndex

        const upperLine = upperLines[position]
        if (isNumber(upperLine)) {
          const matchingUpperIndex = lineData[upperLine][bandDimension].findIndex(
            value => value === targetBandValue
          )
          if (matchingUpperIndex > -1) {
            const offset =
              upperLine === 0 ? 0 : lineDataLastIndices[upperLine - 1] + 1
            latestIndices[upperLine] = matchingUpperIndex + offset
          }
        }

        const lowerLine = lowerLines[position]
        if (isNumber(lowerLine)) {
          const matchingLowerIndex = lineData[lowerLine][bandDimension].findIndex(
            value => value === targetBandValue
          )
          if (matchingLowerIndex > -1) {
            const offset =
              lowerLine === 0 ? 0 : lineDataLastIndices[lowerLine - 1] + 1
            latestIndices[lowerLine] = matchingLowerIndex + offset
          }
        }
      })
      return latestIndices
    }