# All files / utils getMargins.ts

87.5% Statements 14/16
60% Branches 3/5
33.33% Functions 1/3
100% Lines 12/12

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

    2x

    2x

    6x
    3x

    3x
    3x

    3x

    3x

    3x

    3x

     import {getTextMetrics} from './textMetrics'
    import {maxBy} from './extrema'

    import {
      TICK_PADDING_TOP,
      TICK_PADDING_RIGHT,
      AXIS_LABEL_PADDING_BOTTOM,
    } from '../constants'

    export const getMargins = (
      showAxes: boolean,
      xAxisLabel: string,
      yAxisLabel: string,
      yTicks: Array<string | number>,
      yTickFormatter: (tick: number | string) => string,
      tickFont: string
    ) => {
      if (!showAxes) {
        return {top: 1, right: 1, bottom: 1, left: 1}
      }

      const formattedYTicks = yTicks.map(t => yTickFormatter(t))
      const longestYTick = maxBy(d => d.length, formattedYTicks)

      const {width: maxTextWidth, height: textHeight} = getTextMetrics(
        tickFont,
        longestYTick
      )

      const xAxisLabelHeight = xAxisLabel
        ? textHeight + AXIS_LABEL_PADDING_BOTTOM
        : 0

      const yAxisLabelHeight = yAxisLabel
        ? textHeight + AXIS_LABEL_PADDING_BOTTOM
        : 0

      return {
        top: textHeight / 2,
        right: 1,
        bottom: textHeight + TICK_PADDING_TOP + xAxisLabelHeight,
        left: maxTextWidth + TICK_PADDING_RIGHT + yAxisLabelHeight,
      }
    }