/** * @upsetjs/react * https://github.com/upsetjs/upsetjs * * Copyright (c) 2021 Samuel Gratzl */ import type { NumericScaleLike } from '@upsetjs/model'; import React, { PropsWithChildren, CSSProperties } from 'react'; import { clsx } from '../utils'; export type AxisProps = { scale: NumericScaleLike; orient: 'top' | 'bottom' | 'left' | 'right'; tickSizeInner?: number; tickSizeOuter?: number; tickPadding?: number; size: number; start: number; style: AxisStyle; transform?: string; }; export type AxisStyle = { id: string; classNames: { axisTick?: string }; styles: { axisTick?: CSSProperties }; }; declare type TickProps = { pos: number; spacing: number; tickSizeInner: number; orient: 'top' | 'bottom' | 'left' | 'right'; name?: string; style: AxisStyle; }; const HorizontalTick = /*!#__PURE__*/ React.memo(function HorizontalTick({ pos, spacing, tickSizeInner, orient, name, style, }: PropsWithChildren) { const k = orient === 'top' || orient === 'left' ? -1 : 1; return ( {name && ( {name} )} ); }); const VerticalTick = /*!#__PURE__*/ React.memo(function VerticalTick({ pos, name, spacing, orient, tickSizeInner, style, }: PropsWithChildren) { const k = orient === 'top' || orient === 'left' ? -1 : 1; return ( {name && ( {name} )} ); }); export default function Axis({ scale, orient, tickSizeInner = 6, tickSizeOuter = 6, tickPadding = 3, size, start, style, transform, }: PropsWithChildren) { const spacing = Math.max(tickSizeInner, 0) + tickPadding; const range0 = start; const range1 = size; const k = orient === 'top' || orient === 'left' ? -1 : 1; const Tick = orient === 'left' || orient === 'right' ? HorizontalTick : VerticalTick; const values: readonly { value: number; label?: string }[] = scale .ticks() .map((d) => (typeof d === 'number' ? { value: d, label: d.toLocaleString() } : d)); return ( {values.map((d) => ( ))} ); }