# All files / utils lineData.ts

33.33% Statements 14/42
40% Branches 2/5
40% Functions 2/5
45.16% Lines 14/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
    7x
    7x

    7x

    7x

    7x

    18x
    18x
    18x
    10004080x
    1624x

    10004080x
    10004080x
    10004080x
    10004080x

     import {DomainLabel, LineData, Scale, Table} from '../types'
    import {simplify} from '../utils/simplify'
    import {FILL} from '../constants/columnKeys'

    export const collectLineData = (
      table: Table,
      xColKey: string,
      yColKey: string,
      fillScale: Scale<string, string>
    ): LineData => {
      const xCol = table.getColumn(xColKey, 'number')
      const yCol = table.getColumn(yColKey, 'number')
      const groupCol = table.getColumn(FILL, 'string')

      const result = {}

      for (let i = 0; i < table.length; i++) {
        const groupKey = groupCol[i]

        Iif (!result[groupKey]) {
          result[groupKey] = {
            xs: [],
            ys: [],
            fill: fillScale(groupKey),
          }
        }

        result[groupKey].xs.push(xCol[i])
        result[groupKey].ys.push(yCol[i])
      }

      return result
    }

    export const simplifyLineData = (
      lineData: LineData,
      xScale,
      yScale
    ): LineData => {
      const result = {}

      for (const [k, {xs, ys, fill}] of Object.entries(lineData)) {
        const [simplifedXs, simplifiedYs] = simplify(
          xs.map(x => xScale(x || 0)),
          ys.map(y => yScale(y || 0)),
          0.5
        )

        result[k] = {xs: simplifedXs, ys: simplifiedYs, fill}
      }

      return result
    }

    export const getDomainDataFromLines = (
      lineData: LineData,
      fillCol: Array<number | string>,
      domainLabel: DomainLabel
    ): Array<number> => {
      if (Array.isArray(fillCol)) {
        const counters = {}
        return fillCol.map(line => {
          if (!counters[line]) {
            counters[line] = 0
          }
          const index = counters[line]
          const value = lineData[line][domainLabel][index]
          counters[line] += 1
          return value
        })
      }
      return []
    }