import { ReactNode, Fragment, createElement, forwardRef, Ref, ReactElement } from 'react' import { Container, SvgWrapper, useDimensions, WithChartRef } from '@nivo/core' import { InheritedColorConfig, OrdinalColorScaleConfig } from '@nivo/colors' import { AnyScale } from '@nivo/scales' import { Axes, Grid } from '@nivo/axes' import { Mesh } from '@nivo/voronoi' import { ComputedDatum, SwarmPlotLayerId, SwarmPlotSvgProps } from './types' import { defaultProps } from './props' import { useSwarmPlot, useSwarmPlotLayerContext, useNodeMouseHandlers } from './hooks' import { Circles } from './Circles' import { CircleSvg } from './CircleSvg' import { SwarmPlotAnnotations } from './SwarmPlotAnnotations' type InnerSwarmPlotProps = Partial< Omit< SwarmPlotSvgProps, 'data' | 'groups' | 'width' | 'height' | 'isInteractive' | 'animate' | 'motionConfig' > > & Pick, 'data' | 'groups' | 'width' | 'height' | 'isInteractive'> & { forwardedRef: Ref } const InnerSwarmPlot = ({ data, width, height, margin: partialMargin, id = defaultProps.id, value = defaultProps.value, valueScale = defaultProps.valueScale, valueFormat, groups, groupBy = defaultProps.groupBy, size = defaultProps.size, forceStrength = defaultProps.forceStrength, simulationIterations = defaultProps.simulationIterations, colors = defaultProps.colors as OrdinalColorScaleConfig, 'color'>>, colorBy = defaultProps.colorBy, borderColor = defaultProps.borderColor as InheritedColorConfig>, borderWidth = defaultProps.borderWidth, layout = defaultProps.layout, spacing = defaultProps.spacing, gap = defaultProps.gap, layers = defaultProps.layers, circleComponent = CircleSvg, useMesh = defaultProps.useMesh, debugMesh = defaultProps.debugMesh, enableGridX = defaultProps.enableGridX, gridXValues, enableGridY = defaultProps.enableGridY, gridYValues, axisTop = defaultProps.axisTop, axisRight = defaultProps.axisRight, axisBottom = defaultProps.axisBottom, axisLeft = defaultProps.axisLeft, isInteractive, onMouseEnter, onMouseMove, onMouseLeave, onMouseDown, onMouseUp, onClick, onDoubleClick, tooltip = defaultProps.tooltip, annotations = defaultProps.annotations, role = defaultProps.role, forwardedRef, }: InnerSwarmPlotProps) => { const { outerWidth, outerHeight, margin, innerWidth, innerHeight } = useDimensions( width, height, partialMargin ) const { nodes, ...props } = useSwarmPlot({ width: innerWidth, height: innerHeight, data, id, value, valueFormat, valueScale, groups, groupBy, size, spacing, layout, gap, colors, colorBy, forceStrength, simulationIterations, }) const xScale = props.xScale as Exclude[]> const yScale = props.yScale as Exclude[]> const handlers = useNodeMouseHandlers({ isInteractive, onClick, onDoubleClick, onMouseEnter, onMouseLeave, onMouseMove, onMouseDown, onMouseUp, tooltip, }) const layerById: Record = { grid: null, axes: null, circles: null, annotations: null, mesh: null, } if (layers.includes('grid')) { layerById.grid = ( ) } if (layers.includes('axes')) { layerById.axes = ( ) } if (layers.includes('circles')) { layerById.circles = ( key="circles" nodes={nodes} borderWidth={borderWidth} borderColor={borderColor} isInteractive={isInteractive} tooltip={tooltip} component={circleComponent} onMouseEnter={onMouseEnter} onMouseMove={onMouseMove} onMouseLeave={onMouseLeave} onMouseDown={onMouseDown} onMouseUp={onMouseUp} onClick={onClick} onDoubleClick={onDoubleClick} /> ) } if (layers.includes('annotations')) { layerById.annotations = ( key="annotations" nodes={nodes} annotations={annotations} /> ) } if (isInteractive && useMesh) { layerById.mesh = ( ) } const layerContext = useSwarmPlotLayerContext({ nodes, xScale, yScale, innerWidth, innerHeight, outerWidth, outerHeight, margin, }) return ( {layers.map((layer, i) => { if (layerById[layer as SwarmPlotLayerId] !== undefined) { return layerById[layer as SwarmPlotLayerId] } if (typeof layer === 'function') { return {createElement(layer, layerContext)} } return null })} ) } export const SwarmPlot = forwardRef( ( { theme, isInteractive = defaultProps.isInteractive, animate = defaultProps.animate, motionConfig = defaultProps.motionConfig, renderWrapper, ...props }: Partial, 'data' | 'groups' | 'width' | 'height'>> & Pick, 'data' | 'groups' | 'width' | 'height'>, ref: Ref ) => ( {...props} isInteractive={isInteractive} forwardedRef={ref} /> ) ) as ( props: WithChartRef< Partial, 'data' | 'groups' | 'width' | 'height'>> & Pick, 'data' | 'groups' | 'width' | 'height'>, SVGSVGElement > ) => ReactElement