# All files / utils annotationData.ts

27.5% Statements 11/40
3.22% Branches 1/31
22.22% Functions 2/9
28.94% Lines 11/38

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
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    2x

    2x

    2x

    2x

    2x
    10x
    5x

    5x

    2x
    5x

    2x

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

    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
    }

    // is the mouse within the start/stop area ? (using the margin)
    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
      Iif (annotationMark.startValue !== annotationMark.stopValue) {
        Iif (
          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)
    }

    /**
     * Summary: do a weighted comparison between the distance of the mouse to range and point
     * annotations.
     *
     * Details:
     * While a mouse is hovering within a range annotation, the 'distance' is zero.
     *
     * But there might be a point annotation overlapping the range,
     * and then when the mouse is directly over that point, it should show the tooltip for
     * the point and not the range
     *
     * Without weighting it, the point annotation hovered tooltip will never show.
     * (very hard to get it *exactly*; and even if so, 0 === 0)
     *
     * If the types of the annotations are different, 'weighting' the range annotation by a small factor
     * (ANNOTATION_DEFAULT_OVERLAP_HOVER_MARGIN, which is equal to 8)
     * so that when you are *very* close to the point;  then the point, and not the range annotation
     * will show up in the tooltip popup.
     *
     * If the two types of annotations being compared are the same, then they will be weighed the same
     * and the weights will cancel each other out and it will be a simple comparison.
     *
     * NOTE:  the overlap hover margin is currently less than the default hover margin (which is 20)
     * If the default hover margin gets below the the overlap hover margin (or the user sets a lower one),
     * then the overlap margin should be reduced.
     */
    const getWeightedDistance = distance => {
      if (distance.annoType === 'range') {
        return distance.dist + ANNOTATION_DEFAULT_OVERLAP_HOVER_MARGIN
      }
      return distance.dist
    }

    export const sortDistances = (dist1, dist2) => {
      return getWeightedDistance(dist1) - getWeightedDistance(dist2)
    }

    /***********************************************************************************
     * ANNOTATIONS HOVERING
     *
     * This returns the index of the annotation in the cell that should have its
     *  tooltip popup appear.
     *
     * 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'
     *
     *  This method accounts for overlapping annotations; if a point annotation is within a range
     *  annotation, the point annotation tooltip *will* show up when the mouse is within
     *  the ANNOTATION_DEFAULT_OVERLAP_HOVER_MARGIN, which is equal to 10;
     *  which is essentially only when the mouse is right on top of the line.
     *
     *  (see previous method to sort distances for details)
     *
     */
    export const getAnnotationHoverIndices = (
      hoverDimension: LineHoverDimension,
      hoverMargin: number,
      annotationData: AnnotationMark[],
      hoverX: number,
      hoverY: number
    ): number[] => {
      return Array.isArray(annotationData)
        ? annotationData.reduce((result, annotation, i) => {
            Iif (hoverX === null || hoverY === null) {
              return result
            }
            const {dimension, startValue, stopValue} = annotation
            Iif (
              (hoverDimension !== 'xy' && hoverDimension !== dimension) ||
              ((hoverDimension === 'xy' || hoverDimension === dimension) &&
                isWithinHoverableArea(
                  startValue,
                  stopValue,
                  hoverMargin,
                  dimension === 'x' ? hoverX : hoverY
                ))
            ) {
              result.push(i)

              Iif (result.length > 1) {
                const distances = []
                result.forEach(i => {
                  const dist = distanceToMousePointer(
                    annotationData[i],
                    dimension === 'x' ? hoverX : hoverY
                  )
                  const annoType =
                    annotationData[i].startValue === annotationData[i].stopValue
                      ? 'point'
                      : 'range'
                  distances.push({index: i, dist, annoType})
                })

                const closestAnnotation = distances.sort(sortDistances)[0].index
                result = [closestAnnotation]
              }
            }
            return result
          }, [])
        : []
    }