# All files / utils/legend staticLegend.ts

51.39% Statements 37/72
48% Branches 36/75
53.33% Functions 8/15
52.94% Lines 36/68

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

    2x

    2x
    2x
    2x

    2x

    2x

    10x
    10x

    10x
    10x
    10x
    10x
    10x
    10x
    10x

    10x

    232x

    10x

    10x
    10x
    5x

    116x

    5x
    5x
    116x

    116x

    5x

    116x

    10x
    22x
    280x
    280x
    280x

    22x

    10x

    2x

     import {
      BandLayerSpec,
      Formatter,
      LayerSpec,
      LegendData,
      LineData,
      LineLayerSpec,
      LinePosition,
    } from '../../types'
    import {
      FILL,
      LINE_COUNT,
      STACKED_LINE_CUMULATIVE,
    } from '../../constants/columnKeys'

    import {getTooltipBandGroupColumns} from './band'
    import {formatLegendValues} from './format'
    import {sortBandLines, sortIndicesByValueColumn} from './sort'

    export const getLegendData = (
      layerType: string,
      spec: LayerSpec,
      valueColumnKey: string,
      getFormatter?: (columnKey: string) => Formatter
    ): LegendData => {
      switch (layerType) {
        case 'line':
          const position = Array.isArray(
            (spec as LineLayerSpec).stackedDomainValueColumn
          )
            ? 'stacked'
            : 'overlaid'
          return convertLineSpec(
            spec as LineLayerSpec,
            valueColumnKey,
            getFormatter,
            position
          )

        case 'band':
          return convertBandSpec(
            spec as BandLayerSpec,
            valueColumnKey,
            getFormatter
          )

        default:
          return []
      }
    }

    export const convertLineSpec = (
      spec: LineLayerSpec,
      valueColumnKey: string,
      getColumnFormatter: (colKey: string) => Formatter,
      position: LinePosition
    ): LegendData => {
      const columnKeys = spec?.columnGroupMaps?.fill?.columnKeys
      Iif (!Array.isArray(columnKeys)) {
        return null
      }
      const mappings = spec?.columnGroupMaps?.fill?.mappings
      const valueFormatter = getColumnFormatter(valueColumnKey)
      const latestValueIndices = spec?.columnGroupMaps?.latestIndices
      const lineValues = spec.table.getColumn(valueColumnKey)
      const fillIndices = spec.table.getColumn(FILL)
      const lineData: LineData = spec?.lineData
      const stackedDomainValues = spec.stackedDomainValueColumn ?? []

      const sortOrder = sortIndicesByValueColumn(
        position === 'stacked' ? stackedDomainValues : lineValues,
        Object.values(latestValueIndices)
      )

      const colors = sortOrder.map(index => lineData[`${fillIndices[index]}`].fill)

      const valueColumn = {
        key: valueColumnKey,
        name: `Latest ${valueColumnKey}`,
        type: spec.table.getColumnType(valueColumnKey),
        colors,
        values: formatLegendValues(lineValues, sortOrder, valueFormatter),
      }

      const additionalColumns = []
      if (position === 'stacked') {
        additionalColumns.push({
          key: `_${STACKED_LINE_CUMULATIVE}`,
          name: STACKED_LINE_CUMULATIVE,
          type: spec.table.getColumnType(valueColumnKey),
          colors,
          values: sortOrder.map(index =>
            valueFormatter(stackedDomainValues[index])
          ),
        })

        const lineCountByGroupId = {}
        sortOrder
          .map(index => fillIndices[index])
          .sort()
          .forEach((groupId, key) => {
            lineCountByGroupId[`${groupId}`] = key + 1
          })
        additionalColumns.push({
          key: `_${LINE_COUNT}`,
          name: LINE_COUNT,
          type: spec.table.getColumnType(valueColumnKey),
          colors,
          values: sortOrder.map(
            index => lineCountByGroupId[`${fillIndices[index]}`]
          ),
        })
      }

      const fillColumns = columnKeys.map(key => {
        const fillColumn: string[] = sortOrder.map(index => {
          const columns = mappings[`${fillIndices[index]}`]
          const fillFormatter = getColumnFormatter(key)
          return fillFormatter(columns[key])
        })

        return {
          key,
          name: key,
          type: spec.table.getColumnType(key),
          values: fillColumn,
          colors,
        }
      })

      return [valueColumn, ...additionalColumns, ...fillColumns]
    }

    export const convertBandSpec = (
      spec: BandLayerSpec,
      valueColumnKey: string,
      getColumnFormatter: (colKey: string) => Formatter
    ): LegendData => {
      const columnKeys = spec?.columnGroupMaps?.fill?.columnKeys
      if (!Array.isArray(columnKeys)) {
        return null
      }
      const valueFormatter = getColumnFormatter(valueColumnKey)
      const {latestIndices} = spec?.columnGroupMaps
      const lineData: LineData = spec?.lineData

      const bandValues = spec.table.getColumn(valueColumnKey)
      const {bandName, upperColumnName, lowerColumnName} = spec
      const {bandLineMap} = spec

      const sortedBandLineMap = sortBandLines(
        bandValues,
        bandLineMap,
        latestIndices
      )
      const {
        upperLines: sortedUpperLines,
        rowLines: sortedRowLines,
        lowerLines: sortedLowerLines,
      } = sortedBandLineMap

      const colors = sortedRowLines.map(line => lineData[line].fill)

      const rowValuesColumn = {
        key: valueColumnKey,
        name: `Latest ${valueColumnKey}:${bandName}`,
        type: spec.table.getColumnType(valueColumnKey),
        colors,
        values: formatLegendValues(
          bandValues,
          sortedRowLines.map(line => latestIndices[line]),
          valueFormatter
        ),
      }

      const tooltipAdditionalColumns = []

      if (upperColumnName) {
        tooltipAdditionalColumns.push({
          key: valueColumnKey,
          name: `${valueColumnKey}:${upperColumnName}`,
          type: spec.table.getColumnType(valueColumnKey),
          colors,
          values: formatLegendValues(
            bandValues,
            sortedUpperLines.map(line => latestIndices[line]),
            valueFormatter
          ),
        })
      }

      if (lowerColumnName) {
        tooltipAdditionalColumns.push({
          key: valueColumnKey,
          name: `${valueColumnKey}:${lowerColumnName}`,
          type: spec.table.getColumnType(valueColumnKey),
          colors,
          values: formatLegendValues(
            bandValues,
            sortedLowerLines.map(line => latestIndices[line]),
            valueFormatter
          ),
        })
      }

      const fillColumns = getTooltipBandGroupColumns(
        spec.table,
        sortedRowLines.map(line => latestIndices[line]),
        columnKeys,
        getColumnFormatter,
        colors
      )

      return [rowValuesColumn, ...tooltipAdditionalColumns, ...fillColumns]
    }