|
| 1 | +import { ReactNode, Fragment } from 'react' |
| 2 | +import { Container, useDimensions, SvgWrapper } from '@nivo/core' |
| 3 | +import { BoxLegendSvg } from '@nivo/legends' |
| 4 | +import { RadarShapes } from './RadarShapes' |
| 5 | +import { RadarGrid } from './RadarGrid' |
| 6 | +import { RadarTooltip } from './RadarTooltip' |
| 7 | +import { RadarDots } from './RadarDots' |
| 8 | +import { svgDefaultProps } from './props' |
| 9 | +import { RadarLayerId, RadarSvgProps } from './types' |
| 10 | +import { useRadar } from './hooks' |
| 11 | + |
| 12 | +type InnerRadarProps<D extends Record<string, unknown>> = Omit< |
| 13 | + RadarSvgProps<D>, |
| 14 | + 'animate' | 'motionConfig' | 'renderWrapper' | 'theme' |
| 15 | +> |
| 16 | + |
| 17 | +const InnerRadar = <D extends Record<string, unknown>>({ |
| 18 | + data, |
| 19 | + keys, |
| 20 | + indexBy, |
| 21 | + layers = svgDefaultProps.layers, |
| 22 | + maxValue = svgDefaultProps.maxValue, |
| 23 | + curve = svgDefaultProps.curve, |
| 24 | + margin: partialMargin, |
| 25 | + width, |
| 26 | + height, |
| 27 | + borderWidth = svgDefaultProps.borderWidth, |
| 28 | + borderColor = svgDefaultProps.borderColor, |
| 29 | + gridLevels = svgDefaultProps.gridLevels, |
| 30 | + gridShape = svgDefaultProps.gridShape, |
| 31 | + gridLabel = svgDefaultProps.gridLabel, |
| 32 | + gridLabelOffset = svgDefaultProps.gridLabelOffset, |
| 33 | + enableDots = svgDefaultProps.enableDots, |
| 34 | + dotSymbol, |
| 35 | + dotSize, |
| 36 | + dotColor, |
| 37 | + dotBorderWidth, |
| 38 | + dotBorderColor, |
| 39 | + enableDotLabel, |
| 40 | + dotLabel, |
| 41 | + dotLabelFormat, |
| 42 | + dotLabelYOffset, |
| 43 | + colors = svgDefaultProps.colors, |
| 44 | + fillOpacity = svgDefaultProps.fillOpacity, |
| 45 | + blendMode = svgDefaultProps.blendMode, |
| 46 | + isInteractive = svgDefaultProps.isInteractive, |
| 47 | + tooltipFormat, |
| 48 | + legends = svgDefaultProps.legends, |
| 49 | + role, |
| 50 | + ariaLabel, |
| 51 | + ariaLabelledBy, |
| 52 | + ariaDescribedBy, |
| 53 | +}: InnerRadarProps<D>) => { |
| 54 | + const { margin, innerWidth, innerHeight, outerWidth, outerHeight } = useDimensions( |
| 55 | + width, |
| 56 | + height, |
| 57 | + partialMargin |
| 58 | + ) |
| 59 | + |
| 60 | + const { |
| 61 | + getIndex, |
| 62 | + indices, |
| 63 | + colorByKey, |
| 64 | + radius, |
| 65 | + radiusScale, |
| 66 | + centerX, |
| 67 | + centerY, |
| 68 | + angleStep, |
| 69 | + curveInterpolator, |
| 70 | + legendData, |
| 71 | + } = useRadar<D>({ |
| 72 | + data, |
| 73 | + keys, |
| 74 | + indexBy, |
| 75 | + maxValue, |
| 76 | + curve, |
| 77 | + width: innerWidth, |
| 78 | + height: innerHeight, |
| 79 | + colors, |
| 80 | + }) |
| 81 | + |
| 82 | + const layerById: Record<RadarLayerId, ReactNode> = { |
| 83 | + grid: null, |
| 84 | + shapes: null, |
| 85 | + dots: null, |
| 86 | + legends: null, |
| 87 | + } |
| 88 | + |
| 89 | + if (layers.includes('grid')) { |
| 90 | + layerById.grid = ( |
| 91 | + <g key="grid" transform={`translate(${centerX}, ${centerY})`}> |
| 92 | + <RadarGrid<D> |
| 93 | + levels={gridLevels} |
| 94 | + shape={gridShape} |
| 95 | + radius={radius} |
| 96 | + angleStep={angleStep} |
| 97 | + indices={indices} |
| 98 | + label={gridLabel} |
| 99 | + labelOffset={gridLabelOffset} |
| 100 | + /> |
| 101 | + </g> |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + if (layers.includes('shapes')) { |
| 106 | + layerById.shapes = ( |
| 107 | + <g key="shapes" transform={`translate(${centerX}, ${centerY})`}> |
| 108 | + {keys.map(key => ( |
| 109 | + <RadarShapes<D> |
| 110 | + key={key} |
| 111 | + data={data} |
| 112 | + item={key} |
| 113 | + colorByKey={colorByKey} |
| 114 | + radiusScale={radiusScale} |
| 115 | + angleStep={angleStep} |
| 116 | + curveInterpolator={curveInterpolator} |
| 117 | + borderWidth={borderWidth} |
| 118 | + borderColor={borderColor} |
| 119 | + fillOpacity={fillOpacity} |
| 120 | + blendMode={blendMode} |
| 121 | + /> |
| 122 | + ))} |
| 123 | + </g> |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + if (layers.includes('dots') && enableDots) { |
| 128 | + layerById.dots = ( |
| 129 | + <g key="dots" transform={`translate(${centerX}, ${centerY})`}> |
| 130 | + <RadarDots<D> |
| 131 | + data={data} |
| 132 | + keys={keys} |
| 133 | + getIndex={getIndex} |
| 134 | + radiusScale={radiusScale} |
| 135 | + angleStep={angleStep} |
| 136 | + symbol={dotSymbol} |
| 137 | + size={dotSize} |
| 138 | + colorByKey={colorByKey} |
| 139 | + color={dotColor} |
| 140 | + borderWidth={dotBorderWidth} |
| 141 | + borderColor={dotBorderColor} |
| 142 | + enableLabel={enableDotLabel} |
| 143 | + label={dotLabel} |
| 144 | + labelFormat={dotLabelFormat} |
| 145 | + labelYOffset={dotLabelYOffset} |
| 146 | + /> |
| 147 | + </g> |
| 148 | + ) |
| 149 | + } |
| 150 | + |
| 151 | + if (layers.includes('legends')) { |
| 152 | + layerById.legends = ( |
| 153 | + <Fragment key="legends"> |
| 154 | + {legends.map((legend, i) => ( |
| 155 | + <BoxLegendSvg |
| 156 | + key={i} |
| 157 | + {...legend} |
| 158 | + containerWidth={width} |
| 159 | + containerHeight={height} |
| 160 | + data={legendData} |
| 161 | + /> |
| 162 | + ))} |
| 163 | + </Fragment> |
| 164 | + ) |
| 165 | + } |
| 166 | + |
| 167 | + return ( |
| 168 | + <SvgWrapper |
| 169 | + width={outerWidth} |
| 170 | + height={outerHeight} |
| 171 | + margin={margin} |
| 172 | + role={role} |
| 173 | + ariaLabel={ariaLabel} |
| 174 | + ariaLabelledBy={ariaLabelledBy} |
| 175 | + ariaDescribedBy={ariaDescribedBy} |
| 176 | + > |
| 177 | + {layerById.grid} |
| 178 | + {layerById.shapes} |
| 179 | + {isInteractive && ( |
| 180 | + <g transform={`translate(${centerX}, ${centerY})`}> |
| 181 | + <RadarTooltip<D> |
| 182 | + data={data} |
| 183 | + keys={keys} |
| 184 | + getIndex={getIndex} |
| 185 | + colorByKey={colorByKey} |
| 186 | + radius={radius} |
| 187 | + angleStep={angleStep} |
| 188 | + tooltipFormat={tooltipFormat} |
| 189 | + /> |
| 190 | + </g> |
| 191 | + )} |
| 192 | + {layerById.dots} |
| 193 | + {layerById.legends} |
| 194 | + </SvgWrapper> |
| 195 | + ) |
| 196 | +} |
| 197 | + |
| 198 | +export const Radar = <D extends Record<string, unknown>>({ |
| 199 | + isInteractive = svgDefaultProps.isInteractive, |
| 200 | + animate = svgDefaultProps.animate, |
| 201 | + motionConfig = svgDefaultProps.motionConfig, |
| 202 | + theme, |
| 203 | + renderWrapper, |
| 204 | + ...otherProps |
| 205 | +}: RadarSvgProps<D>) => ( |
| 206 | + <Container |
| 207 | + {...{ |
| 208 | + animate, |
| 209 | + isInteractive, |
| 210 | + motionConfig, |
| 211 | + renderWrapper, |
| 212 | + theme, |
| 213 | + }} |
| 214 | + > |
| 215 | + <InnerRadar<D> isInteractive={isInteractive} {...otherProps} /> |
| 216 | + </Container> |
| 217 | +) |
0 commit comments