@octanejs/recharts 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/package.json +53 -0
- package/src/animation/AnimatedItems.tsrx +82 -0
- package/src/animation/AnimationControllerImpl.js +33 -0
- package/src/animation/AnimationHandle.js +291 -0
- package/src/animation/JavascriptAnimate.tsrx +81 -0
- package/src/animation/easing.js +193 -0
- package/src/animation/matchBy.js +279 -0
- package/src/animation/timeoutController.js +49 -0
- package/src/animation/useAnimationController.ts +22 -0
- package/src/animation/useAnimationStartSnapshot.ts +43 -0
- package/src/animation/util.js +11 -0
- package/src/cartesian/Bar.ts +4 -0
- package/src/cartesian/Bar.tsrx +716 -0
- package/src/cartesian/Bar.tsrx.d.ts +4 -0
- package/src/cartesian/BarStack.tsrx +97 -0
- package/src/cartesian/BarStack.tsrx.d.ts +6 -0
- package/src/cartesian/CartesianAxis.tsrx +413 -0
- package/src/cartesian/CartesianAxis.tsrx.d.ts +3 -0
- package/src/cartesian/GraphicalItemClipPath.tsrx +44 -0
- package/src/cartesian/Line.ts +4 -0
- package/src/cartesian/Line.tsrx +544 -0
- package/src/cartesian/Line.tsrx.d.ts +4 -0
- package/src/cartesian/LineDrawShape.tsrx +106 -0
- package/src/cartesian/LineDrawShape.tsrx.d.ts +2 -0
- package/src/cartesian/XAxis.tsrx +156 -0
- package/src/cartesian/XAxis.tsrx.d.ts +3 -0
- package/src/cartesian/YAxis.tsrx +183 -0
- package/src/cartesian/YAxis.tsrx.d.ts +3 -0
- package/src/cartesian/getCartesianPosition.js +277 -0
- package/src/cartesian/getEquidistantTicks.js +133 -0
- package/src/cartesian/getTicks.js +280 -0
- package/src/cartesian/useAnimatedLineLength.ts +33 -0
- package/src/chart/BarChart.tsrx +22 -0
- package/src/chart/BarChart.tsrx.d.ts +2 -0
- package/src/chart/CartesianChart.tsrx +76 -0
- package/src/chart/CartesianChart.tsrx.d.ts +10 -0
- package/src/chart/CategoricalChart.tsrx +54 -0
- package/src/chart/LineChart.tsrx +22 -0
- package/src/chart/LineChart.tsrx.d.ts +2 -0
- package/src/chart/RechartsWrapper.tsrx +294 -0
- package/src/component/ActivePoints.tsrx +76 -0
- package/src/component/Cell.ts +37 -0
- package/src/component/Dots.tsrx +74 -0
- package/src/component/Label.tsrx +309 -0
- package/src/component/Label.tsrx.d.ts +12 -0
- package/src/component/LabelList.tsrx +83 -0
- package/src/component/LabelList.tsrx.d.ts +8 -0
- package/src/component/ResponsiveContainer.ts +22 -0
- package/src/component/Text.tsrx +281 -0
- package/src/component/Text.tsrx.d.ts +9 -0
- package/src/component/responsiveContainerUtils.js +111 -0
- package/src/container/ClipPathProvider.tsrx +29 -0
- package/src/container/Layer.tsrx +9 -0
- package/src/container/Layer.tsrx.d.ts +6 -0
- package/src/container/RootSurface.tsrx +76 -0
- package/src/container/Surface.tsrx +23 -0
- package/src/container/Surface.tsrx.d.ts +12 -0
- package/src/context/CellsContext.ts +80 -0
- package/src/context/ErrorBarContext.tsrx +53 -0
- package/src/context/PanoramaContext.ts +10 -0
- package/src/context/RegisterGraphicalItemId.tsrx +19 -0
- package/src/context/accessibilityContext.js +10 -0
- package/src/context/chartDataContext.ts +57 -0
- package/src/context/chartLayoutContext.ts +171 -0
- package/src/context/legendPortalContext.ts +5 -0
- package/src/context/tooltipContext.js +55 -0
- package/src/context/tooltipPortalContext.ts +5 -0
- package/src/hooks.js +509 -0
- package/src/index.ts +64 -0
- package/src/internal.ts +37 -0
- package/src/polar/defaultPolarAngleAxisProps.js +27 -0
- package/src/polar/defaultPolarRadiusAxisProps.js +24 -0
- package/src/shape/Cross.tsrx +25 -0
- package/src/shape/Cross.tsrx.d.ts +12 -0
- package/src/shape/Curve.tsrx +132 -0
- package/src/shape/Curve.tsrx.d.ts +42 -0
- package/src/shape/Dot.tsrx +21 -0
- package/src/shape/Dot.tsrx.d.ts +9 -0
- package/src/shape/Polygon.tsrx +95 -0
- package/src/shape/Polygon.tsrx.d.ts +13 -0
- package/src/shape/Rectangle.tsrx +162 -0
- package/src/shape/Rectangle.tsrx.d.ts +31 -0
- package/src/shape/Sector.tsrx +219 -0
- package/src/shape/Sector.tsrx.d.ts +29 -0
- package/src/shape/Symbols.tsrx +95 -0
- package/src/shape/Symbols.tsrx.d.ts +15 -0
- package/src/shape/Trapezoid.tsrx +56 -0
- package/src/shape/Trapezoid.tsrx.d.ts +29 -0
- package/src/state/RechartsReduxContext.ts +7 -0
- package/src/state/RechartsStoreProvider.tsrx +24 -0
- package/src/state/ReportChartProps.ts +13 -0
- package/src/state/ReportEventSettings.ts +15 -0
- package/src/state/ReportMainChartProps.ts +22 -0
- package/src/state/SetGraphicalItem.ts +61 -0
- package/src/state/SetLegendPayload.ts +61 -0
- package/src/state/SetTooltipEntrySettings.ts +40 -0
- package/src/state/brushSlice.js +36 -0
- package/src/state/cartesianAxisSlice.js +256 -0
- package/src/state/chartDataSlice.js +67 -0
- package/src/state/errorBarSlice.js +53 -0
- package/src/state/eventSettingsSlice.js +25 -0
- package/src/state/externalEventsMiddleware.js +127 -0
- package/src/state/graphicalItemsSlice.js +91 -0
- package/src/state/hooks.ts +46 -0
- package/src/state/keyboardEventsMiddleware.js +226 -0
- package/src/state/layoutSlice.js +57 -0
- package/src/state/legendSlice.js +83 -0
- package/src/state/mouseEventsMiddleware.js +130 -0
- package/src/state/optionsSlice.js +45 -0
- package/src/state/polarAxisSlice.js +33 -0
- package/src/state/polarOptionsSlice.js +26 -0
- package/src/state/reduxDevtoolsJsonStringifyReplacer.js +14 -0
- package/src/state/referenceElementsSlice.js +51 -0
- package/src/state/renderedTicksSlice.js +41 -0
- package/src/state/rootPropsSlice.js +48 -0
- package/src/state/selectors/arrayEqualityCheck.js +32 -0
- package/src/state/selectors/axisSelectors.js +2113 -0
- package/src/state/selectors/barSelectors.js +263 -0
- package/src/state/selectors/barStackSelectors.js +65 -0
- package/src/state/selectors/brushSelectors.js +21 -0
- package/src/state/selectors/combiners/combineActiveLabel.js +18 -0
- package/src/state/selectors/combiners/combineActiveTooltipIndex.js +75 -0
- package/src/state/selectors/combiners/combineAllBarPositions.js +169 -0
- package/src/state/selectors/combiners/combineAxisRangeWithReverse.js +11 -0
- package/src/state/selectors/combiners/combineBarPosition.js +17 -0
- package/src/state/selectors/combiners/combineBarSizeList.js +115 -0
- package/src/state/selectors/combiners/combineCheckedDomain.js +44 -0
- package/src/state/selectors/combiners/combineConfiguredScale.js +46 -0
- package/src/state/selectors/combiners/combineCoordinateForDefaultIndex.js +48 -0
- package/src/state/selectors/combiners/combineDisplayedStackedData.js +54 -0
- package/src/state/selectors/combiners/combineInverseScaleFunction.js +12 -0
- package/src/state/selectors/combiners/combineRealScaleType.js +36 -0
- package/src/state/selectors/combiners/combineStackedData.js +22 -0
- package/src/state/selectors/combiners/combineTooltipInteractionState.js +121 -0
- package/src/state/selectors/combiners/combineTooltipPayload.js +248 -0
- package/src/state/selectors/combiners/combineTooltipPayloadConfigurations.js +59 -0
- package/src/state/selectors/containerSelectors.js +6 -0
- package/src/state/selectors/dataSelectors.js +97 -0
- package/src/state/selectors/graphicalItemSelectors.js +25 -0
- package/src/state/selectors/legendSelectors.js +18 -0
- package/src/state/selectors/lineSelectors.js +106 -0
- package/src/state/selectors/numberDomainEqualityCheck.js +11 -0
- package/src/state/selectors/pickAxisId.js +3 -0
- package/src/state/selectors/pickAxisType.js +3 -0
- package/src/state/selectors/polarAxisSelectors.js +237 -0
- package/src/state/selectors/rootPropsSelectors.js +13 -0
- package/src/state/selectors/selectActivePropsFromChartPointer.js +23 -0
- package/src/state/selectors/selectAllAxes.js +15 -0
- package/src/state/selectors/selectChartOffset.js +12 -0
- package/src/state/selectors/selectChartOffsetInternal.js +174 -0
- package/src/state/selectors/selectPlotArea.js +19 -0
- package/src/state/selectors/selectTooltipAxisId.js +3 -0
- package/src/state/selectors/selectTooltipAxisType.js +25 -0
- package/src/state/selectors/selectTooltipEventType.js +27 -0
- package/src/state/selectors/selectTooltipPayloadSearcher.js +3 -0
- package/src/state/selectors/selectTooltipSettings.js +3 -0
- package/src/state/selectors/selectTooltipState.js +3 -0
- package/src/state/selectors/selectors.js +222 -0
- package/src/state/selectors/tooltipSelectors.js +471 -0
- package/src/state/selectors/touchSelectors.js +31 -0
- package/src/state/store.js +101 -0
- package/src/state/tooltipSlice.js +237 -0
- package/src/state/touchEventsMiddleware.js +140 -0
- package/src/state/types/StackedGraphicalItem.js +18 -0
- package/src/state/zIndexSlice.js +167 -0
- package/src/synchronisation/syncSelectors.js +5 -0
- package/src/synchronisation/useChartSynchronisation.tsrx +285 -0
- package/src/util/ActiveShapeUtils.tsrx +62 -0
- package/src/util/BarUtils.tsrx +35 -0
- package/src/util/CartesianUtils.js +66 -0
- package/src/util/ChartUtils.js +630 -0
- package/src/util/Constants.js +42 -0
- package/src/util/DOMUtils.js +180 -0
- package/src/util/DataUtils.js +199 -0
- package/src/util/Events.js +7 -0
- package/src/util/Global.js +13 -0
- package/src/util/LRUCache.js +58 -0
- package/src/util/LogUtils.js +29 -0
- package/src/util/PolarUtils.js +190 -0
- package/src/util/ReactUtils.ts +94 -0
- package/src/util/ReduceCSSCalc.js +260 -0
- package/src/util/TickUtils.js +43 -0
- package/src/util/YAxisUtils.js +43 -0
- package/src/util/axisPropsAreEqual.js +63 -0
- package/src/util/createEventProxy.js +18 -0
- package/src/util/excludeEventProps.js +172 -0
- package/src/util/getActiveCoordinate.js +253 -0
- package/src/util/getAxisTypeBasedOnLayout.js +17 -0
- package/src/util/getClassNameFromUnknown.js +8 -0
- package/src/util/getEveryNth.js +29 -0
- package/src/util/getRadiusAndStrokeWidthFromDot.js +28 -0
- package/src/util/getRelativeCoordinate.js +96 -0
- package/src/util/getSliced.js +11 -0
- package/src/util/isDomainSpecifiedByUser.js +255 -0
- package/src/util/isWellBehavedNumber.js +8 -0
- package/src/util/propsAreEqual.js +96 -0
- package/src/util/resolveDefaultProps.js +131 -0
- package/src/util/round.js +39 -0
- package/src/util/scale/RechartsScale.js +68 -0
- package/src/util/scale/createCategoricalInverse.js +77 -0
- package/src/util/scale/getNiceTickValues.js +354 -0
- package/src/util/scale/index.js +3 -0
- package/src/util/scale/util/arithmetic.js +51 -0
- package/src/util/stacks/getStackSeriesIdentifier.js +10 -0
- package/src/util/svgPropertiesAndEvents.ts +29 -0
- package/src/util/svgPropertiesNoEvents.js +383 -0
- package/src/util/types.ts +59 -0
- package/src/util/useAnimationId.ts +14 -0
- package/src/util/useId.ts +13 -0
- package/src/util/usePrefersReducedMotion.ts +30 -0
- package/src/util/useReportScale.ts +26 -0
- package/src/util/useUniqueId.js +34 -0
- package/src/zIndex/DefaultZIndexes.js +75 -0
- package/src/zIndex/ZIndexLayer.tsrx +80 -0
- package/src/zIndex/ZIndexLayer.tsrx.d.ts +2 -0
- package/src/zIndex/ZIndexPortal.tsrx +43 -0
- package/src/zIndex/getZIndexFromUnknown.js +15 -0
- package/src/zIndex/zIndexSelectors.js +45 -0
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
// Port of component/Label.tsx — chart labels with cartesian + polar
|
|
2
|
+
// positioning, radial (textPath) rendering, and the label-as-prop parser
|
|
3
|
+
// (boolean | value | element | function | object). Element-as-prop resolves
|
|
4
|
+
// through octane cloneElement/createElement on descriptors.
|
|
5
|
+
import {
|
|
6
|
+
cloneElement,
|
|
7
|
+
createContext,
|
|
8
|
+
createElement,
|
|
9
|
+
isValidElement,
|
|
10
|
+
useContext,
|
|
11
|
+
useMemo,
|
|
12
|
+
} from 'octane';
|
|
13
|
+
import { clsx } from 'clsx';
|
|
14
|
+
import { isValidTextAnchor, Text } from './Text.tsrx';
|
|
15
|
+
import { isNullish, isNumber, isNumOrStr, mathSign, uniqueId } from '../util/DataUtils';
|
|
16
|
+
import { polarToCartesian } from '../util/PolarUtils';
|
|
17
|
+
import { cartesianViewBoxToTrapezoid, useViewBox } from '../context/chartLayoutContext.ts';
|
|
18
|
+
import { useAppSelector } from '../state/hooks';
|
|
19
|
+
import { selectPolarViewBox } from '../state/selectors/polarAxisSelectors';
|
|
20
|
+
import { resolveDefaultProps } from '../util/resolveDefaultProps';
|
|
21
|
+
import { svgPropertiesAndEvents } from '../util/svgPropertiesAndEvents.ts';
|
|
22
|
+
import { ZIndexLayer } from '../zIndex/ZIndexLayer.tsrx';
|
|
23
|
+
import { DefaultZIndexes } from '../zIndex/DefaultZIndexes';
|
|
24
|
+
import { getCartesianPosition } from '../cartesian/getCartesianPosition';
|
|
25
|
+
|
|
26
|
+
const CartesianLabelContext = createContext(null);
|
|
27
|
+
|
|
28
|
+
export function CartesianLabelContextProvider(props) @{
|
|
29
|
+
const { x, y, upperWidth, lowerWidth, width, height, children } = props;
|
|
30
|
+
const viewBox = useMemo(() => ({ x, y, upperWidth, lowerWidth, width, height }), [
|
|
31
|
+
x,
|
|
32
|
+
y,
|
|
33
|
+
upperWidth,
|
|
34
|
+
lowerWidth,
|
|
35
|
+
width,
|
|
36
|
+
height,
|
|
37
|
+
]);
|
|
38
|
+
<CartesianLabelContext.Provider value={viewBox}>{children}</CartesianLabelContext.Provider>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function useCartesianLabelContext() {
|
|
42
|
+
const labelChildContext = useContext(CartesianLabelContext);
|
|
43
|
+
const chartContext = useViewBox();
|
|
44
|
+
return labelChildContext ||
|
|
45
|
+
(chartContext ? cartesianViewBoxToTrapezoid(chartContext) : undefined);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const PolarLabelContext = createContext(null);
|
|
49
|
+
|
|
50
|
+
export function PolarLabelContextProvider(props) @{
|
|
51
|
+
const { cx, cy, innerRadius, outerRadius, startAngle, endAngle, clockWise, children } = props;
|
|
52
|
+
const viewBox = useMemo(
|
|
53
|
+
() => ({ cx, cy, innerRadius, outerRadius, startAngle, endAngle, clockWise }),
|
|
54
|
+
[cx, cy, innerRadius, outerRadius, startAngle, endAngle, clockWise],
|
|
55
|
+
);
|
|
56
|
+
<PolarLabelContext.Provider value={viewBox}>{children}</PolarLabelContext.Provider>
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function usePolarLabelContext() {
|
|
60
|
+
const labelChildContext = useContext(PolarLabelContext);
|
|
61
|
+
const chartContext = useAppSelector(selectPolarViewBox);
|
|
62
|
+
return labelChildContext || chartContext;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const getLabel = (props) => {
|
|
66
|
+
const { value, formatter } = props;
|
|
67
|
+
const label = isNullish(props.children) ? value : props.children;
|
|
68
|
+
if (typeof formatter === 'function') {
|
|
69
|
+
return formatter(label);
|
|
70
|
+
}
|
|
71
|
+
return label;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export function isLabelContentAFunction(content) {
|
|
75
|
+
return content != null && typeof content === 'function';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const getDeltaAngle = (startAngle, endAngle) => {
|
|
79
|
+
const sign = mathSign(endAngle - startAngle);
|
|
80
|
+
const deltaAngle = Math.min(Math.abs(endAngle - startAngle), 360);
|
|
81
|
+
return sign * deltaAngle;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
function renderRadialLabel(labelProps, position, label, attrs, viewBox) {
|
|
85
|
+
const { offset, className } = labelProps;
|
|
86
|
+
const { cx, cy, innerRadius, outerRadius, startAngle, endAngle, clockWise } = viewBox;
|
|
87
|
+
const radius = (innerRadius + outerRadius) / 2;
|
|
88
|
+
const deltaAngle = getDeltaAngle(startAngle, endAngle);
|
|
89
|
+
const sign =
|
|
90
|
+
deltaAngle >= 0 ? 1 : -1;
|
|
91
|
+
let labelAngle, direction;
|
|
92
|
+
switch (position) {
|
|
93
|
+
case 'insideStart':
|
|
94
|
+
labelAngle = startAngle + sign * offset;
|
|
95
|
+
direction = clockWise;
|
|
96
|
+
break;
|
|
97
|
+
case 'insideEnd':
|
|
98
|
+
labelAngle = endAngle - sign * offset;
|
|
99
|
+
direction = !clockWise;
|
|
100
|
+
break;
|
|
101
|
+
case 'end':
|
|
102
|
+
labelAngle = endAngle + sign * offset;
|
|
103
|
+
direction = clockWise;
|
|
104
|
+
break;
|
|
105
|
+
default:
|
|
106
|
+
throw new Error('Unsupported position ' + position);
|
|
107
|
+
}
|
|
108
|
+
direction = deltaAngle <= 0 ? direction : !direction;
|
|
109
|
+
const startPoint = polarToCartesian(cx, cy, radius, labelAngle);
|
|
110
|
+
const endPoint = polarToCartesian(cx, cy, radius, labelAngle + (direction ? 1 : -1) * 359);
|
|
111
|
+
const path =
|
|
112
|
+
'M' + startPoint.x + ',' + startPoint.y + '\n A' + radius + ',' + radius + ',0,1,' +
|
|
113
|
+
(direction ? 0 : 1) +
|
|
114
|
+
',\n ' +
|
|
115
|
+
endPoint.x +
|
|
116
|
+
',' +
|
|
117
|
+
endPoint.y;
|
|
118
|
+
const id =
|
|
119
|
+
isNullish(labelProps.id) ? uniqueId('recharts-radial-line-') : labelProps.id;
|
|
120
|
+
return <text
|
|
121
|
+
{...attrs}
|
|
122
|
+
dominantBaseline="central"
|
|
123
|
+
class={clsx('recharts-radial-bar-label', className)}
|
|
124
|
+
>
|
|
125
|
+
<defs>
|
|
126
|
+
<path id={id} d={path} />
|
|
127
|
+
</defs>
|
|
128
|
+
<textPath xlinkHref={'#' + id}>{label}</textPath>
|
|
129
|
+
</text>;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const getAttrsOfPolarLabel = (viewBox, offset, position) => {
|
|
133
|
+
const { cx, cy, innerRadius, outerRadius, startAngle, endAngle } = viewBox;
|
|
134
|
+
const midAngle = (startAngle + endAngle) / 2;
|
|
135
|
+
if (position === 'outside') {
|
|
136
|
+
const { x, y } = polarToCartesian(cx, cy, outerRadius + offset, midAngle);
|
|
137
|
+
return {
|
|
138
|
+
x,
|
|
139
|
+
y,
|
|
140
|
+
textAnchor: x >= cx ? 'start' : 'end',
|
|
141
|
+
verticalAnchor: 'middle',
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
if (position === 'center') {
|
|
145
|
+
return { x: cx, y: cy, textAnchor: 'middle', verticalAnchor: 'middle' };
|
|
146
|
+
}
|
|
147
|
+
if (position === 'centerTop') {
|
|
148
|
+
return { x: cx, y: cy, textAnchor: 'middle', verticalAnchor: 'start' };
|
|
149
|
+
}
|
|
150
|
+
if (position === 'centerBottom') {
|
|
151
|
+
return { x: cx, y: cy, textAnchor: 'middle', verticalAnchor: 'end' };
|
|
152
|
+
}
|
|
153
|
+
const r = (innerRadius + outerRadius) / 2;
|
|
154
|
+
const { x, y } = polarToCartesian(cx, cy, r, midAngle);
|
|
155
|
+
return { x, y, textAnchor: 'middle', verticalAnchor: 'middle' };
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const isPolar = (viewBox) => viewBox != null && 'cx' in viewBox && isNumber(viewBox.cx);
|
|
159
|
+
|
|
160
|
+
export const defaultLabelProps = {
|
|
161
|
+
angle: 0,
|
|
162
|
+
offset: 5,
|
|
163
|
+
zIndex: DefaultZIndexes.label,
|
|
164
|
+
position: 'middle',
|
|
165
|
+
textBreakAll: false,
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
function polarViewBoxToTrapezoid(viewBox) {
|
|
169
|
+
if (!isPolar(viewBox)) {
|
|
170
|
+
return viewBox;
|
|
171
|
+
}
|
|
172
|
+
const { cx, cy, outerRadius } = viewBox;
|
|
173
|
+
const diameter = outerRadius * 2;
|
|
174
|
+
return {
|
|
175
|
+
x: cx - outerRadius,
|
|
176
|
+
y: cy - outerRadius,
|
|
177
|
+
width: diameter,
|
|
178
|
+
upperWidth: diameter,
|
|
179
|
+
lowerWidth: diameter,
|
|
180
|
+
height: diameter,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function Label(outerProps) {
|
|
185
|
+
const props = resolveDefaultProps(outerProps, defaultLabelProps);
|
|
186
|
+
const {
|
|
187
|
+
viewBox: viewBoxFromProps,
|
|
188
|
+
parentViewBox,
|
|
189
|
+
position,
|
|
190
|
+
value,
|
|
191
|
+
children,
|
|
192
|
+
content,
|
|
193
|
+
className = '',
|
|
194
|
+
textBreakAll,
|
|
195
|
+
labelRef,
|
|
196
|
+
} = props;
|
|
197
|
+
const polarViewBox = usePolarLabelContext();
|
|
198
|
+
const cartesianViewBox = useCartesianLabelContext();
|
|
199
|
+
|
|
200
|
+
// position 'center' prefers the cartesian box (recharts#6030 quick fix).
|
|
201
|
+
const resolvedViewBox =
|
|
202
|
+
position === 'center' ? cartesianViewBox : polarViewBox ?? cartesianViewBox;
|
|
203
|
+
let viewBox, label, positionAttrs;
|
|
204
|
+
if (viewBoxFromProps == null) {
|
|
205
|
+
viewBox = resolvedViewBox;
|
|
206
|
+
} else if (isPolar(viewBoxFromProps)) {
|
|
207
|
+
viewBox = viewBoxFromProps;
|
|
208
|
+
} else {
|
|
209
|
+
viewBox = cartesianViewBoxToTrapezoid(viewBoxFromProps);
|
|
210
|
+
}
|
|
211
|
+
const cartesianBox = polarViewBoxToTrapezoid(viewBox);
|
|
212
|
+
if (
|
|
213
|
+
!viewBox ||
|
|
214
|
+
isNullish(value) && isNullish(children) && !isValidElement(content) &&
|
|
215
|
+
typeof content !== 'function'
|
|
216
|
+
) {
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
const propsWithViewBox = { ...props, viewBox };
|
|
220
|
+
if (isValidElement(content)) {
|
|
221
|
+
const { labelRef: _, ...propsWithoutLabelRef } = propsWithViewBox;
|
|
222
|
+
return cloneElement(content, propsWithoutLabelRef);
|
|
223
|
+
}
|
|
224
|
+
if (typeof content === 'function') {
|
|
225
|
+
const { content: _2, ...propsForContent } = propsWithViewBox;
|
|
226
|
+
label = createElement(content, propsForContent);
|
|
227
|
+
if (isValidElement(label)) {
|
|
228
|
+
return label;
|
|
229
|
+
}
|
|
230
|
+
} else {
|
|
231
|
+
label = getLabel(props);
|
|
232
|
+
}
|
|
233
|
+
const attrs = svgPropertiesAndEvents(props);
|
|
234
|
+
if (isPolar(viewBox)) {
|
|
235
|
+
if (position === 'insideStart' || position === 'insideEnd' || position === 'end') {
|
|
236
|
+
return renderRadialLabel(props, position, label, attrs, viewBox);
|
|
237
|
+
}
|
|
238
|
+
positionAttrs = getAttrsOfPolarLabel(viewBox, props.offset, props.position);
|
|
239
|
+
} else {
|
|
240
|
+
if (!cartesianBox) {
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
const cartesianResult = getCartesianPosition({
|
|
244
|
+
viewBox: cartesianBox,
|
|
245
|
+
position,
|
|
246
|
+
offset: props.offset,
|
|
247
|
+
parentViewBox: isPolar(parentViewBox) ? undefined : parentViewBox,
|
|
248
|
+
clamp: true,
|
|
249
|
+
});
|
|
250
|
+
positionAttrs = {
|
|
251
|
+
x: cartesianResult.x,
|
|
252
|
+
y: cartesianResult.y,
|
|
253
|
+
textAnchor: cartesianResult.horizontalAnchor,
|
|
254
|
+
verticalAnchor: cartesianResult.verticalAnchor,
|
|
255
|
+
...(cartesianResult.width !== undefined ? { width: cartesianResult.width } : {}),
|
|
256
|
+
...(cartesianResult.height !== undefined ? { height: cartesianResult.height } : {}),
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
return <ZIndexLayer zIndex={props.zIndex}>
|
|
260
|
+
<Text
|
|
261
|
+
ref={labelRef}
|
|
262
|
+
className={clsx('recharts-label', className)}
|
|
263
|
+
{...attrs}
|
|
264
|
+
{...positionAttrs}
|
|
265
|
+
textAnchor={isValidTextAnchor(attrs.textAnchor) ? attrs.textAnchor : positionAttrs.textAnchor}
|
|
266
|
+
breakAll={textBreakAll}
|
|
267
|
+
>{label}</Text>
|
|
268
|
+
</ZIndexLayer>;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
Label.displayName = 'Label';
|
|
272
|
+
|
|
273
|
+
const parseLabel = (label, viewBox, labelRef) => {
|
|
274
|
+
if (!label) {
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
const commonProps = { viewBox, labelRef };
|
|
278
|
+
if (label === true) {
|
|
279
|
+
return <Label key="label-implicit" {...commonProps} />;
|
|
280
|
+
}
|
|
281
|
+
if (isNumOrStr(label)) {
|
|
282
|
+
return <Label key="label-implicit" value={label} {...commonProps} />;
|
|
283
|
+
}
|
|
284
|
+
if (isValidElement(label)) {
|
|
285
|
+
if (label.type === Label) {
|
|
286
|
+
return cloneElement(label, { key: 'label-implicit', ...commonProps });
|
|
287
|
+
}
|
|
288
|
+
return <Label key="label-implicit" content={label} {...commonProps} />;
|
|
289
|
+
}
|
|
290
|
+
if (isLabelContentAFunction(label)) {
|
|
291
|
+
return <Label key="label-implicit" content={label} {...commonProps} />;
|
|
292
|
+
}
|
|
293
|
+
if (label && typeof label === 'object') {
|
|
294
|
+
return <Label {...label} key="label-implicit" {...commonProps} />;
|
|
295
|
+
}
|
|
296
|
+
return null;
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
export function CartesianLabelFromLabelProp(props) {
|
|
300
|
+
const { label, labelRef } = props;
|
|
301
|
+
const viewBox = useCartesianLabelContext();
|
|
302
|
+
return parseLabel(label, viewBox, labelRef) || null;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export function PolarLabelFromLabelProp(props) {
|
|
306
|
+
const { label } = props;
|
|
307
|
+
const viewBox = usePolarLabelContext();
|
|
308
|
+
return parseLabel(label, viewBox) || null;
|
|
309
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Type declaration for the .tsrx component (resolved by relative path).
|
|
2
|
+
export declare const defaultLabelProps: Record<string, unknown>;
|
|
3
|
+
export declare const Label: {
|
|
4
|
+
(props: Record<string, unknown>): unknown;
|
|
5
|
+
displayName: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function isLabelContentAFunction(content: unknown): boolean;
|
|
8
|
+
export declare const CartesianLabelContextProvider: (props: Record<string, unknown>) => unknown;
|
|
9
|
+
export declare const PolarLabelContextProvider: (props: Record<string, unknown>) => unknown;
|
|
10
|
+
export declare function usePolarLabelContext(): unknown;
|
|
11
|
+
export declare function CartesianLabelFromLabelProp(props: Record<string, unknown>): unknown;
|
|
12
|
+
export declare function PolarLabelFromLabelProp(props: Record<string, unknown>): unknown;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Port of component/LabelList.tsx — one Label per data entry, fed by the
|
|
2
|
+
// cartesian/polar label-list contexts the graphical items provide.
|
|
3
|
+
import { createContext, useContext, isValidElement } from 'octane';
|
|
4
|
+
import { isLabelContentAFunction, Label } from './Label.tsrx';
|
|
5
|
+
import { Layer } from '../container/Layer.tsrx';
|
|
6
|
+
import { getValueByDataKey } from '../util/ChartUtils';
|
|
7
|
+
import { isNullish } from '../util/DataUtils';
|
|
8
|
+
import { svgPropertiesAndEvents } from '../util/svgPropertiesAndEvents.ts';
|
|
9
|
+
import { ZIndexLayer } from '../zIndex/ZIndexLayer.tsrx';
|
|
10
|
+
import { DefaultZIndexes } from '../zIndex/DefaultZIndexes';
|
|
11
|
+
import { isRenderableText } from './Text.tsrx';
|
|
12
|
+
|
|
13
|
+
const defaultAccessor = (entry) => {
|
|
14
|
+
const val = Array.isArray(entry.value) ? entry.value[entry.value.length - 1] : entry.value;
|
|
15
|
+
if (isRenderableText(val)) {
|
|
16
|
+
return val;
|
|
17
|
+
}
|
|
18
|
+
return undefined;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const CartesianLabelListContext = createContext(undefined);
|
|
22
|
+
export const CartesianLabelListContextProvider = CartesianLabelListContext.Provider;
|
|
23
|
+
const PolarLabelListContext = createContext(undefined);
|
|
24
|
+
export const PolarLabelListContextProvider = PolarLabelListContext.Provider;
|
|
25
|
+
|
|
26
|
+
function useCartesianLabelListContext() {
|
|
27
|
+
return useContext(CartesianLabelListContext);
|
|
28
|
+
}
|
|
29
|
+
function usePolarLabelListContext() {
|
|
30
|
+
return useContext(PolarLabelListContext);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function LabelList(labelListProps) {
|
|
34
|
+
const { valueAccessor = defaultAccessor, ...restProps } = labelListProps;
|
|
35
|
+
const { dataKey, clockWise, id, textBreakAll, zIndex, ...others } = restProps;
|
|
36
|
+
const cartesianData = useCartesianLabelListContext();
|
|
37
|
+
const polarData = usePolarLabelListContext();
|
|
38
|
+
const data = cartesianData || polarData;
|
|
39
|
+
if (!data || !data.length) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
return <ZIndexLayer zIndex={zIndex ?? DefaultZIndexes.label}>
|
|
43
|
+
<Layer className="recharts-label-list">{data.map((entry, index) => {
|
|
44
|
+
const value =
|
|
45
|
+
isNullish(dataKey)
|
|
46
|
+
? valueAccessor(entry, index)
|
|
47
|
+
: getValueByDataKey(entry.payload, dataKey);
|
|
48
|
+
const idProps = isNullish(id) ? {} : { id: id + '-' + index };
|
|
49
|
+
return <Label
|
|
50
|
+
key={'label-' + index}
|
|
51
|
+
{...svgPropertiesAndEvents(entry)}
|
|
52
|
+
{...others}
|
|
53
|
+
{...idProps}
|
|
54
|
+
fill={restProps.fill ?? entry.fill}
|
|
55
|
+
parentViewBox={entry.parentViewBox}
|
|
56
|
+
value={value}
|
|
57
|
+
textBreakAll={textBreakAll}
|
|
58
|
+
viewBox={entry.viewBox}
|
|
59
|
+
index={index}
|
|
60
|
+
zIndex={0}
|
|
61
|
+
/>;
|
|
62
|
+
})}</Layer>
|
|
63
|
+
</ZIndexLayer>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
LabelList.displayName = 'LabelList';
|
|
67
|
+
|
|
68
|
+
export function LabelListFromLabelProp(props) {
|
|
69
|
+
const { label } = props;
|
|
70
|
+
if (!label) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
if (label === true) {
|
|
74
|
+
return <LabelList key="labelList-implicit" />;
|
|
75
|
+
}
|
|
76
|
+
if (isValidElement(label) || isLabelContentAFunction(label)) {
|
|
77
|
+
return <LabelList key="labelList-implicit" content={label} />;
|
|
78
|
+
}
|
|
79
|
+
if (typeof label === 'object') {
|
|
80
|
+
return <LabelList key="labelList-implicit" {...label} type={String(label.type)} />;
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Type declaration for the .tsrx component (resolved by relative path).
|
|
2
|
+
export declare const LabelList: {
|
|
3
|
+
(props: Record<string, unknown>): unknown;
|
|
4
|
+
displayName: string;
|
|
5
|
+
};
|
|
6
|
+
export declare const CartesianLabelListContextProvider: unknown;
|
|
7
|
+
export declare const PolarLabelListContextProvider: unknown;
|
|
8
|
+
export declare function LabelListFromLabelProp(props: Record<string, unknown>): unknown;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// PARTIAL port of component/ResponsiveContainer.tsx — Phase 1 needs only the
|
|
2
|
+
// CONTEXT surface (chartLayoutContext's ReportChartSize prefers container-
|
|
3
|
+
// measured dimensions when present). The ResizeObserver-driven container
|
|
4
|
+
// component itself is a Phase 2 port (measurement + interaction phase).
|
|
5
|
+
import { createContext, useContext } from 'octane';
|
|
6
|
+
|
|
7
|
+
export interface ResponsiveContainerDimensions {
|
|
8
|
+
width: number;
|
|
9
|
+
height: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const initialDimension: ResponsiveContainerDimensions = { width: -1, height: -1 };
|
|
13
|
+
|
|
14
|
+
// Upstream defaults the context VALUE to the {-1,-1} dimension object (always
|
|
15
|
+
// truthy); consumers gate on `.width > 0`. Mirror that exactly.
|
|
16
|
+
const ResponsiveContainerContext = createContext<ResponsiveContainerDimensions>(initialDimension);
|
|
17
|
+
|
|
18
|
+
export const ResponsiveContainerContextProviderInternal = ResponsiveContainerContext.Provider;
|
|
19
|
+
|
|
20
|
+
export const useResponsiveContainerContext = () => useContext(ResponsiveContainerContext);
|
|
21
|
+
|
|
22
|
+
export { initialDimension as defaultResponsiveContainerDimension };
|