@coinbase/cds-web-visualization 3.3.0 → 3.4.0-beta.2
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/CHANGELOG.md +30 -0
- package/dts/chart/CartesianChart.d.ts +36 -0
- package/dts/chart/CartesianChart.d.ts.map +1 -0
- package/dts/chart/ChartProvider.d.ts +6 -0
- package/dts/chart/ChartProvider.d.ts.map +1 -0
- package/dts/chart/Path.d.ts +34 -0
- package/dts/chart/Path.d.ts.map +1 -0
- package/dts/chart/PeriodSelector.d.ts +61 -0
- package/dts/chart/PeriodSelector.d.ts.map +1 -0
- package/dts/chart/Point.d.ts +153 -0
- package/dts/chart/Point.d.ts.map +1 -0
- package/dts/chart/area/Area.d.ts +48 -0
- package/dts/chart/area/Area.d.ts.map +1 -0
- package/dts/chart/area/AreaChart.d.ts +52 -0
- package/dts/chart/area/AreaChart.d.ts.map +1 -0
- package/dts/chart/area/DottedArea.d.ts +68 -0
- package/dts/chart/area/DottedArea.d.ts.map +1 -0
- package/dts/chart/area/GradientArea.d.ts +30 -0
- package/dts/chart/area/GradientArea.d.ts.map +1 -0
- package/dts/chart/area/SolidArea.d.ts +8 -0
- package/dts/chart/area/SolidArea.d.ts.map +1 -0
- package/dts/chart/area/index.d.ts +6 -0
- package/dts/chart/area/index.d.ts.map +1 -0
- package/dts/chart/axis/Axis.d.ts +208 -0
- package/dts/chart/axis/Axis.d.ts.map +1 -0
- package/dts/chart/axis/XAxis.d.ts +16 -0
- package/dts/chart/axis/XAxis.d.ts.map +1 -0
- package/dts/chart/axis/YAxis.d.ts +21 -0
- package/dts/chart/axis/YAxis.d.ts.map +1 -0
- package/dts/chart/axis/index.d.ts +4 -0
- package/dts/chart/axis/index.d.ts.map +1 -0
- package/dts/chart/bar/Bar.d.ts +91 -0
- package/dts/chart/bar/Bar.d.ts.map +1 -0
- package/dts/chart/bar/BarChart.d.ts +53 -0
- package/dts/chart/bar/BarChart.d.ts.map +1 -0
- package/dts/chart/bar/BarPlot.d.ts +29 -0
- package/dts/chart/bar/BarPlot.d.ts.map +1 -0
- package/dts/chart/bar/BarStack.d.ts +111 -0
- package/dts/chart/bar/BarStack.d.ts.map +1 -0
- package/dts/chart/bar/BarStackGroup.d.ts +35 -0
- package/dts/chart/bar/BarStackGroup.d.ts.map +1 -0
- package/dts/chart/bar/DefaultBar.d.ts +17 -0
- package/dts/chart/bar/DefaultBar.d.ts.map +1 -0
- package/dts/chart/bar/DefaultBarStack.d.ts +16 -0
- package/dts/chart/bar/DefaultBarStack.d.ts.map +1 -0
- package/dts/chart/bar/index.d.ts +8 -0
- package/dts/chart/bar/index.d.ts.map +1 -0
- package/dts/chart/index.d.ts +13 -0
- package/dts/chart/index.d.ts.map +1 -0
- package/dts/chart/line/DottedLine.d.ts +14 -0
- package/dts/chart/line/DottedLine.d.ts.map +1 -0
- package/dts/chart/line/GradientLine.d.ts +42 -0
- package/dts/chart/line/GradientLine.d.ts.map +1 -0
- package/dts/chart/line/Line.d.ts +80 -0
- package/dts/chart/line/Line.d.ts.map +1 -0
- package/dts/chart/line/LineChart.d.ts +59 -0
- package/dts/chart/line/LineChart.d.ts.map +1 -0
- package/dts/chart/line/ReferenceLine.d.ts +131 -0
- package/dts/chart/line/ReferenceLine.d.ts.map +1 -0
- package/dts/chart/line/SolidLine.d.ts +14 -0
- package/dts/chart/line/SolidLine.d.ts.map +1 -0
- package/dts/chart/line/index.d.ts +7 -0
- package/dts/chart/line/index.d.ts.map +1 -0
- package/dts/chart/scrubber/Scrubber.d.ts +149 -0
- package/dts/chart/scrubber/Scrubber.d.ts.map +1 -0
- package/dts/chart/scrubber/ScrubberBeacon.d.ts +93 -0
- package/dts/chart/scrubber/ScrubberBeacon.d.ts.map +1 -0
- package/dts/chart/scrubber/ScrubberBeaconLabel.d.ts +7 -0
- package/dts/chart/scrubber/ScrubberBeaconLabel.d.ts.map +1 -0
- package/dts/chart/scrubber/ScrubberProvider.d.ts +17 -0
- package/dts/chart/scrubber/ScrubberProvider.d.ts.map +1 -0
- package/dts/chart/scrubber/index.d.ts +2 -0
- package/dts/chart/scrubber/index.d.ts.map +1 -0
- package/dts/chart/text/ChartText.d.ts +114 -0
- package/dts/chart/text/ChartText.d.ts.map +1 -0
- package/dts/chart/text/SmartChartTextGroup.d.ts +55 -0
- package/dts/chart/text/SmartChartTextGroup.d.ts.map +1 -0
- package/dts/chart/text/index.d.ts +3 -0
- package/dts/chart/text/index.d.ts.map +1 -0
- package/dts/chart/utils/axis.d.ts +342 -0
- package/dts/chart/utils/axis.d.ts.map +1 -0
- package/dts/chart/utils/bar.d.ts +20 -0
- package/dts/chart/utils/bar.d.ts.map +1 -0
- package/dts/chart/utils/chart.d.ts +97 -0
- package/dts/chart/utils/chart.d.ts.map +1 -0
- package/dts/chart/utils/context.d.ts +95 -0
- package/dts/chart/utils/context.d.ts.map +1 -0
- package/dts/chart/utils/index.d.ts +8 -0
- package/dts/chart/utils/index.d.ts.map +1 -0
- package/dts/chart/utils/path.d.ts +107 -0
- package/dts/chart/utils/path.d.ts.map +1 -0
- package/dts/chart/utils/point.d.ts +75 -0
- package/dts/chart/utils/point.d.ts.map +1 -0
- package/dts/chart/utils/scale.d.ts +43 -0
- package/dts/chart/utils/scale.d.ts.map +1 -0
- package/dts/index.d.ts +2 -1
- package/dts/index.d.ts.map +1 -1
- package/dts/sparkline/Counter.d.ts +7 -2
- package/dts/sparkline/Sparkline.d.ts +61 -16
- package/dts/sparkline/Sparkline.d.ts.map +1 -1
- package/dts/sparkline/SparklineArea.d.ts +12 -4
- package/dts/sparkline/SparklineArea.d.ts.map +1 -1
- package/dts/sparkline/SparklineAreaPattern.d.ts +12 -4
- package/dts/sparkline/SparklineAreaPattern.d.ts.map +1 -1
- package/dts/sparkline/SparklineGradient.d.ts +21 -10
- package/dts/sparkline/SparklineGradient.d.ts.map +1 -1
- package/dts/sparkline/SparklinePath.d.ts +8 -6
- package/dts/sparkline/__figma__/Sparkline.figma.d.ts +1 -1
- package/dts/sparkline/generateSparklineWithId.d.ts +8 -2
- package/dts/sparkline/generateSparklineWithId.d.ts.map +1 -1
- package/dts/sparkline/index.d.ts +1 -1
- package/dts/sparkline/sparkline-interactive/InnerSparklineInteractiveProvider.d.ts +9 -5
- package/dts/sparkline/sparkline-interactive/SparklineInteractive.d.ts +168 -118
- package/dts/sparkline/sparkline-interactive/SparklineInteractive.d.ts.map +1 -1
- package/dts/sparkline/sparkline-interactive/SparklineInteractiveAnimatedPath.d.ts +22 -9
- package/dts/sparkline/sparkline-interactive/SparklineInteractiveAnimatedPath.d.ts.map +1 -1
- package/dts/sparkline/sparkline-interactive/SparklineInteractiveHoverDate.d.ts +4 -2
- package/dts/sparkline/sparkline-interactive/SparklineInteractiveHoverPrice.d.ts +4 -2
- package/dts/sparkline/sparkline-interactive/SparklineInteractiveLineVertical.d.ts +5 -3
- package/dts/sparkline/sparkline-interactive/SparklineInteractiveMarkerDates.d.ts +11 -6
- package/dts/sparkline/sparkline-interactive/SparklineInteractivePaths.d.ts +21 -7
- package/dts/sparkline/sparkline-interactive/SparklineInteractivePaths.d.ts.map +1 -1
- package/dts/sparkline/sparkline-interactive/SparklineInteractivePeriodSelector.d.ts +21 -16
- package/dts/sparkline/sparkline-interactive/SparklineInteractiveProvider.d.ts +17 -12
- package/dts/sparkline/sparkline-interactive/SparklineInteractiveScrubHandler.d.ts +23 -10
- package/dts/sparkline/sparkline-interactive/SparklineInteractiveScrubProvider.d.ts +12 -12
- package/dts/sparkline/sparkline-interactive/SparklineInteractiveTimeseriesPaths.d.ts +22 -14
- package/dts/sparkline/sparkline-interactive/__figma__/SparklineInteractive.figma.d.ts +1 -1
- package/dts/sparkline/sparkline-interactive/fade.d.ts +1 -1
- package/dts/sparkline/sparkline-interactive/useSparklineInteractiveConstants.d.ts +11 -11
- package/dts/sparkline/sparkline-interactive-header/SparklineInteractiveHeader.d.ts +101 -93
- package/dts/sparkline/sparkline-interactive-header/__figma__/SparklineInteractiveHeader.figma.d.ts +1 -1
- package/esm/chart/CartesianChart.css +1 -0
- package/esm/chart/CartesianChart.js +258 -0
- package/esm/chart/ChartProvider.js +10 -0
- package/esm/chart/Path.js +89 -0
- package/esm/chart/PeriodSelector.css +1 -0
- package/esm/chart/PeriodSelector.js +126 -0
- package/esm/chart/Point.css +2 -0
- package/esm/chart/Point.js +171 -0
- package/esm/chart/area/Area.js +85 -0
- package/esm/chart/area/AreaChart.js +164 -0
- package/esm/chart/area/DottedArea.js +141 -0
- package/esm/chart/area/GradientArea.js +111 -0
- package/esm/chart/area/SolidArea.js +29 -0
- package/esm/chart/area/index.js +7 -0
- package/esm/chart/axis/Axis.js +46 -0
- package/esm/chart/axis/XAxis.css +2 -0
- package/esm/chart/axis/XAxis.js +195 -0
- package/esm/chart/axis/YAxis.css +2 -0
- package/esm/chart/axis/YAxis.js +183 -0
- package/esm/chart/axis/index.js +5 -0
- package/esm/chart/bar/Bar.js +59 -0
- package/esm/chart/bar/BarChart.js +147 -0
- package/esm/chart/bar/BarPlot.js +96 -0
- package/esm/chart/bar/BarStack.js +519 -0
- package/esm/chart/bar/BarStackGroup.js +96 -0
- package/esm/chart/bar/DefaultBar.js +64 -0
- package/esm/chart/bar/DefaultBarStack.js +60 -0
- package/esm/chart/bar/index.js +9 -0
- package/esm/chart/index.js +14 -0
- package/esm/chart/line/DottedLine.js +38 -0
- package/esm/chart/line/GradientLine.js +58 -0
- package/esm/chart/line/Line.js +159 -0
- package/esm/chart/line/LineChart.js +120 -0
- package/esm/chart/line/ReferenceLine.js +142 -0
- package/esm/chart/line/SolidLine.js +34 -0
- package/esm/chart/line/index.js +8 -0
- package/esm/chart/scrubber/Scrubber.js +483 -0
- package/esm/chart/scrubber/ScrubberBeacon.js +195 -0
- package/esm/chart/scrubber/ScrubberBeaconLabel.js +33 -0
- package/esm/chart/scrubber/ScrubberProvider.js +228 -0
- package/esm/chart/scrubber/index.js +2 -0
- package/esm/chart/text/ChartText.js +236 -0
- package/esm/chart/text/SmartChartTextGroup.js +226 -0
- package/esm/chart/text/index.js +4 -0
- package/esm/chart/utils/axis.js +593 -0
- package/esm/chart/utils/bar.js +24 -0
- package/esm/chart/utils/chart.js +229 -0
- package/esm/chart/utils/context.js +15 -0
- package/esm/chart/utils/index.js +9 -0
- package/esm/chart/utils/path.js +204 -0
- package/esm/chart/utils/point.js +118 -0
- package/esm/chart/utils/scale.js +48 -0
- package/esm/index.js +4 -1
- package/esm/sparkline/Sparkline.js +129 -15
- package/esm/sparkline/SparklineArea.js +7 -2
- package/esm/sparkline/SparklineAreaPattern.js +4 -2
- package/esm/sparkline/SparklineGradient.js +16 -58
- package/esm/sparkline/generateSparklineWithId.js +3 -2
- package/esm/sparkline/sparkline-interactive/SparklineInteractive.js +5 -1
- package/esm/sparkline/sparkline-interactive/SparklineInteractiveAnimatedPath.js +5 -2
- package/esm/sparkline/sparkline-interactive/SparklineInteractivePaths.js +4 -0
- package/package.json +15 -11
- package/dts/sparkline/sparkline-interactive/__stories__/SparklineInteractive.stories.d.ts +0 -374
- package/dts/sparkline/sparkline-interactive/__stories__/SparklineInteractive.stories.d.ts.map +0 -1
- package/dts/sparkline/sparkline-interactive/__tests__/SparklineInteractive.test.d.ts +0 -2
- package/dts/sparkline/sparkline-interactive/__tests__/SparklineInteractive.test.d.ts.map +0 -1
- package/dts/sparkline/sparkline-interactive/__tests__/SparklineInteractivePeriodSelector.test.d.ts +0 -2
- package/dts/sparkline/sparkline-interactive/__tests__/SparklineInteractivePeriodSelector.test.d.ts.map +0 -1
- package/dts/sparkline/sparkline-interactive-header/__stories__/SparklineInteractiveHeader.stories.d.ts +0 -87
- package/dts/sparkline/sparkline-interactive-header/__stories__/SparklineInteractiveHeader.stories.d.ts.map +0 -1
- package/dts/sparkline/sparkline-interactive-header/__tests__/SparklineInteractiveHeader.test.d.ts +0 -2
- package/dts/sparkline/sparkline-interactive-header/__tests__/SparklineInteractiveHeader.test.d.ts.map +0 -1
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
const _excluded = ["onDimensionsChange"];
|
|
2
|
+
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; }
|
|
3
|
+
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; }
|
|
4
|
+
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; }
|
|
5
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
6
|
+
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); }
|
|
7
|
+
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; }
|
|
8
|
+
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; }
|
|
9
|
+
import { memo, useEffect, useMemo, useState } from 'react';
|
|
10
|
+
import { ChartText } from './ChartText';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Configuration for a single text label in the display list
|
|
14
|
+
*/
|
|
15
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
16
|
+
/**
|
|
17
|
+
* Overlap check that enforces a minimum pixel gap between two rectangles.
|
|
18
|
+
* We inflate each rect by gap/2 on all sides so two neighbors must be at
|
|
19
|
+
* least `gap` pixels apart to be considered non-overlapping.
|
|
20
|
+
*/
|
|
21
|
+
function doRectsOverlapWithGap(a, b, gap) {
|
|
22
|
+
const g = gap / 2;
|
|
23
|
+
const overlapX = a.x - g < b.x + b.width + g && a.x + a.width + g > b.x - g;
|
|
24
|
+
const overlapY = a.y - g < b.y + b.height + g && a.y + a.height + g > b.y - g;
|
|
25
|
+
return overlapX && overlapY;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Suppress state churn due to sub-pixel jitter in measurements
|
|
29
|
+
const EPSILON_PX = 0.5;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A smart text display component that prevents label overlap through collision detection.
|
|
33
|
+
*
|
|
34
|
+
* This component renders a list of ChartText components and automatically hides overlapping elements
|
|
35
|
+
* to ensure readability.
|
|
36
|
+
*
|
|
37
|
+
* The component focuses solely on overlap prevention logic for better separation of concerns.
|
|
38
|
+
*/
|
|
39
|
+
export const SmartChartTextGroup = /*#__PURE__*/memo(_ref => {
|
|
40
|
+
let {
|
|
41
|
+
labels,
|
|
42
|
+
minGap = 8,
|
|
43
|
+
prioritizeEndLabels = true,
|
|
44
|
+
chartTextProps
|
|
45
|
+
} = _ref;
|
|
46
|
+
const [boundingBoxes, setBoundingBoxes] = useState(new Map());
|
|
47
|
+
const _ref2 = chartTextProps !== null && chartTextProps !== void 0 ? chartTextProps : {},
|
|
48
|
+
{
|
|
49
|
+
onDimensionsChange: propsOnDimensionsChange
|
|
50
|
+
} = _ref2,
|
|
51
|
+
restChartTextProps = _objectWithoutProperties(_ref2, _excluded);
|
|
52
|
+
|
|
53
|
+
// Generate a unique key to reference each label with.
|
|
54
|
+
const labelsWithKeys = useMemo(() => {
|
|
55
|
+
return labels.map((labelData, index) => _objectSpread(_objectSpread({}, labelData), {}, {
|
|
56
|
+
key: "".concat(labelData.label, "-").concat(index)
|
|
57
|
+
}));
|
|
58
|
+
}, [labels]);
|
|
59
|
+
|
|
60
|
+
// Cleans up `boundingBoxes` state so that it only includes entries for the current set of labels
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
const allLabelsKeys = new Set(labelsWithKeys.map(l => l.key));
|
|
63
|
+
setBoundingBoxes(prev => {
|
|
64
|
+
let changed = false;
|
|
65
|
+
const next = new Map();
|
|
66
|
+
for (const [k, v] of prev) {
|
|
67
|
+
if (allLabelsKeys.has(k)) next.set(k, v);else changed = true;
|
|
68
|
+
}
|
|
69
|
+
return changed ? next : prev;
|
|
70
|
+
});
|
|
71
|
+
}, [labelsWithKeys]);
|
|
72
|
+
|
|
73
|
+
// Build stable per-label measurement callbacks that recreate when labels change
|
|
74
|
+
const onDimensionsChangeByKey = useMemo(() => {
|
|
75
|
+
const map = new Map();
|
|
76
|
+
for (const labelData of labelsWithKeys) {
|
|
77
|
+
const {
|
|
78
|
+
key,
|
|
79
|
+
chartTextProps: labelChartTextProps
|
|
80
|
+
} = labelData;
|
|
81
|
+
map.set(key, bounds => {
|
|
82
|
+
var _labelChartTextProps$;
|
|
83
|
+
labelChartTextProps === null || labelChartTextProps === void 0 || (_labelChartTextProps$ = labelChartTextProps.onDimensionsChange) === null || _labelChartTextProps$ === void 0 || _labelChartTextProps$.call(labelChartTextProps, bounds);
|
|
84
|
+
propsOnDimensionsChange === null || propsOnDimensionsChange === void 0 || propsOnDimensionsChange(bounds);
|
|
85
|
+
// Ignore zero-sized bounds and no-op updates (epsilon compare)
|
|
86
|
+
if (bounds.width === 0 || bounds.height === 0) return;
|
|
87
|
+
setBoundingBoxes(prev => {
|
|
88
|
+
const prevRect = prev.get(key);
|
|
89
|
+
const nearlyEqual = (a, b) => Math.abs(a - b) <= EPSILON_PX;
|
|
90
|
+
const isSame = prevRect !== undefined && nearlyEqual(prevRect.x, bounds.x) && nearlyEqual(prevRect.y, bounds.y) && nearlyEqual(prevRect.width, bounds.width) && nearlyEqual(prevRect.height, bounds.height);
|
|
91
|
+
if (isSame) return prev;
|
|
92
|
+
const newMap = new Map(prev);
|
|
93
|
+
newMap.set(key, bounds);
|
|
94
|
+
return newMap;
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
return map;
|
|
99
|
+
}, [labelsWithKeys, propsOnDimensionsChange]);
|
|
100
|
+
|
|
101
|
+
// Determine readiness: all current labels have measured bounding boxes
|
|
102
|
+
const isReady = useMemo(() => labelsWithKeys.every(l => boundingBoxes.has(l.key)), [labelsWithKeys, boundingBoxes]);
|
|
103
|
+
|
|
104
|
+
// Compute visible keys using stride attempts then greedy fallback
|
|
105
|
+
const visibleKeySet = useMemo(() => {
|
|
106
|
+
// Build ordered set of labels with rects for collision detection algorithm
|
|
107
|
+
const orderedWithRects = labelsWithKeys.map((l, idx) => _objectSpread(_objectSpread({}, l), {}, {
|
|
108
|
+
rect: boundingBoxes.get(l.key)
|
|
109
|
+
})).filter(x => x.rect !== undefined);
|
|
110
|
+
|
|
111
|
+
// 1) Sort by horizontal position so neighbor checks are O(1)
|
|
112
|
+
// For ties, sort bottom-to-top (higher y first) to get stable ordering
|
|
113
|
+
orderedWithRects.sort((a, b) => a.x === b.x ? b.y - a.y : a.x - b.x);
|
|
114
|
+
|
|
115
|
+
// 2) Defer selection until all labels have measured to avoid flicker and early hiding
|
|
116
|
+
if (!isReady) return null;
|
|
117
|
+
const n = orderedWithRects.length;
|
|
118
|
+
// 3) Trivial cases
|
|
119
|
+
if (n === 0) return new Set();
|
|
120
|
+
if (n === 1) return new Set([orderedWithRects[0].key]);
|
|
121
|
+
|
|
122
|
+
// 4) Two-label rule: if overlapping, prefer the first label (original order)
|
|
123
|
+
if (n === 2) {
|
|
124
|
+
const a = orderedWithRects[0];
|
|
125
|
+
const b = orderedWithRects[1];
|
|
126
|
+
const overlap = doRectsOverlapWithGap(a.rect, b.rect, minGap);
|
|
127
|
+
if (overlap) {
|
|
128
|
+
var _labelsWithKeys$;
|
|
129
|
+
const firstOriginal = (_labelsWithKeys$ = labelsWithKeys[0]) === null || _labelsWithKeys$ === void 0 ? void 0 : _labelsWithKeys$.key;
|
|
130
|
+
return new Set([firstOriginal !== null && firstOriginal !== void 0 ? firstOriginal : a.key]);
|
|
131
|
+
}
|
|
132
|
+
return new Set([a.key, b.key]);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// 5) Utility: check only adjacent neighbors in x-order for overlap with gap
|
|
136
|
+
const hasNeighborOverlap = keysOrdered => {
|
|
137
|
+
for (let i = 0; i < keysOrdered.length - 1; i++) {
|
|
138
|
+
const ra = boundingBoxes.get(keysOrdered[i]);
|
|
139
|
+
const rb = boundingBoxes.get(keysOrdered[i + 1]);
|
|
140
|
+
if (doRectsOverlapWithGap(ra, rb, minGap)) return true;
|
|
141
|
+
}
|
|
142
|
+
return false;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// 6) Fast path: if every label fits, show them all without reduction
|
|
146
|
+
const allKeys = orderedWithRects.map(l => l.key);
|
|
147
|
+
if (!hasNeighborOverlap(allKeys)) {
|
|
148
|
+
return new Set(allKeys);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 7) Try stride patterns: every 2nd, every 3rd, ... while ensuring ends when prioritized
|
|
152
|
+
const tryStride = stride => {
|
|
153
|
+
const selected = [];
|
|
154
|
+
for (let i = 0; i < n; i += stride) selected.push(orderedWithRects[i].key);
|
|
155
|
+
if (prioritizeEndLabels) {
|
|
156
|
+
const firstKey = orderedWithRects[0].key;
|
|
157
|
+
const lastKey = orderedWithRects[n - 1].key;
|
|
158
|
+
if (selected[0] !== firstKey) selected.unshift(firstKey);
|
|
159
|
+
if (selected[selected.length - 1] !== lastKey) selected.push(lastKey);
|
|
160
|
+
}
|
|
161
|
+
// Deduplicate while preserving order
|
|
162
|
+
const unique = Array.from(new Set(selected));
|
|
163
|
+
return hasNeighborOverlap(unique) ? new Set() : new Set(unique);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
// 8) Increase stride until something fits or we exhaust options
|
|
167
|
+
for (let stride = 2; stride <= n; stride++) {
|
|
168
|
+
const attempt = tryStride(stride);
|
|
169
|
+
if (attempt.size > 0) return attempt;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// 9) Greedy fallback: walk left-to-right and keep a label only if it
|
|
173
|
+
// does not overlap the previously accepted label. Optionally ensure last.
|
|
174
|
+
const greedy = [];
|
|
175
|
+
const firstKey = orderedWithRects[0].key;
|
|
176
|
+
const lastKey = orderedWithRects[n - 1].key;
|
|
177
|
+
greedy.push(firstKey);
|
|
178
|
+
for (let i = 1; i < n - 1; i++) {
|
|
179
|
+
const k = orderedWithRects[i].key;
|
|
180
|
+
const prevKey = greedy[greedy.length - 1];
|
|
181
|
+
const ra = boundingBoxes.get(prevKey);
|
|
182
|
+
const rb = boundingBoxes.get(k);
|
|
183
|
+
if (!doRectsOverlapWithGap(ra, rb, minGap)) {
|
|
184
|
+
greedy.push(k);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
// Ensure last key when prioritized
|
|
188
|
+
if (prioritizeEndLabels) {
|
|
189
|
+
const lastIncluded = greedy[greedy.length - 1];
|
|
190
|
+
const ra = boundingBoxes.get(lastIncluded);
|
|
191
|
+
const rb = boundingBoxes.get(lastKey);
|
|
192
|
+
if (doRectsOverlapWithGap(ra, rb, minGap)) {
|
|
193
|
+
// Replace the last conflicting with the lastKey
|
|
194
|
+
greedy[greedy.length - 1] = lastKey;
|
|
195
|
+
} else if (lastIncluded !== lastKey) {
|
|
196
|
+
greedy.push(lastKey);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return new Set(greedy);
|
|
200
|
+
}, [isReady, boundingBoxes, minGap, prioritizeEndLabels, labelsWithKeys]);
|
|
201
|
+
return /*#__PURE__*/_jsx("g", {
|
|
202
|
+
children: labelsWithKeys.map(labelData => {
|
|
203
|
+
var _restChartTextProps$s, _restChartTextProps$s2, _labelData$chartTextP, _labelData$chartTextP2, _restChartTextProps$s3, _restChartTextProps$s4, _labelData$chartTextP3, _labelData$chartTextP4;
|
|
204
|
+
const hasMeasurement = boundingBoxes.has(labelData.key);
|
|
205
|
+
const isVisible = hasMeasurement && isReady && (visibleKeySet === null || visibleKeySet === void 0 ? void 0 : visibleKeySet.has(labelData.key));
|
|
206
|
+
const mergedStyles = {
|
|
207
|
+
text: _objectSpread(_objectSpread(_objectSpread({}, (_restChartTextProps$s = restChartTextProps === null || restChartTextProps === void 0 || (_restChartTextProps$s2 = restChartTextProps.styles) === null || _restChartTextProps$s2 === void 0 ? void 0 : _restChartTextProps$s2.text) !== null && _restChartTextProps$s !== void 0 ? _restChartTextProps$s : {}), (_labelData$chartTextP = (_labelData$chartTextP2 = labelData.chartTextProps) === null || _labelData$chartTextP2 === void 0 || (_labelData$chartTextP2 = _labelData$chartTextP2.styles) === null || _labelData$chartTextP2 === void 0 ? void 0 : _labelData$chartTextP2.text) !== null && _labelData$chartTextP !== void 0 ? _labelData$chartTextP : {}), !isVisible ? {
|
|
208
|
+
visibility: 'hidden',
|
|
209
|
+
opacity: 0,
|
|
210
|
+
pointerEvents: 'none'
|
|
211
|
+
} : {}),
|
|
212
|
+
backgroundRect: _objectSpread(_objectSpread(_objectSpread({}, (_restChartTextProps$s3 = restChartTextProps === null || restChartTextProps === void 0 || (_restChartTextProps$s4 = restChartTextProps.styles) === null || _restChartTextProps$s4 === void 0 ? void 0 : _restChartTextProps$s4.backgroundRect) !== null && _restChartTextProps$s3 !== void 0 ? _restChartTextProps$s3 : {}), (_labelData$chartTextP3 = (_labelData$chartTextP4 = labelData.chartTextProps) === null || _labelData$chartTextP4 === void 0 || (_labelData$chartTextP4 = _labelData$chartTextP4.styles) === null || _labelData$chartTextP4 === void 0 ? void 0 : _labelData$chartTextP4.backgroundRect) !== null && _labelData$chartTextP3 !== void 0 ? _labelData$chartTextP3 : {}), !isVisible ? {
|
|
213
|
+
display: 'none'
|
|
214
|
+
} : {})
|
|
215
|
+
};
|
|
216
|
+
return /*#__PURE__*/_jsx(ChartText, _objectSpread(_objectSpread(_objectSpread({
|
|
217
|
+
x: labelData.x,
|
|
218
|
+
y: labelData.y
|
|
219
|
+
}, restChartTextProps), labelData.chartTextProps), {}, {
|
|
220
|
+
onDimensionsChange: onDimensionsChangeByKey.get(labelData.key),
|
|
221
|
+
styles: mergedStyles,
|
|
222
|
+
children: labelData.label
|
|
223
|
+
}), labelData.key);
|
|
224
|
+
})
|
|
225
|
+
});
|
|
226
|
+
});
|