# All files / utils annotationData.ts

17.65% Statements 3/17
0% Branches 0/23
0% Functions 0/5
20% Lines 3/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
    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

    1x

    1x

    1x

     import {AnnotationMark, LineHoverDimension, Scale} from '../types'

    export const getAnnotationsPositions = (
      annotationData: AnnotationMark[],
      xScale: Scale,
      yScale: Scale
    ): AnnotationMark[] => {
      const annotationMarks: AnnotationMark[] = []

      annotationData.forEach(annotation => {
        annotationMarks.push({
          ...annotation,
          pin: annotation.pin || 'none',
          startValue:
            annotation.dimension === 'y'
              ? yScale(annotation.startValue)
              : xScale(annotation.startValue),
          stopValue:
            annotation.dimension === 'y'
              ? yScale(annotation.stopValue)
              : xScale(annotation.stopValue),
        })
      })
      return annotationMarks
    }

    const isWithinHoverableArea = (
      startValue: number,
      stopValue: number,
      margin: number,
      hoverPosition: number
    ): boolean => {
      return (
        startValue - margin <= hoverPosition && stopValue + margin >= hoverPosition
      )
    }

    /***********************************************************************************
     * ANNOTATIONS HOVERING
     *
     * hover dimension 'xy' means find annotations only near the mouse position
     * hover dimension 'x' means find annotations along the x-axis at mouse position
     * hover dimension 'y' means find annotations along the y-axis at mouse position
     * hover dimension 'auto' means 'xy' see above
     *
     * Check to see if mouse position is within annotation's hoverable area only when
     *   hover dimension is 'xy' or hover dimension is same as annotation dimension
     *
     * Cross dimensions means the annotation is always hovered, occurs when:
     *   hover dimension is 'x' && annotation dimension is 'y'
     *   hover dimension is 'y' && annotation dimension is 'x'
     *
     */
    export const getAnnotationHoverIndices = (
      hoverDimension: LineHoverDimension,
      hoverMargin: number,
      annotationData: AnnotationMark[],
      hoverX: number,
      hoverY: number
    ): number[] => {
      return Array.isArray(annotationData)
        ? annotationData.reduce((result, annotation, i) => {
            if (hoverX === null || hoverY === null) {
              return result
            }
            const {dimension, startValue, stopValue} = annotation
            if (
              (hoverDimension !== 'xy' && hoverDimension !== dimension) ||
              ((hoverDimension === 'xy' || hoverDimension === dimension) &&
                isWithinHoverableArea(
                  startValue,
                  stopValue,
                  hoverMargin,
                  dimension === 'x' ? hoverX : hoverY
                ))
            ) {
              result.push(i)
            }
            return result
          }, [])
        : []
    }