# All files / utils lineData.ts

35.71% Statements 15/42
25% Branches 2/8
25% Functions 1/4
44.83% Lines 13/29

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

    7x

    7x

    7x

    15x
    15x
    15x
    1540x
    1540x
    1540x
    10003244x

    15x

     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]

        if (!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,
      domainLabel: DomainLabel
    ): number[] => {
      const result = []
      const numberOfLines = Object.keys(lineData).length
      for (let lineIndex = 0; lineIndex < numberOfLines; lineIndex += 1) {
        const line = lineData[lineIndex] ? lineData[lineIndex][domainLabel] : []
        Eif (Array.isArray(line)) {
          for (let domainIndex = 0; domainIndex < line.length; domainIndex += 1) {
            result.push(line[domainIndex])
          }
        }
      }
      return result
    }