# All files / components ScatterLayer.tsx

45% Statements 9/20
0% Branches 0/8
0% Functions 0/2
60% Lines 9/15

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
    551x
    1x

    1x
    1x
    1x
    1x
    1x

    1x

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

    import {ScatterLayerConfig, ScatterLayerSpec, LayerProps} from '../types'
    import {drawPoints} from '../utils/drawPoints'
    import {useCanvas} from '../utils/useCanvas'
    import {ScatterHoverLayer} from './ScatterHoverLayer'
    import {SCATTER_POINT_SIZE} from '../constants'
    import {FILL, SYMBOL} from '../constants/columnKeys'

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

    export const ScatterLayer: FunctionComponent<Props> = props => {
      const {config, spec, width, height, xScale, yScale} = props

      const canvasRef = useRef<HTMLCanvasElement>(null)

      const drawPointsOptions = {
        xColData: spec.table.getColumn(config.x, 'number') || [],
        yColData: spec.table.getColumn(config.y, 'number') || [],
        fillColData: spec.table.getColumn(FILL, 'number') || [],
        symbolColData: spec.table.getColumn(SYMBOL, 'number') || [],
        xScale,
        yScale,
        fillScale: spec.scales.fill,
        symbolScale: spec.scales.symbol,
        pointSize: SCATTER_POINT_SIZE,
      }

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

      return (
        <>
          <canvas
            className="giraffe-layer scatter"
            ref={canvasRef}
            style={{position: 'absolute'}}
            data-testid="giraffe-layer--scatter"
          />
          <ScatterHoverLayer {...props} />
        </>
      )
    }

    ScatterLayer.displayName = 'ScatterLayer'