React axis components for data visualization with D3 scale integration, providing pre-built orientations and flexible customization options.
—
Pre-configured axis components for standard chart orientations. Each component includes appropriate default styling, positioning, and tick label alignment for its orientation.
Left-oriented axis component with right-aligned tick labels positioned to the left of tick marks.
function AxisLeft<Scale extends AxisScale>(props: SharedAxisProps<Scale>): JSX.Element;Default styling:
textAnchor: 'end', positioned with dx: '-0.25em', dy: '0.25em', fill: '#222', fontFamily: 'Arial', fontSize: 10Orientation.leftUsage:
import { AxisLeft } from '@visx/axis';
import { scaleLinear } from '@visx/scale';
const yScale = scaleLinear({
range: [300, 0],
domain: [0, 100],
});
<AxisLeft
scale={yScale}
left={50}
label="Y Values"
stroke="#333"
tickStroke="#333"
tickLabelProps={{
fill: '#333',
fontSize: 12,
}}
/>Right-oriented axis component with left-aligned tick labels positioned to the right of tick marks.
function AxisRight<Scale extends AxisScale>(props: AxisRightProps<Scale>): JSX.Element;
type AxisRightProps<Scale extends AxisScale> = SharedAxisProps<Scale>;Default styling:
textAnchor: 'start', positioned with dx: '0.25em', dy: '0.25em', fill: '#222', fontFamily: 'Arial', fontSize: 10Orientation.rightUsage:
import { AxisRight } from '@visx/axis';
<AxisRight
scale={yScale}
left={450}
label="Secondary Y"
numTicks={5}
/>Top-oriented axis component with center-aligned tick labels positioned above tick marks.
function AxisTop<Scale extends AxisScale>(props: AxisTopProps<Scale>): JSX.Element;
type AxisTopProps<Scale extends AxisScale> = SharedAxisProps<Scale>;Default styling:
textAnchor: 'middle', positioned with dy: '-0.75em', fill: '#222', fontFamily: 'Arial', fontSize: 10Orientation.topUsage:
import { AxisTop } from '@visx/axis';
import { scaleTime } from '@visx/scale';
const timeScale = scaleTime({
range: [0, 400],
domain: [new Date('2023-01-01'), new Date('2023-12-31')],
});
<AxisTop
scale={timeScale}
top={20}
left={50}
label="Timeline"
tickFormat={(date) => date.toLocaleDateString()}
/>Bottom-oriented axis component with center-aligned tick labels positioned below tick marks.
function AxisBottom<Scale extends AxisScale>(props: SharedAxisProps<Scale>): JSX.Element;Default styling:
textAnchor: 'middle', positioned with dy: '0.25em', fill: '#222', fontFamily: 'Arial', fontSize: 10Orientation.bottomUsage:
import { AxisBottom } from '@visx/axis';
import { scaleBand } from '@visx/scale';
const categoryScale = scaleBand({
range: [0, 400],
domain: ['A', 'B', 'C', 'D'],
padding: 0.1,
});
<AxisBottom
scale={categoryScale}
top={300}
left={50}
label="Categories"
tickLabelProps={(value) => ({
fill: value === 'C' ? 'red' : 'black',
fontSize: 11,
})}
/>All pre-built axis components accept the same SharedAxisProps interface:
interface SharedAxisProps<Scale extends AxisScale> {
// Required
scale: Scale;
// Position
left?: number;
top?: number;
// 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;
}All pre-built axis components work with any D3-compatible scale:
// Linear scales
import { scaleLinear } from '@visx/scale';
const linearScale = scaleLinear({ range: [0, 400], domain: [0, 100] });
// Band scales
import { scaleBand } from '@visx/scale';
const bandScale = scaleBand({ range: [0, 400], domain: ['A', 'B', 'C'] });
// Time scales
import { scaleTime } from '@visx/scale';
const timeScale = scaleTime({
range: [0, 400],
domain: [new Date('2023-01-01'), new Date('2023-12-31')]
});
// Ordinal scales
import { scaleOrdinal } from '@visx/scale';
const colorScale = scaleOrdinal({
range: ['red', 'blue', 'green'],
domain: ['Type A', 'Type B', 'Type C']
});<AxisBottom
scale={linearScale}
tickFormat={(value) => `$${value.toFixed(2)}`}
/><AxisLeft
scale={yScale}
tickLabelProps={(value, index) => ({
fill: value > 50 ? 'red' : 'black',
fontSize: index % 2 === 0 ? 12 : 10,
fontWeight: value === 0 ? 'bold' : 'normal',
})}
/><AxisBottom
scale={linearScale}
tickValues={[0, 25, 50, 75, 100]}
numTicks={undefined} // Ignored when tickValues provided
/><AxisLeft
scale={yScale}
hideAxisLine={true}
hideTicks={false}
hideZero={true}
/>import { AxisLeft, leftTickLabelProps } from '@visx/axis';
// Use default left tick label props as base
<AxisLeft
scale={yScale}
tickLabelProps={{
...leftTickLabelProps,
fontSize: 12, // Override specific properties
fill: 'blue'
}}
/>
// Or use them directly for custom styling
const customTickProps = {
...leftTickLabelProps,
fontWeight: 'bold'
};Install with Tessl CLI
npx tessl i tessl/npm-visx--axis