# All files / utils/legend mosaicTooltip.ts

10.42% Statements 5/48
0% Branches 0/24
0% Functions 0/10
10.64% Lines 5/47

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

    1x
    1x

    1x

    1x

     import {range} from 'd3-array'

    import {getRangeLabel} from './tooltip'
    import {
      DISPLAY_NAME,
      FILL,
      SERIES,
      X_MAX,
      X_MIN,
    } from '../../constants/columnKeys'
    import {
      Table,
      Scale,
      LegendData,
      LegendColumn,
      ColumnGroupMap,
    } from '../../types'

    export const findHoveredBoxes = (
      hoverDimension: string,
      hoverX: number | null,
      hoverY: number | null,
      boxTable: Table,
      xScale: Scale<number, number>,
      yScale: Scale<number, number>,
      yDomain: number[],
      yTicks: string[],
      width: number,
      height: number
    ): number[] => {
      const isActive =
        hoverX !== undefined &&
        hoverX !== null &&
        hoverX >= 0 &&
        hoverX < width &&
        hoverY !== undefined &&
        hoverY !== null &&
        hoverY >= 0 &&
        hoverY <= height

      if (!isActive) {
        return []
      }

      const xMinData = boxTable.getColumn(X_MIN, 'number')
      const xMaxData = boxTable.getColumn(X_MAX, 'number')
      const yTickData = boxTable.getColumn(SERIES, 'string')

      const dataX = xScale.invert(hoverX)
      const dataY = yScale.invert(hoverY)

      const xRange = range(0, xMinData.length)
      const hoveredYTick =
        Math.floor(dataY) >= yDomain[0] && Math.floor(dataY) < yDomain[1]
          ? yTicks[Math.floor(dataY)]
          : ''

      const containsHoverX = (index: number): boolean =>
        xMinData[index] <= dataX && xMaxData[index] > dataX
      const containsHoverY = (index: number): boolean =>
        yTickData[index] === hoveredYTick

      /****************************************************************************
       * Mosaic hover dimensions
       * 'xy' means a single piece at the mouse position
       * 'x'  means all pieces with the same time value along the y-axis at mouse position
       * 'y'  means all pieces with the same y-tick along the x-axis at mouse position
       * 'auto' menas 'xy' see above
       */

      if (hoverDimension === 'x') {
        return xRange.filter(containsHoverX)
      }

      if (hoverDimension === 'y') {
        return xRange.filter(containsHoverY)
      }

      // Otherwise treat as 'xy' regardless of what the hoverDimension is
      return xRange.filter(i => containsHoverX(i) && containsHoverY(i))
    }

    export const getMosaicTooltipData = (
      hoveredRowIndices: number[],
      boxTable: Table,
      inputTable: Table,
      xColKey: string,
      yColKey: string,
      fillGroupMap: ColumnGroupMap,
      fillScale: Scale<number, string>,
      columnFormatter: (colKey: string) => (x: any) => string
    ): LegendData => {
      const xMinCol = boxTable.getColumn(X_MIN, 'number')
      const xMaxCol = boxTable.getColumn(X_MAX, 'number')
      const valCol = boxTable.getColumn(FILL, 'string')
      const yCol = boxTable.getColumn(DISPLAY_NAME, 'string')
      const xFormatter = columnFormatter(xColKey)
      const yFormatter = columnFormatter(yColKey)
      const valFormatter = columnFormatter(FILL)

      // Use reverse order because
      // Mosaic graph contents are filled in from bottom to top
      // Mosaic tooltip contents are filled in from top to bottom
      const hoveredBoxRows = hoveredRowIndices.reverse()

      const colors = hoveredBoxRows.map(i =>
        fillScale((valCol[i] as unknown) as number)
      )

      const xTooltipColumn: LegendColumn = {
        key: xColKey,
        name: inputTable.getColumnName(xColKey),
        type: inputTable.getColumnType(xColKey),
        colors,
        values: hoveredBoxRows.map(i =>
          getRangeLabel(xMinCol[i], xMaxCol[i], xFormatter)
        ),
      }

      const yTooltipColumn: LegendColumn = {
        key: yColKey,
        name: yColKey,
        type: 'string',
        colors,
        values: hoveredBoxRows.map(i => yFormatter(yCol[i])),
      }

      const durationTooltipColumn: LegendColumn = {
        key: 'duration column',
        name: 'Duration',
        type: 'number',
        colors,
        values: hoveredBoxRows.map(
          i => ((((xMaxCol[i] - xMinCol[i]) / 1000) as unknown) + ' sec') as string
        ),
      }

      // Mosaic uses only one key for the fill column
      //   By capturing this in an array, we can destructure to get
      //   either the fill column or nothing into the tooltips
      const fillColumns = []
      const key = fillGroupMap.columnKeys[0]
      if (key) {
        fillColumns.push({
          key,
          name: inputTable.getColumnName(key),
          type: inputTable.getColumnType(key),
          colors,
          values: hoveredBoxRows.map(i => valFormatter(valCol[i])),
        })
      }
      const tooltipColumns = [
        xTooltipColumn,
        ...fillColumns,
        yTooltipColumn,
        durationTooltipColumn,
      ]
      return tooltipColumns
    }