# All files / components Legend.tsx

14.89% Statements 7/47
0% Branches 0/10
0% Functions 0/7
17.07% Lines 7/41

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

    1x

    1x

    1x

    1x

    1x

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

    import {LegendData, Config} from '../types'
    import {generateLegendStyles, LegendPillsStyles} from './LegendStyles'

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

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

      let columns = []
      if (Array.isArray(columnsWhitelist)) {
        if (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(
        isScrollable,
        columns,
        switchToVertical,
        colorizeRows,
        fontColor,
        fontBrightColor
      )

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

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

    const LegendColumn: FunctionComponent<LegendColumnProps> = ({
      name,
      maxLength,
      values,
      columnStyle,
      columnHeaderStyle,
      columnValueStyles,
    }) => {
      const valuesLimitedByPlotDimensions = values.slice(0, maxLength)
      return (
        <div className="giraffe-legend-column" style={columnStyle}>
          <div className="giraffe-legend-column-header" style={columnHeaderStyle}>
            {name}
          </div>
          {valuesLimitedByPlotDimensions.map((value, i) => (
            <div
              className="giraffe-legend-column-value"
              key={i}
              style={columnValueStyles[i]}
            >
              {String(value)}
            </div>
          ))}
        </div>
      )
    }

    LegendColumn.displayName = 'LegendColumn'

    interface LegendPillColumnProps {
      styles: LegendPillsStyles
    }

    const LegendPillColumn: FunctionComponent<LegendPillColumnProps> = ({
      styles,
    }) => {
      const {column, header, value, pills} = styles

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

    LegendPillColumn.displayName = 'LegendPillColumn'