import { useLocaleDateTimeFormat } from '@centreon/ui'; import { Axis } from '@visx/visx'; import type { ScaleBand, ScaleLinear, ScaleTime } from 'd3-scale'; import { equals, head, isNil, last } from 'ramda'; import { margin } from '../../Chart/common'; import { getXAxisTickFormat } from '../../Chart/helpers'; import { getUnits } from '../timeSeries'; import type { Data } from './models'; import UnitLabel from './UnitLabel'; import useAxisY from './useAxisY'; interface Props { allUnits: Array; data: Data; height: number; leftScale: ScaleLinear; orientation: 'horizontal' | 'vertical'; rightScale: ScaleLinear; width: number; xScale: | ScaleLinear | ScaleTime | ScaleBand; } const Axes = ({ height, width, data, rightScale, leftScale, xScale, orientation, allUnits }: Props): JSX.Element => { const { format } = useLocaleDateTimeFormat(); const { lines, showBorder, yAxisTickLabelRotation } = data; const isHorizontal = equals(orientation, 'horizontal'); const { axisLeft, axisRight } = useAxisY({ data, graphHeight: height, graphWidth: width, isHorizontal }); const [, secondUnit] = getUnits(lines); const xTickCount = Math.floor(Math.min(width / 100, 12)); const domain = xScale.domain() as Array; const start = head(domain); const end = last(domain); const toISOString = (v: number | Date | undefined): string | undefined => v !== undefined ? (v instanceof Date ? v : new Date(v)).toISOString() : undefined; const tickFormat = data?.axisX?.xAxisTickFormat ?? getXAxisTickFormat({ end: toISOString(end), start: toISOString(start) }); const formatAxisTick = (tick: unknown): string => format({ date: new Date(tick as number | Date), formatString: tickFormat }); const displayAxisRight = !isNil(secondUnit) && !isNil(rightScale); const AxisBottom = isHorizontal ? Axis.AxisBottom : Axis.AxisLeft; const AxisLeft = isHorizontal ? Axis.AxisLeft : Axis.AxisTop; const AxisRight = isHorizontal ? Axis.AxisRight : Axis.AxisBottom; const axisBottomProps = isHorizontal ? {} : ({ angle: 90, textAnchor: 'middle' } as const); return ( ({ ...(axisLeft.tickLabelProps as () => Record)(), dx: data?.axisX?.dx ?? (isHorizontal ? 16 : -12), ...axisBottomProps })} top={isHorizontal ? height - margin.bottom : 0} /> {axisLeft.displayUnit && ( )} ({ ...(axisLeft.tickLabelProps as () => Record)(), angle: yAxisTickLabelRotation, dx: isHorizontal ? -4 : 4, dy: isHorizontal ? 4 : -6 })} tickLength={2} /> {displayAxisRight && ( ({ ...(axisRight.tickLabelProps as () => Record)(), angle: yAxisTickLabelRotation, dx: isHorizontal ? 4 : -4, dy: 4 })} tickLength={2} top={isHorizontal ? 0 : height - margin.bottom} /> )} {axisRight.displayUnit && ( )} ); }; export default Axes;