# All files / components Legend.tsx

75.47% Statements 40/53
38.46% Branches 5/13
57.14% Functions 4/7
80.85% Lines 38/47

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

    1x
    1x

    1x
    1x
    1x
    1x
    2x

    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x

    1x

    1x

    1x

    1x

    1x

    2x

    1x
    2x
    2x
    2x
    2x
    2x
    2x
    2x

    2x

    2x

    2x

    2x

    1x

    1x

    1x
     import React, {CSSProperties} from 'react'
    import {FunctionComponent} from 'react'

    import {Config, LegendData, LegendType} from '../types'
    import {generateLegendStyles, LegendPillsStyles} from './LegendStyles'
    import {
      LEGEND_COLUMN_CLASSNAME,
      STATIC_LEGEND_COLUMN_CLASSNAME,
    } from '../constants'

    interface Props {
      type: LegendType
      data: LegendData
      config: Config
      isScrollable?: boolean
    }

    export const Legend: FunctionComponent<Props> = ({
      type: legendType,
      data,
      config,
      isScrollable = false,
    }) => {
      const {
        width,
        height,
        legendFontColor: fontColor,
        legendFontBrightColor: fontBrightColor,
        legendColumns: columnsWhitelist,
        legendOrientationThreshold: orientationThreshold,
        legendColorizeRows: colorizeRows,
      } = config

      let columns = []
      Iif (Array.isArray(columnsWhitelist)) {
        Iif (columnsWhitelist.length === 0) {
          return null
        }
        columns = data.filter(column => columnsWhitelist.includes(column.key))
      } else {
        columns = data
      }

      const switchToVertical = columns.length > orientationThreshold

      // 'switchToVertical': true
      //   each column of data displays vertically, and
      //   additional columns are next to the previous column, therefore,
      //   the limit is the horizontal space (width)
      // 'switchToVertical': false
      //   each column of data displays horizontally, and
      //   additional columns are stacked below the previous column, therefore,
      //   the limit is the vertical space (height)
      const maxLength = switchToVertical ? width : height

      const styles = generateLegendStyles(
        legendType,
        isScrollable,
        columns,
        switchToVertical,
        colorizeRows,
        fontColor,
        fontBrightColor
      )

      return (
        <div
          className="giraffe-legend-table"
          style={styles.table}
          data-testid="giraffe-legend-table"
        >
          {!colorizeRows && (
            <LegendPillColumn type={legendType} styles={styles.pills} />
          )}
          {columns.map(({name, values}, i) => (
            <LegendColumn
              type={legendType}
              key={name}
              name={name}
              maxLength={maxLength}
              values={values}
              columnStyle={styles.columns[i]}
              columnHeaderStyle={styles.headers}
              columnValueStyles={styles.values[i]}
            />
          ))}
        </div>
      )
    }

    interface LegendColumnProps {
      type: LegendType
      name: string
      maxLength: number
      values: string[]
      columnStyle: CSSProperties
      columnHeaderStyle: CSSProperties
      columnValueStyles: CSSProperties[]
    }

    const LegendColumn: FunctionComponent<LegendColumnProps> = ({
      type: legendType,
      name,
      maxLength,
      values,
      columnStyle,
      columnHeaderStyle,
      columnValueStyles,
    }) => {
      const valuesLimitedByPlotDimensions = values.slice(0, maxLength)
      const classNameBase =
        legendType === 'static'
          ? STATIC_LEGEND_COLUMN_CLASSNAME
          : LEGEND_COLUMN_CLASSNAME

      return (
        <div className={classNameBase} style={columnStyle}>
          <div className={`${classNameBase}-header`} style={columnHeaderStyle}>
            {name}
          </div>
          {valuesLimitedByPlotDimensions.map((value, i) => (
            <div
              className={`${classNameBase}-value`}
              key={i}
              style={columnValueStyles[i]}
            >
              {String(value)}
            </div>
          ))}
        </div>
      )
    }

    LegendColumn.displayName = 'LegendColumn'

    interface LegendPillColumnProps {
      type: LegendType
      styles: LegendPillsStyles
    }

    const LegendPillColumn: FunctionComponent<LegendPillColumnProps> = ({
      type: legendType,
      styles,
    }) => {
      const {column, header, value, pills} = styles
      const classNameBase =
        legendType === 'static'
          ? STATIC_LEGEND_COLUMN_CLASSNAME
          : LEGEND_COLUMN_CLASSNAME

      return (
        <div className={classNameBase} style={column}>
          <div className={`${classNameBase}-header`} style={header}>
            &nbsp;
          </div>
          {pills.map((pill, i) => (
            <div className={`${classNameBase}-value`} key={i} style={value}>
              <div className={`${classNameBase}-pill`} style={pill} />
            </div>
          ))}
        </div>
      )
    }

    LegendPillColumn.displayName = 'LegendPillColumn'