# All files / utils annotationData.ts

15.15% Statements 5/33
0% Branches 0/33
0% Functions 0/7
16.13% Lines 5/31

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

    1x

    1x

    1x

    1x

     import {AnnotationMark, LineHoverDimension, Scale} from '../types'
    import {min} from 'd3-array'

    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
      )
    }

    const distanceToMousePointer = (
      annotationMark: AnnotationMark,
      hoverPosition: number
    ) => {
      // check if the Annotation is a range type
      if (annotationMark.startValue !== annotationMark.stopValue) {
        if (
          annotationMark.startValue <= hoverPosition &&
          hoverPosition <= annotationMark.stopValue
        ) {
          // hover position is in the range, effective distance is 0
          return 0
        }
        // distance is the shortest of:
        // the distance between hoverPosition and annotation.startValue,
        // and the distance between hoverPosition and annotation.stopValue
        const distanceToStartLine = Math.abs(
          hoverPosition - annotationMark.startValue
        )
        const distanceToStopLine = Math.abs(
          hoverPosition - annotationMark.stopValue
        )
        return Math.min(distanceToStartLine, distanceToStopLine)
      }
      return Math.abs(hoverPosition - annotationMark.startValue)
    }

    /***********************************************************************************
     * 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)

              if (result.length > 1) {
                const distances = []
                result.forEach(i => {
                  const dist = distanceToMousePointer(
                    annotationData[i],
                    dimension === 'x' ? hoverX : hoverY
                  )
                  distances.push(dist)
                })
                const closestAnnotation = result[distances.indexOf(min(distances))]
                result = [closestAnnotation]
              }
            }
            return result
          }, [])
        : []
    }