Skip to content

Axis

Axis renders an axis line, tick marks, and tick labels. It is an absolutely positioned SVG layer, so place it inside a relatively positioned plot container.

Use direction="horizontal" for an x-axis and direction="vertical" for a y-axis. The position prop replaces the old x/y-specific position props: for a horizontal axis it is the y data-space position, and for a vertical axis it is the x data-space position.

Import

ts
import { Axis } from '@putianyi888/vue3-plots'

Example

JanFebMarAprMayJun0255075100
vue
<template>
    <div class="example-frame" :style="{ height: `${size.height}px`, width: `${size.width}px` }">
        <TransformGroup :domain="domain" :size="size" :padding="padding">
            <Grid :x-ticks="xTicks" :y-ticks="yTicks" stroke-color="#e2e8f0" :stroke-opacity="0.5" />
            <Axis direction="horizontal" :position="25" :ticks="xTicks">
                <template #tick="{ tick }">
                    {{ months[tick] }}
                </template>
            </Axis>
            <Axis direction="vertical" :ticks="yTicks" />
        </TransformGroup>
    </div>
</template>

<script setup lang="ts">
import { Axis, Grid, TransformGroup } from '@putianyi888/vue3-plots'
import type { PlotDomain, PlotPadding, PlotSize } from '@putianyi888/vue3-plots'

const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
const size: PlotSize = { width: 640, height: 240 }
const padding: PlotPadding = { top: 24, right: 24, bottom: 40, left: 48 }
const domain: PlotDomain = { xMin: -0.5, xMax: months.length - 0.5, yMin: 0, yMax: 100 }
const xTicks = months.map((_, index) => index)
const yTicks = [0, 25, 50, 75, 100]
</script>

<style scoped>
.example-frame {
  overflow-x: auto;
  position: relative;
}
</style>

Props

PropTypeDefaultDescription
domainPlotDomainData-space bounds used to map tick values onto the plot area.
sizePlotSizeOuter SVG size in pixels.
paddingPlotPaddingInsets shared with other plot layers.
ticksnumber[][]Axis tick values in data-space coordinates.
positionnumberAxis line position in data-space coordinates on the opposite axis.
offsetnumberTick label center offset from the axis line in pixels.
textAnchorTextAnchor'middle'SVG text-anchor value for tick labels.
tickSizeMaybeArray<number>4Tick mark length in pixels.
strokeOpacityMaybeArray<number>1Axis line and tick mark opacity.
strokeWidthMaybeArray<number>1Axis line and tick mark width in pixels.
directionAxisDirectionRequiredAxis direction. Horizontal maps ticks on x; vertical maps ticks on y.

Slots

SlotPropsDescription
tick{ tick: number; x: number; y: number; }Custom renderer for a tick label.

Notes

Tick labels use formatTick by default.

For horizontal axes, positive tick sizes extend below the axis line and negative tick sizes extend above it. The default label offset is 24.

For vertical axes, positive tick sizes extend to the left of the axis line and negative tick sizes extend to the right. The default label offset is -24.