@coinbase/cds-mobile-visualization 3.4.0-beta.8 → 3.4.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/CHANGELOG.md +142 -0
- package/dts/chart/CartesianChart.d.ts +92 -7
- package/dts/chart/CartesianChart.d.ts.map +1 -1
- package/dts/chart/ChartContextBridge.d.ts.map +1 -1
- package/dts/chart/ChartProvider.d.ts +3 -0
- package/dts/chart/ChartProvider.d.ts.map +1 -1
- package/dts/chart/Path.d.ts +36 -13
- package/dts/chart/Path.d.ts.map +1 -1
- package/dts/chart/PeriodSelector.d.ts +21 -6
- package/dts/chart/PeriodSelector.d.ts.map +1 -1
- package/dts/chart/area/Area.d.ts +14 -11
- package/dts/chart/area/Area.d.ts.map +1 -1
- package/dts/chart/area/AreaChart.d.ts +33 -9
- package/dts/chart/area/AreaChart.d.ts.map +1 -1
- package/dts/chart/area/DottedArea.d.ts.map +1 -1
- package/dts/chart/area/GradientArea.d.ts.map +1 -1
- package/dts/chart/area/SolidArea.d.ts.map +1 -1
- package/dts/chart/axis/Axis.d.ts +22 -42
- package/dts/chart/axis/Axis.d.ts.map +1 -1
- package/dts/chart/axis/XAxis.d.ts +6 -0
- package/dts/chart/axis/XAxis.d.ts.map +1 -1
- package/dts/chart/axis/YAxis.d.ts +1 -0
- package/dts/chart/axis/YAxis.d.ts.map +1 -1
- package/dts/chart/bar/Bar.d.ts +51 -51
- package/dts/chart/bar/Bar.d.ts.map +1 -1
- package/dts/chart/bar/BarChart.d.ts +56 -11
- package/dts/chart/bar/BarChart.d.ts.map +1 -1
- package/dts/chart/bar/BarPlot.d.ts +2 -1
- package/dts/chart/bar/BarPlot.d.ts.map +1 -1
- package/dts/chart/bar/BarStack.d.ts +45 -20
- package/dts/chart/bar/BarStack.d.ts.map +1 -1
- package/dts/chart/bar/BarStackGroup.d.ts +2 -1
- package/dts/chart/bar/BarStackGroup.d.ts.map +1 -1
- package/dts/chart/bar/DefaultBar.d.ts.map +1 -1
- package/dts/chart/bar/DefaultBarStack.d.ts.map +1 -1
- package/dts/chart/gradient/Gradient.d.ts +5 -0
- package/dts/chart/gradient/Gradient.d.ts.map +1 -1
- package/dts/chart/index.d.ts +1 -0
- package/dts/chart/index.d.ts.map +1 -1
- package/dts/chart/legend/DefaultLegendEntry.d.ts +5 -0
- package/dts/chart/legend/DefaultLegendEntry.d.ts.map +1 -0
- package/dts/chart/legend/DefaultLegendShape.d.ts +5 -0
- package/dts/chart/legend/DefaultLegendShape.d.ts.map +1 -0
- package/dts/chart/legend/Legend.d.ts +168 -0
- package/dts/chart/legend/Legend.d.ts.map +1 -0
- package/dts/chart/legend/index.d.ts +4 -0
- package/dts/chart/legend/index.d.ts.map +1 -0
- package/dts/chart/line/DottedLine.d.ts.map +1 -1
- package/dts/chart/line/Line.d.ts +23 -19
- package/dts/chart/line/Line.d.ts.map +1 -1
- package/dts/chart/line/LineChart.d.ts +26 -9
- package/dts/chart/line/LineChart.d.ts.map +1 -1
- package/dts/chart/line/ReferenceLine.d.ts +1 -0
- package/dts/chart/line/ReferenceLine.d.ts.map +1 -1
- package/dts/chart/line/SolidLine.d.ts.map +1 -1
- package/dts/chart/point/Point.d.ts +26 -2
- package/dts/chart/point/Point.d.ts.map +1 -1
- package/dts/chart/scrubber/DefaultScrubberBeacon.d.ts +32 -2
- package/dts/chart/scrubber/DefaultScrubberBeacon.d.ts.map +1 -1
- package/dts/chart/scrubber/DefaultScrubberLabel.d.ts +2 -1
- package/dts/chart/scrubber/DefaultScrubberLabel.d.ts.map +1 -1
- package/dts/chart/scrubber/Scrubber.d.ts +86 -17
- package/dts/chart/scrubber/Scrubber.d.ts.map +1 -1
- package/dts/chart/scrubber/ScrubberAccessibilityView.d.ts +12 -0
- package/dts/chart/scrubber/ScrubberAccessibilityView.d.ts.map +1 -0
- package/dts/chart/scrubber/ScrubberBeaconGroup.d.ts +10 -0
- package/dts/chart/scrubber/ScrubberBeaconGroup.d.ts.map +1 -1
- package/dts/chart/scrubber/ScrubberBeaconLabelGroup.d.ts +16 -1
- package/dts/chart/scrubber/ScrubberBeaconLabelGroup.d.ts.map +1 -1
- package/dts/chart/scrubber/ScrubberProvider.d.ts.map +1 -1
- package/dts/chart/utils/axis.d.ts +45 -10
- package/dts/chart/utils/axis.d.ts.map +1 -1
- package/dts/chart/utils/bar.d.ts +190 -0
- package/dts/chart/utils/bar.d.ts.map +1 -1
- package/dts/chart/utils/chart.d.ts +32 -0
- package/dts/chart/utils/chart.d.ts.map +1 -1
- package/dts/chart/utils/context.d.ts +21 -6
- package/dts/chart/utils/context.d.ts.map +1 -1
- package/dts/chart/utils/gradient.d.ts +3 -1
- package/dts/chart/utils/gradient.d.ts.map +1 -1
- package/dts/chart/utils/path.d.ts +26 -0
- package/dts/chart/utils/path.d.ts.map +1 -1
- package/dts/chart/utils/point.d.ts +24 -12
- package/dts/chart/utils/point.d.ts.map +1 -1
- package/dts/chart/utils/scale.d.ts +11 -0
- package/dts/chart/utils/scale.d.ts.map +1 -1
- package/dts/chart/utils/scrubber.d.ts +2 -1
- package/dts/chart/utils/scrubber.d.ts.map +1 -1
- package/dts/chart/utils/transition.d.ts +63 -22
- package/dts/chart/utils/transition.d.ts.map +1 -1
- package/dts/sparkline/Sparkline.d.ts +2 -1
- package/dts/sparkline/Sparkline.d.ts.map +1 -1
- package/dts/sparkline/SparklineArea.d.ts +2 -1
- package/dts/sparkline/SparklineArea.d.ts.map +1 -1
- package/dts/sparkline/SparklineGradient.d.ts +2 -1
- package/dts/sparkline/SparklineGradient.d.ts.map +1 -1
- package/dts/sparkline/sparkline-interactive/SparklineInteractive.d.ts +2 -1
- package/dts/sparkline/sparkline-interactive/SparklineInteractive.d.ts.map +1 -1
- package/esm/chart/CartesianChart.js +176 -82
- package/esm/chart/ChartContextBridge.js +14 -3
- package/esm/chart/ChartProvider.js +2 -2
- package/esm/chart/Path.js +34 -29
- package/esm/chart/PeriodSelector.js +6 -2
- package/esm/chart/__stories__/CartesianChart.stories.js +27 -86
- package/esm/chart/__stories__/ChartAccessibility.stories.js +721 -0
- package/esm/chart/__stories__/ChartTransitions.stories.js +625 -0
- package/esm/chart/__stories__/PeriodSelector.stories.js +102 -4
- package/esm/chart/area/Area.js +21 -9
- package/esm/chart/area/AreaChart.js +18 -13
- package/esm/chart/area/DottedArea.js +28 -18
- package/esm/chart/area/GradientArea.js +14 -7
- package/esm/chart/area/SolidArea.js +6 -2
- package/esm/chart/area/__stories__/AreaChart.stories.js +47 -5
- package/esm/chart/axis/Axis.js +5 -41
- package/esm/chart/axis/XAxis.js +116 -47
- package/esm/chart/axis/YAxis.js +105 -26
- package/esm/chart/axis/__stories__/Axis.stories.js +324 -48
- package/esm/chart/bar/Bar.js +17 -15
- package/esm/chart/bar/BarChart.js +38 -33
- package/esm/chart/bar/BarPlot.js +40 -45
- package/esm/chart/bar/BarStack.js +92 -475
- package/esm/chart/bar/BarStackGroup.js +37 -27
- package/esm/chart/bar/DefaultBar.js +27 -18
- package/esm/chart/bar/DefaultBarStack.js +25 -9
- package/esm/chart/bar/__stories__/BarChart.stories.js +728 -54
- package/esm/chart/gradient/Gradient.js +2 -1
- package/esm/chart/index.js +1 -0
- package/esm/chart/legend/DefaultLegendEntry.js +42 -0
- package/esm/chart/legend/DefaultLegendShape.js +64 -0
- package/esm/chart/legend/Legend.js +59 -0
- package/esm/chart/legend/__stories__/Legend.stories.js +574 -0
- package/esm/chart/legend/index.js +3 -0
- package/esm/chart/line/DottedLine.js +6 -2
- package/esm/chart/line/Line.js +42 -38
- package/esm/chart/line/LineChart.js +36 -12
- package/esm/chart/line/SolidLine.js +6 -2
- package/esm/chart/line/__stories__/LineChart.stories.js +241 -594
- package/esm/chart/line/__stories__/ReferenceLine.stories.js +95 -1
- package/esm/chart/point/Point.js +35 -36
- package/esm/chart/scrubber/DefaultScrubberBeacon.js +41 -38
- package/esm/chart/scrubber/DefaultScrubberLabel.js +26 -10
- package/esm/chart/scrubber/Scrubber.js +67 -35
- package/esm/chart/scrubber/ScrubberAccessibilityView.js +177 -0
- package/esm/chart/scrubber/ScrubberBeaconGroup.js +30 -22
- package/esm/chart/scrubber/ScrubberBeaconLabelGroup.js +35 -8
- package/esm/chart/scrubber/ScrubberProvider.js +29 -24
- package/esm/chart/scrubber/__stories__/Scrubber.stories.js +946 -0
- package/esm/chart/utils/axis.js +88 -44
- package/esm/chart/utils/bar.js +820 -0
- package/esm/chart/utils/chart.js +34 -7
- package/esm/chart/utils/context.js +7 -0
- package/esm/chart/utils/gradient.js +8 -4
- package/esm/chart/utils/path.js +91 -61
- package/esm/chart/utils/point.js +92 -39
- package/esm/chart/utils/scale.js +13 -2
- package/esm/chart/utils/scrubber.js +12 -5
- package/esm/chart/utils/transition.js +108 -60
- package/esm/sparkline/Sparkline.js +2 -1
- package/esm/sparkline/SparklineArea.js +2 -1
- package/esm/sparkline/SparklineGradient.js +2 -1
- package/esm/sparkline/__figma__/Sparkline.figma.js +1 -1
- package/esm/sparkline/__stories__/Sparkline.stories.js +11 -7
- package/esm/sparkline/__stories__/SparklineGradient.stories.js +7 -4
- package/esm/sparkline/sparkline-interactive/SparklineInteractive.js +2 -1
- package/esm/sparkline/sparkline-interactive/__figma__/SparklineInteractive.figma.js +1 -1
- package/esm/sparkline/sparkline-interactive/__stories__/SparklineInteractive.stories.js +51 -26
- package/esm/sparkline/sparkline-interactive-header/__figma__/SparklineInteractiveHeader.figma.js +1 -1
- package/esm/sparkline/sparkline-interactive-header/__stories__/SparklineInteractiveHeader.stories.js +19 -9
- package/package.json +13 -10
- package/esm/chart/__stories__/Chart.stories.js +0 -77
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
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); }
|
|
2
1
|
import React, { memo, useMemo } from 'react';
|
|
3
2
|
import { useTheme } from '@coinbase/cds-mobile/hooks/useTheme';
|
|
4
3
|
import { useCartesianChartContext } from '../ChartProvider';
|
|
5
|
-
import {
|
|
4
|
+
import { EPSILON, getBars, getStackBaseline, getStackOrigin } from '../utils/bar';
|
|
5
|
+
import { getGradientStops } from '../utils/gradient';
|
|
6
6
|
import { convertToSerializableScale } from '../utils/scale';
|
|
7
7
|
import { Bar } from './Bar';
|
|
8
8
|
import { DefaultBarStack } from './DefaultBarStack';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Extended series type that includes bar-specific properties.
|
|
12
|
+
*/
|
|
9
13
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
10
|
-
const EPSILON = 1e-4;
|
|
11
14
|
/**
|
|
12
15
|
* BarStack component that renders a single stack of bars at a specific category index.
|
|
13
16
|
* Handles the stacking logic for bars within a single category.
|
|
@@ -16,10 +19,13 @@ export const BarStack = /*#__PURE__*/memo(_ref => {
|
|
|
16
19
|
let {
|
|
17
20
|
series,
|
|
18
21
|
categoryIndex,
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
indexPos,
|
|
23
|
+
thickness,
|
|
24
|
+
indexScale,
|
|
25
|
+
valueScale,
|
|
22
26
|
rect,
|
|
27
|
+
xAxisId,
|
|
28
|
+
yAxisId,
|
|
23
29
|
BarComponent: defaultBarComponent,
|
|
24
30
|
fillOpacity: defaultFillOpacity,
|
|
25
31
|
stroke: defaultStroke,
|
|
@@ -30,28 +36,23 @@ export const BarStack = /*#__PURE__*/memo(_ref => {
|
|
|
30
36
|
barMinSize,
|
|
31
37
|
stackMinSize,
|
|
32
38
|
roundBaseline,
|
|
39
|
+
transitions,
|
|
33
40
|
transition
|
|
34
41
|
} = _ref;
|
|
35
42
|
const theme = useTheme();
|
|
36
43
|
const {
|
|
44
|
+
layout,
|
|
37
45
|
getSeriesData,
|
|
38
46
|
getXAxis,
|
|
39
|
-
|
|
47
|
+
getYAxis
|
|
40
48
|
} = useCartesianChartContext();
|
|
41
|
-
const xAxis = getXAxis();
|
|
42
|
-
const
|
|
43
|
-
const baseline = useMemo(() =>
|
|
44
|
-
var _yScale;
|
|
45
|
-
const domain = yScale.domain();
|
|
46
|
-
const [domainMin, domainMax] = domain;
|
|
47
|
-
const baselineValue = domainMin >= 0 ? domainMin : domainMax <= 0 ? domainMax : 0;
|
|
48
|
-
const baseline = (_yScale = yScale(baselineValue)) != null ? _yScale : rect.y + rect.height;
|
|
49
|
-
return Math.max(rect.y, Math.min(baseline, rect.y + rect.height));
|
|
50
|
-
}, [rect.height, rect.y, yScale]);
|
|
49
|
+
const xAxis = getXAxis(xAxisId);
|
|
50
|
+
const yAxis = getYAxis(yAxisId);
|
|
51
|
+
const baseline = useMemo(() => getStackBaseline(valueScale, rect, layout), [rect, valueScale, layout]);
|
|
51
52
|
const seriesGradients = useMemo(() => {
|
|
52
53
|
return series.map(s => {
|
|
53
|
-
if (!s.gradient
|
|
54
|
-
const gradientScale = s.gradient.axis === 'x' ?
|
|
54
|
+
if (!s.gradient) return;
|
|
55
|
+
const gradientScale = s.gradient.axis === 'x' ? layout === 'vertical' ? indexScale : valueScale : layout === 'vertical' ? valueScale : indexScale;
|
|
55
56
|
const serializableScale = convertToSerializableScale(gradientScale);
|
|
56
57
|
if (!serializableScale) return;
|
|
57
58
|
const domain = {
|
|
@@ -66,486 +67,102 @@ export const BarStack = /*#__PURE__*/memo(_ref => {
|
|
|
66
67
|
stops
|
|
67
68
|
};
|
|
68
69
|
});
|
|
69
|
-
}, [series,
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (isAboveBaseline) {
|
|
108
|
-
positiveBarCount++;
|
|
109
|
-
} else if (isBelowBaseline) {
|
|
110
|
-
negativeBarCount++;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// Calculate height (remember SVG y coordinates are inverted)
|
|
115
|
-
const height = Math.abs(barBottom - barTop);
|
|
116
|
-
const y = Math.min(barBottom, barTop);
|
|
117
|
-
|
|
118
|
-
// Skip bars that would have zero or negative height
|
|
119
|
-
if (height <= 0) {
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// Update stack bounds
|
|
124
|
-
minY = Math.min(minY, y);
|
|
125
|
-
maxY = Math.max(maxY, y + height);
|
|
126
|
-
|
|
127
|
-
// Determine fill color, respecting gradient if present
|
|
128
|
-
let barFill = s.color || theme.color.fgPrimary;
|
|
129
|
-
|
|
130
|
-
// Evaluate gradient if provided (using precomputed stops)
|
|
131
|
-
const seriesGradientConfig = seriesGradients.find(g => (g == null ? void 0 : g.seriesId) === s.id);
|
|
132
|
-
if (seriesGradientConfig) {
|
|
133
|
-
var _seriesGradientConfig;
|
|
134
|
-
const axis = (_seriesGradientConfig = seriesGradientConfig.gradient.axis) != null ? _seriesGradientConfig : 'y';
|
|
135
|
-
// For x-axis gradient, use the categoryIndex
|
|
136
|
-
// For y-axis gradient, use the actual data value
|
|
137
|
-
const dataValue = axis === 'x' ? categoryIndex : top;
|
|
138
|
-
const evaluatedColor = evaluateGradientAtValue(seriesGradientConfig.stops, dataValue, seriesGradientConfig.scale);
|
|
139
|
-
if (evaluatedColor) {
|
|
140
|
-
// Only apply gradient color if fill is not explicitly set
|
|
141
|
-
barFill = evaluatedColor;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
allBars.push({
|
|
145
|
-
seriesId: s.id,
|
|
146
|
-
x,
|
|
147
|
-
y,
|
|
148
|
-
width,
|
|
149
|
-
height,
|
|
150
|
-
dataY: value,
|
|
151
|
-
// Store the actual data value
|
|
152
|
-
fill: barFill,
|
|
153
|
-
// Check if the bar should be rounded based on the baseline, with an epsilon to handle floating-point rounding
|
|
154
|
-
roundTop: roundBaseline || Math.abs(barTop - baseline) >= EPSILON,
|
|
155
|
-
roundBottom: roundBaseline || Math.abs(barBottom - baseline) >= EPSILON,
|
|
156
|
-
shouldApplyGap
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
// Apply proportional gap distribution to maintain total stack height
|
|
161
|
-
if (stackGap && allBars.length > 1) {
|
|
162
|
-
// Separate bars by baseline side
|
|
163
|
-
const barsAboveBaseline = allBars.filter(bar => {
|
|
164
|
-
const [bottom, top] = bar.dataY.sort((a, b) => a - b);
|
|
165
|
-
return bottom >= 0 && top !== bottom && bar.shouldApplyGap;
|
|
166
|
-
});
|
|
167
|
-
const barsBelowBaseline = allBars.filter(bar => {
|
|
168
|
-
const [bottom, top] = bar.dataY.sort((a, b) => a - b);
|
|
169
|
-
return bottom <= 0 && bottom !== top && bar.shouldApplyGap;
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
// Apply proportional gaps to bars above baseline
|
|
173
|
-
if (barsAboveBaseline.length > 1) {
|
|
174
|
-
const totalGapSpace = stackGap * (barsAboveBaseline.length - 1);
|
|
175
|
-
const totalDataHeight = barsAboveBaseline.reduce((sum, bar) => sum + bar.height, 0);
|
|
176
|
-
const heightReduction = totalGapSpace / totalDataHeight;
|
|
177
|
-
|
|
178
|
-
// Sort bars by position (from baseline upward)
|
|
179
|
-
const sortedBars = barsAboveBaseline.sort((a, b) => b.y - a.y);
|
|
180
|
-
let currentY = baseline;
|
|
181
|
-
sortedBars.forEach((bar, index) => {
|
|
182
|
-
// Reduce bar height proportionally
|
|
183
|
-
const newHeight = bar.height * (1 - heightReduction);
|
|
184
|
-
const newY = currentY - newHeight;
|
|
185
|
-
|
|
186
|
-
// Update the bar in allBars array
|
|
187
|
-
const barIndex = allBars.findIndex(b => b.seriesId === bar.seriesId);
|
|
188
|
-
if (barIndex !== -1) {
|
|
189
|
-
allBars[barIndex] = _extends({}, allBars[barIndex], {
|
|
190
|
-
height: newHeight,
|
|
191
|
-
y: newY
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// Move to next position (include gap for next bar)
|
|
196
|
-
currentY = newY - (index < sortedBars.length - 1 ? stackGap : 0);
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// Apply proportional gaps to bars below baseline
|
|
201
|
-
if (barsBelowBaseline.length > 1) {
|
|
202
|
-
const totalGapSpace = stackGap * (barsBelowBaseline.length - 1);
|
|
203
|
-
const totalDataHeight = barsBelowBaseline.reduce((sum, bar) => sum + bar.height, 0);
|
|
204
|
-
const heightReduction = totalGapSpace / totalDataHeight;
|
|
205
|
-
|
|
206
|
-
// Sort bars by position (from baseline downward)
|
|
207
|
-
const sortedBars = barsBelowBaseline.sort((a, b) => a.y - b.y);
|
|
208
|
-
let currentY = baseline;
|
|
209
|
-
sortedBars.forEach((bar, index) => {
|
|
210
|
-
// Reduce bar height proportionally
|
|
211
|
-
const newHeight = bar.height * (1 - heightReduction);
|
|
212
|
-
|
|
213
|
-
// Update the bar in allBars array
|
|
214
|
-
const barIndex = allBars.findIndex(b => b.seriesId === bar.seriesId);
|
|
215
|
-
if (barIndex !== -1) {
|
|
216
|
-
allBars[barIndex] = _extends({}, allBars[barIndex], {
|
|
217
|
-
height: newHeight,
|
|
218
|
-
y: currentY
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// Move to next position (include gap for next bar)
|
|
223
|
-
currentY = currentY + newHeight + (index < sortedBars.length - 1 ? stackGap : 0);
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// Recalculate stack bounds after gap adjustments
|
|
228
|
-
if (allBars.length > 0) {
|
|
229
|
-
minY = Math.min(...allBars.map(bar => bar.y));
|
|
230
|
-
maxY = Math.max(...allBars.map(bar => bar.y + bar.height));
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
// Apply barMinSize constraints
|
|
235
|
-
if (barMinSize) {
|
|
236
|
-
// First, expand bars that need it and track the expansion
|
|
237
|
-
const expandedBars = allBars.map((bar, index) => {
|
|
238
|
-
if (bar.height < barMinSize) {
|
|
239
|
-
var _yScale4, _yScale5, _yScale6, _yScale7;
|
|
240
|
-
const heightIncrease = barMinSize - bar.height;
|
|
241
|
-
const [bottom, top] = bar.dataY.sort((a, b) => a - b);
|
|
242
|
-
|
|
243
|
-
// Determine how to expand the bar
|
|
244
|
-
let newBottom = bottom;
|
|
245
|
-
let newTop = top;
|
|
246
|
-
const scaleUnit = Math.abs(((_yScale4 = yScale(1)) != null ? _yScale4 : 0) - ((_yScale5 = yScale(0)) != null ? _yScale5 : 0));
|
|
247
|
-
if (bottom === 0) {
|
|
248
|
-
// Expand away from baseline (upward for positive)
|
|
249
|
-
newTop = top + heightIncrease / scaleUnit;
|
|
250
|
-
} else if (top === 0) {
|
|
251
|
-
// Expand away from baseline (downward for negative)
|
|
252
|
-
newBottom = bottom - heightIncrease / scaleUnit;
|
|
253
|
-
} else {
|
|
254
|
-
// Expand in both directions
|
|
255
|
-
const halfIncrease = heightIncrease / scaleUnit / 2;
|
|
256
|
-
newBottom = bottom - halfIncrease;
|
|
257
|
-
newTop = top + halfIncrease;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
// Recalculate bar position with new data values
|
|
261
|
-
const newBarBottom = (_yScale6 = yScale(newBottom)) != null ? _yScale6 : baseline;
|
|
262
|
-
const newBarTop = (_yScale7 = yScale(newTop)) != null ? _yScale7 : baseline;
|
|
263
|
-
const newHeight = Math.abs(newBarBottom - newBarTop);
|
|
264
|
-
const newY = Math.min(newBarBottom, newBarTop);
|
|
265
|
-
return _extends({}, bar, {
|
|
266
|
-
height: newHeight,
|
|
267
|
-
y: newY,
|
|
268
|
-
wasExpanded: true
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
return _extends({}, bar, {
|
|
272
|
-
wasExpanded: false
|
|
273
|
-
});
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
// Now reposition all bars to avoid overlaps, similar to stackMinSize logic
|
|
277
|
-
|
|
278
|
-
// Sort bars by position to maintain order
|
|
279
|
-
const sortedExpandedBars = [...expandedBars].sort((a, b) => a.y - b.y);
|
|
280
|
-
|
|
281
|
-
// Determine if we have bars above and below baseline
|
|
282
|
-
const barsAboveBaseline = sortedExpandedBars.filter(bar => bar.y + bar.height <= baseline);
|
|
283
|
-
const barsBelowBaseline = sortedExpandedBars.filter(bar => bar.y >= baseline);
|
|
284
|
-
|
|
285
|
-
// Create a map of new positions
|
|
286
|
-
const newPositions = new Map();
|
|
287
|
-
|
|
288
|
-
// Start positioning from the baseline and work outward
|
|
289
|
-
let currentYAbove = baseline; // Start at baseline, work upward (decreasing Y)
|
|
290
|
-
let currentYBelow = baseline; // Start at baseline, work downward (increasing Y)
|
|
291
|
-
|
|
292
|
-
// Position bars above baseline (positive values, decreasing Y)
|
|
293
|
-
for (let i = barsAboveBaseline.length - 1; i >= 0; i--) {
|
|
294
|
-
const bar = barsAboveBaseline[i];
|
|
295
|
-
const newY = currentYAbove - bar.height;
|
|
296
|
-
newPositions.set(bar.seriesId, {
|
|
297
|
-
y: newY,
|
|
298
|
-
height: bar.height
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
// Update currentYAbove for next bar (preserve gaps)
|
|
302
|
-
if (i > 0) {
|
|
303
|
-
const currentBar = barsAboveBaseline[i];
|
|
304
|
-
const nextBar = barsAboveBaseline[i - 1];
|
|
305
|
-
// Find original bars to get original gap
|
|
306
|
-
const originalCurrent = allBars.find(b => b.seriesId === currentBar.seriesId);
|
|
307
|
-
const originalNext = allBars.find(b => b.seriesId === nextBar.seriesId);
|
|
308
|
-
const originalGap = originalCurrent.y - (originalNext.y + originalNext.height);
|
|
309
|
-
currentYAbove = newY - originalGap;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
// Position bars below baseline (negative values, increasing Y)
|
|
314
|
-
for (let i = 0; i < barsBelowBaseline.length; i++) {
|
|
315
|
-
const bar = barsBelowBaseline[i];
|
|
316
|
-
const newY = currentYBelow;
|
|
317
|
-
newPositions.set(bar.seriesId, {
|
|
318
|
-
y: newY,
|
|
319
|
-
height: bar.height
|
|
320
|
-
});
|
|
321
|
-
|
|
322
|
-
// Update currentYBelow for next bar (preserve gaps)
|
|
323
|
-
if (i < barsBelowBaseline.length - 1) {
|
|
324
|
-
const currentBar = barsBelowBaseline[i];
|
|
325
|
-
const nextBar = barsBelowBaseline[i + 1];
|
|
326
|
-
// Find original bars to get original gap
|
|
327
|
-
const originalCurrent = allBars.find(b => b.seriesId === currentBar.seriesId);
|
|
328
|
-
const originalNext = allBars.find(b => b.seriesId === nextBar.seriesId);
|
|
329
|
-
const originalGap = originalNext.y - (originalCurrent.y + originalCurrent.height);
|
|
330
|
-
currentYBelow = newY + bar.height + originalGap;
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
// Apply new positions to all bars
|
|
335
|
-
allBars = expandedBars.map(bar => {
|
|
336
|
-
const newPos = newPositions.get(bar.seriesId);
|
|
337
|
-
if (newPos) {
|
|
338
|
-
return _extends({}, bar, {
|
|
339
|
-
y: newPos.y,
|
|
340
|
-
height: newPos.height
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
return bar;
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
// Recalculate stack bounds after barMinSize expansion and repositioning
|
|
347
|
-
if (allBars.length > 0) {
|
|
348
|
-
minY = Math.min(...allBars.map(bar => bar.y));
|
|
349
|
-
maxY = Math.max(...allBars.map(bar => bar.y + bar.height));
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
// Apply border radius logic (will be reapplied after stackMinSize if needed)
|
|
354
|
-
const applyBorderRadiusLogic = bars => {
|
|
355
|
-
return bars.sort((a, b) => b.y - a.y).map((a, index) => {
|
|
356
|
-
const barBefore = index > 0 ? bars[index - 1] : null;
|
|
357
|
-
const barAfter = index < bars.length - 1 ? bars[index + 1] : null;
|
|
358
|
-
const shouldRoundTop = index === bars.length - 1 || a.shouldApplyGap && stackGap || !a.shouldApplyGap && barAfter && barAfter.y + barAfter.height !== a.y;
|
|
359
|
-
const shouldRoundBottom = index === 0 || a.shouldApplyGap && stackGap || !a.shouldApplyGap && barBefore && barBefore.y !== a.y + a.height;
|
|
360
|
-
return _extends({}, a, {
|
|
361
|
-
roundTop: Boolean(a.roundTop && shouldRoundTop),
|
|
362
|
-
roundBottom: Boolean(a.roundBottom && shouldRoundBottom)
|
|
363
|
-
});
|
|
364
|
-
});
|
|
365
|
-
};
|
|
366
|
-
allBars = applyBorderRadiusLogic(allBars);
|
|
367
|
-
|
|
368
|
-
// Calculate the bounding rect for the entire stack
|
|
369
|
-
let stackBounds = {
|
|
370
|
-
x,
|
|
371
|
-
y: minY === Infinity ? baseline : minY,
|
|
372
|
-
width,
|
|
373
|
-
height: maxY === -Infinity ? 0 : maxY - minY
|
|
374
|
-
};
|
|
375
|
-
|
|
376
|
-
// Apply stackMinSize constraints
|
|
377
|
-
if (stackMinSize) {
|
|
378
|
-
if (allBars.length === 1 && stackBounds.height < stackMinSize) {
|
|
379
|
-
var _yScale8, _yScale9, _yScale0, _yScale1;
|
|
380
|
-
// For single bars (non-stacked), treat stackMinSize like barMinSize
|
|
381
|
-
|
|
382
|
-
const bar = allBars[0];
|
|
383
|
-
const heightIncrease = stackMinSize - bar.height;
|
|
384
|
-
const [bottom, top] = bar.dataY.sort((a, b) => a - b);
|
|
385
|
-
|
|
386
|
-
// Determine how to expand the bar (same logic as barMinSize)
|
|
387
|
-
let newBottom = bottom;
|
|
388
|
-
let newTop = top;
|
|
389
|
-
const scaleUnit = Math.abs(((_yScale8 = yScale(1)) != null ? _yScale8 : 0) - ((_yScale9 = yScale(0)) != null ? _yScale9 : 0));
|
|
390
|
-
if (bottom === 0) {
|
|
391
|
-
// Expand away from baseline (upward for positive)
|
|
392
|
-
newTop = top + heightIncrease / scaleUnit;
|
|
393
|
-
} else if (top === 0) {
|
|
394
|
-
// Expand away from baseline (downward for negative)
|
|
395
|
-
newBottom = bottom - heightIncrease / scaleUnit;
|
|
396
|
-
} else {
|
|
397
|
-
// Expand in both directions
|
|
398
|
-
const halfIncrease = heightIncrease / scaleUnit / 2;
|
|
399
|
-
newBottom = bottom - halfIncrease;
|
|
400
|
-
newTop = top + halfIncrease;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
// Recalculate bar position with new data values
|
|
404
|
-
const newBarBottom = (_yScale0 = yScale(newBottom)) != null ? _yScale0 : baseline;
|
|
405
|
-
const newBarTop = (_yScale1 = yScale(newTop)) != null ? _yScale1 : baseline;
|
|
406
|
-
const newHeight = Math.abs(newBarBottom - newBarTop);
|
|
407
|
-
const newY = Math.min(newBarBottom, newBarTop);
|
|
408
|
-
allBars[0] = _extends({}, bar, {
|
|
409
|
-
height: newHeight,
|
|
410
|
-
y: newY
|
|
411
|
-
});
|
|
412
|
-
|
|
413
|
-
// Recalculate stack bounds
|
|
414
|
-
stackBounds = {
|
|
415
|
-
x,
|
|
416
|
-
y: newY,
|
|
417
|
-
width,
|
|
418
|
-
height: newHeight
|
|
419
|
-
};
|
|
420
|
-
} else if (allBars.length > 1 && stackBounds.height < stackMinSize) {
|
|
421
|
-
// For multiple bars (stacked), scale heights while preserving gaps
|
|
422
|
-
|
|
423
|
-
// Calculate total bar height (excluding gaps)
|
|
424
|
-
const totalBarHeight = allBars.reduce((sum, bar) => sum + bar.height, 0);
|
|
425
|
-
const totalGapHeight = stackBounds.height - totalBarHeight;
|
|
426
|
-
|
|
427
|
-
// Calculate how much we need to increase bar heights
|
|
428
|
-
const requiredBarHeight = stackMinSize - totalGapHeight;
|
|
429
|
-
const barScaleFactor = requiredBarHeight / totalBarHeight;
|
|
430
|
-
|
|
431
|
-
// Sort bars by position to maintain order
|
|
432
|
-
const sortedBars = [...allBars].sort((a, b) => a.y - b.y);
|
|
433
|
-
|
|
434
|
-
// Determine if we have bars above and below baseline
|
|
435
|
-
const barsAboveBaseline = sortedBars.filter(bar => bar.y + bar.height <= baseline);
|
|
436
|
-
const barsBelowBaseline = sortedBars.filter(bar => bar.y >= baseline);
|
|
437
|
-
|
|
438
|
-
// Create a map of new positions
|
|
439
|
-
const newPositions = new Map();
|
|
440
|
-
|
|
441
|
-
// Start positioning from the baseline and work outward
|
|
442
|
-
let currentYAbove = baseline; // Start at baseline, work upward (decreasing Y)
|
|
443
|
-
let currentYBelow = baseline; // Start at baseline, work downward (increasing Y)
|
|
444
|
-
|
|
445
|
-
// Position bars above baseline (positive values, decreasing Y)
|
|
446
|
-
for (let i = barsAboveBaseline.length - 1; i >= 0; i--) {
|
|
447
|
-
const bar = barsAboveBaseline[i];
|
|
448
|
-
const newHeight = bar.height * barScaleFactor;
|
|
449
|
-
const newY = currentYAbove - newHeight;
|
|
450
|
-
newPositions.set(bar.seriesId, {
|
|
451
|
-
y: newY,
|
|
452
|
-
height: newHeight
|
|
453
|
-
});
|
|
454
|
-
|
|
455
|
-
// Update currentYAbove for next bar (preserve gaps)
|
|
456
|
-
if (i > 0) {
|
|
457
|
-
const currentBar = barsAboveBaseline[i];
|
|
458
|
-
const nextBar = barsAboveBaseline[i - 1];
|
|
459
|
-
const originalGap = currentBar.y - (nextBar.y + nextBar.height);
|
|
460
|
-
currentYAbove = newY - originalGap;
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
// Position bars below baseline (negative values, increasing Y)
|
|
465
|
-
for (let i = 0; i < barsBelowBaseline.length; i++) {
|
|
466
|
-
const bar = barsBelowBaseline[i];
|
|
467
|
-
const newHeight = bar.height * barScaleFactor;
|
|
468
|
-
const newY = currentYBelow;
|
|
469
|
-
newPositions.set(bar.seriesId, {
|
|
470
|
-
y: newY,
|
|
471
|
-
height: newHeight
|
|
472
|
-
});
|
|
473
|
-
|
|
474
|
-
// Update currentYBelow for next bar (preserve gaps)
|
|
475
|
-
if (i < barsBelowBaseline.length - 1) {
|
|
476
|
-
const currentBar = barsBelowBaseline[i];
|
|
477
|
-
const nextBar = barsBelowBaseline[i + 1];
|
|
478
|
-
const originalGap = nextBar.y - (currentBar.y + currentBar.height);
|
|
479
|
-
currentYBelow = newY + newHeight + originalGap;
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
// Apply new positions to all bars
|
|
484
|
-
allBars = allBars.map(bar => {
|
|
485
|
-
const newPos = newPositions.get(bar.seriesId);
|
|
486
|
-
if (!newPos) return bar;
|
|
487
|
-
return _extends({}, bar, {
|
|
488
|
-
height: newPos.height,
|
|
489
|
-
y: newPos.y
|
|
490
|
-
});
|
|
491
|
-
});
|
|
492
|
-
|
|
493
|
-
// Recalculate stack bounds
|
|
494
|
-
const newMinY = Math.min(...allBars.map(bar => bar.y));
|
|
495
|
-
const newMaxY = Math.max(...allBars.map(bar => bar.y + bar.height));
|
|
496
|
-
stackBounds = {
|
|
497
|
-
x,
|
|
498
|
-
y: newMinY,
|
|
499
|
-
width,
|
|
500
|
-
height: newMaxY - newMinY
|
|
501
|
-
};
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
// Reapply border radius logic only if we actually scaled
|
|
505
|
-
if (stackBounds.height < stackMinSize) {
|
|
506
|
-
allBars = applyBorderRadiusLogic(allBars);
|
|
507
|
-
}
|
|
70
|
+
}, [series, indexScale, valueScale, layout]);
|
|
71
|
+
const categoryAxis = layout === 'vertical' ? xAxis : yAxis;
|
|
72
|
+
const categoryData = categoryAxis != null && categoryAxis.data && Array.isArray(categoryAxis.data) && typeof categoryAxis.data[0] === 'number' ? categoryAxis.data : undefined;
|
|
73
|
+
const categoryValue = categoryData ? categoryData[categoryIndex] : categoryIndex;
|
|
74
|
+
const seriesData = useMemo(() => Object.fromEntries(series.map(s => {
|
|
75
|
+
var _getSeriesData;
|
|
76
|
+
return [s.id, (_getSeriesData = getSeriesData(s.id)) != null ? _getSeriesData : []];
|
|
77
|
+
})), [series, getSeriesData]);
|
|
78
|
+
const bars = useMemo(() => getBars({
|
|
79
|
+
series,
|
|
80
|
+
seriesData,
|
|
81
|
+
categoryIndex,
|
|
82
|
+
categoryValue,
|
|
83
|
+
indexPos,
|
|
84
|
+
thickness,
|
|
85
|
+
valueScale,
|
|
86
|
+
seriesGradients,
|
|
87
|
+
roundBaseline,
|
|
88
|
+
layout,
|
|
89
|
+
baseline,
|
|
90
|
+
stackGap,
|
|
91
|
+
barMinSize,
|
|
92
|
+
stackMinSize,
|
|
93
|
+
defaultFill: theme.color.fgPrimary,
|
|
94
|
+
borderRadius,
|
|
95
|
+
defaultFillOpacity,
|
|
96
|
+
defaultStroke,
|
|
97
|
+
defaultStrokeWidth,
|
|
98
|
+
defaultBarComponent
|
|
99
|
+
}), [series, seriesData, indexPos, thickness, categoryIndex, categoryValue, roundBaseline, baseline, stackGap, barMinSize, stackMinSize, valueScale, seriesGradients, theme.color.fgPrimary, layout, borderRadius, defaultFillOpacity, defaultStroke, defaultStrokeWidth, defaultBarComponent]);
|
|
100
|
+
const stackRect = useMemo(() => {
|
|
101
|
+
if (bars.length === 0) {
|
|
102
|
+
return {
|
|
103
|
+
x: layout === 'vertical' ? indexPos : baseline,
|
|
104
|
+
y: layout === 'vertical' ? baseline : indexPos,
|
|
105
|
+
width: layout === 'vertical' ? thickness : 0,
|
|
106
|
+
height: layout === 'vertical' ? 0 : thickness
|
|
107
|
+
};
|
|
508
108
|
}
|
|
109
|
+
const minX = Math.min(...bars.map(b => b.x));
|
|
110
|
+
const minY = Math.min(...bars.map(b => b.y));
|
|
111
|
+
const maxX = Math.max(...bars.map(b => b.x + b.width));
|
|
112
|
+
const maxY = Math.max(...bars.map(b => b.y + b.height));
|
|
509
113
|
return {
|
|
510
|
-
|
|
511
|
-
|
|
114
|
+
x: minX,
|
|
115
|
+
y: minY,
|
|
116
|
+
width: maxX - minX,
|
|
117
|
+
height: maxY - minY
|
|
512
118
|
};
|
|
513
|
-
}, [
|
|
514
|
-
const
|
|
515
|
-
|
|
119
|
+
}, [bars, baseline, indexPos, layout, thickness]);
|
|
120
|
+
const stackOrigin = useMemo(() => {
|
|
121
|
+
var _getStackOrigin;
|
|
122
|
+
return (_getStackOrigin = getStackOrigin(bars.map(b => b.origin), bars.map(b => {
|
|
123
|
+
var _b$minSize;
|
|
124
|
+
return (_b$minSize = b.minSize) != null ? _b$minSize : 0;
|
|
125
|
+
}))) != null ? _getStackOrigin : baseline;
|
|
126
|
+
}, [bars, baseline]);
|
|
516
127
|
const barElements = bars.map((bar, index) => /*#__PURE__*/_jsx(Bar, {
|
|
517
|
-
BarComponent:
|
|
518
|
-
borderRadius: borderRadius,
|
|
519
|
-
dataX: dataX,
|
|
128
|
+
BarComponent: bar.BarComponent,
|
|
129
|
+
borderRadius: bar.borderRadius,
|
|
130
|
+
dataX: bar.dataX,
|
|
520
131
|
dataY: bar.dataY,
|
|
521
132
|
fill: bar.fill,
|
|
522
|
-
fillOpacity:
|
|
133
|
+
fillOpacity: bar.fillOpacity,
|
|
523
134
|
height: bar.height,
|
|
524
|
-
|
|
135
|
+
minSize: bar.minSize,
|
|
136
|
+
origin: bar.origin,
|
|
525
137
|
roundBottom: bar.roundBottom,
|
|
526
138
|
roundTop: bar.roundTop,
|
|
527
|
-
|
|
528
|
-
|
|
139
|
+
seriesId: bar.seriesId,
|
|
140
|
+
stroke: bar.stroke,
|
|
141
|
+
strokeWidth: bar.strokeWidth,
|
|
529
142
|
transition: transition,
|
|
143
|
+
transitions: transitions,
|
|
530
144
|
width: bar.width,
|
|
531
145
|
x: bar.x,
|
|
532
146
|
y: bar.y
|
|
533
147
|
}, bar.seriesId + "-" + categoryIndex + "-" + index));
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
const
|
|
537
|
-
const
|
|
148
|
+
const edge = layout === 'vertical' ? stackRect.y : stackRect.x;
|
|
149
|
+
const size = layout === 'vertical' ? stackRect.height : stackRect.width;
|
|
150
|
+
const stackRoundLower = roundBaseline || Math.abs(edge - baseline) >= EPSILON;
|
|
151
|
+
const stackRoundHigher = roundBaseline || Math.abs(edge + size - baseline) >= EPSILON;
|
|
152
|
+
const stackRoundTop = layout === 'vertical' ? stackRoundLower : stackRoundHigher;
|
|
153
|
+
const stackRoundBottom = layout === 'vertical' ? stackRoundHigher : stackRoundLower;
|
|
538
154
|
return /*#__PURE__*/_jsx(BarStackComponent, {
|
|
539
155
|
borderRadius: borderRadius,
|
|
540
156
|
categoryIndex: categoryIndex,
|
|
541
157
|
height: stackRect.height,
|
|
158
|
+
origin: stackOrigin,
|
|
542
159
|
roundBottom: stackRoundBottom,
|
|
543
160
|
roundTop: stackRoundTop,
|
|
544
161
|
transition: transition,
|
|
162
|
+
transitions: transitions,
|
|
545
163
|
width: stackRect.width,
|
|
546
164
|
x: stackRect.x,
|
|
547
165
|
y: stackRect.y,
|
|
548
|
-
yOrigin: baseline,
|
|
549
166
|
children: barElements
|
|
550
167
|
});
|
|
551
168
|
});
|