@sybilion/uilib 1.7.2 → 1.8.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/dist/agent-glossary/content-prompt.generated.ts +2 -0
- package/dist/agent-glossary/content.generated.ts +2 -0
- package/dist/agent-glossary/content.md +2 -0
- package/dist/agent-glossary/content.prompt.md +2 -0
- package/dist/agent-glossary/workspace.generated.ts +1 -0
- package/dist/agent-glossary/workspace.index.md +1 -0
- package/dist/agent-glossary/workspace.md +1 -0
- package/dist/esm/src/components/ui/AnalysesSelector/AnalysesSelector.js +37 -20
- package/dist/esm/src/components/ui/Chart/Chart.styl.js +2 -2
- package/dist/esm/src/components/ui/Chart/components/BaseChartWrapper.js +46 -51
- package/dist/esm/src/components/ui/Chart/components/ChartContainer.js +2 -2
- package/dist/esm/src/components/ui/Chart/components/ChartGrid.js +2 -2
- package/dist/esm/src/components/ui/Chart/components/ChartLegendContent.js +2 -2
- package/dist/esm/src/components/ui/Chart/components/ChartLegendItem.js +2 -2
- package/dist/esm/src/components/ui/Chart/components/ChartTooltipContent.js +2 -2
- package/dist/esm/src/components/ui/Chart/components/ChartTooltipItem.js +3 -3
- package/dist/esm/src/components/ui/Chart/components/CustomChartLegend/CustomChartLegend.js +8 -3
- package/dist/esm/src/components/ui/Chart/components/LegendSvg/LegendSvg.js +1 -1
- package/dist/esm/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.helpers.js +17 -6
- package/dist/esm/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.js +40 -10
- package/dist/esm/src/components/ui/ChartAreaInteractive/ChartLines.js +43 -13
- package/dist/esm/src/components/ui/ChartAreaInteractive/chartHistories.js +44 -0
- package/dist/esm/src/components/ui/ChartAreaInteractive/overlays/useChartYRange.js +32 -7
- package/dist/esm/src/components/ui/Page/PageTabs/PageTabs.styl.js +2 -2
- package/dist/esm/src/components/ui/Page/PageXScroll/PageXScroll.js +50 -15
- package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.js +212 -0
- package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.js +123 -0
- package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.styl.js +7 -0
- package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.js +29 -0
- package/dist/esm/src/index.js +3 -1
- package/dist/esm/src/utils/chartConnectionPoint.js +31 -16
- package/dist/esm/types/src/components/ui/AnalysesSelector/AnalysesSelector.d.ts +1 -1
- package/dist/esm/types/src/components/ui/AnalysesSelector/AnalysesSelector.types.d.ts +12 -0
- package/dist/esm/types/src/components/ui/AnalysesSelector/index.d.ts +1 -1
- package/dist/esm/types/src/components/ui/Chart/components/BaseChartWrapper.d.ts +8 -0
- package/dist/esm/types/src/components/ui/Chart/components/CustomChartLegend/CustomChartLegend.d.ts +8 -1
- package/dist/esm/types/src/components/ui/Chart/components/LegendSvg/LegendSvg.d.ts +2 -0
- package/dist/esm/types/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.d.ts +1 -1
- package/dist/esm/types/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.helpers.d.ts +5 -0
- package/dist/esm/types/src/components/ui/ChartAreaInteractive/ChartLines.d.ts +19 -1
- package/dist/esm/types/src/components/ui/ChartAreaInteractive/chartHistories.d.ts +40 -0
- package/dist/esm/types/src/components/ui/ChartAreaInteractive/index.d.ts +1 -0
- package/dist/esm/types/src/components/ui/ChartAreaInteractive/overlays/useChartYRange.d.ts +6 -1
- package/dist/esm/types/src/components/ui/Page/PageXScroll/PageXScroll.d.ts +1 -1
- package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.d.ts +3 -0
- package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.d.ts +65 -0
- package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.d.ts +20 -0
- package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.types.d.ts +85 -0
- package/dist/esm/types/src/components/ui/StackedAreaChart/index.d.ts +2 -0
- package/dist/esm/types/src/docs/pages/StackedAreaChartPage.d.ts +1 -0
- package/dist/esm/types/src/index.d.ts +1 -0
- package/dist/esm/types/src/utils/chartConnectionPoint.d.ts +11 -3
- package/dist/esm/types/tests/chartHistories.test.d.ts +1 -0
- package/dist/esm/types/tests/pageXScroll.test.d.ts +1 -0
- package/dist/esm/types/tests/stackedAreaChart.test.d.ts +1 -0
- package/package.json +1 -1
- package/src/components/ui/AnalysesSelector/AnalysesSelector.tsx +50 -22
- package/src/components/ui/AnalysesSelector/AnalysesSelector.types.ts +13 -0
- package/src/components/ui/AnalysesSelector/index.ts +1 -0
- package/src/components/ui/Chart/components/BaseChartWrapper.tsx +71 -52
- package/src/components/ui/Chart/components/CustomChartLegend/CustomChartLegend.tsx +21 -1
- package/src/components/ui/Chart/components/LegendSvg/LegendSvg.tsx +3 -1
- package/src/components/ui/ChartAreaInteractive/AGENT.md +2 -0
- package/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.helpers.ts +27 -5
- package/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.tsx +64 -10
- package/src/components/ui/ChartAreaInteractive/ChartLines.tsx +86 -30
- package/src/components/ui/ChartAreaInteractive/chartHistories.ts +63 -0
- package/src/components/ui/ChartAreaInteractive/index.ts +1 -0
- package/src/components/ui/ChartAreaInteractive/overlays/useChartYRange.ts +52 -6
- package/src/components/ui/Page/AGENT.md +2 -0
- package/src/components/ui/Page/PageTabs/PageTabs.styl +8 -0
- package/src/components/ui/Page/PageXScroll/PageXScroll.tsx +57 -13
- package/src/components/ui/StackedAreaChart/AGENT.md +26 -0
- package/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.ts +268 -0
- package/src/components/ui/StackedAreaChart/StackedAreaChart.styl +32 -0
- package/src/components/ui/StackedAreaChart/StackedAreaChart.styl.d.ts +13 -0
- package/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.tsx +82 -0
- package/src/components/ui/StackedAreaChart/StackedAreaChart.tsx +416 -0
- package/src/components/ui/StackedAreaChart/StackedAreaChart.types.ts +97 -0
- package/src/components/ui/StackedAreaChart/index.ts +2 -0
- package/src/docs/pages/ChartAreaInteractivePage.tsx +67 -0
- package/src/docs/pages/StackedAreaChartPage.tsx +141 -0
- package/src/docs/registry.ts +6 -0
- package/src/index.ts +1 -0
- package/src/utils/chartConnectionPoint.ts +41 -18
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
import cn from 'classnames';
|
|
2
|
+
import { ReactNode, forwardRef, useMemo } from 'react';
|
|
3
|
+
|
|
4
|
+
import type { ChartConfig } from '#uilib/components/ui/Chart';
|
|
5
|
+
import { ChartContainer, ChartTooltip } from '#uilib/components/ui/Chart';
|
|
6
|
+
import ChartS from '#uilib/components/ui/Chart/Chart.styl';
|
|
7
|
+
import { getChartSeriesColor } from '#uilib/components/ui/Chart/chartSeriesPalette';
|
|
8
|
+
import { useLegendState } from '#uilib/components/ui/Chart/hooks/useLegendState';
|
|
9
|
+
import {
|
|
10
|
+
Area,
|
|
11
|
+
Bar,
|
|
12
|
+
CartesianGrid,
|
|
13
|
+
ComposedChart,
|
|
14
|
+
Line,
|
|
15
|
+
ReferenceArea,
|
|
16
|
+
XAxis,
|
|
17
|
+
YAxis,
|
|
18
|
+
} from 'recharts';
|
|
19
|
+
|
|
20
|
+
import {
|
|
21
|
+
MARGIN_KEY,
|
|
22
|
+
chartTop,
|
|
23
|
+
compareRowZones,
|
|
24
|
+
compareZones,
|
|
25
|
+
yearTicks as findYearTicks,
|
|
26
|
+
stackRows,
|
|
27
|
+
stackWeights,
|
|
28
|
+
valueAxis,
|
|
29
|
+
yearLabel,
|
|
30
|
+
} from './StackedAreaChart.helpers';
|
|
31
|
+
import S from './StackedAreaChart.styl';
|
|
32
|
+
import { StackedTooltip, type TooltipRow } from './StackedAreaChart.tooltip';
|
|
33
|
+
import type {
|
|
34
|
+
StackedAreaChartLayer,
|
|
35
|
+
StackedAreaChartOverlay,
|
|
36
|
+
StackedAreaChartProps,
|
|
37
|
+
} from './StackedAreaChart.types';
|
|
38
|
+
|
|
39
|
+
const STACK_ID = 'stack';
|
|
40
|
+
const DEFAULT_BARS_BELOW = 3;
|
|
41
|
+
const DEFAULT_MARGIN_COLOR = 'var(--sb-slate-300)';
|
|
42
|
+
const DEFAULT_OVERLAY_COLOR = 'var(--foreground)';
|
|
43
|
+
const ZONE_FILL = {
|
|
44
|
+
above: 'var(--sb-green-100)',
|
|
45
|
+
below: 'var(--sb-red-100)',
|
|
46
|
+
} as const;
|
|
47
|
+
const DEFAULT_ZONE_LABELS = { above: 'above', below: 'below' };
|
|
48
|
+
const NO_OVERLAYS: StackedAreaChartOverlay[] = [];
|
|
49
|
+
/** Joins keys into one memo dependency; a key cannot hold this character. */
|
|
50
|
+
const KEY_SEP = '\u0000';
|
|
51
|
+
/** Each key is prefixed with the separator, so an empty key survives the trip. */
|
|
52
|
+
const joinKeys = (keys: readonly string[]) =>
|
|
53
|
+
keys.map(key => KEY_SEP + key).join('');
|
|
54
|
+
const splitKeys = (joined: string) => joined.split(KEY_SEP).slice(1);
|
|
55
|
+
/**
|
|
56
|
+
* Recharts 3 draws by layer: grid -100, area 100, bar 300, line 400. Zones go
|
|
57
|
+
* over the grid and under the stack.
|
|
58
|
+
*/
|
|
59
|
+
const ZONE_Z_INDEX = 50;
|
|
60
|
+
|
|
61
|
+
const AXIS_LINE = { stroke: 'var(--border)', strokeWidth: 1 };
|
|
62
|
+
|
|
63
|
+
const layerColor = (layer: StackedAreaChartLayer, index: number) =>
|
|
64
|
+
layer.color ?? getChartSeriesColor(index);
|
|
65
|
+
const overlayColor = (overlay: StackedAreaChartOverlay) =>
|
|
66
|
+
overlay.color ?? DEFAULT_OVERLAY_COLOR;
|
|
67
|
+
|
|
68
|
+
function LegendEntry({
|
|
69
|
+
swatch,
|
|
70
|
+
label,
|
|
71
|
+
extra,
|
|
72
|
+
dimmed,
|
|
73
|
+
onClick,
|
|
74
|
+
}: {
|
|
75
|
+
swatch: ReactNode;
|
|
76
|
+
label: ReactNode;
|
|
77
|
+
extra?: ReactNode;
|
|
78
|
+
dimmed?: boolean;
|
|
79
|
+
onClick?: () => void;
|
|
80
|
+
}) {
|
|
81
|
+
const content = (
|
|
82
|
+
<>
|
|
83
|
+
{swatch}
|
|
84
|
+
{label}
|
|
85
|
+
{extra != null && <span className={S.legendExtra}>{extra}</span>}
|
|
86
|
+
</>
|
|
87
|
+
);
|
|
88
|
+
return onClick ? (
|
|
89
|
+
<button
|
|
90
|
+
type="button"
|
|
91
|
+
className={cn(ChartS.legendItem, S.legendButton, dimmed && S.dimmed)}
|
|
92
|
+
aria-pressed={!dimmed}
|
|
93
|
+
onClick={onClick}
|
|
94
|
+
>
|
|
95
|
+
{content}
|
|
96
|
+
</button>
|
|
97
|
+
) : (
|
|
98
|
+
<span className={ChartS.legendItem}>{content}</span>
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const StackedAreaChart = forwardRef<HTMLDivElement, StackedAreaChartProps>(
|
|
103
|
+
(
|
|
104
|
+
{
|
|
105
|
+
className,
|
|
106
|
+
data,
|
|
107
|
+
xKey,
|
|
108
|
+
layers,
|
|
109
|
+
overlays = NO_OVERLAYS,
|
|
110
|
+
margin,
|
|
111
|
+
showWeights = true,
|
|
112
|
+
yearTicks = false,
|
|
113
|
+
barsBelow = DEFAULT_BARS_BELOW,
|
|
114
|
+
zoneLabels = DEFAULT_ZONE_LABELS,
|
|
115
|
+
hiddenKeys,
|
|
116
|
+
onToggleKey,
|
|
117
|
+
showLegend = true,
|
|
118
|
+
showTooltip = true,
|
|
119
|
+
showGrid = true,
|
|
120
|
+
valueFormatter,
|
|
121
|
+
xTickFormatter,
|
|
122
|
+
tooltipLabelFormatter,
|
|
123
|
+
height,
|
|
124
|
+
...props
|
|
125
|
+
},
|
|
126
|
+
ref,
|
|
127
|
+
) => {
|
|
128
|
+
const legend = useLegendState();
|
|
129
|
+
const hidden: ReadonlySet<string> = hiddenKeys ?? legend.hiddenSeries;
|
|
130
|
+
// With hiddenKeys and no onToggleKey nothing can change visibility, so
|
|
131
|
+
// the legend entries are not buttons.
|
|
132
|
+
const toggle =
|
|
133
|
+
hiddenKeys && !onToggleKey
|
|
134
|
+
? undefined
|
|
135
|
+
: (key: string) => {
|
|
136
|
+
onToggleKey?.(key);
|
|
137
|
+
if (!hiddenKeys) legend.toggleSeries(key);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// Keyed on the joined keys, so layers passed inline do not redo the maths.
|
|
141
|
+
const layerKeysJoined = joinKeys(layers.map(l => l.key));
|
|
142
|
+
const visibleKeysJoined = joinKeys(
|
|
143
|
+
layers.filter(l => !hidden.has(l.key)).map(l => l.key),
|
|
144
|
+
);
|
|
145
|
+
const layerKeys = useMemo(
|
|
146
|
+
() => splitKeys(layerKeysJoined),
|
|
147
|
+
[layerKeysJoined],
|
|
148
|
+
);
|
|
149
|
+
// Recharts leaves hidden layers out of the stack, so the margin and the
|
|
150
|
+
// zones follow the stack that is drawn.
|
|
151
|
+
const visibleKeys = useMemo(
|
|
152
|
+
() => splitKeys(visibleKeysJoined),
|
|
153
|
+
[visibleKeysJoined],
|
|
154
|
+
);
|
|
155
|
+
const marginPct = margin?.pct ?? null;
|
|
156
|
+
const marginOn = marginPct != null && marginPct > 0;
|
|
157
|
+
const marginLabel = margin?.label ?? `margin ${marginPct ?? 0}%`;
|
|
158
|
+
const marginColor = margin?.color ?? DEFAULT_MARGIN_COLOR;
|
|
159
|
+
|
|
160
|
+
const rows = useMemo(
|
|
161
|
+
() => stackRows(data, layerKeys, marginPct, visibleKeys),
|
|
162
|
+
[data, layerKeys, marginPct, visibleKeys],
|
|
163
|
+
);
|
|
164
|
+
// Shares of the layers that are drawn, from the data: typing a margin
|
|
165
|
+
// does not change them.
|
|
166
|
+
const weights = useMemo(
|
|
167
|
+
() => (showWeights ? stackWeights(data, visibleKeys) : undefined),
|
|
168
|
+
[showWeights, data, visibleKeys],
|
|
169
|
+
);
|
|
170
|
+
const asBars = rows.length < barsBelow;
|
|
171
|
+
const compareKey = overlays.find(o => o.compare)?.key;
|
|
172
|
+
const zonesOn = compareKey != null && !hidden.has(compareKey);
|
|
173
|
+
const zones = useMemo(() => {
|
|
174
|
+
if (!zonesOn || compareKey == null) return [];
|
|
175
|
+
const find = asBars ? compareRowZones : compareZones;
|
|
176
|
+
return find(rows, xKey, visibleKeys, compareKey);
|
|
177
|
+
}, [zonesOn, compareKey, asBars, rows, xKey, visibleKeys]);
|
|
178
|
+
const zoneKinds = (['above', 'below'] as const).filter(kind =>
|
|
179
|
+
zones.some(zone => zone.kind === kind),
|
|
180
|
+
);
|
|
181
|
+
const visibleLinesJoined = joinKeys(
|
|
182
|
+
overlays.filter(o => !hidden.has(o.key)).map(o => o.key),
|
|
183
|
+
);
|
|
184
|
+
const yAxis = useMemo(
|
|
185
|
+
() =>
|
|
186
|
+
valueAxis(chartTop(rows, visibleKeys, splitKeys(visibleLinesJoined))),
|
|
187
|
+
[rows, visibleKeys, visibleLinesJoined],
|
|
188
|
+
);
|
|
189
|
+
const ticks = useMemo(
|
|
190
|
+
() => (yearTicks ? findYearTicks(data, xKey) : undefined),
|
|
191
|
+
[yearTicks, data, xKey],
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
const config = useMemo(() => {
|
|
195
|
+
const out: ChartConfig = {};
|
|
196
|
+
layers.forEach((layer, index) => {
|
|
197
|
+
out[layer.key] = {
|
|
198
|
+
label: layer.label ?? layer.key,
|
|
199
|
+
color: layerColor(layer, index),
|
|
200
|
+
};
|
|
201
|
+
});
|
|
202
|
+
out[MARGIN_KEY] = { label: marginLabel, color: marginColor };
|
|
203
|
+
overlays.forEach(overlay => {
|
|
204
|
+
out[overlay.key] = {
|
|
205
|
+
label: overlay.label ?? overlay.key,
|
|
206
|
+
color: overlayColor(overlay),
|
|
207
|
+
};
|
|
208
|
+
});
|
|
209
|
+
return out;
|
|
210
|
+
}, [layers, overlays, marginLabel, marginColor]);
|
|
211
|
+
|
|
212
|
+
const stackProps = {
|
|
213
|
+
stackId: STACK_ID,
|
|
214
|
+
stroke: 'var(--background)',
|
|
215
|
+
strokeWidth: 1,
|
|
216
|
+
isAnimationActive: false,
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
return (
|
|
220
|
+
<div ref={ref} className={cn(S.root, className)} {...props}>
|
|
221
|
+
<ChartContainer
|
|
222
|
+
config={config}
|
|
223
|
+
style={height ? { height: `${height}px` } : undefined}
|
|
224
|
+
>
|
|
225
|
+
<ComposedChart data={rows} accessibilityLayer>
|
|
226
|
+
{showGrid && (
|
|
227
|
+
<CartesianGrid
|
|
228
|
+
stroke="var(--border)"
|
|
229
|
+
strokeWidth={0.6}
|
|
230
|
+
vertical={false}
|
|
231
|
+
/>
|
|
232
|
+
)}
|
|
233
|
+
<XAxis
|
|
234
|
+
dataKey={xKey}
|
|
235
|
+
// A point scale runs the areas edge to edge and starts and ends
|
|
236
|
+
// each zone on its rows; bars keep a band each.
|
|
237
|
+
scale={asBars ? 'auto' : 'point'}
|
|
238
|
+
ticks={ticks}
|
|
239
|
+
tickFormatter={ticks ? yearLabel : xTickFormatter}
|
|
240
|
+
minTickGap={ticks ? 8 : 24}
|
|
241
|
+
tickLine={false}
|
|
242
|
+
axisLine={AXIS_LINE}
|
|
243
|
+
/>
|
|
244
|
+
<YAxis
|
|
245
|
+
// Our own steps: automatic rounding can double the axis when the
|
|
246
|
+
// margin nudges the top past a round number.
|
|
247
|
+
domain={[0, yAxis.max]}
|
|
248
|
+
ticks={yAxis.ticks}
|
|
249
|
+
allowDataOverflow
|
|
250
|
+
tickFormatter={valueFormatter}
|
|
251
|
+
width={56}
|
|
252
|
+
tickLine={false}
|
|
253
|
+
axisLine={AXIS_LINE}
|
|
254
|
+
/>
|
|
255
|
+
{showTooltip && (
|
|
256
|
+
<ChartTooltip
|
|
257
|
+
content={tooltip => (
|
|
258
|
+
<StackedTooltip
|
|
259
|
+
active={tooltip.active}
|
|
260
|
+
payload={tooltip.payload as ReadonlyArray<TooltipRow>}
|
|
261
|
+
label={tooltip.label}
|
|
262
|
+
config={config}
|
|
263
|
+
valueFormatter={valueFormatter}
|
|
264
|
+
labelFormatter={tooltipLabelFormatter}
|
|
265
|
+
/>
|
|
266
|
+
)}
|
|
267
|
+
/>
|
|
268
|
+
)}
|
|
269
|
+
{zones.map(zone => (
|
|
270
|
+
<ReferenceArea
|
|
271
|
+
key={`${zone.kind}-${String(zone.x1)}`}
|
|
272
|
+
x1={zone.x1}
|
|
273
|
+
x2={zone.x2}
|
|
274
|
+
fill={ZONE_FILL[zone.kind]}
|
|
275
|
+
fillOpacity={1}
|
|
276
|
+
stroke="none"
|
|
277
|
+
ifOverflow="hidden"
|
|
278
|
+
zIndex={ZONE_Z_INDEX}
|
|
279
|
+
/>
|
|
280
|
+
))}
|
|
281
|
+
{layers.map((layer, index) =>
|
|
282
|
+
asBars ? (
|
|
283
|
+
<Bar
|
|
284
|
+
key={layer.key}
|
|
285
|
+
dataKey={layer.key}
|
|
286
|
+
name={layer.key}
|
|
287
|
+
fill={layerColor(layer, index)}
|
|
288
|
+
maxBarSize={96}
|
|
289
|
+
hide={hidden.has(layer.key)}
|
|
290
|
+
{...stackProps}
|
|
291
|
+
/>
|
|
292
|
+
) : (
|
|
293
|
+
<Area
|
|
294
|
+
key={layer.key}
|
|
295
|
+
type="monotone"
|
|
296
|
+
dataKey={layer.key}
|
|
297
|
+
name={layer.key}
|
|
298
|
+
fill={layerColor(layer, index)}
|
|
299
|
+
fillOpacity={1}
|
|
300
|
+
hide={hidden.has(layer.key)}
|
|
301
|
+
{...stackProps}
|
|
302
|
+
/>
|
|
303
|
+
),
|
|
304
|
+
)}
|
|
305
|
+
{margin &&
|
|
306
|
+
(asBars ? (
|
|
307
|
+
<Bar
|
|
308
|
+
key={MARGIN_KEY}
|
|
309
|
+
dataKey={MARGIN_KEY}
|
|
310
|
+
name={MARGIN_KEY}
|
|
311
|
+
fill={marginColor}
|
|
312
|
+
maxBarSize={96}
|
|
313
|
+
hide={!marginOn}
|
|
314
|
+
{...stackProps}
|
|
315
|
+
/>
|
|
316
|
+
) : (
|
|
317
|
+
<Area
|
|
318
|
+
key={MARGIN_KEY}
|
|
319
|
+
type="monotone"
|
|
320
|
+
dataKey={MARGIN_KEY}
|
|
321
|
+
name={MARGIN_KEY}
|
|
322
|
+
fill={marginColor}
|
|
323
|
+
fillOpacity={1}
|
|
324
|
+
hide={!marginOn}
|
|
325
|
+
{...stackProps}
|
|
326
|
+
/>
|
|
327
|
+
))}
|
|
328
|
+
{overlays.map(overlay => {
|
|
329
|
+
const color = overlayColor(overlay);
|
|
330
|
+
return (
|
|
331
|
+
<Line
|
|
332
|
+
key={overlay.key}
|
|
333
|
+
type="linear"
|
|
334
|
+
dataKey={overlay.key}
|
|
335
|
+
name={overlay.key}
|
|
336
|
+
stroke={color}
|
|
337
|
+
strokeWidth={1.75}
|
|
338
|
+
dot={asBars ? { r: 4, fill: color, stroke: color } : false}
|
|
339
|
+
activeDot={false}
|
|
340
|
+
connectNulls
|
|
341
|
+
isAnimationActive={false}
|
|
342
|
+
hide={hidden.has(overlay.key)}
|
|
343
|
+
/>
|
|
344
|
+
);
|
|
345
|
+
})}
|
|
346
|
+
</ComposedChart>
|
|
347
|
+
</ChartContainer>
|
|
348
|
+
|
|
349
|
+
{showLegend && (
|
|
350
|
+
<div className={cn(ChartS.legendContainer, S.legend)}>
|
|
351
|
+
{layers.map((layer, index) => (
|
|
352
|
+
<LegendEntry
|
|
353
|
+
key={layer.key}
|
|
354
|
+
swatch={
|
|
355
|
+
<span
|
|
356
|
+
className={ChartS.legendIndicator}
|
|
357
|
+
style={{ backgroundColor: layerColor(layer, index) }}
|
|
358
|
+
/>
|
|
359
|
+
}
|
|
360
|
+
label={layer.label ?? layer.key}
|
|
361
|
+
extra={
|
|
362
|
+
weights && !hidden.has(layer.key)
|
|
363
|
+
? `${weights[layer.key] ?? 0}%`
|
|
364
|
+
: undefined
|
|
365
|
+
}
|
|
366
|
+
dimmed={hidden.has(layer.key)}
|
|
367
|
+
onClick={toggle && (() => toggle(layer.key))}
|
|
368
|
+
/>
|
|
369
|
+
))}
|
|
370
|
+
{marginOn && (
|
|
371
|
+
<LegendEntry
|
|
372
|
+
swatch={
|
|
373
|
+
<span
|
|
374
|
+
className={ChartS.legendIndicator}
|
|
375
|
+
style={{ backgroundColor: marginColor }}
|
|
376
|
+
/>
|
|
377
|
+
}
|
|
378
|
+
label={marginLabel}
|
|
379
|
+
/>
|
|
380
|
+
)}
|
|
381
|
+
{overlays.map(overlay => (
|
|
382
|
+
<LegendEntry
|
|
383
|
+
key={overlay.key}
|
|
384
|
+
swatch={
|
|
385
|
+
<span
|
|
386
|
+
className={S.legendLine}
|
|
387
|
+
style={{ backgroundColor: overlayColor(overlay) }}
|
|
388
|
+
/>
|
|
389
|
+
}
|
|
390
|
+
label={overlay.label ?? overlay.key}
|
|
391
|
+
dimmed={hidden.has(overlay.key)}
|
|
392
|
+
onClick={toggle && (() => toggle(overlay.key))}
|
|
393
|
+
/>
|
|
394
|
+
))}
|
|
395
|
+
{zoneKinds.map(kind => (
|
|
396
|
+
<LegendEntry
|
|
397
|
+
key={kind}
|
|
398
|
+
swatch={
|
|
399
|
+
<span
|
|
400
|
+
className={cn(ChartS.legendIndicator, S.legendZone)}
|
|
401
|
+
style={{ backgroundColor: ZONE_FILL[kind] }}
|
|
402
|
+
/>
|
|
403
|
+
}
|
|
404
|
+
label={zoneLabels[kind]}
|
|
405
|
+
/>
|
|
406
|
+
))}
|
|
407
|
+
</div>
|
|
408
|
+
)}
|
|
409
|
+
</div>
|
|
410
|
+
);
|
|
411
|
+
},
|
|
412
|
+
);
|
|
413
|
+
|
|
414
|
+
StackedAreaChart.displayName = 'StackedAreaChart';
|
|
415
|
+
|
|
416
|
+
export { StackedAreaChart };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { HTMLAttributes, ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
export type StackedAreaChartDatum = Record<
|
|
4
|
+
string,
|
|
5
|
+
string | number | null | undefined
|
|
6
|
+
>;
|
|
7
|
+
|
|
8
|
+
export interface StackedAreaChartLayer {
|
|
9
|
+
/** Data key of the layer in each datum. A value that is not a number stacks as 0. */
|
|
10
|
+
key: string;
|
|
11
|
+
/** Legend and tooltip label; falls back to the key. */
|
|
12
|
+
label?: ReactNode;
|
|
13
|
+
/** CSS colour; falls back to the built-in series palette. */
|
|
14
|
+
color?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface StackedAreaChartOverlay {
|
|
18
|
+
/** Data key of the line in each datum. Gaps are bridged. */
|
|
19
|
+
key: string;
|
|
20
|
+
/** Legend and tooltip label; falls back to the key. */
|
|
21
|
+
label?: ReactNode;
|
|
22
|
+
/** CSS colour; falls back to the foreground colour. */
|
|
23
|
+
color?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Shade the x ranges where this line is above or below the stacked total
|
|
26
|
+
* (the visible layers plus the margin), changing at rows. Hidden with the
|
|
27
|
+
* line. At most one overlay should set it; the first one that does is used.
|
|
28
|
+
*/
|
|
29
|
+
compare?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface StackedAreaChartMargin {
|
|
33
|
+
/**
|
|
34
|
+
* Margin on top of the layers, in percent of the layers' total at each x.
|
|
35
|
+
* null or 0 hides the margin layer; it stays mounted, so changing it as the
|
|
36
|
+
* user types does not redraw the chart from scratch.
|
|
37
|
+
*/
|
|
38
|
+
pct: number | null;
|
|
39
|
+
/** Legend and tooltip label; defaults to `margin <pct>%`. */
|
|
40
|
+
label?: ReactNode;
|
|
41
|
+
/** CSS colour of the margin layer. */
|
|
42
|
+
color?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface StackedAreaChartZoneLabels {
|
|
46
|
+
above: ReactNode;
|
|
47
|
+
below: ReactNode;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface StackedAreaChartProps extends Omit<
|
|
51
|
+
HTMLAttributes<HTMLDivElement>,
|
|
52
|
+
'children'
|
|
53
|
+
> {
|
|
54
|
+
/** Rows in x order. */
|
|
55
|
+
data: StackedAreaChartDatum[];
|
|
56
|
+
/** Data key holding the x value (a date string such as `2026-01-01`, or a label). */
|
|
57
|
+
xKey: string;
|
|
58
|
+
/** Stacked layers, bottom first. */
|
|
59
|
+
layers: StackedAreaChartLayer[];
|
|
60
|
+
/** Lines drawn over the stack (e.g. prices paid). */
|
|
61
|
+
overlays?: StackedAreaChartOverlay[];
|
|
62
|
+
/** An extra layer on top of the stack, a percentage of the layers' total. */
|
|
63
|
+
margin?: StackedAreaChartMargin;
|
|
64
|
+
/**
|
|
65
|
+
* Show each visible layer's share of the total in the legend, from the last
|
|
66
|
+
* row where every visible layer has a value (or, when none has, the last
|
|
67
|
+
* row with a total).
|
|
68
|
+
*/
|
|
69
|
+
showWeights?: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Label the x axis by year, one tick at the first row of each year, when
|
|
72
|
+
* the x values are dates (`YYYY…`) spanning at least three years.
|
|
73
|
+
*/
|
|
74
|
+
yearTicks?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* With fewer rows than this, the stack draws as bars (an area needs a
|
|
77
|
+
* few points to show anything). Default 3.
|
|
78
|
+
*/
|
|
79
|
+
barsBelow?: number;
|
|
80
|
+
/** Legend labels of the compare zones. */
|
|
81
|
+
zoneLabels?: StackedAreaChartZoneLabels;
|
|
82
|
+
/** Keys of hidden layers and overlays. With it, the host owns visibility. */
|
|
83
|
+
hiddenKeys?: ReadonlySet<string>;
|
|
84
|
+
/** Called with a layer or overlay key when its legend entry is clicked. */
|
|
85
|
+
onToggleKey?: (key: string) => void;
|
|
86
|
+
showLegend?: boolean;
|
|
87
|
+
showTooltip?: boolean;
|
|
88
|
+
showGrid?: boolean;
|
|
89
|
+
/** Formats numeric y-axis ticks and tooltip values. */
|
|
90
|
+
valueFormatter?: (value: number) => string;
|
|
91
|
+
/** Formats x-axis ticks (ignored for year ticks, which show the year). */
|
|
92
|
+
xTickFormatter?: (value: string | number) => string;
|
|
93
|
+
/** Formats the tooltip's x label. */
|
|
94
|
+
tooltipLabelFormatter?: (value: string | number) => string;
|
|
95
|
+
/** Fixed chart height in px; defaults to a 16:9 responsive box. */
|
|
96
|
+
height?: number;
|
|
97
|
+
}
|
|
@@ -7,6 +7,7 @@ import type {
|
|
|
7
7
|
OverlayMode,
|
|
8
8
|
} from '#uilib/components/ui/ChartAreaInteractive/ChartAreaInteractive.types';
|
|
9
9
|
import { ForecastItemData } from '#uilib/components/ui/ChartAreaInteractive/ChartLines';
|
|
10
|
+
import type { HistoryItemData } from '#uilib/components/ui/ChartAreaInteractive/chartHistories';
|
|
10
11
|
import { Label } from '#uilib/components/ui/Label';
|
|
11
12
|
import { PageContentSection } from '#uilib/components/ui/Page';
|
|
12
13
|
import { Switch } from '#uilib/components/ui/Switch';
|
|
@@ -95,6 +96,62 @@ const DEMO_FORECAST_ITEMS: ForecastItemData[] = [
|
|
|
95
96
|
|
|
96
97
|
type DemoMode = 'none' | OverlayMode;
|
|
97
98
|
|
|
99
|
+
// Two products, each with its own history; each forecast `continues` its own.
|
|
100
|
+
const MULTI_HISTORIES: HistoryItemData[] = [
|
|
101
|
+
{ id: 'a', name: 'Product A', color: '#216BF7' },
|
|
102
|
+
{ id: 'b', name: 'Product B', color: '#059643' },
|
|
103
|
+
];
|
|
104
|
+
|
|
105
|
+
const MULTI_FORECASTS: ForecastItemData[] = [
|
|
106
|
+
{ id: 11, name: 'Product A forecast', color: '#216BF7', continues: 'a' },
|
|
107
|
+
{ id: 12, name: 'Product B forecast', color: '#059643', continues: 'b' },
|
|
108
|
+
];
|
|
109
|
+
|
|
110
|
+
const MULTI_CHART: ChartDataPoint[] = [
|
|
111
|
+
{ date: '2025-10-01', historical_a: 10, historical_b: 14 },
|
|
112
|
+
{ date: '2025-11-01', historical_a: 10.4, historical_b: 13.6 },
|
|
113
|
+
{ date: '2025-12-01', historical_a: 10.9, historical_b: 13.1 },
|
|
114
|
+
{ date: '2026-01-01', historical_a: 11.2, historical_b: 12.8 },
|
|
115
|
+
{ date: '2026-02-01', historical_a: 11.0, historical_b: 12.9 },
|
|
116
|
+
{ date: '2026-03-01', historical_a: 11.6, historical_b: 12.4 },
|
|
117
|
+
{ date: '2026-04-01', forecast_11: 11.9, forecast_12: 12.2 },
|
|
118
|
+
{ date: '2026-05-01', forecast_11: 12.3, forecast_12: 12.0 },
|
|
119
|
+
{ date: '2026-06-01', forecast_11: 12.6, forecast_12: 11.7 },
|
|
120
|
+
];
|
|
121
|
+
|
|
122
|
+
function MultiHistoryDemo({ isDarkTheme }: { isDarkTheme: boolean }) {
|
|
123
|
+
const [timeRange, setTimeRange] = useState<string>('All');
|
|
124
|
+
const [pinMonth, setPinMonth] = useState<string | undefined>(undefined);
|
|
125
|
+
const [hidden, setHidden] = useState<Set<string>>(new Set());
|
|
126
|
+
const toggleLegendSeries = useCallback((key: string) => {
|
|
127
|
+
setHidden(prev => {
|
|
128
|
+
const next = new Set(prev);
|
|
129
|
+
if (next.has(key)) next.delete(key);
|
|
130
|
+
else next.add(key);
|
|
131
|
+
return next;
|
|
132
|
+
});
|
|
133
|
+
}, []);
|
|
134
|
+
|
|
135
|
+
return (
|
|
136
|
+
<ChartAreaInteractive
|
|
137
|
+
chartRenderId="multi-history-demo"
|
|
138
|
+
timeRange={timeRange}
|
|
139
|
+
onTimeRangeChange={setTimeRange}
|
|
140
|
+
pinMonth={pinMonth}
|
|
141
|
+
onPinMonthChange={setPinMonth}
|
|
142
|
+
chartData={MULTI_CHART}
|
|
143
|
+
historyData={MULTI_HISTORIES}
|
|
144
|
+
forecastData={MULTI_FORECASTS}
|
|
145
|
+
loading={false}
|
|
146
|
+
isDarkTheme={isDarkTheme}
|
|
147
|
+
toggleLegendSeries={toggleLegendSeries}
|
|
148
|
+
ensureAnalysisSeriesVisible={() => {}}
|
|
149
|
+
hiddenSeries={hidden}
|
|
150
|
+
maxVisibleItems={MULTI_FORECASTS.length}
|
|
151
|
+
/>
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
98
155
|
export default function ChartAreaInteractivePage() {
|
|
99
156
|
const { isDarkMode } = useTheme();
|
|
100
157
|
const [timeRange, setTimeRange] = useState<string>('1y');
|
|
@@ -205,6 +262,16 @@ export default function ChartAreaInteractivePage() {
|
|
|
205
262
|
discreteThresholdValues={DEMO_DISCRETE_THRESHOLDS}
|
|
206
263
|
/>
|
|
207
264
|
</PageContentSection>
|
|
265
|
+
<PageContentSection>
|
|
266
|
+
<h3 style={{ marginBottom: '1rem' }}>Several history lines</h3>
|
|
267
|
+
<p>
|
|
268
|
+
One history per product via <code>historyData</code> (data keys{' '}
|
|
269
|
+
<code>historical_<id></code>). A forecast joins the history its{' '}
|
|
270
|
+
<code>continues</code> names; with several histories and no{' '}
|
|
271
|
+
<code>continues</code>, it starts on its own first point.
|
|
272
|
+
</p>
|
|
273
|
+
<MultiHistoryDemo isDarkTheme={isDarkMode} />
|
|
274
|
+
</PageContentSection>
|
|
208
275
|
</>
|
|
209
276
|
);
|
|
210
277
|
}
|