import type { Domain } from '@h5web/shared'; import { formatBound } from '@h5web/shared'; import { useMeasure } from '@react-hookz/web'; import { AxisRight, AxisBottom } from '@visx/axis'; import type { VisScaleType } from '../models'; import Tick from '../shared/Tick'; import { adaptedNumTicks, createAxisScale } from '../utils'; import styles from './ColorBar.module.css'; import type { ColorMap } from './models'; import { getInterpolator, getLinearGradient } from './utils'; interface Props { domain: Domain; scaleType: VisScaleType; colorMap: ColorMap; horizontal?: boolean; withBounds?: boolean; invertColorMap: boolean; } function ColorBar(props: Props) { const { domain, scaleType, colorMap, horizontal, withBounds, invertColorMap, } = props; const [barSize, barRef] = useMeasure(); const gradientLength = barSize ? horizontal ? barSize.width : barSize.height : 0; const Axis = horizontal ? AxisBottom : AxisRight; const isEmptyDomain = domain[0] === domain[1]; const axisScale = createAxisScale(scaleType, { domain, range: horizontal ? [0, Math.round(gradientLength + 0.5)] // fix sub-pixel misalignment : [Math.round(gradientLength + 0.5), 0], }); return (
{withBounds && ( <>

{isEmptyDomain ? '−∞' : formatBound(domain[0])}

{isEmptyDomain ? '+∞' : formatBound(domain[1])}

)}
{gradientLength > 0 && ( )}
); } export type { Props as ColorBarProps }; export default ColorBar;