# YAxis A vertical axis component for CartesianChart. Displays tick marks, labels, gridlines, and supports custom formatting, positioning, and data domains. ## Import ```tsx import { YAxis } from '@coinbase/cds-web-visualization' ``` ## Examples ### Basic Example The YAxis component provides a vertical axis for charts with automatic tick generation and labeling. ```jsx live ``` ### Axis Config Properties related to the scale of an axis are set on the Chart component. This includes `scaleType`, `domain`, `domainLimit`, `range`, `data`, and `categoryPadding`. #### Scale Type YAxis supports `linear` (default) and `log` scale types. Both `linear` and `log` are numeric scales. ```jsx live value.toLocaleString()} /> ``` #### Domain An axis's domain is the range of values that the axis will display. You can pass in either an object (AxisBounds) with `min` and `max` properties (both optional), or a function that receives initial `AxisBounds` and returns an adjusted `AxisBounds`. ```jsx live ({ min: min - 50, max: max + 50 }), }} > ``` ##### Domain Limit You can set the domain limit to `nice` (default for YAxis) or `strict`. `nice` will round the domain to human-friendly values, while `strict` will use the exact min/max values from the data. See [d3-scale](https://d3js.org/d3-scale/linear#linear_nice) for more details. #### Range An axis's range is the range of values that the axis will display in pixels. This is most useful for adjusting the sizing of the data inside of the chart's drawing area. You can pass in either an object (AxisBounds) with `min` and `max` properties (both optional), or a function that receives initial `AxisBounds` and returns an adjusted `AxisBounds`. ```jsx live ({ min: min + 96, max: max - 96 }), }} > ``` ### Axis Props Properties related to the visual appearance of the YAxis are set on the component itself. This includes `position`, `showGrid`, `showLine`, `showTickMarks`, `size`, `tickInterval`, `ticks`, `tickLabelFormatter`, and `tickMarkSize`. #### Position You can set the position of an axis to `left` or `right` (default). ```jsx live ``` #### Grid You can show grid lines at each tick position using the `showGrid` prop. ```jsx live function YAxisGridExample() { const [showGrid, setShowGrid] = useState(true); return ( setShowGrid(!showGrid)}> Show Grid ); } ``` You can also customize the grid lines using the `GridLineComponent` prop. ```jsx live function CustomGridLineExample() { const ThinSolidLine = memo((props: SolidLineProps) => ); const categories = Array.from({ length: 31 }, (_, i) => `3/${i + 1}`); const gains = [ 5, 0, 6, 18, 0, 5, 12, 0, 12, 22, 28, 18, 0, 12, 6, 0, 0, 24, 0, 0, 4, 0, 18, 0, 0, 14, 10, 16, 0, 0, 0, ]; const losses = [ -4, 0, -8, -12, -6, 0, 0, 0, -18, 0, -12, 0, -9, -6, 0, 0, 0, 0, -22, -8, 0, 0, -10, -14, 0, 0, 0, 0, 0, -12, -10, ]; const series = [ { id: 'gains', data: gains, color: 'var(--color-fgPositive)', stackId: 'bars' }, { id: 'losses', data: losses, color: 'var(--color-fgNegative)', stackId: 'bars' }, ]; return ( `$${value}M`} /> ); }; ``` #### Line You can show the axis line using the `showLine` prop. ```jsx live function YAxisLineExample() { const [showLine, setShowLine] = useState(true); return ( setShowLine(!showLine)}> Show Line ); } ``` You can also customize the axis line using the `classNames` and `styles` props. ```jsx live function YAxisLineStylesExample() { const [showLine, setShowLine] = useState(true); return ( setShowLine(!showLine)}> Show Line ); } ``` #### Size The `size` prop sets the size of the axis in pixels. The default is 44 for YAxis, but can be adjusted to fit the size of your data. ```jsx live value.toLocaleString()} /> ``` #### Ticks You can use the `ticks`, `requestedTickCount` (default for YAxis), and `tickInterval` props to control the number and placement of ticks on the YAxis. `ticks` accepts an array of numbers, which corresponds to the values of that axis that you would like to display ticks for. ```jsx live ``` Using `requestedTickCount` will use [D3's ticks function](https://d3js.org/d3-array/ticks#ticks) to determine the number and placement of ticks. Note that this count is not guaranteed to be respected. This is the default behavior for YAxis, and defaults to `5`. ```jsx live ``` `tickInterval`, which accepts a number for the gap between ticks in pixels, will measure the available space and try to create evenly spaced ticks. It will always include the first and last values of the domain. ```jsx live ``` #### Tick Marks You can show tick marks on the axis using the `showTickMarks` prop. You can also customize the tick mark size using the `tickMarkSize` prop. ```jsx live function YAxisTickMarksExample() { const [showTickMarks, setShowTickMarks] = useState(true); return ( setShowTickMarks(!showTickMarks)}> Show Tick Marks ); } ``` #### Tick Labels You can customize the tick labels using the `tickLabelFormatter` prop. ```jsx live `$${value}`} /> ``` ### Customization #### Multiple Y Axes ```jsx live `$${value}k`} /> `${value}%`} /> Revenue ($) Profit Margin (%) ``` ## Props | Prop | Type | Required | Default | Description | | --- | --- | --- | --- | --- | | `GridLineComponent` | `LineComponent` | No | `DottedLine` | Component to render the grid lines. | | `axisId` | `string` | No | `-` | The ID of the axis to render. Defaults to defaultAxisId if not specified. | | `className` | `string` | No | `-` | Custom className for the axis. | | `classNames` | `{ root?: string; tickLabel?: string \| undefined; gridLine?: string \| undefined; line?: string \| undefined; tickMark?: string \| undefined; } \| undefined` | No | `-` | Custom classNames for the axis. | | `minTickLabelGap` | `number` | No | `4` | Minimum gap between tick labels. Labels will be hidden if they are closer than this gap. | | `position` | `left \| right` | No | `'right'` | The position of the axis relative to the charts drawing area. | | `requestedTickCount` | `number` | No | `5 (for y-axis)` | Requested number of ticks to display. This value is passed into d3 and may not be respected. | | `showGrid` | `boolean` | No | `-` | Whether to show grid lines at each tick position. | | `showLine` | `boolean` | No | `-` | Whether to show the axis line. | | `showTickMarks` | `boolean` | No | `-` | Whether to show tick marks on the axis. | | `style` | `CSSProperties` | No | `-` | Custom style for the axis. | | `styles` | `{ root?: CSSProperties; tickLabel?: CSSProperties \| undefined; gridLine?: CSSProperties \| undefined; line?: CSSProperties \| undefined; tickMark?: CSSProperties \| undefined; } \| undefined` | No | `-` | Custom styles for the axis. | | `tickInterval` | `number` | No | `32 (for x-axis)` | Interval at which to show ticks. When provided, calculates tick count based on available space. | | `tickLabelFormatter` | `((value: any) => ChartTextChildren)` | No | `-` | Formatter function for axis tick values. Tick values will be wrapped in ChartText component. | | `tickMarkLabelGap` | `number` | No | `2 for x-axis, 8 for y-axis` | Space between the axis tick mark and labels. If tick marks are not shown, this is the gap between the axis and the chart. | | `tickMarkSize` | `number` | No | `4` | Size of the tick marks. | | `tickMaxStep` | `number` | No | `-` | Maximum step size for tick generation. Prevents the step from being larger than this value. | | `tickMinStep` | `number` | No | `1` | Minimum step size for tick generation. Prevents the step from being smaller than this value. | | `ticks` | `number[] \| ((value: number) => boolean)` | No | `-` | Custom tick configuration for the axis. When provided, this overrides the requestedTickCount property. - **Array**: Uses these exact values for tick positioning and labels. - **Function**: Filters based on the predicate function. - For **x-axis**: Checks every data index (0, 1, 2, ..., dataLength-1) - For **y-axis**: Filters d3-generated tick values | | `width` | `number` | No | `44` | Width of the axis. This value is inclusive of the padding. |