|
|
|
|
|
|
|
|
|
|
|
import * as React from 'react'; |
|
|
import { ReactElement, SVGProps } from 'react'; |
|
|
|
|
|
import { warn } from '../util/LogUtils'; |
|
|
import { isNumber } from '../util/DataUtils'; |
|
|
import { ChartOffsetInternal } from '../util/types'; |
|
|
|
|
|
import { filterProps } from '../util/ReactUtils'; |
|
|
import { AxisPropsNeededForTicksGenerator, getCoordinatesOfGrid, getTicksOfAxis } from '../util/ChartUtils'; |
|
|
import { getTicks, GetTicksInput } from './getTicks'; |
|
|
import { CartesianAxis } from './CartesianAxis'; |
|
|
import { useChartHeight, useChartWidth, useOffsetInternal } from '../context/chartLayoutContext'; |
|
|
import { AxisId } from '../state/cartesianAxisSlice'; |
|
|
import { selectAxisPropsNeededForCartesianGridTicksGenerator } from '../state/selectors/axisSelectors'; |
|
|
import { useAppSelector } from '../state/hooks'; |
|
|
import { useIsPanorama } from '../context/PanoramaContext'; |
|
|
import { resolveDefaultProps } from '../util/resolveDefaultProps'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type GridLineTypeFunctionProps = Omit<LineItemProps, 'key'> & { |
|
|
|
|
|
key: LineItemProps['key'] | undefined; |
|
|
|
|
|
offset: ChartOffsetInternal; |
|
|
}; |
|
|
|
|
|
export type AxisPropsForCartesianGridTicksGeneration = AxisPropsNeededForTicksGenerator & |
|
|
Omit<GetTicksInput, 'ticks' | 'viewBox'>; |
|
|
|
|
|
type GridLineType = |
|
|
| SVGProps<SVGLineElement> |
|
|
| ReactElement<SVGElement> |
|
|
| ((props: GridLineTypeFunctionProps) => ReactElement<SVGElement>) |
|
|
| boolean; |
|
|
|
|
|
export type HorizontalCoordinatesGenerator = ( |
|
|
props: { |
|
|
yAxis: AxisPropsForCartesianGridTicksGeneration; |
|
|
width: number; |
|
|
height: number; |
|
|
offset: ChartOffsetInternal; |
|
|
}, |
|
|
syncWithTicks: boolean, |
|
|
) => number[]; |
|
|
|
|
|
export type VerticalCoordinatesGenerator = ( |
|
|
props: { |
|
|
xAxis: AxisPropsForCartesianGridTicksGeneration; |
|
|
width: number; |
|
|
height: number; |
|
|
offset: ChartOffsetInternal; |
|
|
}, |
|
|
syncWithTicks: boolean, |
|
|
) => number[]; |
|
|
|
|
|
interface InternalCartesianGridProps { |
|
|
width?: number; |
|
|
height?: number; |
|
|
horizontalCoordinatesGenerator?: HorizontalCoordinatesGenerator; |
|
|
verticalCoordinatesGenerator?: VerticalCoordinatesGenerator; |
|
|
} |
|
|
|
|
|
interface CartesianGridProps extends InternalCartesianGridProps { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
x?: number; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
y?: number; |
|
|
horizontal?: GridLineType; |
|
|
vertical?: GridLineType; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
horizontalPoints?: number[]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
verticalPoints?: number[]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
verticalFill?: string[]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
horizontalFill?: string[]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
syncWithTicks?: boolean; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
horizontalValues?: number[] | string[]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
verticalValues?: number[] | string[]; |
|
|
xAxisId?: AxisId; |
|
|
yAxisId?: AxisId; |
|
|
} |
|
|
|
|
|
type AcceptedSvgProps = Omit<SVGProps<SVGRectElement>, 'offset'>; |
|
|
|
|
|
export type Props = AcceptedSvgProps & CartesianGridProps; |
|
|
|
|
|
const Background = (props: Pick<AcceptedSvgProps, 'fill' | 'fillOpacity' | 'x' | 'y' | 'width' | 'height' | 'ry'>) => { |
|
|
const { fill } = props; |
|
|
|
|
|
if (!fill || fill === 'none') { |
|
|
return null; |
|
|
} |
|
|
|
|
|
const { fillOpacity, x, y, width, height, ry } = props; |
|
|
|
|
|
return ( |
|
|
<rect |
|
|
x={x} |
|
|
y={y} |
|
|
ry={ry} |
|
|
width={width} |
|
|
height={height} |
|
|
stroke="none" |
|
|
fill={fill} |
|
|
fillOpacity={fillOpacity} |
|
|
className="recharts-cartesian-grid-bg" |
|
|
/> |
|
|
); |
|
|
}; |
|
|
|
|
|
type LineItemProps = Props & { |
|
|
offset: ChartOffsetInternal; |
|
|
xAxis: null | AxisPropsForCartesianGridTicksGeneration; |
|
|
yAxis: null | AxisPropsForCartesianGridTicksGeneration; |
|
|
x1: number; |
|
|
y1: number; |
|
|
x2: number; |
|
|
y2: number; |
|
|
key: string; |
|
|
index: number; |
|
|
}; |
|
|
|
|
|
function renderLineItem(option: GridLineType, props: LineItemProps) { |
|
|
let lineItem; |
|
|
|
|
|
if (React.isValidElement(option)) { |
|
|
|
|
|
lineItem = React.cloneElement(option, props); |
|
|
} else if (typeof option === 'function') { |
|
|
lineItem = option(props); |
|
|
} else { |
|
|
const { x1, y1, x2, y2, key, ...others } = props; |
|
|
const { offset: __, ...restOfFilteredProps } = filterProps(others, false); |
|
|
lineItem = <line {...restOfFilteredProps} x1={x1} y1={y1} x2={x2} y2={y2} fill="none" key={key} />; |
|
|
} |
|
|
|
|
|
return lineItem; |
|
|
} |
|
|
|
|
|
type GridLinesProps = Props & { |
|
|
offset: GridLineTypeFunctionProps['offset']; |
|
|
xAxis: GridLineTypeFunctionProps['xAxis']; |
|
|
yAxis: GridLineTypeFunctionProps['yAxis']; |
|
|
}; |
|
|
|
|
|
function HorizontalGridLines(props: GridLinesProps) { |
|
|
const { x, width, horizontal = true, horizontalPoints } = props; |
|
|
|
|
|
if (!horizontal || !horizontalPoints || !horizontalPoints.length) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
const { xAxisId, yAxisId, ...otherLineItemProps } = props; |
|
|
|
|
|
const items = horizontalPoints.map((entry, i) => { |
|
|
const lineItemProps: LineItemProps = { |
|
|
...otherLineItemProps, |
|
|
x1: x, |
|
|
y1: entry, |
|
|
x2: x + width, |
|
|
y2: entry, |
|
|
key: `line-${i}`, |
|
|
index: i, |
|
|
}; |
|
|
|
|
|
return renderLineItem(horizontal, lineItemProps); |
|
|
}); |
|
|
|
|
|
return <g className="recharts-cartesian-grid-horizontal">{items}</g>; |
|
|
} |
|
|
|
|
|
function VerticalGridLines(props: GridLinesProps) { |
|
|
const { y, height, vertical = true, verticalPoints } = props; |
|
|
|
|
|
if (!vertical || !verticalPoints || !verticalPoints.length) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
const { xAxisId, yAxisId, ...otherLineItemProps } = props; |
|
|
|
|
|
const items = verticalPoints.map((entry, i) => { |
|
|
const lineItemProps: LineItemProps = { |
|
|
...otherLineItemProps, |
|
|
x1: entry, |
|
|
y1: y, |
|
|
x2: entry, |
|
|
y2: y + height, |
|
|
key: `line-${i}`, |
|
|
index: i, |
|
|
}; |
|
|
|
|
|
return renderLineItem(vertical, lineItemProps); |
|
|
}); |
|
|
|
|
|
return <g className="recharts-cartesian-grid-vertical">{items}</g>; |
|
|
} |
|
|
|
|
|
function HorizontalStripes(props: Props) { |
|
|
const { horizontalFill, fillOpacity, x, y, width, height, horizontalPoints, horizontal = true } = props; |
|
|
if (!horizontal || !horizontalFill || !horizontalFill.length) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
|
|
|
const roundedSortedHorizontalPoints = horizontalPoints.map(e => Math.round(e + y - y)).sort((a, b) => a - b); |
|
|
|
|
|
if (y !== roundedSortedHorizontalPoints[0]) { |
|
|
roundedSortedHorizontalPoints.unshift(0); |
|
|
} |
|
|
|
|
|
const items = roundedSortedHorizontalPoints.map((entry, i) => { |
|
|
|
|
|
const lastStripe = !roundedSortedHorizontalPoints[i + 1]; |
|
|
const lineHeight = lastStripe ? y + height - entry : roundedSortedHorizontalPoints[i + 1] - entry; |
|
|
if (lineHeight <= 0) { |
|
|
return null; |
|
|
} |
|
|
const colorIndex = i % horizontalFill.length; |
|
|
return ( |
|
|
<rect |
|
|
key={`react-${i}`} // eslint-disable-line react/no-array-index-key |
|
|
y={entry} |
|
|
x={x} |
|
|
height={lineHeight} |
|
|
width={width} |
|
|
stroke="none" |
|
|
fill={horizontalFill[colorIndex]} |
|
|
fillOpacity={fillOpacity} |
|
|
className="recharts-cartesian-grid-bg" |
|
|
/> |
|
|
); |
|
|
}); |
|
|
|
|
|
return <g className="recharts-cartesian-gridstripes-horizontal">{items}</g>; |
|
|
} |
|
|
|
|
|
function VerticalStripes(props: Props) { |
|
|
const { vertical = true, verticalFill, fillOpacity, x, y, width, height, verticalPoints } = props; |
|
|
if (!vertical || !verticalFill || !verticalFill.length) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
const roundedSortedVerticalPoints = verticalPoints.map(e => Math.round(e + x - x)).sort((a, b) => a - b); |
|
|
|
|
|
if (x !== roundedSortedVerticalPoints[0]) { |
|
|
roundedSortedVerticalPoints.unshift(0); |
|
|
} |
|
|
|
|
|
const items = roundedSortedVerticalPoints.map((entry, i) => { |
|
|
const lastStripe = !roundedSortedVerticalPoints[i + 1]; |
|
|
const lineWidth = lastStripe ? x + width - entry : roundedSortedVerticalPoints[i + 1] - entry; |
|
|
|
|
|
if (lineWidth <= 0) { |
|
|
return null; |
|
|
} |
|
|
const colorIndex = i % verticalFill.length; |
|
|
return ( |
|
|
<rect |
|
|
key={`react-${i}`} // eslint-disable-line react/no-array-index-key |
|
|
x={entry} |
|
|
y={y} |
|
|
width={lineWidth} |
|
|
height={height} |
|
|
stroke="none" |
|
|
fill={verticalFill[colorIndex]} |
|
|
fillOpacity={fillOpacity} |
|
|
className="recharts-cartesian-grid-bg" |
|
|
/> |
|
|
); |
|
|
}); |
|
|
|
|
|
return <g className="recharts-cartesian-gridstripes-vertical">{items}</g>; |
|
|
} |
|
|
|
|
|
const defaultVerticalCoordinatesGenerator: VerticalCoordinatesGenerator = ( |
|
|
{ xAxis, width, height, offset }, |
|
|
syncWithTicks, |
|
|
) => |
|
|
getCoordinatesOfGrid( |
|
|
getTicks({ |
|
|
...CartesianAxis.defaultProps, |
|
|
...xAxis, |
|
|
ticks: getTicksOfAxis(xAxis, true), |
|
|
viewBox: { x: 0, y: 0, width, height }, |
|
|
}), |
|
|
offset.left, |
|
|
offset.left + offset.width, |
|
|
syncWithTicks, |
|
|
); |
|
|
|
|
|
const defaultHorizontalCoordinatesGenerator: HorizontalCoordinatesGenerator = ( |
|
|
{ yAxis, width, height, offset }, |
|
|
syncWithTicks, |
|
|
) => |
|
|
getCoordinatesOfGrid( |
|
|
getTicks({ |
|
|
...CartesianAxis.defaultProps, |
|
|
...yAxis, |
|
|
ticks: getTicksOfAxis(yAxis, true), |
|
|
viewBox: { x: 0, y: 0, width, height }, |
|
|
}), |
|
|
offset.top, |
|
|
offset.top + offset.height, |
|
|
syncWithTicks, |
|
|
); |
|
|
|
|
|
const defaultProps = { |
|
|
horizontal: true, |
|
|
vertical: true, |
|
|
|
|
|
horizontalPoints: [], |
|
|
|
|
|
verticalPoints: [], |
|
|
|
|
|
stroke: '#ccc', |
|
|
fill: 'none', |
|
|
|
|
|
verticalFill: [], |
|
|
horizontalFill: [], |
|
|
xAxisId: 0, |
|
|
yAxisId: 0, |
|
|
} as const satisfies Partial<Props>; |
|
|
|
|
|
export function CartesianGrid(props: Props) { |
|
|
const chartWidth = useChartWidth(); |
|
|
const chartHeight = useChartHeight(); |
|
|
const offset = useOffsetInternal(); |
|
|
const propsIncludingDefaults = { |
|
|
...resolveDefaultProps(props, defaultProps), |
|
|
x: isNumber(props.x) ? props.x : offset.left, |
|
|
y: isNumber(props.y) ? props.y : offset.top, |
|
|
width: isNumber(props.width) ? props.width : offset.width, |
|
|
height: isNumber(props.height) ? props.height : offset.height, |
|
|
}; |
|
|
|
|
|
const { xAxisId, yAxisId, x, y, width, height, syncWithTicks, horizontalValues, verticalValues } = |
|
|
propsIncludingDefaults; |
|
|
|
|
|
const isPanorama = useIsPanorama(); |
|
|
const xAxis: AxisPropsForCartesianGridTicksGeneration = useAppSelector(state => |
|
|
selectAxisPropsNeededForCartesianGridTicksGenerator(state, 'xAxis', xAxisId, isPanorama), |
|
|
); |
|
|
const yAxis: AxisPropsForCartesianGridTicksGeneration = useAppSelector(state => |
|
|
selectAxisPropsNeededForCartesianGridTicksGenerator(state, 'yAxis', yAxisId, isPanorama), |
|
|
); |
|
|
|
|
|
if ( |
|
|
!isNumber(width) || |
|
|
width <= 0 || |
|
|
!isNumber(height) || |
|
|
height <= 0 || |
|
|
!isNumber(x) || |
|
|
x !== +x || |
|
|
!isNumber(y) || |
|
|
y !== +y |
|
|
) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const verticalCoordinatesGenerator = |
|
|
propsIncludingDefaults.verticalCoordinatesGenerator || defaultVerticalCoordinatesGenerator; |
|
|
const horizontalCoordinatesGenerator = |
|
|
propsIncludingDefaults.horizontalCoordinatesGenerator || defaultHorizontalCoordinatesGenerator; |
|
|
|
|
|
let { horizontalPoints, verticalPoints } = propsIncludingDefaults; |
|
|
|
|
|
|
|
|
if ((!horizontalPoints || !horizontalPoints.length) && typeof horizontalCoordinatesGenerator === 'function') { |
|
|
const isHorizontalValues = horizontalValues && horizontalValues.length; |
|
|
|
|
|
const generatorResult = horizontalCoordinatesGenerator( |
|
|
{ |
|
|
yAxis: yAxis |
|
|
? { |
|
|
...yAxis, |
|
|
ticks: isHorizontalValues ? horizontalValues : yAxis.ticks, |
|
|
} |
|
|
: undefined, |
|
|
width: chartWidth, |
|
|
height: chartHeight, |
|
|
offset, |
|
|
}, |
|
|
isHorizontalValues ? true : syncWithTicks, |
|
|
); |
|
|
|
|
|
warn( |
|
|
Array.isArray(generatorResult), |
|
|
`horizontalCoordinatesGenerator should return Array but instead it returned [${typeof generatorResult}]`, |
|
|
); |
|
|
if (Array.isArray(generatorResult)) { |
|
|
horizontalPoints = generatorResult; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if ((!verticalPoints || !verticalPoints.length) && typeof verticalCoordinatesGenerator === 'function') { |
|
|
const isVerticalValues = verticalValues && verticalValues.length; |
|
|
const generatorResult = verticalCoordinatesGenerator( |
|
|
{ |
|
|
xAxis: xAxis |
|
|
? { |
|
|
...xAxis, |
|
|
ticks: isVerticalValues ? verticalValues : xAxis.ticks, |
|
|
} |
|
|
: undefined, |
|
|
width: chartWidth, |
|
|
height: chartHeight, |
|
|
offset, |
|
|
}, |
|
|
isVerticalValues ? true : syncWithTicks, |
|
|
); |
|
|
warn( |
|
|
Array.isArray(generatorResult), |
|
|
`verticalCoordinatesGenerator should return Array but instead it returned [${typeof generatorResult}]`, |
|
|
); |
|
|
if (Array.isArray(generatorResult)) { |
|
|
verticalPoints = generatorResult; |
|
|
} |
|
|
} |
|
|
|
|
|
return ( |
|
|
<g className="recharts-cartesian-grid"> |
|
|
<Background |
|
|
fill={propsIncludingDefaults.fill} |
|
|
fillOpacity={propsIncludingDefaults.fillOpacity} |
|
|
x={propsIncludingDefaults.x} |
|
|
y={propsIncludingDefaults.y} |
|
|
width={propsIncludingDefaults.width} |
|
|
height={propsIncludingDefaults.height} |
|
|
ry={propsIncludingDefaults.ry} |
|
|
/> |
|
|
|
|
|
<HorizontalStripes {...propsIncludingDefaults} horizontalPoints={horizontalPoints} /> |
|
|
<VerticalStripes {...propsIncludingDefaults} verticalPoints={verticalPoints} /> |
|
|
|
|
|
<HorizontalGridLines |
|
|
{...propsIncludingDefaults} |
|
|
offset={offset} |
|
|
horizontalPoints={horizontalPoints} |
|
|
xAxis={xAxis} |
|
|
yAxis={yAxis} |
|
|
/> |
|
|
|
|
|
<VerticalGridLines |
|
|
{...propsIncludingDefaults} |
|
|
offset={offset} |
|
|
verticalPoints={verticalPoints} |
|
|
xAxis={xAxis} |
|
|
yAxis={yAxis} |
|
|
/> |
|
|
</g> |
|
|
); |
|
|
} |
|
|
|
|
|
CartesianGrid.displayName = 'CartesianGrid'; |
|
|
|