# All files / utils getMargins.ts

37.5% Statements 6/16
16.67% Branches 1/6
33.33% Functions 1/3
50% Lines 6/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
    451x
    1x

    1x

    1x

    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
    ) => {
      Eif (!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,
      }
    }