| 1 | var _excluded = ["onMouseEnter", "onMouseLeave", "onClick"],
|
|---|
| 2 | _excluded2 = ["value", "background", "tooltipPosition"],
|
|---|
| 3 | _excluded3 = ["id"],
|
|---|
| 4 | _excluded4 = ["onMouseEnter", "onClick", "onMouseLeave"];
|
|---|
| 5 | function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
|
|---|
| 6 | function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|---|
| 7 | function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|---|
| 8 | function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|---|
| 9 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 10 | function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|---|
| 11 | function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
|
|---|
| 12 | function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
|
|---|
| 13 | import * as React from 'react';
|
|---|
| 14 | import { PureComponent, useCallback, useRef, useState } from 'react';
|
|---|
| 15 | import { clsx } from 'clsx';
|
|---|
| 16 | import { Layer } from '../container/Layer';
|
|---|
| 17 | import { Cell } from '../component/Cell';
|
|---|
| 18 | import { CartesianLabelListContextProvider, LabelListFromLabelProp } from '../component/LabelList';
|
|---|
| 19 | import { interpolate, isNan, mathSign, noop } from '../util/DataUtils';
|
|---|
| 20 | import { findAllByType } from '../util/ReactUtils';
|
|---|
| 21 | import { getBaseValueOfBar, getCateCoordinateOfBar, getTooltipNameProp, getValueByDataKey, truncateByDomain } from '../util/ChartUtils';
|
|---|
| 22 | import { adaptEventsOfChild } from '../util/types';
|
|---|
| 23 | import { BarRectangle, minPointSizeCallback } from '../util/BarUtils';
|
|---|
| 24 | import { useMouseClickItemDispatch, useMouseEnterItemDispatch, useMouseLeaveItemDispatch } from '../context/tooltipContext';
|
|---|
| 25 | import { SetTooltipEntrySettings } from '../state/SetTooltipEntrySettings';
|
|---|
| 26 | import { SetErrorBarContext } from '../context/ErrorBarContext';
|
|---|
| 27 | import { GraphicalItemClipPath, useNeedsClip } from './GraphicalItemClipPath';
|
|---|
| 28 | import { useChartLayout } from '../context/chartLayoutContext';
|
|---|
| 29 | import { selectBarRectangles } from '../state/selectors/barSelectors';
|
|---|
| 30 | import { useAppSelector } from '../state/hooks';
|
|---|
| 31 | import { useIsPanorama } from '../context/PanoramaContext';
|
|---|
| 32 | import { selectActiveTooltipDataKey, selectActiveTooltipIndex } from '../state/selectors/tooltipSelectors';
|
|---|
| 33 | import { SetLegendPayload } from '../state/SetLegendPayload';
|
|---|
| 34 | import { useAnimationId } from '../util/useAnimationId';
|
|---|
| 35 | import { resolveDefaultProps } from '../util/resolveDefaultProps';
|
|---|
| 36 | import { RegisterGraphicalItemId } from '../context/RegisterGraphicalItemId';
|
|---|
| 37 | import { SetCartesianGraphicalItem } from '../state/SetGraphicalItem';
|
|---|
| 38 | import { svgPropertiesNoEvents, svgPropertiesNoEventsFromUnknown } from '../util/svgPropertiesNoEvents';
|
|---|
| 39 | import { JavascriptAnimate } from '../animation/JavascriptAnimate';
|
|---|
| 40 | import { ZIndexLayer } from '../zIndex/ZIndexLayer';
|
|---|
| 41 | import { DefaultZIndexes } from '../zIndex/DefaultZIndexes';
|
|---|
| 42 | import { getZIndexFromUnknown } from '../zIndex/getZIndexFromUnknown';
|
|---|
| 43 | import { propsAreEqual } from '../util/propsAreEqual';
|
|---|
| 44 | import { BarStackClipLayer, useStackId } from './BarStack';
|
|---|
| 45 | var computeLegendPayloadFromBarData = props => {
|
|---|
| 46 | var {
|
|---|
| 47 | dataKey,
|
|---|
| 48 | name,
|
|---|
| 49 | fill,
|
|---|
| 50 | legendType,
|
|---|
| 51 | hide
|
|---|
| 52 | } = props;
|
|---|
| 53 | return [{
|
|---|
| 54 | inactive: hide,
|
|---|
| 55 | dataKey,
|
|---|
| 56 | type: legendType,
|
|---|
| 57 | color: fill,
|
|---|
| 58 | value: getTooltipNameProp(name, dataKey),
|
|---|
| 59 | payload: props
|
|---|
| 60 | }];
|
|---|
| 61 | };
|
|---|
| 62 | var SetBarTooltipEntrySettings = /*#__PURE__*/React.memo(_ref => {
|
|---|
| 63 | var {
|
|---|
| 64 | dataKey,
|
|---|
| 65 | stroke,
|
|---|
| 66 | strokeWidth,
|
|---|
| 67 | fill,
|
|---|
| 68 | name,
|
|---|
| 69 | hide,
|
|---|
| 70 | unit,
|
|---|
| 71 | tooltipType,
|
|---|
| 72 | id
|
|---|
| 73 | } = _ref;
|
|---|
| 74 | var tooltipEntrySettings = {
|
|---|
| 75 | dataDefinedOnItem: undefined,
|
|---|
| 76 | getPosition: noop,
|
|---|
| 77 | settings: {
|
|---|
| 78 | stroke,
|
|---|
| 79 | strokeWidth,
|
|---|
| 80 | fill,
|
|---|
| 81 | dataKey,
|
|---|
| 82 | nameKey: undefined,
|
|---|
| 83 | name: getTooltipNameProp(name, dataKey),
|
|---|
| 84 | hide,
|
|---|
| 85 | type: tooltipType,
|
|---|
| 86 | color: fill,
|
|---|
| 87 | unit,
|
|---|
| 88 | graphicalItemId: id
|
|---|
| 89 | }
|
|---|
| 90 | };
|
|---|
| 91 | return /*#__PURE__*/React.createElement(SetTooltipEntrySettings, {
|
|---|
| 92 | tooltipEntrySettings: tooltipEntrySettings
|
|---|
| 93 | });
|
|---|
| 94 | });
|
|---|
| 95 | function BarBackground(props) {
|
|---|
| 96 | var activeIndex = useAppSelector(selectActiveTooltipIndex);
|
|---|
| 97 | var {
|
|---|
| 98 | data,
|
|---|
| 99 | dataKey,
|
|---|
| 100 | background: backgroundFromProps,
|
|---|
| 101 | allOtherBarProps
|
|---|
| 102 | } = props;
|
|---|
| 103 | var {
|
|---|
| 104 | onMouseEnter: onMouseEnterFromProps,
|
|---|
| 105 | onMouseLeave: onMouseLeaveFromProps,
|
|---|
| 106 | onClick: onItemClickFromProps
|
|---|
| 107 | } = allOtherBarProps,
|
|---|
| 108 | restOfAllOtherProps = _objectWithoutProperties(allOtherBarProps, _excluded);
|
|---|
| 109 |
|
|---|
| 110 | // @ts-expect-error bar mouse events are not compatible with recharts mouse events
|
|---|
| 111 | var onMouseEnterFromContext = useMouseEnterItemDispatch(onMouseEnterFromProps, dataKey, allOtherBarProps.id);
|
|---|
| 112 | // @ts-expect-error bar mouse events are not compatible with recharts mouse events
|
|---|
| 113 | var onMouseLeaveFromContext = useMouseLeaveItemDispatch(onMouseLeaveFromProps);
|
|---|
| 114 | // @ts-expect-error bar mouse events are not compatible with recharts mouse events
|
|---|
| 115 | var onClickFromContext = useMouseClickItemDispatch(onItemClickFromProps, dataKey, allOtherBarProps.id);
|
|---|
| 116 | if (!backgroundFromProps || data == null) {
|
|---|
| 117 | return null;
|
|---|
| 118 | }
|
|---|
| 119 | var backgroundProps = svgPropertiesNoEventsFromUnknown(backgroundFromProps);
|
|---|
| 120 | return /*#__PURE__*/React.createElement(ZIndexLayer, {
|
|---|
| 121 | zIndex: getZIndexFromUnknown(backgroundFromProps, DefaultZIndexes.barBackground)
|
|---|
| 122 | }, data.map((entry, i) => {
|
|---|
| 123 | var {
|
|---|
| 124 | value,
|
|---|
| 125 | background: backgroundFromDataEntry,
|
|---|
| 126 | tooltipPosition
|
|---|
| 127 | } = entry,
|
|---|
| 128 | rest = _objectWithoutProperties(entry, _excluded2);
|
|---|
| 129 | if (!backgroundFromDataEntry) {
|
|---|
| 130 | return null;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | // @ts-expect-error BarRectangleItem type definition says it's missing properties, but I can see them present in debugger!
|
|---|
| 134 | var onMouseEnter = onMouseEnterFromContext(entry, i);
|
|---|
| 135 | // @ts-expect-error BarRectangleItem type definition says it's missing properties, but I can see them present in debugger!
|
|---|
| 136 | var onMouseLeave = onMouseLeaveFromContext(entry, i);
|
|---|
| 137 | // @ts-expect-error BarRectangleItem type definition says it's missing properties, but I can see them present in debugger!
|
|---|
| 138 | var onClick = onClickFromContext(entry, i);
|
|---|
| 139 | var barRectangleProps = _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({
|
|---|
| 140 | option: backgroundFromProps,
|
|---|
| 141 | isActive: String(i) === activeIndex
|
|---|
| 142 | }, rest), {}, {
|
|---|
| 143 | // @ts-expect-error backgroundProps is contributing unknown props
|
|---|
| 144 | fill: '#eee'
|
|---|
| 145 | }, backgroundFromDataEntry), backgroundProps), adaptEventsOfChild(restOfAllOtherProps, entry, i)), {}, {
|
|---|
| 146 | onMouseEnter,
|
|---|
| 147 | onMouseLeave,
|
|---|
| 148 | onClick,
|
|---|
| 149 | dataKey,
|
|---|
| 150 | index: i,
|
|---|
| 151 | className: 'recharts-bar-background-rectangle'
|
|---|
| 152 | });
|
|---|
| 153 | return /*#__PURE__*/React.createElement(BarRectangle, _extends({
|
|---|
| 154 | key: "background-bar-".concat(i)
|
|---|
| 155 | }, barRectangleProps));
|
|---|
| 156 | }));
|
|---|
| 157 | }
|
|---|
| 158 | function BarLabelListProvider(_ref2) {
|
|---|
| 159 | var {
|
|---|
| 160 | showLabels,
|
|---|
| 161 | children,
|
|---|
| 162 | rects
|
|---|
| 163 | } = _ref2;
|
|---|
| 164 | var labelListEntries = rects === null || rects === void 0 ? void 0 : rects.map(entry => {
|
|---|
| 165 | var viewBox = {
|
|---|
| 166 | x: entry.x,
|
|---|
| 167 | y: entry.y,
|
|---|
| 168 | width: entry.width,
|
|---|
| 169 | lowerWidth: entry.width,
|
|---|
| 170 | upperWidth: entry.width,
|
|---|
| 171 | height: entry.height
|
|---|
| 172 | };
|
|---|
| 173 | return _objectSpread(_objectSpread({}, viewBox), {}, {
|
|---|
| 174 | value: entry.value,
|
|---|
| 175 | payload: entry.payload,
|
|---|
| 176 | parentViewBox: entry.parentViewBox,
|
|---|
| 177 | viewBox,
|
|---|
| 178 | fill: entry.fill
|
|---|
| 179 | });
|
|---|
| 180 | });
|
|---|
| 181 | return /*#__PURE__*/React.createElement(CartesianLabelListContextProvider, {
|
|---|
| 182 | value: showLabels ? labelListEntries : undefined
|
|---|
| 183 | }, children);
|
|---|
| 184 | }
|
|---|
| 185 | function BarRectangleWithActiveState(props) {
|
|---|
| 186 | var {
|
|---|
| 187 | shape,
|
|---|
| 188 | activeBar,
|
|---|
| 189 | baseProps,
|
|---|
| 190 | entry,
|
|---|
| 191 | index,
|
|---|
| 192 | dataKey
|
|---|
| 193 | } = props;
|
|---|
| 194 | var activeIndex = useAppSelector(selectActiveTooltipIndex);
|
|---|
| 195 | var activeDataKey = useAppSelector(selectActiveTooltipDataKey);
|
|---|
| 196 | /*
|
|---|
| 197 | * Bars support stacking, meaning that there can be multiple bars at the same x value.
|
|---|
| 198 | * With Tooltip shared=false we only want to highlight the currently active Bar, not all.
|
|---|
| 199 | *
|
|---|
| 200 | * Also, if the tooltip is shared, we want to highlight all bars at the same x value
|
|---|
| 201 | * regardless of the dataKey.
|
|---|
| 202 | *
|
|---|
| 203 | * With shared Tooltip, the activeDataKey is undefined.
|
|---|
| 204 | */
|
|---|
| 205 | var isActive = activeBar && String(index) === activeIndex && (activeDataKey == null || dataKey === activeDataKey);
|
|---|
| 206 | var option = isActive ? activeBar : shape;
|
|---|
| 207 | if (isActive) {
|
|---|
| 208 | return /*#__PURE__*/React.createElement(ZIndexLayer, {
|
|---|
| 209 | zIndex: DefaultZIndexes.activeBar
|
|---|
| 210 | }, /*#__PURE__*/React.createElement(BarStackClipLayer, {
|
|---|
| 211 | index: index
|
|---|
| 212 | }, /*#__PURE__*/React.createElement(BarRectangle, _extends({}, baseProps, {
|
|---|
| 213 | name: String(baseProps.name)
|
|---|
| 214 | }, entry, {
|
|---|
| 215 | isActive: isActive,
|
|---|
| 216 | option: option,
|
|---|
| 217 | index: index,
|
|---|
| 218 | dataKey: dataKey
|
|---|
| 219 | }))));
|
|---|
| 220 | }
|
|---|
| 221 | return /*#__PURE__*/React.createElement(BarRectangle, _extends({}, baseProps, {
|
|---|
| 222 | name: String(baseProps.name)
|
|---|
| 223 | }, entry, {
|
|---|
| 224 | isActive: isActive,
|
|---|
| 225 | option: option,
|
|---|
| 226 | index: index,
|
|---|
| 227 | dataKey: dataKey
|
|---|
| 228 | }));
|
|---|
| 229 | }
|
|---|
| 230 | function BarRectangleNeverActive(props) {
|
|---|
| 231 | var {
|
|---|
| 232 | shape,
|
|---|
| 233 | baseProps,
|
|---|
| 234 | entry,
|
|---|
| 235 | index,
|
|---|
| 236 | dataKey
|
|---|
| 237 | } = props;
|
|---|
| 238 | return /*#__PURE__*/React.createElement(BarRectangle, _extends({}, baseProps, {
|
|---|
| 239 | name: String(baseProps.name)
|
|---|
| 240 | }, entry, {
|
|---|
| 241 | isActive: false,
|
|---|
| 242 | option: shape,
|
|---|
| 243 | index: index,
|
|---|
| 244 | dataKey: dataKey
|
|---|
| 245 | }));
|
|---|
| 246 | }
|
|---|
| 247 | function BarRectangles(_ref3) {
|
|---|
| 248 | var _svgPropertiesNoEvent;
|
|---|
| 249 | var {
|
|---|
| 250 | data,
|
|---|
| 251 | props
|
|---|
| 252 | } = _ref3;
|
|---|
| 253 | var _ref4 = (_svgPropertiesNoEvent = svgPropertiesNoEvents(props)) !== null && _svgPropertiesNoEvent !== void 0 ? _svgPropertiesNoEvent : {},
|
|---|
| 254 | {
|
|---|
| 255 | id
|
|---|
| 256 | } = _ref4,
|
|---|
| 257 | baseProps = _objectWithoutProperties(_ref4, _excluded3);
|
|---|
| 258 | var {
|
|---|
| 259 | shape,
|
|---|
| 260 | dataKey,
|
|---|
| 261 | activeBar
|
|---|
| 262 | } = props;
|
|---|
| 263 | var {
|
|---|
| 264 | onMouseEnter: onMouseEnterFromProps,
|
|---|
| 265 | onClick: onItemClickFromProps,
|
|---|
| 266 | onMouseLeave: onMouseLeaveFromProps
|
|---|
| 267 | } = props,
|
|---|
| 268 | restOfAllOtherProps = _objectWithoutProperties(props, _excluded4);
|
|---|
| 269 |
|
|---|
| 270 | // @ts-expect-error bar mouse events are not compatible with recharts mouse events
|
|---|
| 271 | var onMouseEnterFromContext = useMouseEnterItemDispatch(onMouseEnterFromProps, dataKey, id);
|
|---|
| 272 | // @ts-expect-error bar mouse events are not compatible with recharts mouse events
|
|---|
| 273 | var onMouseLeaveFromContext = useMouseLeaveItemDispatch(onMouseLeaveFromProps);
|
|---|
| 274 | // @ts-expect-error bar mouse events are not compatible with recharts mouse events
|
|---|
| 275 | var onClickFromContext = useMouseClickItemDispatch(onItemClickFromProps, dataKey, id);
|
|---|
| 276 | if (!data) {
|
|---|
| 277 | return null;
|
|---|
| 278 | }
|
|---|
| 279 | return /*#__PURE__*/React.createElement(React.Fragment, null, data.map((entry, i) => {
|
|---|
| 280 | return /*#__PURE__*/React.createElement(BarStackClipLayer, _extends({
|
|---|
| 281 | index: i
|
|---|
| 282 | // https://github.com/recharts/recharts/issues/5415
|
|---|
| 283 | ,
|
|---|
| 284 | key: "rectangle-".concat(entry === null || entry === void 0 ? void 0 : entry.x, "-").concat(entry === null || entry === void 0 ? void 0 : entry.y, "-").concat(entry === null || entry === void 0 ? void 0 : entry.value, "-").concat(i),
|
|---|
| 285 | className: "recharts-bar-rectangle"
|
|---|
| 286 | }, adaptEventsOfChild(restOfAllOtherProps, entry, i), {
|
|---|
| 287 | // @ts-expect-error BarRectangleItem type definition says it's missing properties, but I can see them present in debugger!
|
|---|
| 288 | onMouseEnter: onMouseEnterFromContext(entry, i)
|
|---|
| 289 | // @ts-expect-error BarRectangleItem type definition says it's missing properties, but I can see them present in debugger!
|
|---|
| 290 | ,
|
|---|
| 291 | onMouseLeave: onMouseLeaveFromContext(entry, i)
|
|---|
| 292 | // @ts-expect-error BarRectangleItem type definition says it's missing properties, but I can see them present in debugger!
|
|---|
| 293 | ,
|
|---|
| 294 | onClick: onClickFromContext(entry, i)
|
|---|
| 295 | }), activeBar ? /*#__PURE__*/React.createElement(BarRectangleWithActiveState, {
|
|---|
| 296 | shape: shape,
|
|---|
| 297 | activeBar: activeBar,
|
|---|
| 298 | baseProps: baseProps,
|
|---|
| 299 | entry: entry,
|
|---|
| 300 | index: i,
|
|---|
| 301 | dataKey: dataKey
|
|---|
| 302 | }) :
|
|---|
| 303 | /*#__PURE__*/
|
|---|
| 304 | /*
|
|---|
| 305 | * If the `activeBar` prop is falsy, then let's call the variant without hooks.
|
|---|
| 306 | * Using the `selectActiveTooltipIndex` selector is usually fast
|
|---|
| 307 | * but in charts with large-ish amount of data even the few nanoseconds add up to a noticeable jank.
|
|---|
| 308 | * If the activeBar is false then we don't need to know which index is active - because we won't use it anyway.
|
|---|
| 309 | * So let's just skip the hooks altogether. That way, React can skip rendering the component,
|
|---|
| 310 | * and can skip the tree reconciliation for its children too.
|
|---|
| 311 | * Because we can't call hooks conditionally, we need to have a separate component for that.
|
|---|
| 312 | */
|
|---|
| 313 | React.createElement(BarRectangleNeverActive, {
|
|---|
| 314 | shape: shape,
|
|---|
| 315 | baseProps: baseProps,
|
|---|
| 316 | entry: entry,
|
|---|
| 317 | index: i,
|
|---|
| 318 | dataKey: dataKey
|
|---|
| 319 | }));
|
|---|
| 320 | }));
|
|---|
| 321 | }
|
|---|
| 322 | function RectanglesWithAnimation(_ref5) {
|
|---|
| 323 | var {
|
|---|
| 324 | props,
|
|---|
| 325 | previousRectanglesRef
|
|---|
| 326 | } = _ref5;
|
|---|
| 327 | var {
|
|---|
| 328 | data,
|
|---|
| 329 | layout,
|
|---|
| 330 | isAnimationActive,
|
|---|
| 331 | animationBegin,
|
|---|
| 332 | animationDuration,
|
|---|
| 333 | animationEasing,
|
|---|
| 334 | onAnimationEnd,
|
|---|
| 335 | onAnimationStart
|
|---|
| 336 | } = props;
|
|---|
| 337 | var prevData = previousRectanglesRef.current;
|
|---|
| 338 | var animationId = useAnimationId(props, 'recharts-bar-');
|
|---|
| 339 | var [isAnimating, setIsAnimating] = useState(false);
|
|---|
| 340 | var showLabels = !isAnimating;
|
|---|
| 341 | var handleAnimationEnd = useCallback(() => {
|
|---|
| 342 | if (typeof onAnimationEnd === 'function') {
|
|---|
| 343 | onAnimationEnd();
|
|---|
| 344 | }
|
|---|
| 345 | setIsAnimating(false);
|
|---|
| 346 | }, [onAnimationEnd]);
|
|---|
| 347 | var handleAnimationStart = useCallback(() => {
|
|---|
| 348 | if (typeof onAnimationStart === 'function') {
|
|---|
| 349 | onAnimationStart();
|
|---|
| 350 | }
|
|---|
| 351 | setIsAnimating(true);
|
|---|
| 352 | }, [onAnimationStart]);
|
|---|
| 353 | return /*#__PURE__*/React.createElement(BarLabelListProvider, {
|
|---|
| 354 | showLabels: showLabels,
|
|---|
| 355 | rects: data
|
|---|
| 356 | }, /*#__PURE__*/React.createElement(JavascriptAnimate, {
|
|---|
| 357 | animationId: animationId,
|
|---|
| 358 | begin: animationBegin,
|
|---|
| 359 | duration: animationDuration,
|
|---|
| 360 | isActive: isAnimationActive,
|
|---|
| 361 | easing: animationEasing,
|
|---|
| 362 | onAnimationEnd: handleAnimationEnd,
|
|---|
| 363 | onAnimationStart: handleAnimationStart,
|
|---|
| 364 | key: animationId
|
|---|
| 365 | }, t => {
|
|---|
| 366 | var stepData = t === 1 ? data : data === null || data === void 0 ? void 0 : data.map((entry, index) => {
|
|---|
| 367 | var prev = prevData && prevData[index];
|
|---|
| 368 | if (prev) {
|
|---|
| 369 | return _objectSpread(_objectSpread({}, entry), {}, {
|
|---|
| 370 | x: interpolate(prev.x, entry.x, t),
|
|---|
| 371 | y: interpolate(prev.y, entry.y, t),
|
|---|
| 372 | width: interpolate(prev.width, entry.width, t),
|
|---|
| 373 | height: interpolate(prev.height, entry.height, t)
|
|---|
| 374 | });
|
|---|
| 375 | }
|
|---|
| 376 | if (layout === 'horizontal') {
|
|---|
| 377 | var height = interpolate(0, entry.height, t);
|
|---|
| 378 | var y = interpolate(entry.stackedBarStart, entry.y, t);
|
|---|
| 379 | return _objectSpread(_objectSpread({}, entry), {}, {
|
|---|
| 380 | y,
|
|---|
| 381 | height
|
|---|
| 382 | });
|
|---|
| 383 | }
|
|---|
| 384 | var w = interpolate(0, entry.width, t);
|
|---|
| 385 | var x = interpolate(entry.stackedBarStart, entry.x, t);
|
|---|
| 386 | return _objectSpread(_objectSpread({}, entry), {}, {
|
|---|
| 387 | width: w,
|
|---|
| 388 | x
|
|---|
| 389 | });
|
|---|
| 390 | });
|
|---|
| 391 | if (t > 0) {
|
|---|
| 392 | // eslint-disable-next-line no-param-reassign
|
|---|
| 393 | previousRectanglesRef.current = stepData !== null && stepData !== void 0 ? stepData : null;
|
|---|
| 394 | }
|
|---|
| 395 | if (stepData == null) {
|
|---|
| 396 | return null;
|
|---|
| 397 | }
|
|---|
| 398 | return /*#__PURE__*/React.createElement(Layer, null, /*#__PURE__*/React.createElement(BarRectangles, {
|
|---|
| 399 | props: props,
|
|---|
| 400 | data: stepData
|
|---|
| 401 | }));
|
|---|
| 402 | }), /*#__PURE__*/React.createElement(LabelListFromLabelProp, {
|
|---|
| 403 | label: props.label
|
|---|
| 404 | }), props.children);
|
|---|
| 405 | }
|
|---|
| 406 | function RenderRectangles(props) {
|
|---|
| 407 | var previousRectanglesRef = useRef(null);
|
|---|
| 408 | return /*#__PURE__*/React.createElement(RectanglesWithAnimation, {
|
|---|
| 409 | previousRectanglesRef: previousRectanglesRef,
|
|---|
| 410 | props: props
|
|---|
| 411 | });
|
|---|
| 412 | }
|
|---|
| 413 | var defaultMinPointSize = 0;
|
|---|
| 414 | var errorBarDataPointFormatter = (dataPoint, dataKey) => {
|
|---|
| 415 | /**
|
|---|
| 416 | * if the value coming from `selectBarRectangles` is an array then this is a stacked bar chart.
|
|---|
| 417 | * arr[1] represents end value of the bar since the data is in the form of [startValue, endValue].
|
|---|
| 418 | * */
|
|---|
| 419 | var value = Array.isArray(dataPoint.value) ? dataPoint.value[1] : dataPoint.value;
|
|---|
| 420 | return {
|
|---|
| 421 | x: dataPoint.x,
|
|---|
| 422 | y: dataPoint.y,
|
|---|
| 423 | value,
|
|---|
| 424 | // @ts-expect-error getValueByDataKey does not validate the output type
|
|---|
| 425 | errorVal: getValueByDataKey(dataPoint, dataKey)
|
|---|
| 426 | };
|
|---|
| 427 | };
|
|---|
| 428 | class BarWithState extends PureComponent {
|
|---|
| 429 | render() {
|
|---|
| 430 | var {
|
|---|
| 431 | hide,
|
|---|
| 432 | data,
|
|---|
| 433 | dataKey,
|
|---|
| 434 | className,
|
|---|
| 435 | xAxisId,
|
|---|
| 436 | yAxisId,
|
|---|
| 437 | needClip,
|
|---|
| 438 | background,
|
|---|
| 439 | id
|
|---|
| 440 | } = this.props;
|
|---|
| 441 | if (hide || data == null) {
|
|---|
| 442 | return null;
|
|---|
| 443 | }
|
|---|
| 444 | var layerClass = clsx('recharts-bar', className);
|
|---|
| 445 | var clipPathId = id;
|
|---|
| 446 | return /*#__PURE__*/React.createElement(Layer, {
|
|---|
| 447 | className: layerClass,
|
|---|
| 448 | id: id
|
|---|
| 449 | }, needClip && /*#__PURE__*/React.createElement("defs", null, /*#__PURE__*/React.createElement(GraphicalItemClipPath, {
|
|---|
| 450 | clipPathId: clipPathId,
|
|---|
| 451 | xAxisId: xAxisId,
|
|---|
| 452 | yAxisId: yAxisId
|
|---|
| 453 | })), /*#__PURE__*/React.createElement(Layer, {
|
|---|
| 454 | className: "recharts-bar-rectangles",
|
|---|
| 455 | clipPath: needClip ? "url(#clipPath-".concat(clipPathId, ")") : undefined
|
|---|
| 456 | }, /*#__PURE__*/React.createElement(BarBackground, {
|
|---|
| 457 | data: data,
|
|---|
| 458 | dataKey: dataKey,
|
|---|
| 459 | background: background,
|
|---|
| 460 | allOtherBarProps: this.props
|
|---|
| 461 | }), /*#__PURE__*/React.createElement(RenderRectangles, this.props)));
|
|---|
| 462 | }
|
|---|
| 463 | }
|
|---|
| 464 | export var defaultBarProps = {
|
|---|
| 465 | activeBar: false,
|
|---|
| 466 | animationBegin: 0,
|
|---|
| 467 | animationDuration: 400,
|
|---|
| 468 | animationEasing: 'ease',
|
|---|
| 469 | background: false,
|
|---|
| 470 | hide: false,
|
|---|
| 471 | isAnimationActive: 'auto',
|
|---|
| 472 | label: false,
|
|---|
| 473 | legendType: 'rect',
|
|---|
| 474 | minPointSize: defaultMinPointSize,
|
|---|
| 475 | xAxisId: 0,
|
|---|
| 476 | yAxisId: 0,
|
|---|
| 477 | zIndex: DefaultZIndexes.bar
|
|---|
| 478 | };
|
|---|
| 479 | function BarImpl(props) {
|
|---|
| 480 | var {
|
|---|
| 481 | xAxisId,
|
|---|
| 482 | yAxisId,
|
|---|
| 483 | hide,
|
|---|
| 484 | legendType,
|
|---|
| 485 | minPointSize,
|
|---|
| 486 | activeBar,
|
|---|
| 487 | animationBegin,
|
|---|
| 488 | animationDuration,
|
|---|
| 489 | animationEasing,
|
|---|
| 490 | isAnimationActive
|
|---|
| 491 | } = props;
|
|---|
| 492 | var {
|
|---|
| 493 | needClip
|
|---|
| 494 | } = useNeedsClip(xAxisId, yAxisId);
|
|---|
| 495 | var layout = useChartLayout();
|
|---|
| 496 | var isPanorama = useIsPanorama();
|
|---|
| 497 | var cells = findAllByType(props.children, Cell);
|
|---|
| 498 | var rects = useAppSelector(state => selectBarRectangles(state, props.id, isPanorama, cells));
|
|---|
| 499 | if (layout !== 'vertical' && layout !== 'horizontal') {
|
|---|
| 500 | return null;
|
|---|
| 501 | }
|
|---|
| 502 | var errorBarOffset;
|
|---|
| 503 | var firstDataPoint = rects === null || rects === void 0 ? void 0 : rects[0];
|
|---|
| 504 | if (firstDataPoint == null || firstDataPoint.height == null || firstDataPoint.width == null) {
|
|---|
| 505 | errorBarOffset = 0;
|
|---|
| 506 | } else {
|
|---|
| 507 | errorBarOffset = layout === 'vertical' ? firstDataPoint.height / 2 : firstDataPoint.width / 2;
|
|---|
| 508 | }
|
|---|
| 509 | return /*#__PURE__*/React.createElement(SetErrorBarContext, {
|
|---|
| 510 | xAxisId: xAxisId,
|
|---|
| 511 | yAxisId: yAxisId,
|
|---|
| 512 | data: rects,
|
|---|
| 513 | dataPointFormatter: errorBarDataPointFormatter,
|
|---|
| 514 | errorBarOffset: errorBarOffset
|
|---|
| 515 | }, /*#__PURE__*/React.createElement(BarWithState, _extends({}, props, {
|
|---|
| 516 | layout: layout,
|
|---|
| 517 | needClip: needClip,
|
|---|
| 518 | data: rects,
|
|---|
| 519 | xAxisId: xAxisId,
|
|---|
| 520 | yAxisId: yAxisId,
|
|---|
| 521 | hide: hide,
|
|---|
| 522 | legendType: legendType,
|
|---|
| 523 | minPointSize: minPointSize,
|
|---|
| 524 | activeBar: activeBar,
|
|---|
| 525 | animationBegin: animationBegin,
|
|---|
| 526 | animationDuration: animationDuration,
|
|---|
| 527 | animationEasing: animationEasing,
|
|---|
| 528 | isAnimationActive: isAnimationActive
|
|---|
| 529 | })));
|
|---|
| 530 | }
|
|---|
| 531 | export function computeBarRectangles(_ref6) {
|
|---|
| 532 | var {
|
|---|
| 533 | layout,
|
|---|
| 534 | barSettings: {
|
|---|
| 535 | dataKey,
|
|---|
| 536 | minPointSize: minPointSizeProp
|
|---|
| 537 | },
|
|---|
| 538 | pos,
|
|---|
| 539 | bandSize,
|
|---|
| 540 | xAxis,
|
|---|
| 541 | yAxis,
|
|---|
| 542 | xAxisTicks,
|
|---|
| 543 | yAxisTicks,
|
|---|
| 544 | stackedData,
|
|---|
| 545 | displayedData,
|
|---|
| 546 | offset,
|
|---|
| 547 | cells,
|
|---|
| 548 | parentViewBox,
|
|---|
| 549 | dataStartIndex
|
|---|
| 550 | } = _ref6;
|
|---|
| 551 | var numericAxis = layout === 'horizontal' ? yAxis : xAxis;
|
|---|
| 552 | // @ts-expect-error this assumes that the domain is always numeric, but doesn't check for it
|
|---|
| 553 | var stackedDomain = stackedData ? numericAxis.scale.domain() : null;
|
|---|
| 554 | var baseValue = getBaseValueOfBar({
|
|---|
| 555 | numericAxis
|
|---|
| 556 | });
|
|---|
| 557 | var stackedBarStart = numericAxis.scale.map(baseValue);
|
|---|
| 558 | return displayedData.map((entry, index) => {
|
|---|
| 559 | var value, x, y, width, height, background;
|
|---|
| 560 | if (stackedData) {
|
|---|
| 561 | // Use dataStartIndex to access the correct element in the full stackedData array
|
|---|
| 562 | var untruncatedValue = stackedData[index + dataStartIndex];
|
|---|
| 563 | if (untruncatedValue == null) {
|
|---|
| 564 | return null;
|
|---|
| 565 | }
|
|---|
| 566 | value = truncateByDomain(untruncatedValue, stackedDomain);
|
|---|
| 567 | } else {
|
|---|
| 568 | value = getValueByDataKey(entry, dataKey);
|
|---|
| 569 | if (!Array.isArray(value)) {
|
|---|
| 570 | value = [baseValue, value];
|
|---|
| 571 | }
|
|---|
| 572 | }
|
|---|
| 573 | var minPointSize = minPointSizeCallback(minPointSizeProp, defaultMinPointSize)(value[1], index);
|
|---|
| 574 | if (layout === 'horizontal') {
|
|---|
| 575 | var _ref7;
|
|---|
| 576 | var baseValueScale = yAxis.scale.map(value[0]);
|
|---|
| 577 | var currentValueScale = yAxis.scale.map(value[1]);
|
|---|
| 578 | if (baseValueScale == null || currentValueScale == null) {
|
|---|
| 579 | return null;
|
|---|
| 580 | }
|
|---|
| 581 | x = getCateCoordinateOfBar({
|
|---|
| 582 | axis: xAxis,
|
|---|
| 583 | ticks: xAxisTicks,
|
|---|
| 584 | bandSize,
|
|---|
| 585 | offset: pos.offset,
|
|---|
| 586 | entry,
|
|---|
| 587 | index
|
|---|
| 588 | });
|
|---|
| 589 | y = (_ref7 = currentValueScale !== null && currentValueScale !== void 0 ? currentValueScale : baseValueScale) !== null && _ref7 !== void 0 ? _ref7 : undefined;
|
|---|
| 590 | width = pos.size;
|
|---|
| 591 | var computedHeight = baseValueScale - currentValueScale;
|
|---|
| 592 | height = isNan(computedHeight) ? 0 : computedHeight;
|
|---|
| 593 | background = {
|
|---|
| 594 | x,
|
|---|
| 595 | y: offset.top,
|
|---|
| 596 | width,
|
|---|
| 597 | height: offset.height
|
|---|
| 598 | };
|
|---|
| 599 | if (Math.abs(minPointSize) > 0 && Math.abs(height) < Math.abs(minPointSize)) {
|
|---|
| 600 | var delta = mathSign(height || minPointSize) * (Math.abs(minPointSize) - Math.abs(height));
|
|---|
| 601 | y -= delta;
|
|---|
| 602 | height += delta;
|
|---|
| 603 | }
|
|---|
| 604 | } else {
|
|---|
| 605 | var _baseValueScale = xAxis.scale.map(value[0]);
|
|---|
| 606 | var _currentValueScale = xAxis.scale.map(value[1]);
|
|---|
| 607 | if (_baseValueScale == null || _currentValueScale == null) {
|
|---|
| 608 | return null;
|
|---|
| 609 | }
|
|---|
| 610 | x = _baseValueScale;
|
|---|
| 611 | y = getCateCoordinateOfBar({
|
|---|
| 612 | axis: yAxis,
|
|---|
| 613 | ticks: yAxisTicks,
|
|---|
| 614 | bandSize,
|
|---|
| 615 | offset: pos.offset,
|
|---|
| 616 | entry,
|
|---|
| 617 | index
|
|---|
| 618 | });
|
|---|
| 619 | width = _currentValueScale - _baseValueScale;
|
|---|
| 620 | height = pos.size;
|
|---|
| 621 | background = {
|
|---|
| 622 | x: offset.left,
|
|---|
| 623 | y,
|
|---|
| 624 | width: offset.width,
|
|---|
| 625 | height
|
|---|
| 626 | };
|
|---|
| 627 | if (Math.abs(minPointSize) > 0 && Math.abs(width) < Math.abs(minPointSize)) {
|
|---|
| 628 | var _delta = mathSign(width || minPointSize) * (Math.abs(minPointSize) - Math.abs(width));
|
|---|
| 629 | width += _delta;
|
|---|
| 630 | }
|
|---|
| 631 | }
|
|---|
| 632 | if (x == null || y == null || width == null || height == null) {
|
|---|
| 633 | return null;
|
|---|
| 634 | }
|
|---|
| 635 | var barRectangleItem = _objectSpread(_objectSpread({}, entry), {}, {
|
|---|
| 636 | stackedBarStart,
|
|---|
| 637 | x,
|
|---|
| 638 | y,
|
|---|
| 639 | width,
|
|---|
| 640 | height,
|
|---|
| 641 | value: stackedData ? value : value[1],
|
|---|
| 642 | payload: entry,
|
|---|
| 643 | background,
|
|---|
| 644 | tooltipPosition: {
|
|---|
| 645 | x: x + width / 2,
|
|---|
| 646 | y: y + height / 2
|
|---|
| 647 | },
|
|---|
| 648 | parentViewBox
|
|---|
| 649 | }, cells && cells[index] && cells[index].props);
|
|---|
| 650 | return barRectangleItem;
|
|---|
| 651 | }).filter(Boolean);
|
|---|
| 652 | }
|
|---|
| 653 | function BarFn(outsideProps) {
|
|---|
| 654 | var props = resolveDefaultProps(outsideProps, defaultBarProps);
|
|---|
| 655 | // stackId may arrive from props or from BarStack context
|
|---|
| 656 | var stackId = useStackId(props.stackId);
|
|---|
| 657 | var isPanorama = useIsPanorama();
|
|---|
| 658 | // Report all props to Redux store first, before calling any hooks, to avoid circular dependencies.
|
|---|
| 659 | return /*#__PURE__*/React.createElement(RegisterGraphicalItemId, {
|
|---|
| 660 | id: props.id,
|
|---|
| 661 | type: "bar"
|
|---|
| 662 | }, id => /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(SetLegendPayload, {
|
|---|
| 663 | legendPayload: computeLegendPayloadFromBarData(props)
|
|---|
| 664 | }), /*#__PURE__*/React.createElement(SetBarTooltipEntrySettings, {
|
|---|
| 665 | dataKey: props.dataKey,
|
|---|
| 666 | stroke: props.stroke,
|
|---|
| 667 | strokeWidth: props.strokeWidth,
|
|---|
| 668 | fill: props.fill,
|
|---|
| 669 | name: props.name,
|
|---|
| 670 | hide: props.hide,
|
|---|
| 671 | unit: props.unit,
|
|---|
| 672 | tooltipType: props.tooltipType,
|
|---|
| 673 | id: id
|
|---|
| 674 | }), /*#__PURE__*/React.createElement(SetCartesianGraphicalItem, {
|
|---|
| 675 | type: "bar",
|
|---|
| 676 | id: id
|
|---|
| 677 | // Bar does not allow setting data directly on the graphical item (why?)
|
|---|
| 678 | ,
|
|---|
| 679 | data: undefined,
|
|---|
| 680 | xAxisId: props.xAxisId,
|
|---|
| 681 | yAxisId: props.yAxisId,
|
|---|
| 682 | zAxisId: 0,
|
|---|
| 683 | dataKey: props.dataKey,
|
|---|
| 684 | stackId: stackId,
|
|---|
| 685 | hide: props.hide,
|
|---|
| 686 | barSize: props.barSize,
|
|---|
| 687 | minPointSize: props.minPointSize,
|
|---|
| 688 | maxBarSize: props.maxBarSize,
|
|---|
| 689 | isPanorama: isPanorama
|
|---|
| 690 | }), /*#__PURE__*/React.createElement(ZIndexLayer, {
|
|---|
| 691 | zIndex: props.zIndex
|
|---|
| 692 | }, /*#__PURE__*/React.createElement(BarImpl, _extends({}, props, {
|
|---|
| 693 | id: id
|
|---|
| 694 | })))));
|
|---|
| 695 | }
|
|---|
| 696 |
|
|---|
| 697 | /**
|
|---|
| 698 | * @provides ErrorBarContext
|
|---|
| 699 | * @provides LabelListContext
|
|---|
| 700 | * @provides CellReader
|
|---|
| 701 | * @consumes CartesianChartContext
|
|---|
| 702 | * @consumes BarStackContext
|
|---|
| 703 | */
|
|---|
| 704 | export var Bar = /*#__PURE__*/React.memo(BarFn, propsAreEqual);
|
|---|
| 705 | Bar.displayName = 'Bar'; |
|---|