@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
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { render } from '@testing-library/react';
|
|
2
|
+
|
|
3
|
+
import { formatNumber as compact } from '../tools/formatters';
|
|
4
|
+
import { ChartAxes } from './ChartAxes';
|
|
5
|
+
|
|
6
|
+
// Recharts' axes draw no tick text in jsdom, so this reads the formatter
|
|
7
|
+
// ChartAxes hands its YAxis.
|
|
8
|
+
const yAxisProps: Array<{ tickFormatter?: (v: number) => string }> = [];
|
|
9
|
+
jest.mock('recharts', () => ({
|
|
10
|
+
...jest.requireActual('recharts'),
|
|
11
|
+
XAxis: () => null,
|
|
12
|
+
YAxis: (props: { tickFormatter?: (v: number) => string }) => {
|
|
13
|
+
yAxisProps.push(props);
|
|
14
|
+
return null;
|
|
15
|
+
},
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
const formatDate = (v: string) => v;
|
|
19
|
+
|
|
20
|
+
describe('ChartAxes y-axis numbers', () => {
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
yAxisProps.length = 0;
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("uses the host's formatNumber", () => {
|
|
26
|
+
const own = (v: number) => `${v.toFixed(2)} u`;
|
|
27
|
+
render(<ChartAxes formatDate={formatDate} formatNumber={own} />);
|
|
28
|
+
expect(yAxisProps.at(-1)?.tickFormatter).toBe(own);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('is compact without one', () => {
|
|
32
|
+
render(<ChartAxes formatDate={formatDate} />);
|
|
33
|
+
expect(yAxisProps.at(-1)?.tickFormatter).toBe(compact);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('formats fixed-range ticks with it too', () => {
|
|
37
|
+
const own = jest.fn((v: number) => `${v} u`);
|
|
38
|
+
render(
|
|
39
|
+
<ChartAxes
|
|
40
|
+
formatDate={formatDate}
|
|
41
|
+
formatNumber={own}
|
|
42
|
+
yMin={0}
|
|
43
|
+
yMax={1}
|
|
44
|
+
autoScaleYAxis={false}
|
|
45
|
+
/>,
|
|
46
|
+
);
|
|
47
|
+
expect(own).toHaveBeenCalled();
|
|
48
|
+
});
|
|
49
|
+
});
|
|
@@ -3,7 +3,7 @@ import { useMemo, useRef } from 'react';
|
|
|
3
3
|
import { Label, XAxis, YAxis } from 'recharts';
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
|
-
formatNumber,
|
|
6
|
+
formatNumber as defaultFormatNumber,
|
|
7
7
|
formatYAxisNumber,
|
|
8
8
|
generateRoundedYTicks,
|
|
9
9
|
} from '../tools/formatters';
|
|
@@ -20,6 +20,13 @@ interface ChartAxesProps {
|
|
|
20
20
|
yMin?: number;
|
|
21
21
|
yMax?: number;
|
|
22
22
|
autoScaleYAxis?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Draw no axes. A hidden x-axis is kept, so the tooltip still names the
|
|
25
|
+
* row's date. The y-axis keeps fixed `yMin`/`yMax` (with `autoScaleYAxis`
|
|
26
|
+
* off) as its range, widened if the data goes past it; otherwise it is
|
|
27
|
+
* recharts' default, as without axes.
|
|
28
|
+
*/
|
|
29
|
+
hide?: boolean;
|
|
23
30
|
}
|
|
24
31
|
|
|
25
32
|
export function ChartAxes({
|
|
@@ -28,18 +35,21 @@ export function ChartAxes({
|
|
|
28
35
|
xAxisLabel,
|
|
29
36
|
yAxisLabel,
|
|
30
37
|
formatDate,
|
|
31
|
-
//
|
|
38
|
+
// The host's number format for the y-axis ticks; uilib's compact one
|
|
39
|
+
// (K/M/B, whole numbers below 1000) without it.
|
|
40
|
+
formatNumber = defaultFormatNumber,
|
|
32
41
|
xMin,
|
|
33
42
|
xMax,
|
|
34
43
|
yMin,
|
|
35
44
|
yMax,
|
|
36
45
|
autoScaleYAxis = false,
|
|
46
|
+
hide = false,
|
|
37
47
|
}: ChartAxesProps) {
|
|
38
48
|
const tickColor = 'var(--muted-foreground)';
|
|
39
49
|
|
|
40
50
|
// Generate rounded ticks to avoid duplicates when we have explicit domain
|
|
41
51
|
const yTicks = useMemo(() => {
|
|
42
|
-
if (yMin !== undefined && yMax !== undefined && !autoScaleYAxis) {
|
|
52
|
+
if (!hide && yMin !== undefined && yMax !== undefined && !autoScaleYAxis) {
|
|
43
53
|
const ticks = generateRoundedYTicks(yMin, yMax);
|
|
44
54
|
const formatter = formatNumber || formatYAxisNumber;
|
|
45
55
|
|
|
@@ -68,7 +78,7 @@ export function ChartAxes({
|
|
|
68
78
|
return Array.from(ticksByFormatted.values()).sort((a, b) => a - b);
|
|
69
79
|
}
|
|
70
80
|
return undefined;
|
|
71
|
-
}, [yMin, yMax, autoScaleYAxis, formatNumber]);
|
|
81
|
+
}, [hide, yMin, yMax, autoScaleYAxis, formatNumber]);
|
|
72
82
|
|
|
73
83
|
// Determine Y-axis domain
|
|
74
84
|
let yDomain:
|
|
@@ -106,6 +116,7 @@ export function ChartAxes({
|
|
|
106
116
|
<XAxis
|
|
107
117
|
className={xAxisClassName}
|
|
108
118
|
dataKey="date"
|
|
119
|
+
hide={hide}
|
|
109
120
|
tickLine={false}
|
|
110
121
|
axisLine={false}
|
|
111
122
|
tickMargin={8}
|
|
@@ -119,22 +130,27 @@ export function ChartAxes({
|
|
|
119
130
|
<Label value={xAxisLabel} position="insideBottom" offset={-5} />
|
|
120
131
|
)}
|
|
121
132
|
</XAxis>
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
133
|
+
{hide && !autoScaleYAxis && yMin !== undefined && yMax !== undefined && (
|
|
134
|
+
<YAxis hide domain={[yMin, yMax]} />
|
|
135
|
+
)}
|
|
136
|
+
{!hide && (
|
|
137
|
+
<YAxis
|
|
138
|
+
className={yAxisClassName}
|
|
139
|
+
tickLine={false}
|
|
140
|
+
axisLine={false}
|
|
141
|
+
tickMargin={8}
|
|
142
|
+
minTickGap={32}
|
|
143
|
+
tick={{ fill: tickColor }}
|
|
144
|
+
tickFormatter={formatNumber}
|
|
145
|
+
{...(yDomain && { domain: yDomain })}
|
|
146
|
+
{...(yTicks && { ticks: yTicks })}
|
|
147
|
+
allowDataOverflow={!autoScaleYAxis}
|
|
148
|
+
>
|
|
149
|
+
{yAxisLabel && (
|
|
150
|
+
<Label value={yAxisLabel} angle={-90} position="insideLeft" />
|
|
151
|
+
)}
|
|
152
|
+
</YAxis>
|
|
153
|
+
)}
|
|
138
154
|
</>
|
|
139
155
|
);
|
|
140
156
|
}
|
|
@@ -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
|
)}
|
|
@@ -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,6 +14,8 @@ 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.
|
|
17
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.
|
|
18
20
|
|
|
19
21
|
## Host provides
|