# All files / components ScatterHoverLayer.tsx

28.57% Statements 10/35
0% Branches 0/4
0% Functions 0/2
28.57% Lines 10/35

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

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

    1x

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

    import {ScatterLayerConfig, ScatterLayerSpec, LayerProps} from '../types'
    import {useHoverPointIndices} from '../utils/useHoverPointIndices'
    import {drawPoints} from '../utils/drawPoints'
    import {Tooltip} from './Tooltip'
    import {getPointsTooltipData} from '../utils/legend/tooltip'
    import {SCATTER_HOVER_POINT_SIZE} from '../constants'
    import {FILL, SYMBOL} from '../constants/columnKeys'
    import {useCanvas} from '../utils/useCanvas'

    interface Props extends LayerProps {
      spec: ScatterLayerSpec
      config: ScatterLayerConfig
    }

    export const ScatterHoverLayer: FunctionComponent<Props> = ({
      config,
      plotConfig,
      spec,
      width,
      height,
      xScale,
      yScale,
      hoverX,
      hoverY,
      columnFormatter,
    }) => {
      const xColData = spec.table.getColumn(config.x, 'number')
      const yColData = spec.table.getColumn(config.y, 'number')
      const fillColData = spec.table.getColumn(FILL, 'number')
      const symbolColData = spec.table.getColumn(SYMBOL, 'number')
      const fillScale = spec.scales.fill
      const symbolScale = spec.scales.symbol

      const rowIndices = useHoverPointIndices(
        'xy',
        hoverX,
        hoverY,
        xColData,
        yColData,
        [],
        xScale,
        yScale,
        width,
        height
      )

      const canvasRef = useRef<HTMLCanvasElement>(null)

      const drawPointsOptions = {
        width,
        height,
        xColData,
        yColData,
        fillColData,
        symbolColData,
        xScale,
        yScale,
        fillScale,
        symbolScale,
        rowIndices,
        pointSize: SCATTER_HOVER_POINT_SIZE,
      }

      useCanvas(canvasRef, width, height, context =>
        drawPoints({context, ...drawPointsOptions})
      )

      if (!rowIndices) {
        return null
      }

      const tooltipData = getPointsTooltipData(
        rowIndices,
        spec.table,
        config.x,
        config.y,
        FILL,
        columnFormatter,
        [...new Set([...config.fill, ...config.symbol])],
        fillScale
      )

      return (
        <>
          <canvas
            className="giraffe-layer scatter-interactions"
            ref={canvasRef}
            style={{position: 'absolute'}}
            data-testid="giraffe-layer--scatter-interact"
          />
          {tooltipData && <Tooltip data={tooltipData} config={plotConfig} />}
        </>
      )
    }

    ScatterHoverLayer.displayName = 'ScatterHoverLayer'