# All files / src/components YAxis.jsx

100% Statements 9/9
87.5% Branches 7/8
100% Functions 1/1
100% Lines 9/9

    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

    2x

    2x

    4x
    4x
    4x

    2x
    2x
    2x
    2x
     import React from 'react';
    import PropTypes from 'prop-types';
    import { AxisLeft, AxisRight } from '@vx/axis';

    import { numTicksForHeight } from '../utils/scale-utils';
    import { yAxisStyles, yTickStyles } from '../theme';

    const propTypes = {
      hideZero: PropTypes.bool,
      label: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
      labelOffset: PropTypes.number,
      numTicks: PropTypes.number,
      orientation: PropTypes.oneOf(['left', 'right']),
      rangePadding: PropTypes.number,
      tickLabelComponent: PropTypes.element,
      tickFormat: PropTypes.func,
      scale: PropTypes.func.isRequired,
      width: PropTypes.number.isRequired,
    };

    const defaultProps = {
      axisStyles: {},
      hideZero: false,
      label: null,
      labelOffset: 0,
      numTicks: null,
      orientation: 'left',
      rangePadding: null,
      tickFormat: null,
      tickLabelComponent: undefined,
      tickStyles: {},
    };

    export default function YAxis({
      hideZero,
      width,
      label,
      labelOffset,
      numTicks,
      orientation,
      rangePadding,
      scale,
      tickFormat,
      tickLabelComponent,
    }) {
      const Axis = orientation === 'left' ? AxisLeft : AxisRight;
      const height = Math.max(...scale.range());
      return (
        <Axis
          top={0}
          left={orientation === 'right' ? width : 0}
          rangePadding={rangePadding}
          hideTicks={numTicks === 0}
          hideZero={hideZero}
          label={typeof label === 'string' ?
            <text {...(yAxisStyles.label)[orientation]}>
              {label}
            </text>
            : label
          }
          labelOffset={labelOffset}
          numTicks={numTicksForHeight(height)}
          scale={scale}
          stroke={yAxisStyles.stroke}
          strokeWidth={yAxisStyles.strokeWidth}
          tickFormat={tickFormat}
          tickLength={yTickStyles.tickLength}
          tickStroke={yTickStyles.stroke}
          tickLabelComponent={
            tickLabelComponent ||
            <text {...(yTickStyles.label)[orientation]} />
          }
        />
      );
    }

    YAxis.width = 50;
    YAxis.propTypes = propTypes;
    YAxis.defaultProps = defaultProps;
    YAxis.displayName = 'YAxis';