@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
|
@@ -15,10 +15,16 @@ import {
|
|
|
15
15
|
ChartLines,
|
|
16
16
|
ForecastItemData,
|
|
17
17
|
getForecastColor,
|
|
18
|
+
resolveHistoryLines,
|
|
18
19
|
} from '#uilib/components/ui/ChartAreaInteractive/ChartLines';
|
|
20
|
+
import {
|
|
21
|
+
HistoryItemData,
|
|
22
|
+
historySeriesKeys,
|
|
23
|
+
isHistorySeriesKey,
|
|
24
|
+
} from '#uilib/components/ui/ChartAreaInteractive/chartHistories';
|
|
19
25
|
import { Skeleton } from '#uilib/components/ui/Skeleton';
|
|
20
26
|
import { chartRenderQueue } from '#uilib/utils/chartRenderQueue';
|
|
21
|
-
import {
|
|
27
|
+
import { ComposedChart, LineChart } from 'recharts';
|
|
22
28
|
import { LegendPayload } from 'recharts/types/component/DefaultLegendContent';
|
|
23
29
|
|
|
24
30
|
import { normalizeTooltipPayload } from '../Chart.helpers';
|
|
@@ -33,6 +39,7 @@ import {
|
|
|
33
39
|
type ChartEmptyStatusTone,
|
|
34
40
|
} from './ChartEmptyState/ChartEmptyState';
|
|
35
41
|
import { ChartGrid } from './ChartGrid';
|
|
42
|
+
import { ChartPlacedTooltip, isTooltipUnder } from './ChartPlacedTooltip';
|
|
36
43
|
import { ChartTooltipContent } from './ChartTooltipContent';
|
|
37
44
|
import { CustomChartLegend } from './CustomChartLegend/CustomChartLegend';
|
|
38
45
|
import { LegendSvg } from './LegendSvg/LegendSvg';
|
|
@@ -78,12 +85,34 @@ function clampTooltipTranslate(args: {
|
|
|
78
85
|
return { x: tx, y: ty };
|
|
79
86
|
}
|
|
80
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
|
+
|
|
81
103
|
export interface BaseChartWrapperProps {
|
|
82
104
|
renderId?: string; // unique id for the render, used to identify the render in the render queue
|
|
83
105
|
chartConfig?: ChartConfig;
|
|
84
106
|
chartData: ChartDataPoint[];
|
|
85
107
|
historicalLineColor?: string;
|
|
86
108
|
forecastData?: ForecastItemData[];
|
|
109
|
+
/**
|
|
110
|
+
* Several history lines (one per product), each under `historical_<id>` in
|
|
111
|
+
* `chartData`, with its own colour and legend entry. Omit for the single
|
|
112
|
+
* `historical` line. With it, `historicalLineColor` does not apply: give
|
|
113
|
+
* each history its `color`.
|
|
114
|
+
*/
|
|
115
|
+
historyData?: HistoryItemData[];
|
|
87
116
|
loading: boolean;
|
|
88
117
|
hasCombinedData?: boolean; // Indicates if combinedData is available (for cache hits)
|
|
89
118
|
isDarkTheme: boolean;
|
|
@@ -104,6 +133,19 @@ export interface BaseChartWrapperProps {
|
|
|
104
133
|
yMax?: number;
|
|
105
134
|
autoScaleYAxis?: boolean;
|
|
106
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;
|
|
107
149
|
formatDate?: (value: string, detailed?: boolean) => string;
|
|
108
150
|
labelFormatter?: (value: unknown) => string;
|
|
109
151
|
onLegendClick?: (data: LegendPayload, index: number, event: any) => void;
|
|
@@ -117,6 +159,10 @@ export interface BaseChartWrapperProps {
|
|
|
117
159
|
*/
|
|
118
160
|
disableLineDrawAnimation?: boolean;
|
|
119
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
|
+
*/
|
|
120
166
|
showAxes?: boolean;
|
|
121
167
|
showTooltip?: boolean;
|
|
122
168
|
showLegend?: boolean;
|
|
@@ -202,6 +248,7 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
202
248
|
chartData,
|
|
203
249
|
historicalLineColor,
|
|
204
250
|
forecastData = [],
|
|
251
|
+
historyData,
|
|
205
252
|
loading,
|
|
206
253
|
hasCombinedData,
|
|
207
254
|
renderId,
|
|
@@ -223,6 +270,11 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
223
270
|
yMax,
|
|
224
271
|
autoScaleYAxis = true,
|
|
225
272
|
formatNumber,
|
|
273
|
+
formatTooltipValue,
|
|
274
|
+
connectHistoryNulls,
|
|
275
|
+
connectForecastNulls,
|
|
276
|
+
historyOnTop,
|
|
277
|
+
showEndDots,
|
|
226
278
|
formatDate: formatDateFn = formatDate,
|
|
227
279
|
labelFormatter,
|
|
228
280
|
onLegendClick,
|
|
@@ -297,10 +349,18 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
297
349
|
const applyTooltipPosition = () => {
|
|
298
350
|
const wrapper = tooltipWrapperRef.current;
|
|
299
351
|
const coord = rawTooltipCoordinateRef.current;
|
|
300
|
-
|
|
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');
|
|
301
357
|
|
|
302
358
|
if (!wrapper || !coord || !(chartWrapper instanceof HTMLElement)) return;
|
|
303
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
|
+
|
|
304
364
|
const measured = wrapper.getBoundingClientRect();
|
|
305
365
|
const width = tooltipSizeRef.current.width || measured.width;
|
|
306
366
|
const height = tooltipSizeRef.current.height || measured.height;
|
|
@@ -350,8 +410,6 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
350
410
|
|
|
351
411
|
if (wrapperEl instanceof HTMLElement) {
|
|
352
412
|
tooltipWrapperRef.current = wrapperEl;
|
|
353
|
-
wrapperEl.style.transition =
|
|
354
|
-
'transform 0.2s ease-out, opacity 0.2s ease-out';
|
|
355
413
|
requestAnimationFrame(() => applyTooltipPositionRef.current());
|
|
356
414
|
}
|
|
357
415
|
};
|
|
@@ -418,6 +476,7 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
418
476
|
label={props.label}
|
|
419
477
|
labelFormatter={labelFormatter || defaultLabelFormatter}
|
|
420
478
|
indicator="dot"
|
|
479
|
+
valueFormatter={formatTooltipValue}
|
|
421
480
|
/>
|
|
422
481
|
);
|
|
423
482
|
}
|
|
@@ -441,47 +500,44 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
441
500
|
label={props.label}
|
|
442
501
|
labelFormatter={labelFormatter || defaultLabelFormatter}
|
|
443
502
|
indicator="dot"
|
|
503
|
+
valueFormatter={formatTooltipValue}
|
|
444
504
|
/>
|
|
445
505
|
);
|
|
446
506
|
};
|
|
447
507
|
|
|
448
|
-
//
|
|
449
|
-
//
|
|
450
|
-
//
|
|
451
|
-
const
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
return linePoint;
|
|
482
|
-
});
|
|
483
|
-
return lineData;
|
|
484
|
-
}, [lineDataKey]);
|
|
508
|
+
// Only the line data (history and forecast_* keys) triggers the draw
|
|
509
|
+
// animation, so a change to quantile band data alone does not re-animate.
|
|
510
|
+
// The string key keeps the array's reference stable while the values are.
|
|
511
|
+
const lineData = useMemo(
|
|
512
|
+
() =>
|
|
513
|
+
chartData.map(point => {
|
|
514
|
+
const linePoint: Record<string, unknown> = { date: point.date };
|
|
515
|
+
Object.keys(point).forEach(key => {
|
|
516
|
+
if (
|
|
517
|
+
(isHistorySeriesKey(key) && point[key] !== undefined) ||
|
|
518
|
+
key.startsWith('forecast_')
|
|
519
|
+
) {
|
|
520
|
+
linePoint[key] = point[key];
|
|
521
|
+
}
|
|
522
|
+
});
|
|
523
|
+
return linePoint;
|
|
524
|
+
}),
|
|
525
|
+
[chartData],
|
|
526
|
+
);
|
|
527
|
+
const lineDataKey = useMemo(() => JSON.stringify(lineData), [lineData]);
|
|
528
|
+
const lineDataForAnimation = useMemo(() => lineData, [lineDataKey]);
|
|
529
|
+
|
|
530
|
+
const historyLegend = useMemo(
|
|
531
|
+
() =>
|
|
532
|
+
showLegend && historyData?.length
|
|
533
|
+
? resolveHistoryLines(historyData).map(h => ({
|
|
534
|
+
value: h.name,
|
|
535
|
+
color: h.color,
|
|
536
|
+
dataKey: h.dataKey,
|
|
537
|
+
}))
|
|
538
|
+
: undefined,
|
|
539
|
+
[historyData, showLegend],
|
|
540
|
+
);
|
|
485
541
|
|
|
486
542
|
const legendPayload = useMemo(() => {
|
|
487
543
|
if (!showLegend) return [];
|
|
@@ -614,12 +670,12 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
614
670
|
let effectiveAutoScale = autoScaleYAxis;
|
|
615
671
|
if (autoScaleYAxis !== false && yMin !== undefined && yMax !== undefined) {
|
|
616
672
|
const dataKeys = chartData.length > 0 ? Object.keys(chartData[0]) : [];
|
|
617
|
-
const
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
673
|
+
const visibleHistoryKeys = historySeriesKeys(historyData).filter(
|
|
674
|
+
k => includeHiddenInYScale || !hiddenSeries?.has(k),
|
|
675
|
+
);
|
|
676
|
+
const historicalValues = chartData
|
|
677
|
+
.flatMap(p => visibleHistoryKeys.map(k => p[k]))
|
|
678
|
+
.filter(v => v !== null && v !== undefined);
|
|
623
679
|
const forecastKeys = dataKeys.filter(k => k.startsWith('forecast_'));
|
|
624
680
|
const visibleForecastKeys =
|
|
625
681
|
!includeHiddenInYScale && hiddenSeries
|
|
@@ -631,10 +687,14 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
631
687
|
const allLineValues = [...historicalValues, ...forecastValues];
|
|
632
688
|
|
|
633
689
|
if (allLineValues.length > 0) {
|
|
634
|
-
//
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
690
|
+
// A loop, not Math.min(...values): one value per row and line can
|
|
691
|
+
// exceed the engine's argument limit.
|
|
692
|
+
let dataMin = Infinity;
|
|
693
|
+
let dataMax = -Infinity;
|
|
694
|
+
for (const v of allLineValues as number[]) {
|
|
695
|
+
if (v < dataMin) dataMin = v;
|
|
696
|
+
if (v > dataMax) dataMax = v;
|
|
697
|
+
}
|
|
638
698
|
const explicitRange = yMax - yMin;
|
|
639
699
|
const dataRange = dataMax - dataMin;
|
|
640
700
|
const wastedSpaceRatio = (explicitRange - dataRange) / dataRange;
|
|
@@ -649,6 +709,26 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
649
709
|
const ChartComponent = chartType === 'line' ? LineChart : ComposedChart;
|
|
650
710
|
const defaultLabelFormatter = (v: unknown) => formatDateFn(v as string, true);
|
|
651
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
|
+
|
|
652
732
|
return (
|
|
653
733
|
<div
|
|
654
734
|
className={cn(
|
|
@@ -676,21 +756,7 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
676
756
|
>
|
|
677
757
|
<ChartComponent data={chartData} margin={margin}>
|
|
678
758
|
<ChartGrid />
|
|
679
|
-
{
|
|
680
|
-
<ChartAxes
|
|
681
|
-
formatDate={formatDateFn}
|
|
682
|
-
formatNumber={formatNumber}
|
|
683
|
-
xAxisClassName={xAxisClassName}
|
|
684
|
-
yAxisClassName={yAxisClassName}
|
|
685
|
-
xAxisLabel={xAxisLabel}
|
|
686
|
-
yAxisLabel={yAxisLabel}
|
|
687
|
-
xMin={xMin}
|
|
688
|
-
xMax={xMax}
|
|
689
|
-
yMin={yMin}
|
|
690
|
-
yMax={yMax}
|
|
691
|
-
autoScaleYAxis={effectiveAutoScale}
|
|
692
|
-
/>
|
|
693
|
-
)}
|
|
759
|
+
{axes}
|
|
694
760
|
</ChartComponent>
|
|
695
761
|
</ChartContainer>
|
|
696
762
|
)}
|
|
@@ -703,22 +769,7 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
703
769
|
{...containerProps}
|
|
704
770
|
>
|
|
705
771
|
<ChartComponent data={chartData} margin={margin}>
|
|
706
|
-
{
|
|
707
|
-
{showAxes && (
|
|
708
|
-
<ChartAxes
|
|
709
|
-
formatDate={formatDateFn}
|
|
710
|
-
formatNumber={formatNumber}
|
|
711
|
-
xAxisClassName={cn(xAxisClassName)}
|
|
712
|
-
yAxisClassName={cn(yAxisClassName)}
|
|
713
|
-
xAxisLabel={xAxisLabel}
|
|
714
|
-
yAxisLabel={yAxisLabel}
|
|
715
|
-
xMin={xMin}
|
|
716
|
-
xMax={xMax}
|
|
717
|
-
yMin={yMin}
|
|
718
|
-
yMax={yMax}
|
|
719
|
-
autoScaleYAxis={effectiveAutoScale}
|
|
720
|
-
/>
|
|
721
|
-
)}
|
|
772
|
+
{axes}
|
|
722
773
|
|
|
723
774
|
{/* Quantile Bands (widest to narrowest) */}
|
|
724
775
|
{quantileBands?.[0] && (
|
|
@@ -737,6 +788,7 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
737
788
|
historicalLineColor={historicalLineColor}
|
|
738
789
|
chartData={chartData}
|
|
739
790
|
forecastData={forecastData}
|
|
791
|
+
historyData={historyData}
|
|
740
792
|
hiddenSeries={hiddenSeries}
|
|
741
793
|
isDarkTheme={isDarkTheme}
|
|
742
794
|
shouldAnimate={shouldAnimate}
|
|
@@ -744,14 +796,19 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
744
796
|
disableHistoricalAnimation={disableHistoricalAnimation}
|
|
745
797
|
showLegend={showLegend}
|
|
746
798
|
forecastLineStyle={forecastLineStyle}
|
|
799
|
+
connectHistoryNulls={connectHistoryNulls}
|
|
800
|
+
connectForecastNulls={connectForecastNulls}
|
|
801
|
+
historyOnTop={historyOnTop}
|
|
802
|
+
showEndDots={showEndDots}
|
|
747
803
|
/>
|
|
748
804
|
|
|
749
805
|
{showTooltip && (
|
|
750
806
|
<div>
|
|
751
|
-
<
|
|
807
|
+
<ChartPlacedTooltip
|
|
752
808
|
cursor={false}
|
|
753
809
|
offset={TOOLTIP_OFFSET}
|
|
754
810
|
allowEscapeViewBox={{ x: false, y: false }}
|
|
811
|
+
wrapperStyle={tooltipWrapperStyle()}
|
|
755
812
|
content={renderTooltipContent}
|
|
756
813
|
/>
|
|
757
814
|
</div>
|
|
@@ -765,11 +822,14 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
765
822
|
{showLegend &&
|
|
766
823
|
(legendVariant === 'svg' ? (
|
|
767
824
|
<LegendSvg
|
|
768
|
-
payload={
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
825
|
+
payload={[
|
|
826
|
+
...(historyLegend ?? []).map(p => ({ ...p, solid: true })),
|
|
827
|
+
...legendPayload.map(p => ({
|
|
828
|
+
value: p.value,
|
|
829
|
+
color: p.color,
|
|
830
|
+
dataKey: p.dataKey,
|
|
831
|
+
})),
|
|
832
|
+
]}
|
|
773
833
|
width={legendWidth}
|
|
774
834
|
marginLeft={legendMarginLeft}
|
|
775
835
|
/>
|
|
@@ -777,6 +837,7 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
777
837
|
<CustomChartLegend
|
|
778
838
|
className={legendClassName}
|
|
779
839
|
payload={legendPayload}
|
|
840
|
+
histories={historyLegend}
|
|
780
841
|
onClick={onLegendClick}
|
|
781
842
|
hiddenSeries={hiddenSeries}
|
|
782
843
|
onAnalysisSelect={onAnalysisSelect}
|
|
@@ -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
|
}
|