# All files / components/geo dimensionCalculations.ts

20.58% Statements 14/68
0% Branches 0/30
0% Functions 0/10
22.58% Lines 14/62

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

    3x

    3x

    3x
    3x

    3x

    3x

    3x
    3x

    3x

    3x

    3x

    3x

    3x

    3x
     // Types
    import {GeoTable, MinAndMax} from './processing/GeoTable'
    import {DashboardColor} from '../../types/geo'

    // Utils
    import {getMinAndMax} from './processing/GeoTable'

    const DEFAULT_COLOR = '#1010FF'

    interface Color {
      r: number
      g: number
      b: number
    }

    const FALLBACK_COLOR: Color = {r: 255, g: 0, b: 0}
    const colorCache: {[color: string]: Color} = {}

    const getColorThresholds = (
      c: DashboardColor[],
      value: number
    ): {low: DashboardColor; high: DashboardColor} => {
      for (let i = 0; i < c.length - 1; i++) {
        Iif (
          (c[i].type === 'min' || value >= c[i].value) &&
          (c[i + 1].type === 'max' || value <= c[i + 1].value)
        ) {
          return {low: c[i], high: c[i + 1]}
        }
      }
    }

    const getColorAsArray: (color: string) => Color = (color: string) => {
      const fromCache = colorCache[color]
      Iif (fromCache) {
        return fromCache
      }
      const match = color.match(/^#([0-9a-f]{6})$/i)[1]
      Iif (match) {
        const result = {
          r: parseInt(match.substr(0, 2), 16),
          g: parseInt(match.substr(2, 2), 16),
          b: parseInt(match.substr(4, 2), 16),
        }
        colorCache[color] = result
        return result
      }
      return FALLBACK_COLOR
    }

    const hex = (n: number) => n.toString(16).padStart(2, '0')
    const toHexColor: (c: Color) => string = c => {
      return `#${hex(c.r)}${hex(c.g)}${hex(c.b)}`
    }

    const fitIntoDefinedRange = (
      value: number,
      low: DashboardColor,
      high: DashboardColor
    ) => {
      let bottomFitted = value
      Iif (low.type === 'min' && value < low.value) {
        bottomFitted = low.value
      }

      Iif (high.type === 'max' && bottomFitted > high.value) {
        return high.value
      }

      return bottomFitted
    }

    const mixColors = (index: number, component1: number, component2: number) => {
      const c1 = Math.min(component1, component2)
      const c2 = Math.max(component1, component2)
      return Math.floor(c1 + index * (c2 - c1))
    }

    export const getColor = (
      color: DashboardColor[],
      value: number,
      interpolateColors: boolean
    ): string => {
      Iif (!color) {
        return DEFAULT_COLOR
      }
      Iif (value === undefined) {
        return color[0].hex
      }
      const {low, high} = getColorThresholds(color, value)
      Iif (!interpolateColors) {
        return value >= high.value ? high.hex : low.hex
      }
      const fittedValue = fitIntoDefinedRange(value, low, high)
      const interpolationMin = low.value
      const interpolationMax = high.value
      const delta = interpolationMax - interpolationMin
      Iif (delta === 0) {
        return low.hex
      }
      const index = (fittedValue - interpolationMin) / delta
      const minColor = getColorAsArray(low.hex)
      const maxColor = getColorAsArray(high.hex)

      return toHexColor({
        r: mixColors(index, minColor.r, maxColor.r),
        g: mixColors(index, minColor.g, maxColor.g),
        b: mixColors(index, minColor.b, maxColor.b),
      })
    }

    export const calculateMinAndMax = (
      bounds: string[],
      table: GeoTable,
      fieldName: string
    ) => {
      return bounds &&
        bounds.length === 2 &&
        bounds[0] !== null &&
        bounds[1] !== null &&
        bounds[0] !== '' &&
        bounds[1] !== ''
        ? {
            min: Number(bounds[0]),
            max: Number(bounds[1]),
          }
        : getMinAndMax(table, fieldName)
    }

    export const normalizeValue = (
      range: MinAndMax,
      maxValue: number,
      value: number
    ): number => {
      if (range) {
        const {min, max} = range
        const delta = max - min
        Iif (delta === 0) {
          return maxValue / 2
        }
        const clampedValue = Math.min(max, Math.max(min, value))
        return (maxValue * (clampedValue - min)) / delta
      } else {
        return maxValue / 2
      }
    }

    export const nameOf = <T>(name: keyof T): string => String(name)