# All files / utils rectTooltip.ts

10.53% Statements 6/57
0% Branches 0/14
0% Functions 0/14
11.11% Lines 6/54

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

    1x
    1x

    1x

    1x

    1x

     import {range} from 'd3-array'

    import {getRangeLabel} from './tooltip'
    import {X_MIN, X_MAX, Y_MIN, Y_MAX, FILL, COUNT} from '../constants/columnKeys'
    import {
      Table,
      Scale,
      TooltipData,
      TooltipColumn,
      ColumnGroupMap,
    } from '../types'

    export const findHoveredRects = (
      rectTable: Table,
      hoverX: number | null,
      hoverY: number | null,
      xScale: Scale<number, number>,
      yScale: Scale<number, number>,
      binDimension: 'xy' | 'x'
    ): number[] => {
      if (!hoverX || !hoverY) {
        return []
      }

      const xMinData = rectTable.getColumn(X_MIN, 'number')
      const xMaxData = rectTable.getColumn(X_MAX, 'number')
      const yMinData = rectTable.getColumn(Y_MIN, 'number')
      const yMaxData = rectTable.getColumn(Y_MAX, 'number')
      const dataX = xScale.invert(hoverX)
      const dataY = yScale.invert(hoverY)

      // Find all bins whose x extent contain the mouse x position
      const xIndices = range(0, xMinData.length).filter(
        i => xMinData[i] <= dataX && xMaxData[i] > dataX
      )

      if (binDimension === 'x' && !xIndices.some(i => yMaxData[i] >= dataY)) {
        return []
      } else if (binDimension === 'x') {
        return xIndices
      }
      const xyIndices = xIndices.filter(
        i => yMinData[i] <= dataY && yMaxData[i] > dataY
      )
      return xyIndices
    }

    export const get1DTooltipData = (
      hoveredRectRows: number[],
      rectTable: Table,
      inputTable: Table,
      xColKey: string,
      fillGroupMap: ColumnGroupMap,
      fillScale: Scale<number, string>,
      columnFormatter: (colKey: string) => (x: any) => string
    ): TooltipData => {
      const xMinCol = rectTable.getColumn(X_MIN, 'number')
      const xMaxCol = rectTable.getColumn(X_MAX, 'number')
      const countCol = rectTable.getColumn(COUNT, 'number')
      const fillCol = rectTable.getColumn(FILL, 'number')
      const xFormatter = columnFormatter(xColKey)
      const countFormatter = columnFormatter(COUNT)
      const colors = hoveredRectRows.map(i => fillScale(fillCol[i]))

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

      const countTooltipColumn: TooltipColumn = {
        key: COUNT,
        name: 'count',
        type: 'number',
        colors,
        values: hoveredRectRows.map(i => countFormatter(countCol[i])),
      }

      const groupTooltipColumns = fillGroupMap.columnKeys.map(key => ({
        key,
        name: inputTable.getColumnName(key),
        type: inputTable.getColumnType(key),
        colors,
        values: hoveredRectRows.map(i =>
          columnFormatter(key)(fillGroupMap.mappings[fillCol[i]][key])
        ),
      }))

      const tooltipColumns = [
        xTooltipColumn,
        countTooltipColumn,
        ...groupTooltipColumns,
      ]

      return tooltipColumns
    }

    export const get2DTooltipData = (
      hoveredRectRows: number[],
      rectTable: Table,
      inputTable: Table,
      xColKey: string,
      yColKey: string,
      columnFormatter: (colKey: string) => (x: any) => string
    ): TooltipData => {
      const xMinCol = rectTable.getColumn(X_MIN, 'number')
      const xMaxCol = rectTable.getColumn(X_MAX, 'number')
      const yMinCol = rectTable.getColumn(Y_MIN, 'number')
      const yMaxCol = rectTable.getColumn(Y_MAX, 'number')
      const countCol = rectTable.getColumn(COUNT, 'number')
      const xFormatter = columnFormatter(xColKey)
      const yFormatter = columnFormatter(yColKey)
      const countFormatter = columnFormatter(COUNT)

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

      const countTooltipColumn: TooltipColumn = {
        key: COUNT,
        name: 'count',
        type: 'number',
        colors: null,
        values: hoveredRectRows.map(i => countFormatter(countCol[i])),
      }

      const yTooltipColumn: TooltipColumn = {
        key: yColKey,
        name: inputTable.getColumnName(yColKey),
        type: inputTable.getColumnType(yColKey),
        colors: null,
        values: hoveredRectRows.map(i =>
          getRangeLabel(yMinCol[i], yMaxCol[i], yFormatter)
        ),
      }

      const tooltipColumns = [xTooltipColumn, yTooltipColumn, countTooltipColumn]

      return tooltipColumns
    }