# All files / components BandLayer.tsx

33.33% Statements 14/42
0% Branches 0/16
0% Functions 0/3
33.33% Lines 14/42

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

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

    1x
    1x

    1x

    1x
     import React, {FunctionComponent, RefObject, useMemo} from 'react'

    import {LayerProps, BandLayerSpec, BandLayerConfig} from '../types'
    import {BandHoverLayer} from './BandHoverLayer'
    import {simplifyLineData} from '../utils/lineData'
    import {useCanvas} from '../utils/useCanvas'
    import {drawBands} from '../utils/drawBands'
    import {useHoverPointIndices} from '../utils/useHoverPointIndices'
    import {useBandHoverColumns} from '../utils/useBandHover'
    import {FILL} from '../constants/columnKeys'
    import {getBandHoverIndices, getLineLengths} from '../utils/bandHover'
    import {
      groupLineIndicesIntoBands,
      alignMinMaxWithBand,
    } from '../transforms/band'

    export interface Props extends LayerProps {
      spec: BandLayerSpec
      config: BandLayerConfig
      canvasRef: RefObject<HTMLCanvasElement>
    }

    const HIGHLIGHT_HOVERED_LINE = 0.4
    const NO_HIGHLIGHT = 1

    export const BandLayer: FunctionComponent<Props> = props => {
      const {
        canvasRef,
        config,
        height,
        hoverX,
        hoverY,
        spec,
        width,
        xScale,
        yScale,
      } = props

      const {
        lowerColumnName = '',
        mainColumnName: rowColumnName,
        upperColumnName = '',
      } = config

      const simplifiedLineData = useMemo(
        () =>
          simplifyLineData(
            alignMinMaxWithBand(spec.lineData, spec.bandIndexMap),
            xScale,
            yScale
          ),
        [spec.lineData, xScale, yScale]
      )

      const drawBandsOptions = {
        bandIndexMap: spec.bandIndexMap,
        interpolation: config.interpolation,
        lineData: simplifiedLineData,
        lineWidth: config.lineWidth,
        lineOpacity: config.lineOpacity,
        shadeOpacity: config.shadeOpacity,
      }

      useCanvas(
        canvasRef,
        width,
        height,
        context => drawBands({context, ...drawBandsOptions}),
        Object.values(drawBandsOptions)
      )

      let hoverDimension: 'x' | 'y' | 'xy'

      if (config.hoverDimension === 'auto') {
        hoverDimension = 'x'
        if (Object.keys(spec.lineData).length > config.maxTooltipRows) {
          hoverDimension = 'xy'
        }
      } else {
        hoverDimension = config.hoverDimension
      }

      // Band Plot allows hovering on the nearest band or bands,
      // and any hoverable point should be associate with a band
      const hoverableColumnData = useBandHoverColumns(
        hoverX,
        hoverY,
        spec.lineData,
        spec.table.getColumn(FILL, 'number'),
        spec.bandIndexMap,
        width,
        height
      )

      const hoverRowIndices = useHoverPointIndices(
        hoverDimension,
        hoverX,
        hoverY,
        hoverableColumnData.xs,
        hoverableColumnData.ys,
        hoverableColumnData.groupColData,
        xScale,
        yScale,
        width,
        height
      )

      const lineLengths = getLineLengths(spec.lineData)

      const bandHoverIndices = getBandHoverIndices(
        lineLengths,
        hoverRowIndices,
        hoverableColumnData.groupColData,
        groupLineIndicesIntoBands(
          spec.columnGroupMaps.fill,
          lowerColumnName,
          rowColumnName,
          upperColumnName
        )
      )

      const hasHoverData = hoverRowIndices && hoverRowIndices.length > 0

      return (
        <>
          <canvas
            className="giraffe-layer giraffe-layer-band-chart"
            ref={canvasRef}
            style={{
              position: 'absolute',
              opacity:
                hoverDimension === 'xy' && hasHoverData
                  ? HIGHLIGHT_HOVERED_LINE
                  : NO_HIGHLIGHT,
            }}
            data-testid="giraffe-layer-band-chart"
          />
          {hasHoverData && (
            <BandHoverLayer
              {...props}
              bandHoverIndices={bandHoverIndices}
              dimension={hoverDimension}
              simplifiedLineData={simplifiedLineData}
            />
          )}
        </>
      )
    }

    BandLayer.displayName = 'BandLayer'