File size: 6,945 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
/**
* @fileOverview Reference Line
*/
import * as React from 'react';
import { Component, ReactElement, SVGProps, useEffect } from 'react';
import { clsx } from 'clsx';
import { Layer } from '../container/Layer';
import { ImplicitLabelType, Label } from '../component/Label';
import { IfOverflow } from '../util/IfOverflow';
import { isNan, isNumOrStr } from '../util/DataUtils';
import { createLabeledScales, rectWithCoords } from '../util/CartesianUtils';
import { CartesianViewBox } from '../util/types';
import { Props as XAxisProps } from './XAxis';
import { Props as YAxisProps } from './YAxis';
import { filterProps } from '../util/ReactUtils';
import { useViewBox } from '../context/chartLayoutContext';
import { addLine, ReferenceLineSettings, removeLine } from '../state/referenceElementsSlice';
import { useAppDispatch, useAppSelector } from '../state/hooks';
import {
BaseAxisWithScale,
selectAxisScale,
selectXAxisSettings,
selectYAxisSettings,
} from '../state/selectors/axisSelectors';
import { useIsPanorama } from '../context/PanoramaContext';
import { useClipPathId } from '../container/ClipPathProvider';
interface InternalReferenceLineProps {
viewBox?: CartesianViewBox;
xAxis?: BaseAxisWithScale;
yAxis?: BaseAxisWithScale;
clipPathId?: number | string;
}
export type Segment = {
x?: number | string;
y?: number | string;
};
export type ReferenceLinePosition = 'middle' | 'start' | 'end';
interface ReferenceLineProps extends InternalReferenceLineProps {
ifOverflow?: IfOverflow;
x?: number | string;
y?: number | string;
segment?: ReadonlyArray<Segment>;
position?: ReferenceLinePosition;
className?: number | string;
yAxisId?: number | string;
xAxisId?: number | string;
shape?: ReactElement<SVGElement> | ((props: any) => ReactElement<SVGElement>);
label?: ImplicitLabelType;
}
/**
* This excludes `viewBox` prop from svg for two reasons:
* 1. The components wants viewBox of object type, and svg wants string
* - so there's a conflict, and the component will throw if it gets string
* 2. Internally the component calls `filterProps` which filters the viewBox away anyway
*/
export type Props = Omit<SVGProps<SVGLineElement>, 'viewBox'> & ReferenceLineProps;
const renderLine = (option: ReferenceLineProps['shape'], props: any) => {
let line;
if (React.isValidElement(option)) {
line = React.cloneElement(option, props);
} else if (typeof option === 'function') {
line = option(props);
} else {
line = <line {...props} className="recharts-reference-line-line" />;
}
return line;
};
type EndPointsPropsSubset = {
ifOverflow?: IfOverflow;
segment?: ReadonlyArray<Segment>;
x?: number | string;
y?: number | string;
};
// TODO: ScaleHelper
export const getEndPoints = (
scales: any,
isFixedX: boolean,
isFixedY: boolean,
isSegment: boolean,
viewBox: CartesianViewBox,
position: Props['position'],
xAxisOrientation: XAxisProps['orientation'],
yAxisOrientation: YAxisProps['orientation'],
props: EndPointsPropsSubset,
) => {
const { x, y, width, height } = viewBox;
if (isFixedY) {
const { y: yCoord } = props;
const coord = scales.y.apply(yCoord, { position });
// don't render the line if the scale can't compute a result that makes sense
if (isNan(coord)) return null;
if (props.ifOverflow === 'discard' && !scales.y.isInRange(coord)) {
return null;
}
const points = [
{ x: x + width, y: coord },
{ x, y: coord },
];
return yAxisOrientation === 'left' ? points.reverse() : points;
}
if (isFixedX) {
const { x: xCoord } = props;
const coord = scales.x.apply(xCoord, { position });
// don't render the line if the scale can't compute a result that makes sense
if (isNan(coord)) return null;
if (props.ifOverflow === 'discard' && !scales.x.isInRange(coord)) {
return null;
}
const points = [
{ x: coord, y: y + height },
{ x: coord, y },
];
return xAxisOrientation === 'top' ? points.reverse() : points;
}
if (isSegment) {
const { segment } = props;
const points = segment.map(p => scales.apply(p, { position }));
if (props.ifOverflow === 'discard' && points.some(p => !scales.isInRange(p))) {
return null;
}
return points;
}
return null;
};
function ReportReferenceLine(props: ReferenceLineSettings): null {
const dispatch = useAppDispatch();
useEffect(() => {
dispatch(addLine(props));
return () => {
dispatch(removeLine(props));
};
});
return null;
}
function ReferenceLineImpl(props: Props) {
const { x: fixedX, y: fixedY, segment, xAxisId, yAxisId, shape, className, ifOverflow } = props;
const isPanorama = useIsPanorama();
const clipPathId = useClipPathId();
const xAxis = useAppSelector(state => selectXAxisSettings(state, xAxisId));
const yAxis = useAppSelector(state => selectYAxisSettings(state, yAxisId));
const xAxisScale = useAppSelector(state => selectAxisScale(state, 'xAxis', xAxisId, isPanorama));
const yAxisScale = useAppSelector(state => selectAxisScale(state, 'yAxis', yAxisId, isPanorama));
const viewBox = useViewBox();
const isFixedX = isNumOrStr(fixedX);
const isFixedY = isNumOrStr(fixedY);
if (!clipPathId || !viewBox || xAxis == null || yAxis == null || xAxisScale == null || yAxisScale == null) {
return null;
}
const scales = createLabeledScales({ x: xAxisScale, y: yAxisScale });
const isSegment = segment && segment.length === 2;
const endPoints = getEndPoints(
scales,
isFixedX,
isFixedY,
isSegment,
viewBox,
props.position,
xAxis.orientation,
yAxis.orientation,
props,
);
if (!endPoints) {
return null;
}
const [{ x: x1, y: y1 }, { x: x2, y: y2 }] = endPoints;
const clipPath = ifOverflow === 'hidden' ? `url(#${clipPathId})` : undefined;
const lineProps = {
clipPath,
...filterProps(props, true),
x1,
y1,
x2,
y2,
};
return (
<Layer className={clsx('recharts-reference-line', className)}>
{renderLine(shape, lineProps)}
{Label.renderCallByParent(props, rectWithCoords({ x1, y1, x2, y2 }))}
</Layer>
);
}
function ReferenceLineSettingsDispatcher(props: Props) {
return (
<>
<ReportReferenceLine
yAxisId={props.yAxisId}
xAxisId={props.xAxisId}
ifOverflow={props.ifOverflow}
x={props.x}
y={props.y}
/>
<ReferenceLineImpl {...props} />
</>
);
}
// eslint-disable-next-line react/prefer-stateless-function
export class ReferenceLine extends Component<Props> {
static displayName = 'ReferenceLine';
static defaultProps = {
ifOverflow: 'discard',
xAxisId: 0,
yAxisId: 0,
fill: 'none',
stroke: '#ccc',
fillOpacity: 1,
strokeWidth: 1,
position: 'middle',
};
render() {
return <ReferenceLineSettingsDispatcher {...this.props} />;
}
}
|