# All files / utils/legend staticLegend.ts

95.12% Statements 39/41
72.22% Branches 26/36
100% Functions 9/9
95% Lines 38/40

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

    1x

    1x

    1x

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

    2x
    2x

    8x
    2x
    8x
    8x
    8x

    2x
    2x

    2x

    2x
    2x
    1x
    1x

    4x

    1x
    1x
    4x

    4x

    1x

    4x

    2x
    6x
    24x
    24x

    6x

    2x

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

    import {getDataSortOrder} from './sort'

    export const convertLineSpec = (
      staticLegend: StaticLegend,
      spec: LineLayerSpec,
      getColumnFormatter: (colKey: string) => Formatter,
      valueColumnKey: string,
      position: LinePosition
    ): LegendData => {
      const {valueAxis} = staticLegend
      const valueFormatter = getColumnFormatter(valueColumnKey)
      const mappings = spec?.columnGroupMaps?.fill?.mappings
      const latestValueIndices = spec?.columnGroupMaps?.latestIndices
      const lineValues = spec.table.getColumn(valueColumnKey)
      const fillIndices = spec.table.getColumn(FILL)
      const lineData: LineData = spec?.lineData

      const domainLabel = valueAxis === 'x' ? DomainLabel.X : DomainLabel.Y
      const sortOrder = getDataSortOrder(
        lineData,
        Object.values(latestValueIndices),
        position,
        domainLabel
      )

      const colors = sortOrder.map(index => lineData[`${fillIndices[index]}`].fill)
      const values = sortOrder.map(index => {
        const latestValue = lineValues[index]
        Eif (latestValue === latestValue) {
          return valueFormatter(latestValue)
        }
        return ''
      })

      const {columnKeys} = spec.columnGroupMaps.fill
      Iif (!Array.isArray(columnKeys)) {
        return null
      }

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

      const additionalColumns = []
      if (position === 'stacked') {
        const stackedDomainValues = spec.stackedDomainValueColumn ?? []
        additionalColumns.push({
          key: valueColumnKey,
          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: valueColumnKey,
          name: LINE_COUNT,
          type: spec.table.getColumnType(valueColumnKey),
          colors,
          values: sortOrder.map(
            index => lineCountByGroupId[`${fillIndices[index]}`]
          ),
        })
      }

      const fillColumns = columnKeys.map(key => {
        const column: string[] = mappings.map(fillColumn => {
          const fillFormatter = getColumnFormatter(key)
          return fillFormatter(fillColumn[key])
        })

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

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