@sybilion/uilib 1.8.0 → 1.8.1
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/esm/src/components/ui/Chart/Chart.js +4 -1
- package/dist/esm/src/components/ui/Chart/components/BaseChartWrapper.js +30 -8
- package/dist/esm/src/components/ui/Chart/components/ChartAxes.js +8 -7
- package/dist/esm/src/components/ui/Chart/components/ChartPlacedTooltip.js +65 -0
- package/dist/esm/src/components/ui/Chart/components/ChartTooltipContent.js +2 -2
- package/dist/esm/src/components/ui/Chart/components/ChartTooltipItem.js +22 -6
- package/dist/esm/src/components/ui/Chart/components/QuantileBands.js +1 -1
- package/dist/esm/src/components/ui/ChartAreaInteractive/ChartLines.js +14 -8
- package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.js +2 -3
- package/dist/esm/src/components/widgets/DriverCard/DriverPerformanceChart.js +5 -25
- package/dist/esm/src/index.js +1 -0
- package/dist/esm/types/src/components/ui/Chart/Chart.d.ts +4 -2
- package/dist/esm/types/src/components/ui/Chart/Chart.types.d.ts +5 -0
- package/dist/esm/types/src/components/ui/Chart/chartForecastVisualization.types.d.ts +7 -0
- package/dist/esm/types/src/components/ui/Chart/components/BaseChartWrapper.d.ts +17 -0
- package/dist/esm/types/src/components/ui/Chart/components/BaseChartWrapper.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/Chart/components/ChartAxes.d.ts +8 -1
- package/dist/esm/types/src/components/ui/Chart/components/ChartAxes.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/Chart/components/ChartPlacedTooltip.d.ts +31 -0
- package/dist/esm/types/src/components/ui/Chart/components/ChartPlacedTooltip.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/Chart/components/ChartTooltipContent.d.ts +1 -1
- package/dist/esm/types/src/components/ui/Chart/components/ChartTooltipContent.test.d.ts +1 -0
- package/dist/esm/types/src/components/ui/Chart/components/ChartTooltipItem.d.ts +2 -1
- package/dist/esm/types/src/components/ui/ChartAreaInteractive/ChartLines.d.ts +12 -1
- package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.d.ts +2 -3
- package/package.json +2 -2
- package/src/components/ui/Chart/AGENT.md +5 -0
- package/src/components/ui/Chart/Chart.tsx +7 -1
- package/src/components/ui/Chart/Chart.types.ts +5 -0
- package/src/components/ui/Chart/chartForecastVisualization.types.ts +7 -0
- package/src/components/ui/Chart/components/BaseChartWrapper.test.tsx +344 -0
- package/src/components/ui/Chart/components/BaseChartWrapper.tsx +78 -36
- package/src/components/ui/Chart/components/ChartAxes.test.tsx +49 -0
- package/src/components/ui/Chart/components/ChartAxes.tsx +36 -20
- package/src/components/ui/Chart/components/ChartPlacedTooltip.test.tsx +160 -0
- package/src/components/ui/Chart/components/ChartPlacedTooltip.tsx +93 -0
- package/src/components/ui/Chart/components/ChartTooltipContent.test.tsx +85 -0
- package/src/components/ui/Chart/components/ChartTooltipContent.tsx +2 -0
- package/src/components/ui/Chart/components/ChartTooltipItem.tsx +41 -10
- package/src/components/ui/Chart/components/QuantileBands.tsx +1 -1
- package/src/components/ui/ChartAreaInteractive/AGENT.md +2 -0
- package/src/components/ui/ChartAreaInteractive/ChartLines.tsx +75 -45
- package/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.tsx +2 -3
- package/src/components/widgets/DriverCard/DriverPerformanceChart.tsx +2 -2
|
@@ -6,9 +6,8 @@ export type TooltipRow = {
|
|
|
6
6
|
color?: string;
|
|
7
7
|
};
|
|
8
8
|
/**
|
|
9
|
-
* Tooltip rows in series order, showing real zeros
|
|
10
|
-
*
|
|
11
|
-
* is its white stack stroke).
|
|
9
|
+
* Tooltip rows in series order, showing real zeros, and colouring each row
|
|
10
|
+
* from the chart config (an area's own colour is its white stack stroke).
|
|
12
11
|
*/
|
|
13
12
|
export declare function StackedTooltip({ active, payload, label, config, valueFormatter, labelFormatter, }: {
|
|
14
13
|
active?: boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sybilion/uilib",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.1",
|
|
4
4
|
"description": "Sybilion Design System — React UI components (Vite + Stylus)",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
@@ -137,7 +137,7 @@
|
|
|
137
137
|
"lightweight-charts": "^5.0.9",
|
|
138
138
|
"lucide-react": "^0.546.0",
|
|
139
139
|
"motion": "^12.23.12",
|
|
140
|
-
"recharts": "^3.
|
|
140
|
+
"recharts": "^3.7.0",
|
|
141
141
|
"style-inject": "^0.3.0",
|
|
142
142
|
"topojson-client": "^3.1.0",
|
|
143
143
|
"vaul": "^1.1.2",
|
|
@@ -12,6 +12,11 @@ ChartContainer wraps Recharts charts with the library’s theming, mapping a Cha
|
|
|
12
12
|
## Do
|
|
13
13
|
|
|
14
14
|
- Define every series in the ChartConfig so labels and colours stay consistent across tooltips and legends.
|
|
15
|
+
- With BaseChartWrapper or ChartAreaInteractive, pass `formatTooltipValue` so tooltip numbers match the axis format, and `connectHistoryNulls` / `connectForecastNulls` (or a band's `connectNulls: false`) to choose whether a line or range bridges a month with no value.
|
|
16
|
+
- `historyOnTop` draws the history lines over the forecast lines, for many faint forecasts that would otherwise cover them.
|
|
17
|
+
- `showAxes={false}` draws no axes but keeps a hidden x-axis, so the tooltip still names the row's date; fixed `yMin`/`yMax` (with `autoScaleYAxis={false}`) still set the y range.
|
|
18
|
+
- `showEndDots={false}` drops the dot on each line's last point (for many lines drawn as one bundle).
|
|
19
|
+
- Use ChartTooltip (not Recharts' Tooltip directly) so a chart narrower than 500px (`NARROW_CHART_PX`) opens its tooltip under the chart instead of over the lines, starting right under the x-axis labels (over any legend below). BaseChartWrapper, ChartAreaInteractive, BarChart, DonutChart, StackedAreaChart and the driver card chart already do. A parent that hides overflow (a chat bubble, a card) can clip it there. Pass `position` to place it yourself.
|
|
15
20
|
|
|
16
21
|
## Example
|
|
17
22
|
|
|
@@ -6,9 +6,14 @@ import {
|
|
|
6
6
|
} from './components/BaseChartWrapper';
|
|
7
7
|
import { ChartContainer, ChartStyle } from './components/ChartContainer';
|
|
8
8
|
import { ChartLegendContent } from './components/ChartLegendContent';
|
|
9
|
+
import {
|
|
10
|
+
ChartPlacedTooltip,
|
|
11
|
+
NARROW_CHART_PX,
|
|
12
|
+
} from './components/ChartPlacedTooltip';
|
|
9
13
|
import { ChartTooltipContent } from './components/ChartTooltipContent';
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
/** recharts' Tooltip, opening under any chart narrower than {@link NARROW_CHART_PX}. */
|
|
16
|
+
const ChartTooltip = ChartPlacedTooltip;
|
|
12
17
|
const ChartLegend = RechartsPrimitive.Legend;
|
|
13
18
|
|
|
14
19
|
export * from './Chart.types';
|
|
@@ -19,6 +24,7 @@ export {
|
|
|
19
24
|
ChartContainer,
|
|
20
25
|
ChartTooltip,
|
|
21
26
|
ChartTooltipContent,
|
|
27
|
+
NARROW_CHART_PX,
|
|
22
28
|
ChartLegend,
|
|
23
29
|
ChartLegendContent,
|
|
24
30
|
ChartStyle,
|
|
@@ -42,5 +42,10 @@ export type ChartTooltipContentProps = ComponentProps<'div'> & {
|
|
|
42
42
|
index: number,
|
|
43
43
|
payload: unknown,
|
|
44
44
|
) => ReactNode;
|
|
45
|
+
/**
|
|
46
|
+
* Formats each row's number (both ends of a range row). Without it the
|
|
47
|
+
* value prints as is.
|
|
48
|
+
*/
|
|
49
|
+
valueFormatter?: (value: number) => string;
|
|
45
50
|
color?: string;
|
|
46
51
|
};
|
|
@@ -96,4 +96,11 @@ export interface QuantileBandConfig {
|
|
|
96
96
|
| 'basis'
|
|
97
97
|
| 'natural';
|
|
98
98
|
fillPattern?: 'solid' | 'dots' | 'lines';
|
|
99
|
+
/**
|
|
100
|
+
* Fill across months with no range (default true). Read by the Recharts
|
|
101
|
+
* charts' bands (BaseChartWrapper, ChartAreaInteractive's default mode),
|
|
102
|
+
* not by LightweightForecastChart or the intervals and thresholds overlays,
|
|
103
|
+
* which build their own bands.
|
|
104
|
+
*/
|
|
105
|
+
connectNulls?: boolean;
|
|
99
106
|
}
|
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
import { act, fireEvent, render, waitFor } from '@testing-library/react';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
BaseChartWrapper,
|
|
5
|
+
type BaseChartWrapperProps,
|
|
6
|
+
} from './BaseChartWrapper';
|
|
7
|
+
import { NARROW_TOOLTIP_GAP_PX } from './ChartPlacedTooltip';
|
|
8
|
+
|
|
9
|
+
// BaseChartWrapper also moves the tooltip itself, a frame after recharts,
|
|
10
|
+
// keeping it inside the plot. A chart narrower than 500px must keep its
|
|
11
|
+
// tooltip under the chart instead (ChartPlacedTooltip), with no slide. Also:
|
|
12
|
+
// the tooltip's number format, and lines drawn across months with no value.
|
|
13
|
+
|
|
14
|
+
// jsdom lays nothing out, so ResponsiveContainer would measure 0×0 and draw
|
|
15
|
+
// no chart. Give every element the chart's size and report it on resize.
|
|
16
|
+
const H = 120;
|
|
17
|
+
let width = 300;
|
|
18
|
+
|
|
19
|
+
beforeAll(() => {
|
|
20
|
+
for (const [prop, get] of [
|
|
21
|
+
['clientWidth', () => width],
|
|
22
|
+
['offsetWidth', () => width],
|
|
23
|
+
['clientHeight', () => H],
|
|
24
|
+
['offsetHeight', () => H],
|
|
25
|
+
] as const) {
|
|
26
|
+
Object.defineProperty(HTMLElement.prototype, prop, {
|
|
27
|
+
configurable: true,
|
|
28
|
+
get,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
HTMLElement.prototype.getBoundingClientRect = () =>
|
|
32
|
+
({
|
|
33
|
+
x: 0,
|
|
34
|
+
y: 0,
|
|
35
|
+
top: 0,
|
|
36
|
+
left: 0,
|
|
37
|
+
right: width,
|
|
38
|
+
bottom: H,
|
|
39
|
+
width,
|
|
40
|
+
height: H,
|
|
41
|
+
toJSON() {},
|
|
42
|
+
}) as DOMRect;
|
|
43
|
+
(globalThis as Record<string, unknown>).ResizeObserver = class {
|
|
44
|
+
private watched = new Set<Element>();
|
|
45
|
+
constructor(private cb: ResizeObserverCallback) {}
|
|
46
|
+
observe(target: Element) {
|
|
47
|
+
this.watched.add(target);
|
|
48
|
+
setTimeout(() => {
|
|
49
|
+
if (!this.watched.has(target)) return;
|
|
50
|
+
this.cb(
|
|
51
|
+
[{ target, contentRect: { width, height: H } } as never],
|
|
52
|
+
this as never,
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
unobserve(target: Element) {
|
|
57
|
+
this.watched.delete(target);
|
|
58
|
+
}
|
|
59
|
+
disconnect() {
|
|
60
|
+
this.watched.clear();
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const chartData = [
|
|
66
|
+
{ date: '2026-01-01', historical: 1 },
|
|
67
|
+
{ date: '2026-02-01', historical: 2 },
|
|
68
|
+
{ date: '2026-03-01', historical: 3 },
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
const nextFrame = () =>
|
|
72
|
+
act(() => new Promise<void>(r => requestAnimationFrame(() => r())));
|
|
73
|
+
|
|
74
|
+
async function renderChart(
|
|
75
|
+
w: number,
|
|
76
|
+
props: Partial<BaseChartWrapperProps> = {},
|
|
77
|
+
) {
|
|
78
|
+
width = w;
|
|
79
|
+
const { container } = render(
|
|
80
|
+
<BaseChartWrapper
|
|
81
|
+
chartData={chartData}
|
|
82
|
+
hasCombinedData
|
|
83
|
+
loading={false}
|
|
84
|
+
isDarkTheme={false}
|
|
85
|
+
height={H}
|
|
86
|
+
showLegend={false}
|
|
87
|
+
margin={{ top: 0, right: 0, bottom: 0, left: 0 }}
|
|
88
|
+
{...props}
|
|
89
|
+
/>,
|
|
90
|
+
);
|
|
91
|
+
await waitFor(() =>
|
|
92
|
+
expect(container.querySelector('.recharts-line')).toBeTruthy(),
|
|
93
|
+
);
|
|
94
|
+
return container;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function hoverAndRead(
|
|
98
|
+
w: number,
|
|
99
|
+
props: Partial<BaseChartWrapperProps> = {},
|
|
100
|
+
) {
|
|
101
|
+
const container = await renderChart(w, props);
|
|
102
|
+
const chart = container.querySelectorAll('.recharts-wrapper');
|
|
103
|
+
await act(async () => {
|
|
104
|
+
fireEvent.mouseMove(chart[chart.length - 1], {
|
|
105
|
+
clientX: w - 20,
|
|
106
|
+
clientY: H / 2,
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
const wrapper = () =>
|
|
110
|
+
container.querySelector<HTMLElement>('.recharts-tooltip-wrapper');
|
|
111
|
+
await waitFor(() => expect(wrapper()?.style.transform).toMatch(/translate/));
|
|
112
|
+
// Let the wrapper's own placement run.
|
|
113
|
+
await nextFrame();
|
|
114
|
+
await nextFrame();
|
|
115
|
+
const style = wrapper()!.style;
|
|
116
|
+
const y = Number(
|
|
117
|
+
/translate\([-\d.]+px,\s*([-\d.]+)px\)/.exec(style.transform)?.[1],
|
|
118
|
+
);
|
|
119
|
+
return { y, transition: style.transition, container };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
describe('BaseChartWrapper tooltip placement', () => {
|
|
123
|
+
it('keeps a narrow chart’s tooltip under the chart', async () => {
|
|
124
|
+
const { y, transition } = await hoverAndRead(300);
|
|
125
|
+
expect(y).toBe(H + NARROW_TOOLTIP_GAP_PX);
|
|
126
|
+
expect(transition).toBe('none');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('keeps a wide chart’s tooltip inside the chart, sliding', async () => {
|
|
130
|
+
const { y, transition } = await hoverAndRead(640);
|
|
131
|
+
expect(y).toBeLessThan(H);
|
|
132
|
+
expect(transition).toMatch(/transform/);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe('BaseChartWrapper tooltip values', () => {
|
|
137
|
+
it('formats them with formatTooltipValue', async () => {
|
|
138
|
+
const { container } = await hoverAndRead(640, {
|
|
139
|
+
formatTooltipValue: v => `${v.toFixed(2)} t`,
|
|
140
|
+
});
|
|
141
|
+
await waitFor(() => expect(container.textContent).toContain('3.00 t'));
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe('BaseChartWrapper without axes', () => {
|
|
146
|
+
it('draws no axis but the tooltip still names the row’s date', async () => {
|
|
147
|
+
const { container } = await hoverAndRead(640, {
|
|
148
|
+
showAxes: false,
|
|
149
|
+
labelFormatter: (v: unknown) => `month ${String(v)}`,
|
|
150
|
+
});
|
|
151
|
+
await waitFor(() =>
|
|
152
|
+
expect(container.textContent).toContain('month 2026-03-01'),
|
|
153
|
+
);
|
|
154
|
+
expect(
|
|
155
|
+
container.querySelectorAll('.recharts-cartesian-axis-tick'),
|
|
156
|
+
).toHaveLength(0);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe('BaseChartWrapper lines across missing months', () => {
|
|
161
|
+
const gappy = [
|
|
162
|
+
{ date: '2026-01-01', historical: 1, forecast_1: 1 },
|
|
163
|
+
{ date: '2026-02-01', historical: null, forecast_1: null },
|
|
164
|
+
{ date: '2026-03-01', historical: 3, forecast_1: 3 },
|
|
165
|
+
];
|
|
166
|
+
const forecastData = [{ id: 1, name: 'Forecast' }];
|
|
167
|
+
// A line broken by a gap is drawn as one path with two "M" moves.
|
|
168
|
+
const moves = (container: HTMLElement, i: number) =>
|
|
169
|
+
(
|
|
170
|
+
container
|
|
171
|
+
.querySelectorAll('.recharts-line-curve')
|
|
172
|
+
[i]?.getAttribute('d')
|
|
173
|
+
?.match(/M/g) ?? []
|
|
174
|
+
).length;
|
|
175
|
+
const lastLayer = (container: HTMLElement) => {
|
|
176
|
+
const layers = container.querySelectorAll('.recharts-wrapper');
|
|
177
|
+
return layers[layers.length - 1] as HTMLElement;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
it('leaves a gap by default', async () => {
|
|
181
|
+
const container = await renderChart(640, {
|
|
182
|
+
chartData: gappy,
|
|
183
|
+
forecastData,
|
|
184
|
+
});
|
|
185
|
+
expect(moves(lastLayer(container), 0)).toBe(2);
|
|
186
|
+
expect(moves(lastLayer(container), 1)).toBe(2);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('joins only history when asked', async () => {
|
|
190
|
+
const history = await renderChart(640, {
|
|
191
|
+
chartData: gappy,
|
|
192
|
+
forecastData,
|
|
193
|
+
connectHistoryNulls: true,
|
|
194
|
+
});
|
|
195
|
+
expect(moves(lastLayer(history), 0)).toBe(1);
|
|
196
|
+
expect(moves(lastLayer(history), 1)).toBe(2);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('joins forecasts when asked', async () => {
|
|
200
|
+
const container = await renderChart(640, {
|
|
201
|
+
chartData: gappy,
|
|
202
|
+
forecastData,
|
|
203
|
+
connectForecastNulls: true,
|
|
204
|
+
});
|
|
205
|
+
expect(moves(lastLayer(container), 0)).toBe(2);
|
|
206
|
+
expect(moves(lastLayer(container), 1)).toBe(1);
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
describe('BaseChartWrapper line order', () => {
|
|
211
|
+
const strokes = (container: HTMLElement) => {
|
|
212
|
+
const layers = container.querySelectorAll('.recharts-wrapper');
|
|
213
|
+
return [
|
|
214
|
+
...layers[layers.length - 1].querySelectorAll('.recharts-line-curve'),
|
|
215
|
+
].map(e => e.getAttribute('stroke'));
|
|
216
|
+
};
|
|
217
|
+
const props: Partial<BaseChartWrapperProps> = {
|
|
218
|
+
chartData: [
|
|
219
|
+
{ date: '2026-01-01', historical_a: 1, forecast_1: 2 },
|
|
220
|
+
{ date: '2026-02-01', historical_a: 2, forecast_1: 3 },
|
|
221
|
+
],
|
|
222
|
+
historyData: [{ id: 'a', name: 'A', color: 'black' }],
|
|
223
|
+
forecastData: [{ id: 1, name: 'F', color: 'cyan' }],
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
it('draws history first, under the forecasts, by default', async () => {
|
|
227
|
+
const container = await renderChart(640, props);
|
|
228
|
+
expect(strokes(container)).toEqual(['black', 'cyan']);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('draws history last, over the forecasts, when asked', async () => {
|
|
232
|
+
const container = await renderChart(640, { ...props, historyOnTop: true });
|
|
233
|
+
// The history layer mounts a moment after the line layer.
|
|
234
|
+
await waitFor(() => expect(strokes(container)).toEqual(['cyan', 'black']));
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('keeps history on top after a forecast is hidden and shown again', async () => {
|
|
238
|
+
width = 640;
|
|
239
|
+
const chart = (hidden: Set<string>) => (
|
|
240
|
+
<BaseChartWrapper
|
|
241
|
+
{...(props as BaseChartWrapperProps)}
|
|
242
|
+
hasCombinedData
|
|
243
|
+
loading={false}
|
|
244
|
+
isDarkTheme={false}
|
|
245
|
+
height={H}
|
|
246
|
+
showLegend={false}
|
|
247
|
+
historyOnTop
|
|
248
|
+
hiddenSeries={hidden}
|
|
249
|
+
/>
|
|
250
|
+
);
|
|
251
|
+
const { container, rerender } = render(chart(new Set()));
|
|
252
|
+
await waitFor(() => expect(strokes(container)).toHaveLength(2));
|
|
253
|
+
rerender(chart(new Set(['forecast_1'])));
|
|
254
|
+
await waitFor(() => expect(strokes(container)).toEqual(['black']));
|
|
255
|
+
rerender(chart(new Set()));
|
|
256
|
+
await waitFor(() => expect(strokes(container)).toHaveLength(2));
|
|
257
|
+
expect(strokes(container)).toEqual(['cyan', 'black']);
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
describe('BaseChartWrapper end dots', () => {
|
|
262
|
+
const dots = (container: HTMLElement) => {
|
|
263
|
+
const layers = container.querySelectorAll('.recharts-wrapper');
|
|
264
|
+
return layers[layers.length - 1].querySelectorAll('circle').length;
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
it('marks each line’s last point by default', async () => {
|
|
268
|
+
const container = await renderChart(640);
|
|
269
|
+
await waitFor(() => expect(dots(container)).toBe(1));
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('leaves them off when asked', async () => {
|
|
273
|
+
const container = await renderChart(640, { showEndDots: false });
|
|
274
|
+
await nextFrame();
|
|
275
|
+
expect(dots(container)).toBe(0);
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
describe('BaseChartWrapper without axes, fixed y range', () => {
|
|
280
|
+
it('keeps yMin/yMax as the hidden y-axis range', async () => {
|
|
281
|
+
// One value of 0.5 on a fixed 0–1 range sits in the middle of the plot;
|
|
282
|
+
// with recharts' default range it would sit at the top.
|
|
283
|
+
const container = await renderChart(640, {
|
|
284
|
+
chartData: [
|
|
285
|
+
{ date: '2026-01-01', historical: 0.5 },
|
|
286
|
+
{ date: '2026-02-01', historical: 0.5 },
|
|
287
|
+
],
|
|
288
|
+
showAxes: false,
|
|
289
|
+
yMin: 0,
|
|
290
|
+
yMax: 1,
|
|
291
|
+
autoScaleYAxis: false,
|
|
292
|
+
});
|
|
293
|
+
const d = container
|
|
294
|
+
.querySelectorAll('.recharts-wrapper')
|
|
295
|
+
[
|
|
296
|
+
container.querySelectorAll('.recharts-wrapper').length - 1
|
|
297
|
+
].querySelector('.recharts-line-curve')!
|
|
298
|
+
.getAttribute('d')!;
|
|
299
|
+
const y = Number(/^M[-\d.]+,([-\d.]+)/.exec(d)?.[1]);
|
|
300
|
+
expect(y).toBeCloseTo(H / 2, 0);
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
describe('BaseChartWrapper range bands across missing months', () => {
|
|
305
|
+
const rows = [
|
|
306
|
+
{ date: '2026-01-01', historical: 1, band: [0, 2] },
|
|
307
|
+
{ date: '2026-02-01', historical: 2, band: null },
|
|
308
|
+
{ date: '2026-03-01', historical: 3, band: [2, 4] },
|
|
309
|
+
];
|
|
310
|
+
const band = {
|
|
311
|
+
key: 'band',
|
|
312
|
+
name: 'Range',
|
|
313
|
+
quantiles: ['0.1', '0.9'] as [string, string],
|
|
314
|
+
opacity: 0.12,
|
|
315
|
+
};
|
|
316
|
+
const areaMoves = async (connectNulls?: boolean) => {
|
|
317
|
+
width = 640;
|
|
318
|
+
const { container } = render(
|
|
319
|
+
<BaseChartWrapper
|
|
320
|
+
chartData={rows as never}
|
|
321
|
+
hasCombinedData
|
|
322
|
+
loading={false}
|
|
323
|
+
isDarkTheme={false}
|
|
324
|
+
height={H}
|
|
325
|
+
showLegend={false}
|
|
326
|
+
quantileBands={[{ ...band, connectNulls }]}
|
|
327
|
+
/>,
|
|
328
|
+
);
|
|
329
|
+
await waitFor(() =>
|
|
330
|
+
expect(container.querySelector('.recharts-area-area')).toBeTruthy(),
|
|
331
|
+
);
|
|
332
|
+
const areas = container.querySelectorAll('.recharts-area-area');
|
|
333
|
+
return (areas[areas.length - 1].getAttribute('d')?.match(/M/g) ?? [])
|
|
334
|
+
.length;
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
it('fills across them by default', async () => {
|
|
338
|
+
expect(await areaMoves()).toBe(1);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it('leaves a gap when the band says so', async () => {
|
|
342
|
+
expect(await areaMoves(false)).toBe(2);
|
|
343
|
+
});
|
|
344
|
+
});
|
|
@@ -24,7 +24,7 @@ import {
|
|
|
24
24
|
} from '#uilib/components/ui/ChartAreaInteractive/chartHistories';
|
|
25
25
|
import { Skeleton } from '#uilib/components/ui/Skeleton';
|
|
26
26
|
import { chartRenderQueue } from '#uilib/utils/chartRenderQueue';
|
|
27
|
-
import {
|
|
27
|
+
import { ComposedChart, LineChart } from 'recharts';
|
|
28
28
|
import { LegendPayload } from 'recharts/types/component/DefaultLegendContent';
|
|
29
29
|
|
|
30
30
|
import { normalizeTooltipPayload } from '../Chart.helpers';
|
|
@@ -39,6 +39,7 @@ import {
|
|
|
39
39
|
type ChartEmptyStatusTone,
|
|
40
40
|
} from './ChartEmptyState/ChartEmptyState';
|
|
41
41
|
import { ChartGrid } from './ChartGrid';
|
|
42
|
+
import { ChartPlacedTooltip, isTooltipUnder } from './ChartPlacedTooltip';
|
|
42
43
|
import { ChartTooltipContent } from './ChartTooltipContent';
|
|
43
44
|
import { CustomChartLegend } from './CustomChartLegend/CustomChartLegend';
|
|
44
45
|
import { LegendSvg } from './LegendSvg/LegendSvg';
|
|
@@ -84,6 +85,21 @@ function clampTooltipTranslate(args: {
|
|
|
84
85
|
return { x: tx, y: ty };
|
|
85
86
|
}
|
|
86
87
|
|
|
88
|
+
/**
|
|
89
|
+
* The tooltip slides after the cursor, unless the user asked for less motion.
|
|
90
|
+
* ChartPlacedTooltip turns this off under a narrow chart.
|
|
91
|
+
*/
|
|
92
|
+
const TOOLTIP_WRAPPER_STYLE = {
|
|
93
|
+
transition: 'transform 0.2s ease-out, opacity 0.2s ease-out',
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
function tooltipWrapperStyle() {
|
|
97
|
+
const reduceMotion =
|
|
98
|
+
typeof window !== 'undefined' &&
|
|
99
|
+
window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
|
|
100
|
+
return reduceMotion ? undefined : TOOLTIP_WRAPPER_STYLE;
|
|
101
|
+
}
|
|
102
|
+
|
|
87
103
|
export interface BaseChartWrapperProps {
|
|
88
104
|
renderId?: string; // unique id for the render, used to identify the render in the render queue
|
|
89
105
|
chartConfig?: ChartConfig;
|
|
@@ -117,6 +133,19 @@ export interface BaseChartWrapperProps {
|
|
|
117
133
|
yMax?: number;
|
|
118
134
|
autoScaleYAxis?: boolean;
|
|
119
135
|
formatNumber?: (value: number) => string;
|
|
136
|
+
/**
|
|
137
|
+
* Formats the numbers in the tooltip. Without it they print as is. Not
|
|
138
|
+
* `formatNumber`, which is the axis's and often compact ("1.2k").
|
|
139
|
+
*/
|
|
140
|
+
formatTooltipValue?: (value: number) => string;
|
|
141
|
+
/** Draw history lines across months with no value (default: a gap). */
|
|
142
|
+
connectHistoryNulls?: boolean;
|
|
143
|
+
/** Draw forecast lines across months with no value (default: a gap). */
|
|
144
|
+
connectForecastNulls?: boolean;
|
|
145
|
+
/** Draw history lines over the forecast lines (default: under them). */
|
|
146
|
+
historyOnTop?: boolean;
|
|
147
|
+
/** Mark each line's last point with a dot (default: on). */
|
|
148
|
+
showEndDots?: boolean;
|
|
120
149
|
formatDate?: (value: string, detailed?: boolean) => string;
|
|
121
150
|
labelFormatter?: (value: unknown) => string;
|
|
122
151
|
onLegendClick?: (data: LegendPayload, index: number, event: any) => void;
|
|
@@ -130,6 +159,10 @@ export interface BaseChartWrapperProps {
|
|
|
130
159
|
*/
|
|
131
160
|
disableLineDrawAnimation?: boolean;
|
|
132
161
|
showGrid?: boolean;
|
|
162
|
+
/**
|
|
163
|
+
* Draw the axes. Off, a hidden x-axis is kept so the tooltip still names
|
|
164
|
+
* the row's date.
|
|
165
|
+
*/
|
|
133
166
|
showAxes?: boolean;
|
|
134
167
|
showTooltip?: boolean;
|
|
135
168
|
showLegend?: boolean;
|
|
@@ -237,6 +270,11 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
237
270
|
yMax,
|
|
238
271
|
autoScaleYAxis = true,
|
|
239
272
|
formatNumber,
|
|
273
|
+
formatTooltipValue,
|
|
274
|
+
connectHistoryNulls,
|
|
275
|
+
connectForecastNulls,
|
|
276
|
+
historyOnTop,
|
|
277
|
+
showEndDots,
|
|
240
278
|
formatDate: formatDateFn = formatDate,
|
|
241
279
|
labelFormatter,
|
|
242
280
|
onLegendClick,
|
|
@@ -311,10 +349,18 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
311
349
|
const applyTooltipPosition = () => {
|
|
312
350
|
const wrapper = tooltipWrapperRef.current;
|
|
313
351
|
const coord = rawTooltipCoordinateRef.current;
|
|
314
|
-
|
|
352
|
+
// The chart the tooltip belongs to (with the grid on, the first chart in
|
|
353
|
+
// the root is the grid layer's).
|
|
354
|
+
const chartWrapper =
|
|
355
|
+
wrapper?.closest('.recharts-wrapper') ??
|
|
356
|
+
rootRef.current?.querySelector('.recharts-wrapper');
|
|
315
357
|
|
|
316
358
|
if (!wrapper || !coord || !(chartWrapper instanceof HTMLElement)) return;
|
|
317
359
|
|
|
360
|
+
// A narrow chart's tooltip sits under it, where ChartPlacedTooltip has
|
|
361
|
+
// recharts put it. Leave it there: one writer, so it does not jump.
|
|
362
|
+
if (isTooltipUnder(wrapper)) return;
|
|
363
|
+
|
|
318
364
|
const measured = wrapper.getBoundingClientRect();
|
|
319
365
|
const width = tooltipSizeRef.current.width || measured.width;
|
|
320
366
|
const height = tooltipSizeRef.current.height || measured.height;
|
|
@@ -364,8 +410,6 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
364
410
|
|
|
365
411
|
if (wrapperEl instanceof HTMLElement) {
|
|
366
412
|
tooltipWrapperRef.current = wrapperEl;
|
|
367
|
-
wrapperEl.style.transition =
|
|
368
|
-
'transform 0.2s ease-out, opacity 0.2s ease-out';
|
|
369
413
|
requestAnimationFrame(() => applyTooltipPositionRef.current());
|
|
370
414
|
}
|
|
371
415
|
};
|
|
@@ -432,6 +476,7 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
432
476
|
label={props.label}
|
|
433
477
|
labelFormatter={labelFormatter || defaultLabelFormatter}
|
|
434
478
|
indicator="dot"
|
|
479
|
+
valueFormatter={formatTooltipValue}
|
|
435
480
|
/>
|
|
436
481
|
);
|
|
437
482
|
}
|
|
@@ -455,6 +500,7 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
455
500
|
label={props.label}
|
|
456
501
|
labelFormatter={labelFormatter || defaultLabelFormatter}
|
|
457
502
|
indicator="dot"
|
|
503
|
+
valueFormatter={formatTooltipValue}
|
|
458
504
|
/>
|
|
459
505
|
);
|
|
460
506
|
};
|
|
@@ -663,6 +709,26 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
663
709
|
const ChartComponent = chartType === 'line' ? LineChart : ComposedChart;
|
|
664
710
|
const defaultLabelFormatter = (v: unknown) => formatDateFn(v as string, true);
|
|
665
711
|
|
|
712
|
+
// The same axes in the grid layer and the chart layer, so both share one
|
|
713
|
+
// scale. Hidden when showAxes is off: the hidden x-axis still places the
|
|
714
|
+
// rows by date, so the tooltip names it.
|
|
715
|
+
const axes = (
|
|
716
|
+
<ChartAxes
|
|
717
|
+
hide={!showAxes}
|
|
718
|
+
formatDate={formatDateFn}
|
|
719
|
+
formatNumber={formatNumber}
|
|
720
|
+
xAxisClassName={xAxisClassName}
|
|
721
|
+
yAxisClassName={yAxisClassName}
|
|
722
|
+
xAxisLabel={xAxisLabel}
|
|
723
|
+
yAxisLabel={yAxisLabel}
|
|
724
|
+
xMin={xMin}
|
|
725
|
+
xMax={xMax}
|
|
726
|
+
yMin={yMin}
|
|
727
|
+
yMax={yMax}
|
|
728
|
+
autoScaleYAxis={effectiveAutoScale}
|
|
729
|
+
/>
|
|
730
|
+
);
|
|
731
|
+
|
|
666
732
|
return (
|
|
667
733
|
<div
|
|
668
734
|
className={cn(
|
|
@@ -690,21 +756,7 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
690
756
|
>
|
|
691
757
|
<ChartComponent data={chartData} margin={margin}>
|
|
692
758
|
<ChartGrid />
|
|
693
|
-
{
|
|
694
|
-
<ChartAxes
|
|
695
|
-
formatDate={formatDateFn}
|
|
696
|
-
formatNumber={formatNumber}
|
|
697
|
-
xAxisClassName={xAxisClassName}
|
|
698
|
-
yAxisClassName={yAxisClassName}
|
|
699
|
-
xAxisLabel={xAxisLabel}
|
|
700
|
-
yAxisLabel={yAxisLabel}
|
|
701
|
-
xMin={xMin}
|
|
702
|
-
xMax={xMax}
|
|
703
|
-
yMin={yMin}
|
|
704
|
-
yMax={yMax}
|
|
705
|
-
autoScaleYAxis={effectiveAutoScale}
|
|
706
|
-
/>
|
|
707
|
-
)}
|
|
759
|
+
{axes}
|
|
708
760
|
</ChartComponent>
|
|
709
761
|
</ChartContainer>
|
|
710
762
|
)}
|
|
@@ -717,22 +769,7 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
717
769
|
{...containerProps}
|
|
718
770
|
>
|
|
719
771
|
<ChartComponent data={chartData} margin={margin}>
|
|
720
|
-
{
|
|
721
|
-
{showAxes && (
|
|
722
|
-
<ChartAxes
|
|
723
|
-
formatDate={formatDateFn}
|
|
724
|
-
formatNumber={formatNumber}
|
|
725
|
-
xAxisClassName={cn(xAxisClassName)}
|
|
726
|
-
yAxisClassName={cn(yAxisClassName)}
|
|
727
|
-
xAxisLabel={xAxisLabel}
|
|
728
|
-
yAxisLabel={yAxisLabel}
|
|
729
|
-
xMin={xMin}
|
|
730
|
-
xMax={xMax}
|
|
731
|
-
yMin={yMin}
|
|
732
|
-
yMax={yMax}
|
|
733
|
-
autoScaleYAxis={effectiveAutoScale}
|
|
734
|
-
/>
|
|
735
|
-
)}
|
|
772
|
+
{axes}
|
|
736
773
|
|
|
737
774
|
{/* Quantile Bands (widest to narrowest) */}
|
|
738
775
|
{quantileBands?.[0] && (
|
|
@@ -759,14 +796,19 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
759
796
|
disableHistoricalAnimation={disableHistoricalAnimation}
|
|
760
797
|
showLegend={showLegend}
|
|
761
798
|
forecastLineStyle={forecastLineStyle}
|
|
799
|
+
connectHistoryNulls={connectHistoryNulls}
|
|
800
|
+
connectForecastNulls={connectForecastNulls}
|
|
801
|
+
historyOnTop={historyOnTop}
|
|
802
|
+
showEndDots={showEndDots}
|
|
762
803
|
/>
|
|
763
804
|
|
|
764
805
|
{showTooltip && (
|
|
765
806
|
<div>
|
|
766
|
-
<
|
|
807
|
+
<ChartPlacedTooltip
|
|
767
808
|
cursor={false}
|
|
768
809
|
offset={TOOLTIP_OFFSET}
|
|
769
810
|
allowEscapeViewBox={{ x: false, y: false }}
|
|
811
|
+
wrapperStyle={tooltipWrapperStyle()}
|
|
770
812
|
content={renderTooltipContent}
|
|
771
813
|
/>
|
|
772
814
|
</div>
|