React axis components for data visualization with D3 scale integration, providing pre-built orientations and flexible customization options.
npx @tessl/cli install tessl/npm-visx--axis@3.12.0@visx/axis provides React components for creating data visualization axes with full D3 scale integration. It offers pre-built axis components for all four orientations (left, right, top, bottom) and a flexible base component for custom configurations, enabling the creation of professional charts with properly positioned ticks, labels, and axis lines.
npm install @visx/axisimport {
Axis,
AxisLeft,
AxisRight,
AxisTop,
AxisBottom,
Orientation,
SharedAxisProps,
AxisProps,
AxisRightProps,
AxisTopProps,
leftTickLabelProps,
rightTickLabelProps,
topTickLabelProps,
bottomTickLabelProps
} from "@visx/axis";For CommonJS:
const { Axis, AxisLeft, AxisRight, AxisTop, AxisBottom, Orientation } = require("@visx/axis");import React from 'react';
import { scaleLinear } from '@visx/scale';
import { AxisLeft, AxisBottom } from '@visx/axis';
// Create scales
const xScale = scaleLinear({
range: [0, 400],
domain: [0, 100],
});
const yScale = scaleLinear({
range: [300, 0],
domain: [0, 50],
});
function Chart() {
return (
<svg width={500} height={400}>
<AxisLeft
scale={yScale}
left={40}
label="Y Values"
stroke="#1b1a1e"
tickStroke="#1b1a1e"
tickLabelProps={{
fill: '#1b1a1e',
fontSize: 11,
textAnchor: 'end',
}}
/>
<AxisBottom
scale={xScale}
top={300}
left={40}
label="X Values"
stroke="#1b1a1e"
tickStroke="#1b1a1e"
tickLabelProps={{
fill: '#1b1a1e',
fontSize: 11,
textAnchor: 'middle',
}}
/>
</svg>
);
}@visx/axis is built around several key components:
Axis component that can be oriented in any directionAxisLeft, AxisRight, AxisTop, AxisBottom) with orientation-specific defaultsReady-to-use axis components with orientation-specific styling and positioning. Perfect for standard chart layouts without configuration overhead.
function AxisLeft<Scale extends AxisScale>(props: SharedAxisProps<Scale>): JSX.Element;
function AxisRight<Scale extends AxisScale>(props: SharedAxisProps<Scale>): JSX.Element;
function AxisTop<Scale extends AxisScale>(props: SharedAxisProps<Scale>): JSX.Element;
function AxisBottom<Scale extends AxisScale>(props: SharedAxisProps<Scale>): JSX.Element;Flexible axis component with full customization control. Ideal for custom orientations, complex styling, or when you need complete control over axis behavior.
function Axis<Scale extends AxisScale>(props: AxisProps<Scale>): JSX.Element;
interface AxisProps<Scale extends AxisScale> extends SharedAxisProps<Scale> {
orientation?: OrientationType;
}Orientation constants for axis positioning and validation.
const Orientation: {
readonly top: "top";
readonly left: "left";
readonly right: "right";
readonly bottom: "bottom";
};
type OrientationType = "top" | "left" | "right" | "bottom";Exported default styling objects for each axis orientation:
const leftTickLabelProps: {
readonly dx: "-0.25em";
readonly dy: "0.25em";
readonly fill: "#222";
readonly fontFamily: "Arial";
readonly fontSize: 10;
readonly textAnchor: "end";
};
const rightTickLabelProps: {
readonly dx: "0.25em";
readonly dy: "0.25em";
readonly fill: "#222";
readonly fontFamily: "Arial";
readonly fontSize: 10;
readonly textAnchor: "start";
};
const topTickLabelProps: {
readonly dy: "-0.75em";
readonly fill: "#222";
readonly fontFamily: "Arial";
readonly fontSize: 10;
readonly textAnchor: "middle";
};
const bottomTickLabelProps: {
readonly dy: "0.25em";
readonly fill: "#222";
readonly fontFamily: "Arial";
readonly fontSize: 10;
readonly textAnchor: "middle";
};External types referenced in the API come from:
TextProps: From @visx/text - properties for text renderingReactNode, Ref, SVGProps: From React - standard React typesD3Scale, ScaleInput, NumberLike: From @visx/scale - scale-related typestype AxisScale<Output extends AxisScaleOutput = AxisScaleOutput> = D3Scale<Output, any, any>;
type AxisScaleOutput = number | NumberLike | undefined;
interface SharedAxisProps<Scale extends AxisScale> {
// Position
left?: number;
top?: number;
// Scale (required)
scale: Scale;
// Ticks
numTicks?: number;
tickValues?: ScaleInput<Scale>[];
tickFormat?: TickFormatter<ScaleInput<Scale>>;
tickLength?: number;
hideTicks?: boolean;
hideZero?: boolean;
// Axis line
hideAxisLine?: boolean;
axisLineClassName?: string;
stroke?: string;
strokeWidth?: number | string;
strokeDasharray?: string;
// Labels
label?: string;
labelOffset?: number;
labelProps?: Partial<TextProps>;
labelClassName?: string;
// Styling
axisClassName?: string;
tickClassName?: string;
tickStroke?: string;
tickLabelProps?: TickLabelProps<ScaleInput<Scale>>;
tickLineProps?: Omit<SVGProps<SVGLineElement>, 'to' | 'from' | 'ref'>;
// Advanced
rangePadding?: number | { start?: number; end?: number };
tickTransform?: string;
tickComponent?: (tickRendererProps: TickRendererProps) => ReactNode;
ticksComponent?: (tickRendererProps: TicksRendererProps<Scale>) => ReactNode;
innerRef?: Ref<SVGGElement>;
children?: (renderProps: AxisRendererProps<Scale>) => ReactNode;
}
type TickFormatter<T> = (
value: T,
index: number,
values: { value: T; index: number }[]
) => string | undefined;
type TickLabelProps<T> =
| Partial<TextProps>
| ((value: T, index: number, values: { value: T; index: number }[]) => Partial<TextProps>);
interface TickRendererProps extends Partial<TextProps> {
x: number;
y: number;
formattedValue: string | undefined;
}
interface TicksRendererProps<Scale extends AxisScale> {
tickLabelProps: Partial<TextProps>[];
hideTicks?: boolean;
horizontal: boolean;
orientation: OrientationType;
scale: Scale;
tickClassName?: string;
tickComponent?: (tickRendererProps: TickRendererProps) => ReactNode;
tickStroke?: string;
tickTransform?: string;
ticks: ComputedTick<Scale>[];
strokeWidth?: number | string;
tickLineProps?: Omit<SVGProps<SVGLineElement>, 'to' | 'from' | 'ref'>;
}
interface AxisRendererProps<Scale extends AxisScale> extends SharedAxisProps<Scale> {
axisFromPoint: { x: number; y: number };
axisToPoint: { x: number; y: number };
horizontal: boolean;
scale: Scale;
tickPosition: (value: ScaleInput<Scale>) => AxisScaleOutput;
tickSign: 1 | -1;
ticks: ComputedTick<Scale>[];
}
interface ComputedTick<Scale extends AxisScale> {
value: ScaleInput<Scale>;
index: number;
from: { x: number; y: number };
to: { x: number; y: number };
formattedValue: string | undefined;
}