@sybilion/uilib 1.7.2 → 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/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.js +4 -1
- package/dist/esm/src/components/ui/Chart/Chart.styl.js +2 -2
- package/dist/esm/src/components/ui/Chart/components/BaseChartWrapper.js +74 -57
- package/dist/esm/src/components/ui/Chart/components/ChartAxes.js +8 -7
- 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/ChartPlacedTooltip.js +65 -0
- package/dist/esm/src/components/ui/Chart/components/ChartTooltipContent.js +4 -4
- package/dist/esm/src/components/ui/Chart/components/ChartTooltipItem.js +24 -8
- 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/Chart/components/QuantileBands.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 +55 -19
- 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 +28 -0
- package/dist/esm/src/components/widgets/DriverCard/DriverPerformanceChart.js +5 -25
- package/dist/esm/src/index.js +4 -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/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 +25 -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/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 +30 -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 +19 -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 +2 -2
- 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/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 +149 -88
- 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/CustomChartLegend/CustomChartLegend.tsx +21 -1
- package/src/components/ui/Chart/components/LegendSvg/LegendSvg.tsx +3 -1
- package/src/components/ui/Chart/components/QuantileBands.tsx +1 -1
- package/src/components/ui/ChartAreaInteractive/AGENT.md +4 -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 +151 -65
- 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 +81 -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/components/widgets/DriverCard/DriverPerformanceChart.tsx +2 -2
- 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,160 @@
|
|
|
1
|
+
import { act, fireEvent, render, waitFor } from '@testing-library/react';
|
|
2
|
+
import { Line, LineChart } from 'recharts';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
ChartPlacedTooltip,
|
|
6
|
+
NARROW_CHART_PX,
|
|
7
|
+
NARROW_TOOLTIP_GAP_PX,
|
|
8
|
+
isTooltipUnder,
|
|
9
|
+
opensTooltipUnder,
|
|
10
|
+
} from './ChartPlacedTooltip';
|
|
11
|
+
|
|
12
|
+
// jsdom lays nothing out; recharts reads the wrapper's box to place the
|
|
13
|
+
// pointer, so give every element the chart's size.
|
|
14
|
+
const H = 100;
|
|
15
|
+
let width = 300;
|
|
16
|
+
const box = () =>
|
|
17
|
+
({
|
|
18
|
+
x: 0,
|
|
19
|
+
y: 0,
|
|
20
|
+
top: 0,
|
|
21
|
+
left: 0,
|
|
22
|
+
right: width,
|
|
23
|
+
bottom: H,
|
|
24
|
+
width,
|
|
25
|
+
height: H,
|
|
26
|
+
toJSON() {},
|
|
27
|
+
}) as DOMRect;
|
|
28
|
+
|
|
29
|
+
beforeAll(() => {
|
|
30
|
+
for (const [prop, get] of [
|
|
31
|
+
['clientWidth', () => width],
|
|
32
|
+
['offsetWidth', () => width],
|
|
33
|
+
['clientHeight', () => H],
|
|
34
|
+
['offsetHeight', () => H],
|
|
35
|
+
] as const) {
|
|
36
|
+
Object.defineProperty(HTMLElement.prototype, prop, {
|
|
37
|
+
configurable: true,
|
|
38
|
+
get,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
HTMLElement.prototype.getBoundingClientRect = box;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const data = [
|
|
45
|
+
{ date: '2026-01', v: 1 },
|
|
46
|
+
{ date: '2026-02', v: 2 },
|
|
47
|
+
{ date: '2026-03', v: 3 },
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
// recharts' default margin is 5 on every side; the tooltip starts under the
|
|
51
|
+
// x-axis, so it leaves the bottom one out.
|
|
52
|
+
const MARGIN = 5;
|
|
53
|
+
|
|
54
|
+
function renderChart(
|
|
55
|
+
w: number,
|
|
56
|
+
tooltip: Partial<Parameters<typeof ChartPlacedTooltip>[0]> = {},
|
|
57
|
+
margin?: { top: number; right: number; bottom: number; left: number },
|
|
58
|
+
) {
|
|
59
|
+
width = w;
|
|
60
|
+
return render(
|
|
61
|
+
<LineChart width={w} height={H} data={data} margin={margin}>
|
|
62
|
+
<Line dataKey="v" isAnimationActive={false} />
|
|
63
|
+
<ChartPlacedTooltip
|
|
64
|
+
content={() => <div className="tip">tip</div>}
|
|
65
|
+
{...tooltip}
|
|
66
|
+
/>
|
|
67
|
+
</LineChart>,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// The tooltip's vertical translate, once the hover has placed it.
|
|
72
|
+
async function hoverAndReadY(container: HTMLElement, w: number) {
|
|
73
|
+
const chart = container.querySelector('.recharts-wrapper');
|
|
74
|
+
expect(chart).toBeTruthy();
|
|
75
|
+
await act(async () => {
|
|
76
|
+
fireEvent.mouseMove(chart!, { clientX: w - 20, clientY: H / 2 });
|
|
77
|
+
});
|
|
78
|
+
const wrapper = () =>
|
|
79
|
+
container.querySelector<HTMLElement>('.recharts-tooltip-wrapper');
|
|
80
|
+
await waitFor(() => expect(wrapper()?.style.visibility).toBe('visible'));
|
|
81
|
+
const m = /translate\(([-\d.]+)px,\s*([-\d.]+)px\)/.exec(
|
|
82
|
+
wrapper()!.style.transform,
|
|
83
|
+
);
|
|
84
|
+
expect(m).toBeTruthy();
|
|
85
|
+
return { y: Number(m![2]), transition: wrapper()!.style.transition };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
describe('ChartPlacedTooltip', () => {
|
|
89
|
+
it('knows which widths are narrow', () => {
|
|
90
|
+
expect(opensTooltipUnder(undefined)).toBe(false);
|
|
91
|
+
expect(opensTooltipUnder(0)).toBe(false);
|
|
92
|
+
expect(opensTooltipUnder(NARROW_CHART_PX - 1)).toBe(true);
|
|
93
|
+
expect(opensTooltipUnder(NARROW_CHART_PX)).toBe(false);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('opens under a narrow chart, without sliding there', async () => {
|
|
97
|
+
const { container } = renderChart(300);
|
|
98
|
+
const { y, transition } = await hoverAndReadY(container, 300);
|
|
99
|
+
expect(y).toBe(H - MARGIN + NARROW_TOOLTIP_GAP_PX);
|
|
100
|
+
expect(transition).toBe('none');
|
|
101
|
+
const wrapper = container.querySelector<HTMLElement>(
|
|
102
|
+
'.recharts-tooltip-wrapper',
|
|
103
|
+
)!;
|
|
104
|
+
expect(isTooltipUnder(wrapper)).toBe(true);
|
|
105
|
+
expect(wrapper.style.display).toBe('');
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('starts right under the x-axis labels, not under the bottom margin', async () => {
|
|
109
|
+
const { container } = renderChart(
|
|
110
|
+
300,
|
|
111
|
+
{},
|
|
112
|
+
{
|
|
113
|
+
top: 20,
|
|
114
|
+
right: 5,
|
|
115
|
+
bottom: 20,
|
|
116
|
+
left: 5,
|
|
117
|
+
},
|
|
118
|
+
);
|
|
119
|
+
const { y } = await hoverAndReadY(container, 300);
|
|
120
|
+
expect(y).toBe(H - 20 + NARROW_TOOLTIP_GAP_PX);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('takes no room under a narrow chart while nothing is hovered', async () => {
|
|
124
|
+
const { container } = renderChart(300);
|
|
125
|
+
await hoverAndReadY(container, 300);
|
|
126
|
+
const chart = container.querySelector('.recharts-wrapper')!;
|
|
127
|
+
await act(async () => {
|
|
128
|
+
fireEvent.mouseLeave(chart);
|
|
129
|
+
});
|
|
130
|
+
await waitFor(() =>
|
|
131
|
+
expect(
|
|
132
|
+
container.querySelector<HTMLElement>('.recharts-tooltip-wrapper')?.style
|
|
133
|
+
.display,
|
|
134
|
+
).toBe('none'),
|
|
135
|
+
);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('stays over a wide chart', async () => {
|
|
139
|
+
const { container } = renderChart(640);
|
|
140
|
+
const { y } = await hoverAndReadY(container, 640);
|
|
141
|
+
expect(y).toBeLessThan(H);
|
|
142
|
+
expect(
|
|
143
|
+
isTooltipUnder(
|
|
144
|
+
container.querySelector<HTMLElement>('.recharts-tooltip-wrapper')!,
|
|
145
|
+
),
|
|
146
|
+
).toBe(false);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("lets the caller's position win", async () => {
|
|
150
|
+
const { container } = renderChart(300, { position: { y: 7 } });
|
|
151
|
+
const { y } = await hoverAndReadY(container, 300);
|
|
152
|
+
expect(y).toBe(7);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('keeps the under-chart y when the caller sets only x', async () => {
|
|
156
|
+
const { container } = renderChart(300, { position: { x: 0 } });
|
|
157
|
+
const { y } = await hoverAndReadY(container, 300);
|
|
158
|
+
expect(y).toBe(H - MARGIN + NARROW_TOOLTIP_GAP_PX);
|
|
159
|
+
});
|
|
160
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { type CSSProperties, type ComponentProps, useMemo } from 'react';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
Tooltip,
|
|
5
|
+
useChartHeight,
|
|
6
|
+
useChartWidth,
|
|
7
|
+
useIsTooltipActive,
|
|
8
|
+
useMargin,
|
|
9
|
+
} from 'recharts';
|
|
10
|
+
|
|
11
|
+
/** Charts narrower than this open their tooltip under the chart, not over it. */
|
|
12
|
+
export const NARROW_CHART_PX = 500;
|
|
13
|
+
|
|
14
|
+
/** Gap between a narrow chart's x-axis labels and its tooltip. */
|
|
15
|
+
export const NARROW_TOOLTIP_GAP_PX = 4;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Set to 1 on the tooltip wrapper's style while it sits under the chart, so
|
|
19
|
+
* code that moves the wrapper itself (BaseChartWrapper) leaves it alone
|
|
20
|
+
* instead of deciding again.
|
|
21
|
+
*/
|
|
22
|
+
export const TOOLTIP_UNDER_VAR = '--chart-tooltip-under';
|
|
23
|
+
|
|
24
|
+
/** Whether a chart this wide opens its tooltip under itself. */
|
|
25
|
+
export function opensTooltipUnder(chartWidth: number | undefined): boolean {
|
|
26
|
+
return chartWidth != null && chartWidth > 0 && chartWidth < NARROW_CHART_PX;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Whether ChartPlacedTooltip put this tooltip wrapper under its chart. */
|
|
30
|
+
export function isTooltipUnder(wrapper: HTMLElement): boolean {
|
|
31
|
+
return wrapper.style.getPropertyValue(TOOLTIP_UNDER_VAR).trim() === '1';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type ChartPlacedTooltipProps = ComponentProps<typeof Tooltip>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A recharts Tooltip that, on a chart narrower than {@link NARROW_CHART_PX},
|
|
38
|
+
* sits under the chart instead of over the lines, starting right under the
|
|
39
|
+
* x-axis labels (the chart's bottom margin is left out), so a small chart stays
|
|
40
|
+
* readable while it is hovered. It may then cover what follows the chart, be
|
|
41
|
+
* clipped by a parent that hides overflow, or run past the page edge; that is
|
|
42
|
+
* accepted. Under a chart it does not slide (no transition, whatever
|
|
43
|
+
* `wrapperStyle` says), and while no point is hovered it takes no room
|
|
44
|
+
* (`display: none`), so it never grows a scrolling parent. A caller's
|
|
45
|
+
* `position.x` and `position.y` each win, as does `isAnimationActive`. With
|
|
46
|
+
* `portal`, recharts places nothing, so neither does this.
|
|
47
|
+
*/
|
|
48
|
+
export function ChartPlacedTooltip(props: ChartPlacedTooltipProps) {
|
|
49
|
+
const width = useChartWidth();
|
|
50
|
+
const height = useChartHeight();
|
|
51
|
+
const hovered = useIsTooltipActive();
|
|
52
|
+
const bottomMargin = useMargin()?.bottom ?? 0;
|
|
53
|
+
const open = props.active ?? hovered;
|
|
54
|
+
|
|
55
|
+
const under =
|
|
56
|
+
props.portal == null &&
|
|
57
|
+
opensTooltipUnder(width) &&
|
|
58
|
+
height != null &&
|
|
59
|
+
height > 0;
|
|
60
|
+
const callerPosition = props.position;
|
|
61
|
+
const y = under
|
|
62
|
+
? (callerPosition?.y ??
|
|
63
|
+
height - Math.max(0, bottomMargin) + NARROW_TOOLTIP_GAP_PX)
|
|
64
|
+
: undefined;
|
|
65
|
+
const position = useMemo(
|
|
66
|
+
() => (y == null ? undefined : { ...callerPosition, y }),
|
|
67
|
+
[callerPosition, y],
|
|
68
|
+
);
|
|
69
|
+
const callerWrapperStyle = props.wrapperStyle;
|
|
70
|
+
const wrapperStyle = useMemo(
|
|
71
|
+
() =>
|
|
72
|
+
under
|
|
73
|
+
? ({
|
|
74
|
+
...callerWrapperStyle,
|
|
75
|
+
[TOOLTIP_UNDER_VAR]: '1',
|
|
76
|
+
transition: 'none',
|
|
77
|
+
...(open ? {} : { display: 'none' }),
|
|
78
|
+
} as CSSProperties)
|
|
79
|
+
: undefined,
|
|
80
|
+
[under, open, callerWrapperStyle],
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
if (!under) return <Tooltip {...props} />;
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<Tooltip
|
|
87
|
+
{...props}
|
|
88
|
+
position={position}
|
|
89
|
+
isAnimationActive={props.isAnimationActive ?? false}
|
|
90
|
+
wrapperStyle={wrapperStyle}
|
|
91
|
+
/>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { render } from '@testing-library/react';
|
|
2
|
+
|
|
3
|
+
import { ChartContext } from '../Chart.context';
|
|
4
|
+
import { ChartTooltipContent } from './ChartTooltipContent';
|
|
5
|
+
|
|
6
|
+
const payload = [
|
|
7
|
+
{ dataKey: 'historical', name: 'History', value: 1234.5678, color: '#000' },
|
|
8
|
+
{ dataKey: 'band', name: 'Range', value: [0.5, 2.25], color: '#0aa' },
|
|
9
|
+
{ dataKey: 'driver', name: 'Driver', value: 0, color: '#a00' },
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
function rows(valueFormatter?: (value: number) => string) {
|
|
13
|
+
const { container } = render(
|
|
14
|
+
<ChartContext.Provider value={{ config: {} }}>
|
|
15
|
+
<ChartTooltipContent
|
|
16
|
+
active
|
|
17
|
+
payload={payload}
|
|
18
|
+
label="2026-03-01"
|
|
19
|
+
valueFormatter={valueFormatter}
|
|
20
|
+
/>
|
|
21
|
+
</ChartContext.Provider>,
|
|
22
|
+
);
|
|
23
|
+
return [...container.querySelectorAll('span')]
|
|
24
|
+
.map(s => s.textContent)
|
|
25
|
+
.filter(Boolean);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('ChartTooltipContent values', () => {
|
|
29
|
+
it('formats each number, both ends of a range, with valueFormatter', () => {
|
|
30
|
+
const fmt = (v: number) => `€${v.toFixed(1)}`;
|
|
31
|
+
expect(rows(fmt)).toEqual([
|
|
32
|
+
'History',
|
|
33
|
+
'€1234.6',
|
|
34
|
+
'Range',
|
|
35
|
+
'€0.5 – €2.3',
|
|
36
|
+
'Driver',
|
|
37
|
+
'€0.0',
|
|
38
|
+
]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('prints the raw value without one, zero included', () => {
|
|
42
|
+
const text = rows();
|
|
43
|
+
expect(text).toContain('Driver');
|
|
44
|
+
expect(text[text.indexOf('Driver') + 1]).toBe('0');
|
|
45
|
+
expect(text[text.indexOf('History') + 1]).toBe(
|
|
46
|
+
(1234.5678).toLocaleString(undefined, { maximumFractionDigits: 20 }),
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('shows a dash for a range end with no number', () => {
|
|
51
|
+
const { container } = render(
|
|
52
|
+
<ChartContext.Provider value={{ config: {} }}>
|
|
53
|
+
<ChartTooltipContent
|
|
54
|
+
active
|
|
55
|
+
payload={[
|
|
56
|
+
{ dataKey: 'band', name: 'Range', value: [null, 5], color: '#0aa' },
|
|
57
|
+
]}
|
|
58
|
+
label="2026-03-01"
|
|
59
|
+
/>
|
|
60
|
+
</ChartContext.Provider>,
|
|
61
|
+
);
|
|
62
|
+
expect(container.textContent).toContain('— – 5');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('prints a string value as is and leaves out NaN and empty ranges', () => {
|
|
66
|
+
const { container } = render(
|
|
67
|
+
<ChartContext.Provider value={{ config: {} }}>
|
|
68
|
+
<ChartTooltipContent
|
|
69
|
+
active
|
|
70
|
+
payload={[
|
|
71
|
+
{ dataKey: 'a', name: 'Text', value: '12.5', color: '#000' },
|
|
72
|
+
{ dataKey: 'b', name: 'Broken', value: Number.NaN, color: '#000' },
|
|
73
|
+
{ dataKey: 'c', name: 'Empty', value: [null, null], color: '#000' },
|
|
74
|
+
]}
|
|
75
|
+
label="2026-03-01"
|
|
76
|
+
valueFormatter={v => v.toFixed(2)}
|
|
77
|
+
/>
|
|
78
|
+
</ChartContext.Provider>,
|
|
79
|
+
);
|
|
80
|
+
const values = [...container.querySelectorAll('span')]
|
|
81
|
+
.map(s => s.textContent)
|
|
82
|
+
.filter(Boolean);
|
|
83
|
+
expect(values).toEqual(['Text', '12.5', 'Broken', 'Empty']);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
@@ -24,6 +24,7 @@ export function ChartTooltipContent({
|
|
|
24
24
|
labelClassName,
|
|
25
25
|
payload,
|
|
26
26
|
formatter,
|
|
27
|
+
valueFormatter,
|
|
27
28
|
color,
|
|
28
29
|
nameKey,
|
|
29
30
|
labelKey,
|
|
@@ -137,6 +138,7 @@ export function ChartTooltipContent({
|
|
|
137
138
|
color={color}
|
|
138
139
|
nameKey={nameKey}
|
|
139
140
|
formatter={formatter}
|
|
141
|
+
valueFormatter={valueFormatter}
|
|
140
142
|
/>
|
|
141
143
|
))}
|
|
142
144
|
</div>
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import cn from 'classnames';
|
|
2
2
|
import { CSSProperties, ReactNode } from 'react';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
type TooltipItem,
|
|
6
|
+
getPayloadConfigFromPayload,
|
|
7
|
+
} from '../Chart.helpers';
|
|
5
8
|
import S from '../Chart.styl';
|
|
6
9
|
import { ChartConfig } from '../Chart.types';
|
|
7
10
|
|
|
@@ -22,8 +25,43 @@ type ChartTooltipItemProps = {
|
|
|
22
25
|
index: number,
|
|
23
26
|
payload: unknown,
|
|
24
27
|
) => ReactNode;
|
|
28
|
+
valueFormatter?: (value: number) => string;
|
|
25
29
|
};
|
|
26
30
|
|
|
31
|
+
const rawValue = (value: number) =>
|
|
32
|
+
value.toLocaleString(undefined, { maximumFractionDigits: 20 });
|
|
33
|
+
|
|
34
|
+
const isMissing = (value: unknown) =>
|
|
35
|
+
value == null || (typeof value === 'number' && Number.isNaN(value));
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A row's value as text: numbers through `format`, anything else as is, and
|
|
39
|
+
* a missing one (null, NaN) as null. A range shows a missing end as a dash,
|
|
40
|
+
* and is missing itself when both ends are.
|
|
41
|
+
*/
|
|
42
|
+
function valueText(
|
|
43
|
+
value: unknown,
|
|
44
|
+
format: (value: number) => string,
|
|
45
|
+
): string | null {
|
|
46
|
+
const one = (v: unknown) =>
|
|
47
|
+
isMissing(v) ? null : typeof v === 'number' ? format(v) : String(v);
|
|
48
|
+
if (!Array.isArray(value)) return one(value);
|
|
49
|
+
const [low, high] = [one(value[0]), one(value[1])];
|
|
50
|
+
if (low == null && high == null) return null;
|
|
51
|
+
return `${low ?? '—'} – ${high ?? '—'}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function TooltipValue({
|
|
55
|
+
value,
|
|
56
|
+
format,
|
|
57
|
+
}: {
|
|
58
|
+
value: unknown;
|
|
59
|
+
format: (value: number) => string;
|
|
60
|
+
}) {
|
|
61
|
+
const text = valueText(value, format);
|
|
62
|
+
return text == null ? null : <span className={S.tooltipValue}>{text}</span>;
|
|
63
|
+
}
|
|
64
|
+
|
|
27
65
|
export function ChartTooltipItem({
|
|
28
66
|
item,
|
|
29
67
|
index,
|
|
@@ -35,6 +73,7 @@ export function ChartTooltipItem({
|
|
|
35
73
|
color,
|
|
36
74
|
nameKey,
|
|
37
75
|
formatter,
|
|
76
|
+
valueFormatter = rawValue,
|
|
38
77
|
}: ChartTooltipItemProps) {
|
|
39
78
|
const key = `${nameKey || item.name || item.dataKey || 'value'}`;
|
|
40
79
|
const itemConfig = getPayloadConfigFromPayload(config, item, key);
|
|
@@ -88,15 +127,7 @@ export function ChartTooltipItem({
|
|
|
88
127
|
</span>
|
|
89
128
|
</div>
|
|
90
129
|
|
|
91
|
-
{item.value
|
|
92
|
-
<span className={S.tooltipValue}>
|
|
93
|
-
{Array.isArray(item.value)
|
|
94
|
-
? `${item.value[0].toLocaleString(undefined, { maximumFractionDigits: 20 })} – ${item.value[1].toLocaleString(undefined, { maximumFractionDigits: 20 })}`
|
|
95
|
-
: item.value.toLocaleString(undefined, {
|
|
96
|
-
maximumFractionDigits: 20,
|
|
97
|
-
})}
|
|
98
|
-
</span>
|
|
99
|
-
)}
|
|
130
|
+
<TooltipValue value={item.value} format={valueFormatter} />
|
|
100
131
|
</div>
|
|
101
132
|
</>
|
|
102
133
|
)}
|
|
@@ -17,8 +17,16 @@ interface CustomChartLegendPayloadItem {
|
|
|
17
17
|
updated_at?: string;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
interface CustomChartLegendHistoryItem {
|
|
21
|
+
value: string;
|
|
22
|
+
color: string;
|
|
23
|
+
dataKey: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
20
26
|
interface CustomChartLegendProps {
|
|
21
27
|
payload?: CustomChartLegendPayloadItem[];
|
|
28
|
+
/** Several history lines, shown in place of the single "Historical" entry. */
|
|
29
|
+
histories?: CustomChartLegendHistoryItem[];
|
|
22
30
|
onClick?: (data: any, index: number, event: any) => void;
|
|
23
31
|
className?: string;
|
|
24
32
|
wrapperStyle?: React.CSSProperties;
|
|
@@ -48,6 +56,7 @@ function extractIdFromDataKey(dataKey?: string): number {
|
|
|
48
56
|
*/
|
|
49
57
|
export function CustomChartLegend({
|
|
50
58
|
payload,
|
|
59
|
+
histories,
|
|
51
60
|
onClick,
|
|
52
61
|
hiddenSeries,
|
|
53
62
|
className,
|
|
@@ -69,6 +78,16 @@ export function CustomChartLegend({
|
|
|
69
78
|
[payload],
|
|
70
79
|
);
|
|
71
80
|
|
|
81
|
+
const historicalItems = useMemo(
|
|
82
|
+
() =>
|
|
83
|
+
histories?.map(h => ({
|
|
84
|
+
seriesKey: h.dataKey,
|
|
85
|
+
label: h.value,
|
|
86
|
+
color: h.color,
|
|
87
|
+
})),
|
|
88
|
+
[histories],
|
|
89
|
+
);
|
|
90
|
+
|
|
72
91
|
const selectedIds = useMemo(() => {
|
|
73
92
|
if (preventDeselection) {
|
|
74
93
|
// Always keep all items selected when preventDeselection is true
|
|
@@ -128,7 +147,7 @@ export function CustomChartLegend({
|
|
|
128
147
|
[onClick, payload, hiddenSeries, onAnalysisSelect],
|
|
129
148
|
);
|
|
130
149
|
|
|
131
|
-
if (!items.length) return null;
|
|
150
|
+
if (!items.length && !histories?.length) return null;
|
|
132
151
|
|
|
133
152
|
return (
|
|
134
153
|
<div className={cn(S.legendContainer, className)}>
|
|
@@ -144,6 +163,7 @@ export function CustomChartLegend({
|
|
|
144
163
|
maxVisibleItems={maxVisibleItems}
|
|
145
164
|
preventDeselection={preventDeselection}
|
|
146
165
|
hiddenSeries={hiddenSeries ?? new Set()}
|
|
166
|
+
historicalItems={historicalItems}
|
|
147
167
|
onToggleSeries={handleToggleSeriesVisibility}
|
|
148
168
|
/>
|
|
149
169
|
</div>
|
|
@@ -2,6 +2,8 @@ export interface LegendSvgPayloadItem {
|
|
|
2
2
|
value: string;
|
|
3
3
|
color: string;
|
|
4
4
|
dataKey: string;
|
|
5
|
+
/** A history line: drawn solid, where forecasts are dashed. */
|
|
6
|
+
solid?: boolean;
|
|
5
7
|
}
|
|
6
8
|
|
|
7
9
|
interface LegendSvgProps {
|
|
@@ -42,7 +44,7 @@ export function LegendSvg({ payload, width, marginLeft = 0 }: LegendSvgProps) {
|
|
|
42
44
|
y2={ROW_HEIGHT / 2}
|
|
43
45
|
stroke={item.color}
|
|
44
46
|
strokeWidth={2}
|
|
45
|
-
strokeDasharray=
|
|
47
|
+
strokeDasharray={item.solid ? undefined : '3 3'}
|
|
46
48
|
/>
|
|
47
49
|
<circle cx={20} cy={ROW_HEIGHT / 2} r={3} fill={item.color} />
|
|
48
50
|
<text
|
|
@@ -58,7 +58,7 @@ export function QuantileBands({
|
|
|
58
58
|
fill={fillColor}
|
|
59
59
|
// fillOpacity={band.opacity}
|
|
60
60
|
legendType={showLegend === false ? 'none' : undefined}
|
|
61
|
-
connectNulls
|
|
61
|
+
connectNulls={band.connectNulls ?? true}
|
|
62
62
|
dot={false}
|
|
63
63
|
activeDot={false}
|
|
64
64
|
isAnimationActive={animate}
|
|
@@ -14,10 +14,14 @@ ChartAreaInteractive is the full chart shell for time series data, combining an
|
|
|
14
14
|
## Do
|
|
15
15
|
|
|
16
16
|
- Pass loading while data is being fetched so the chart shows its skeleton state.
|
|
17
|
+
- Pass `formatTooltipValue` for the tooltip's numbers (they print raw without it). Lines leave a gap at a month with no value unless `connectHistoryNulls` / `connectForecastNulls` is set; a range band fills across unless its `connectNulls` is false.
|
|
18
|
+
- `historyOnTop` (passed through to BaseChartWrapper) draws the history lines over the forecasts, `showEndDots={false}` drops the last-point dots; `showAxes={false}` keeps a hidden x-axis so the tooltip still names the date.
|
|
19
|
+
- For several products, pass `historyData` (one history line each, data key `historical_<id>` in `chartData`) and set each forecast's `continues` to the history it extends. A forecast joins a history only when `continues` names it or there is exactly one history; otherwise it starts on its own first point. Without `historyData`, the single `historical` line works as before.
|
|
17
20
|
|
|
18
21
|
## Host provides
|
|
19
22
|
|
|
20
23
|
- `chartData`, `forecastData` built from API
|
|
24
|
+
- Optional `historyData` for several history lines, with `continues` on each forecast
|
|
21
25
|
- `timeRange` / `onTimeRangeChange` or brush-only range
|
|
22
26
|
- Optional `mode`: pin | intervals | thresholds + overlay state
|
|
23
27
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Analysis, ChartDataPoint } from './ChartAreaInteractive.types';
|
|
2
|
+
import { isHistorySeriesKey } from './chartHistories';
|
|
2
3
|
|
|
3
4
|
// Helper function to format large numbers with k/m abbreviations
|
|
4
5
|
export const formatNumber = (value: number): string => {
|
|
@@ -112,14 +113,24 @@ function hasPlottableChartSeriesValue(item: ChartDataPoint): boolean {
|
|
|
112
113
|
|
|
113
114
|
/**
|
|
114
115
|
* Row counts toward the "end" of the window when anchoring the time range to a
|
|
115
|
-
* selected forecast (
|
|
116
|
+
* selected forecast (the history it joins + that analysis line / quantiles).
|
|
117
|
+
* `historyKey`: that history's data key, null for none, undefined for any.
|
|
116
118
|
*/
|
|
117
119
|
function rowContributesToAnchoredTimeRange(
|
|
118
120
|
item: ChartDataPoint,
|
|
119
121
|
analysisId: number,
|
|
122
|
+
historyKey: string | null | undefined,
|
|
120
123
|
): boolean {
|
|
121
|
-
if (
|
|
122
|
-
|
|
124
|
+
if (historyKey !== undefined) {
|
|
125
|
+
if (historyKey !== null && isPlottableNumber(item[historyKey])) {
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
for (const key of Object.keys(item)) {
|
|
130
|
+
if (isHistorySeriesKey(key) && isPlottableNumber(item[key])) {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
123
134
|
}
|
|
124
135
|
const forecastKey = `forecast_${analysisId}`;
|
|
125
136
|
if (isPlottableNumber(item[forecastKey])) {
|
|
@@ -137,14 +148,20 @@ function rowContributesToAnchoredTimeRange(
|
|
|
137
148
|
|
|
138
149
|
function computeLatestPlottableDate(
|
|
139
150
|
data: ChartDataPoint[],
|
|
140
|
-
options?:
|
|
151
|
+
options?: FilterDataForTimeRangeOptions,
|
|
141
152
|
): Date | null {
|
|
142
153
|
const anchorId = options?.endDateAnchorAnalysisId;
|
|
143
154
|
|
|
144
155
|
const pick = (item: ChartDataPoint, useAnchor: boolean): Date | null => {
|
|
145
156
|
if (!item.date) return null;
|
|
146
157
|
if (useAnchor && anchorId != null) {
|
|
147
|
-
if (
|
|
158
|
+
if (
|
|
159
|
+
!rowContributesToAnchoredTimeRange(
|
|
160
|
+
item,
|
|
161
|
+
anchorId,
|
|
162
|
+
options?.anchorHistoryKey,
|
|
163
|
+
)
|
|
164
|
+
) {
|
|
148
165
|
return null;
|
|
149
166
|
}
|
|
150
167
|
} else if (!hasPlottableChartSeriesValue(item)) {
|
|
@@ -177,6 +194,11 @@ export type FilterDataForTimeRangeOptions = {
|
|
|
177
194
|
/** When set (e.g. selected forecast on Forecast tab), the window ends at the
|
|
178
195
|
* latest point that has shared historical or that analysis — not at another run. */
|
|
179
196
|
endDateAnchorAnalysisId?: number | null;
|
|
197
|
+
/**
|
|
198
|
+
* The history line that anchored forecast joins (`historical`,
|
|
199
|
+
* `historical_<id>`), or null for none. Omitted: any history line counts.
|
|
200
|
+
*/
|
|
201
|
+
anchorHistoryKey?: string | null;
|
|
180
202
|
};
|
|
181
203
|
|
|
182
204
|
export const filterDataForTimeRange = (
|