({
linesProps = [],
dotIndex = -2,
strokeDasharray: restStroke = [5, 3],
curveCorrection = 1,
}: UseStrokeDasharrayProps)
| 107 | } & LineProps; |
| 108 | |
| 109 | export function useStrokeDasharray({ |
| 110 | linesProps = [], |
| 111 | dotIndex = -2, |
| 112 | strokeDasharray: restStroke = [5, 3], |
| 113 | curveCorrection = 1, |
| 114 | }: UseStrokeDasharrayProps): [CalculateStrokeDasharray, LinesStrokeDasharray] { |
| 115 | const linesStrokeDasharray = useRef<LinesStrokeDasharray>([]); |
| 116 | |
| 117 | const calculateStrokeDasharray = useCallback( |
| 118 | (props: RechartsChartProps): null => { |
| 119 | const items = props?.formattedGraphicalItems; |
| 120 | |
| 121 | const getLineWidth = (points: GraphicalItemPoint[]) => { |
| 122 | const width = points?.reduce((acc, point, index) => { |
| 123 | if (!index) return acc; |
| 124 | |
| 125 | const prevPoint = points?.[index - 1]; |
| 126 | |
| 127 | const xAxis = point?.x || 0; |
| 128 | const prevXAxis = prevPoint?.x || 0; |
| 129 | const xWidth = xAxis - prevXAxis; |
| 130 | |
| 131 | const yAxis = point?.y || 0; |
| 132 | const prevYAxis = prevPoint?.y || 0; |
| 133 | const yWidth = Math.abs(yAxis - prevYAxis); |
| 134 | |
| 135 | const hypotenuse = Math.sqrt(xWidth * xWidth + yWidth * yWidth); |
| 136 | acc += hypotenuse; |
| 137 | return acc; |
| 138 | }, 0); |
| 139 | |
| 140 | return width || 0; |
| 141 | }; |
| 142 | |
| 143 | items?.forEach((line) => { |
| 144 | const linePoints = line?.props?.points ?? []; |
| 145 | const lineWidth = getLineWidth(linePoints); |
| 146 | |
| 147 | const name = line?.item?.props?.dataKey; |
| 148 | const targetLine = linesProps?.find((target) => target?.name === name); |
| 149 | const targetIndex = targetLine?.dotIndex ?? dotIndex; |
| 150 | const dashedPoints = linePoints?.slice(targetIndex); |
| 151 | const dashedWidth = getLineWidth(dashedPoints); |
| 152 | |
| 153 | if (!lineWidth || !dashedWidth) return; |
| 154 | |
| 155 | const firstWidth = lineWidth - dashedWidth; |
| 156 | const targetCurve = targetLine?.curveCorrection ?? curveCorrection; |
| 157 | const correctionWidth = (firstWidth * targetCurve) / 100; |
| 158 | const firstDasharray = firstWidth + correctionWidth; |
| 159 | |
| 160 | const targetRestStroke = targetLine?.strokeDasharray || restStroke; |
| 161 | const gapDashWidth = targetRestStroke?.[0] + targetRestStroke?.[1] || 1; |
| 162 | const restDasharrayLength = dashedWidth / gapDashWidth; |
| 163 | const restDasharray = new Array(Math.ceil(restDasharrayLength)).fill( |
| 164 | targetRestStroke.join(' ') |
| 165 | ); |
| 166 |
no test coverage detected