@sybilion/uilib 1.8.2 → 1.8.4
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/BarChart/BarChart.js +39 -7
- package/dist/esm/src/components/ui/Chart/components/BaseChartWrapper.js +71 -6
- package/dist/esm/src/components/ui/Chart/components/BaseChartWrapper.styl.js +2 -2
- package/dist/esm/src/components/ui/Chart/components/ChartAxes.js +11 -3
- package/dist/esm/src/components/ui/Chart/components/ChartTooltipContent.js +6 -3
- package/dist/esm/src/components/ui/Chart/components/QuantileBands.js +1 -1
- package/dist/esm/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.js +20 -0
- package/dist/esm/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.styl.js +7 -0
- package/dist/esm/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.js +2 -2
- package/dist/esm/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.styl.js +2 -2
- package/dist/esm/src/components/ui/ChartAreaInteractive/ChartLines.js +49 -13
- package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.js +45 -11
- package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.js +15 -8
- package/dist/esm/src/components/ui/TimeRangeControls/TimeRangeControls.js +1 -1
- package/dist/esm/src/components/ui/Toggle/Toggle.styl.js +2 -2
- package/dist/esm/src/components/ui/ToggleGroup/ToggleGroup.js +1 -1
- package/dist/esm/src/components/ui/ToggleGroup/ToggleGroup.styl.js +1 -1
- package/dist/esm/types/src/components/ui/BarChart/BarChart.types.d.ts +22 -0
- package/dist/esm/types/src/components/ui/Chart/chartForecastVisualization.types.d.ts +5 -0
- package/dist/esm/types/src/components/ui/Chart/components/BaseChartWrapper.d.ts +22 -5
- package/dist/esm/types/src/components/ui/Chart/components/ChartAxes.d.ts +8 -1
- package/dist/esm/types/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.d.ts +22 -0
- package/dist/esm/types/src/components/ui/Chart/components/SeriesLegend/index.d.ts +1 -0
- package/dist/esm/types/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.d.ts +1 -1
- package/dist/esm/types/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.types.d.ts +7 -0
- package/dist/esm/types/src/components/ui/ChartAreaInteractive/ChartLines.d.ts +10 -1
- package/dist/esm/types/src/components/ui/ChartAreaInteractive/chartHistories.d.ts +5 -0
- package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.d.ts +10 -3
- package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.d.ts +1 -1
- package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.types.d.ts +9 -0
- package/dist/esm/types/src/components/ui/TimeRangeControls/TimeRangeControls.d.ts +7 -1
- package/dist/esm/types/src/components/ui/Toggle/Toggle.types.d.ts +1 -1
- package/dist/esm/types/src/components/ui/ToggleGroup/ToggleGroup.helpers.d.ts +2 -2
- package/dist/esm/types/src/components/ui/ToggleGroup/ToggleGroup.types.d.ts +3 -3
- package/dist/esm/types/tests/viewChartGaps.test.d.ts +1 -0
- package/package.json +1 -1
- package/src/components/ui/BarChart/AGENT.md +4 -0
- package/src/components/ui/BarChart/BarChart.styl.d.ts +1 -1
- package/src/components/ui/BarChart/BarChart.tsx +107 -21
- package/src/components/ui/BarChart/BarChart.types.ts +22 -0
- package/src/components/ui/Chart/AGENT.md +3 -0
- package/src/components/ui/Chart/chartForecastVisualization.types.ts +5 -0
- package/src/components/ui/Chart/components/BaseChartWrapper.styl +5 -0
- package/src/components/ui/Chart/components/BaseChartWrapper.styl.d.ts +1 -0
- package/src/components/ui/Chart/components/BaseChartWrapper.tsx +124 -5
- package/src/components/ui/Chart/components/ChartAxes.tsx +29 -5
- package/src/components/ui/Chart/components/ChartTooltipContent.tsx +6 -3
- package/src/components/ui/Chart/components/QuantileBands.tsx +1 -0
- package/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.styl +34 -0
- package/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.styl.d.ts +12 -0
- package/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.tsx +70 -0
- package/src/components/ui/Chart/components/SeriesLegend/index.ts +1 -0
- package/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.styl +5 -0
- package/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.styl.d.ts +1 -0
- package/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.tsx +7 -1
- package/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.types.ts +7 -0
- package/src/components/ui/ChartAreaInteractive/ChartLines.tsx +110 -40
- package/src/components/ui/ChartAreaInteractive/chartHistories.ts +5 -0
- package/src/components/ui/StackedAreaChart/AGENT.md +1 -0
- package/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.ts +54 -13
- package/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.tsx +1 -1
- package/src/components/ui/StackedAreaChart/StackedAreaChart.tsx +36 -12
- package/src/components/ui/StackedAreaChart/StackedAreaChart.types.ts +9 -0
- package/src/components/ui/TimeRangeControls/AGENT.md +1 -1
- package/src/components/ui/TimeRangeControls/TimeRangeControls.test.tsx +21 -0
- package/src/components/ui/TimeRangeControls/TimeRangeControls.tsx +13 -2
- package/src/components/ui/Toggle/Toggle.styl +24 -0
- package/src/components/ui/Toggle/Toggle.styl.d.ts +1 -0
- package/src/components/ui/Toggle/Toggle.types.ts +1 -1
- package/src/components/ui/ToggleGroup/AGENT.md +1 -0
- package/src/components/ui/ToggleGroup/ToggleGroup.helpers.ts +2 -2
- package/src/components/ui/ToggleGroup/ToggleGroup.styl +8 -0
- package/src/components/ui/ToggleGroup/ToggleGroup.tsx +1 -0
- package/src/components/ui/ToggleGroup/ToggleGroup.types.ts +3 -3
- package/src/docs/pages/ChartAreaInteractivePage.tsx +34 -0
- package/src/docs/pages/TimeRangeControlsPage.tsx +9 -0
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import cn from 'classnames';
|
|
2
2
|
import { forwardRef, useMemo } from 'react';
|
|
3
3
|
|
|
4
|
-
import type { ChartConfig } from '#uilib/components/ui/Chart';
|
|
5
|
-
import {
|
|
6
|
-
ChartContainer,
|
|
7
|
-
ChartLegend,
|
|
8
|
-
ChartLegendContent,
|
|
9
|
-
ChartTooltip,
|
|
10
|
-
ChartTooltipContent,
|
|
11
|
-
} from '#uilib/components/ui/Chart';
|
|
4
|
+
import type { ChartConfig } from '#uilib/components/ui/Chart/Chart.types';
|
|
12
5
|
import { getChartSeriesColor } from '#uilib/components/ui/Chart/chartSeriesPalette';
|
|
6
|
+
// Straight from the components, not the Chart index, which also loads the
|
|
7
|
+
// canvas chart (lightweight-charts) and its styles.
|
|
8
|
+
import { ChartContainer } from '#uilib/components/ui/Chart/components/ChartContainer';
|
|
9
|
+
import { ChartPlacedTooltip as ChartTooltip } from '#uilib/components/ui/Chart/components/ChartPlacedTooltip';
|
|
10
|
+
import { ChartTooltipContent } from '#uilib/components/ui/Chart/components/ChartTooltipContent';
|
|
11
|
+
import { SeriesLegend } from '#uilib/components/ui/Chart/components/SeriesLegend';
|
|
12
|
+
import { useLegendState } from '#uilib/components/ui/Chart/hooks/useLegendState';
|
|
13
13
|
import {
|
|
14
14
|
Bar,
|
|
15
15
|
CartesianGrid,
|
|
16
|
+
Label,
|
|
16
17
|
BarChart as RechartsBarChart,
|
|
17
18
|
XAxis,
|
|
18
19
|
YAxis,
|
|
@@ -22,6 +23,11 @@ import S from './BarChart.styl';
|
|
|
22
23
|
import { BarChartProps, BarChartSeries } from './BarChart.types';
|
|
23
24
|
|
|
24
25
|
const STACK_ID = 'stack';
|
|
26
|
+
/** Room for an axis title beside the ticks (recharts' default is 30). */
|
|
27
|
+
const CATEGORY_AXIS_HEIGHT_WITH_LABEL = 44;
|
|
28
|
+
|
|
29
|
+
const seriesColor = (item: BarChartSeries, index: number) =>
|
|
30
|
+
item.color ?? getChartSeriesColor(index);
|
|
25
31
|
|
|
26
32
|
function buildChartConfig(series: BarChartSeries[]): ChartConfig {
|
|
27
33
|
const config: ChartConfig = {};
|
|
@@ -29,7 +35,7 @@ function buildChartConfig(series: BarChartSeries[]): ChartConfig {
|
|
|
29
35
|
series.forEach((item, index) => {
|
|
30
36
|
config[item.key] = {
|
|
31
37
|
label: item.label ?? item.key,
|
|
32
|
-
color: item
|
|
38
|
+
color: seriesColor(item, index),
|
|
33
39
|
};
|
|
34
40
|
});
|
|
35
41
|
|
|
@@ -49,18 +55,58 @@ const BarChart = forwardRef<HTMLDivElement, BarChartProps>(
|
|
|
49
55
|
showTooltip = true,
|
|
50
56
|
showGrid = true,
|
|
51
57
|
valueFormatter,
|
|
58
|
+
tooltipValueFormatter,
|
|
59
|
+
categoryTickFormatter,
|
|
60
|
+
tooltipLabelFormatter,
|
|
61
|
+
categoryAxisLabel,
|
|
62
|
+
valueAxisLabel,
|
|
63
|
+
hiddenKeys,
|
|
64
|
+
onToggleKey,
|
|
52
65
|
height,
|
|
66
|
+
disableAnimation,
|
|
53
67
|
...props
|
|
54
68
|
},
|
|
55
69
|
ref,
|
|
56
70
|
) => {
|
|
57
71
|
const config = useMemo(() => buildChartConfig(series), [series]);
|
|
72
|
+
const legend = useLegendState();
|
|
73
|
+
const hidden: ReadonlySet<string> = hiddenKeys ?? legend.hiddenSeries;
|
|
74
|
+
// The legend entries are buttons only for a host that asked for toggling
|
|
75
|
+
// (onToggleKey); without hiddenKeys the chart keeps the hidden set itself.
|
|
76
|
+
const toggle = onToggleKey
|
|
77
|
+
? (key: string) => {
|
|
78
|
+
onToggleKey(key);
|
|
79
|
+
if (!hiddenKeys) legend.toggleSeries(key);
|
|
80
|
+
}
|
|
81
|
+
: undefined;
|
|
58
82
|
|
|
59
83
|
const isHorizontal = orientation === 'horizontal';
|
|
60
84
|
const stackId = mode === 'stacked' ? STACK_ID : undefined;
|
|
61
85
|
const barRadius: [number, number, number, number] | number =
|
|
62
86
|
mode === 'stacked' ? 0 : isHorizontal ? [0, 3, 3, 0] : [3, 3, 0, 0];
|
|
63
87
|
|
|
88
|
+
const axisLine = { stroke: 'var(--border)', strokeWidth: 1 };
|
|
89
|
+
const categoryLabel = categoryAxisLabel ? (
|
|
90
|
+
isHorizontal ? (
|
|
91
|
+
<Label value={categoryAxisLabel} angle={-90} position="insideLeft" />
|
|
92
|
+
) : (
|
|
93
|
+
<Label value={categoryAxisLabel} position="insideBottom" offset={0} />
|
|
94
|
+
)
|
|
95
|
+
) : null;
|
|
96
|
+
const valueLabel = valueAxisLabel ? (
|
|
97
|
+
isHorizontal ? (
|
|
98
|
+
<Label value={valueAxisLabel} position="insideBottom" offset={0} />
|
|
99
|
+
) : (
|
|
100
|
+
<Label value={valueAxisLabel} angle={-90} position="insideLeft" />
|
|
101
|
+
)
|
|
102
|
+
) : null;
|
|
103
|
+
const labelFormatter = tooltipLabelFormatter
|
|
104
|
+
? (label: unknown) =>
|
|
105
|
+
tooltipLabelFormatter(
|
|
106
|
+
typeof label === 'number' ? label : String(label ?? ''),
|
|
107
|
+
)
|
|
108
|
+
: undefined;
|
|
109
|
+
|
|
64
110
|
return (
|
|
65
111
|
<div ref={ref} className={cn(S.root, className)} {...props}>
|
|
66
112
|
<ChartContainer
|
|
@@ -85,16 +131,25 @@ const BarChart = forwardRef<HTMLDivElement, BarChartProps>(
|
|
|
85
131
|
<XAxis
|
|
86
132
|
type="number"
|
|
87
133
|
tickLine={false}
|
|
88
|
-
axisLine={
|
|
134
|
+
axisLine={axisLine}
|
|
89
135
|
tickFormatter={valueFormatter}
|
|
90
|
-
|
|
136
|
+
height={
|
|
137
|
+
valueAxisLabel ? CATEGORY_AXIS_HEIGHT_WITH_LABEL : undefined
|
|
138
|
+
}
|
|
139
|
+
>
|
|
140
|
+
{valueLabel}
|
|
141
|
+
</XAxis>
|
|
91
142
|
<YAxis
|
|
92
143
|
type="category"
|
|
93
144
|
dataKey={categoryKey}
|
|
94
145
|
tickLine={false}
|
|
95
|
-
axisLine={
|
|
96
|
-
|
|
97
|
-
|
|
146
|
+
axisLine={axisLine}
|
|
147
|
+
tickFormatter={categoryTickFormatter}
|
|
148
|
+
// Room for the rotated title beside the category names.
|
|
149
|
+
width={categoryAxisLabel ? 96 : 80}
|
|
150
|
+
>
|
|
151
|
+
{categoryLabel}
|
|
152
|
+
</YAxis>
|
|
98
153
|
</>
|
|
99
154
|
) : (
|
|
100
155
|
<>
|
|
@@ -102,21 +157,38 @@ const BarChart = forwardRef<HTMLDivElement, BarChartProps>(
|
|
|
102
157
|
type="category"
|
|
103
158
|
dataKey={categoryKey}
|
|
104
159
|
tickLine={false}
|
|
105
|
-
axisLine={
|
|
106
|
-
|
|
160
|
+
axisLine={axisLine}
|
|
161
|
+
tickFormatter={categoryTickFormatter}
|
|
162
|
+
height={
|
|
163
|
+
categoryAxisLabel
|
|
164
|
+
? CATEGORY_AXIS_HEIGHT_WITH_LABEL
|
|
165
|
+
: undefined
|
|
166
|
+
}
|
|
167
|
+
>
|
|
168
|
+
{categoryLabel}
|
|
169
|
+
</XAxis>
|
|
107
170
|
<YAxis
|
|
108
171
|
type="number"
|
|
109
172
|
tickLine={false}
|
|
110
|
-
axisLine={
|
|
111
|
-
|
|
173
|
+
axisLine={axisLine}
|
|
174
|
+
// Room for the title beside the tick labels.
|
|
175
|
+
width={valueAxisLabel ? 64 : 48}
|
|
112
176
|
tickFormatter={valueFormatter}
|
|
113
|
-
|
|
177
|
+
>
|
|
178
|
+
{valueLabel}
|
|
179
|
+
</YAxis>
|
|
114
180
|
</>
|
|
115
181
|
)}
|
|
116
182
|
{showTooltip && (
|
|
117
183
|
<ChartTooltip
|
|
118
184
|
cursor={{ fill: 'var(--muted)' }}
|
|
119
|
-
content={
|
|
185
|
+
content={
|
|
186
|
+
<ChartTooltipContent
|
|
187
|
+
indicator="dot"
|
|
188
|
+
valueFormatter={tooltipValueFormatter}
|
|
189
|
+
labelFormatter={labelFormatter}
|
|
190
|
+
/>
|
|
191
|
+
}
|
|
120
192
|
/>
|
|
121
193
|
)}
|
|
122
194
|
{series.map(item => (
|
|
@@ -127,11 +199,25 @@ const BarChart = forwardRef<HTMLDivElement, BarChartProps>(
|
|
|
127
199
|
stackId={stackId}
|
|
128
200
|
radius={barRadius}
|
|
129
201
|
maxBarSize={48}
|
|
202
|
+
hide={hidden.has(item.key)}
|
|
203
|
+
isAnimationActive={!disableAnimation}
|
|
130
204
|
/>
|
|
131
205
|
))}
|
|
132
|
-
{showLegend && <ChartLegend content={<ChartLegendContent />} />}
|
|
133
206
|
</RechartsBarChart>
|
|
134
207
|
</ChartContainer>
|
|
208
|
+
|
|
209
|
+
{showLegend && (
|
|
210
|
+
<SeriesLegend
|
|
211
|
+
items={series.map((item, index) => ({
|
|
212
|
+
dataKey: item.key,
|
|
213
|
+
label: item.label ?? item.key,
|
|
214
|
+
color: seriesColor(item, index),
|
|
215
|
+
square: true,
|
|
216
|
+
}))}
|
|
217
|
+
hiddenSeries={hidden}
|
|
218
|
+
onToggle={toggle}
|
|
219
|
+
/>
|
|
220
|
+
)}
|
|
135
221
|
</div>
|
|
136
222
|
);
|
|
137
223
|
},
|
|
@@ -31,6 +31,28 @@ export interface BarChartProps extends Omit<
|
|
|
31
31
|
showGrid?: boolean;
|
|
32
32
|
/** Formats numeric axis tick values. */
|
|
33
33
|
valueFormatter?: (value: number) => string;
|
|
34
|
+
/** Formats the tooltip's values (for example with a unit); without it they print as they are. */
|
|
35
|
+
tooltipValueFormatter?: (value: number) => string;
|
|
36
|
+
/** Formats the category axis's ticks. */
|
|
37
|
+
categoryTickFormatter?: (value: string | number) => string;
|
|
38
|
+
/** Formats the tooltip's category label. */
|
|
39
|
+
tooltipLabelFormatter?: (value: string | number) => string;
|
|
40
|
+
/** Title of the category axis. */
|
|
41
|
+
categoryAxisLabel?: string;
|
|
42
|
+
/** Title of the value axis. */
|
|
43
|
+
valueAxisLabel?: string;
|
|
44
|
+
/**
|
|
45
|
+
* Keys of hidden series. With it, the host owns visibility; without it
|
|
46
|
+
* (and with onToggleKey) the chart keeps the hidden set itself.
|
|
47
|
+
*/
|
|
48
|
+
hiddenKeys?: ReadonlySet<string>;
|
|
49
|
+
/**
|
|
50
|
+
* Called with a series key when its legend entry is clicked. Without it the
|
|
51
|
+
* legend is plain text.
|
|
52
|
+
*/
|
|
53
|
+
onToggleKey?: (key: string) => void;
|
|
34
54
|
/** Fixed chart height in px; defaults to a 16:9 responsive box. */
|
|
35
55
|
height?: number;
|
|
56
|
+
/** Draw the bars at once, without the grow-in animation. */
|
|
57
|
+
disableAnimation?: boolean;
|
|
36
58
|
}
|
|
@@ -16,6 +16,9 @@ ChartContainer wraps Recharts charts with the library’s theming, mapping a Cha
|
|
|
16
16
|
- `historyOnTop` draws the history lines over the forecast lines, for many faint forecasts that would otherwise cover them.
|
|
17
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
18
|
- `showEndDots={false}` drops the dot on each line's last point (for many lines drawn as one bundle).
|
|
19
|
+
- A history or forecast item's `connectNulls` overrides the chart's `connectHistoryNulls` / `connectForecastNulls` for that line. With `dotLonePoints`, a point the line would leave invisible gets a dot: on a line not drawn across gaps, a point with no value on either side; on a line drawn across gaps, its only value.
|
|
20
|
+
- `legendVariant="plain"` lists every line (history and forecast alike) in `legendOrder` (their data keys), each a button that calls `onLegendClick` with its data key; the default legend is the analyses selector (forecast icons, +N overflow, latest first). A band with `showInTooltip: false` stays out of the tooltip.
|
|
21
|
+
- `xAxisType="number"` places the rows by their `date` read as a number, spaced by value; `xTicks` names the x values to label (for example the first day of each month on a daily chart).
|
|
19
22
|
- 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.
|
|
20
23
|
|
|
21
24
|
## Example
|
|
@@ -96,6 +96,11 @@ export interface QuantileBandConfig {
|
|
|
96
96
|
| 'basis'
|
|
97
97
|
| 'natural';
|
|
98
98
|
fillPattern?: 'solid' | 'dots' | 'lines';
|
|
99
|
+
/**
|
|
100
|
+
* List the range in the tooltip (default true). Read by the Recharts charts'
|
|
101
|
+
* bands (BaseChartWrapper, ChartAreaInteractive's default mode).
|
|
102
|
+
*/
|
|
103
|
+
showInTooltip?: boolean;
|
|
99
104
|
/**
|
|
100
105
|
* Fill across months with no range (default true). Read by the Recharts
|
|
101
106
|
* charts' bands (BaseChartWrapper, ChartAreaInteractive's default mode),
|
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
resolveHistoryLines,
|
|
19
19
|
} from '#uilib/components/ui/ChartAreaInteractive/ChartLines';
|
|
20
20
|
import {
|
|
21
|
+
HISTORY_SERIES_KEY,
|
|
21
22
|
HistoryItemData,
|
|
22
23
|
historySeriesKeys,
|
|
23
24
|
isHistorySeriesKey,
|
|
@@ -44,6 +45,7 @@ import { ChartTooltipContent } from './ChartTooltipContent';
|
|
|
44
45
|
import { CustomChartLegend } from './CustomChartLegend/CustomChartLegend';
|
|
45
46
|
import { LegendSvg } from './LegendSvg/LegendSvg';
|
|
46
47
|
import { QuantileBands } from './QuantileBands';
|
|
48
|
+
import { SeriesLegend, type SeriesLegendItem } from './SeriesLegend';
|
|
47
49
|
|
|
48
50
|
function clampTooltipTranslate(args: {
|
|
49
51
|
coordinate: { x: number; y: number };
|
|
@@ -146,6 +148,12 @@ export interface BaseChartWrapperProps {
|
|
|
146
148
|
historyOnTop?: boolean;
|
|
147
149
|
/** Mark each line's last point with a dot (default: on). */
|
|
148
150
|
showEndDots?: boolean;
|
|
151
|
+
/**
|
|
152
|
+
* Dot a point a line would leave invisible, so it still shows: with no
|
|
153
|
+
* value on either side on a line not drawn across gaps, or a line's only
|
|
154
|
+
* value on one drawn across them (default: off).
|
|
155
|
+
*/
|
|
156
|
+
dotLonePoints?: boolean;
|
|
149
157
|
formatDate?: (value: string, detailed?: boolean) => string;
|
|
150
158
|
labelFormatter?: (value: unknown) => string;
|
|
151
159
|
onLegendClick?: (data: LegendPayload, index: number, event: any) => void;
|
|
@@ -169,6 +177,13 @@ export interface BaseChartWrapperProps {
|
|
|
169
177
|
showChartAxesLegend?: boolean;
|
|
170
178
|
xAxisLabel?: string;
|
|
171
179
|
yAxisLabel?: string;
|
|
180
|
+
/**
|
|
181
|
+
* `category` (default) gives every row one slot; `number` places the rows
|
|
182
|
+
* by their `date` read as a number (`'12.5'`), spaced by value.
|
|
183
|
+
*/
|
|
184
|
+
xAxisType?: 'category' | 'number';
|
|
185
|
+
/** The x values to label (for example the first day of each month). */
|
|
186
|
+
xTicks?: ReadonlyArray<string | number>;
|
|
172
187
|
showActiveDots?: boolean;
|
|
173
188
|
overlayElements?: ReactNode;
|
|
174
189
|
hiddenSeries?: Set<string>;
|
|
@@ -179,7 +194,15 @@ export interface BaseChartWrapperProps {
|
|
|
179
194
|
onFailedAnalysisClick?: (analysisId?: number | string) => void;
|
|
180
195
|
maxVisibleItems?: number;
|
|
181
196
|
preventDeselection?: boolean;
|
|
182
|
-
|
|
197
|
+
/**
|
|
198
|
+
* `default`: the analyses legend (forecast icons, +N overflow, latest
|
|
199
|
+
* first). `svg`: drawn into the chart for export. `plain`: one entry per
|
|
200
|
+
* line, in `legendOrder` (else histories then forecasts), each a button
|
|
201
|
+
* that hides or shows it through `onLegendClick`.
|
|
202
|
+
*/
|
|
203
|
+
legendVariant?: 'default' | 'svg' | 'plain';
|
|
204
|
+
/** With `legendVariant="plain"`: the lines' data keys in legend order. */
|
|
205
|
+
legendOrder?: string[];
|
|
183
206
|
legendWidth?: number;
|
|
184
207
|
legendMarginLeft?: number;
|
|
185
208
|
containerProps?: Omit<
|
|
@@ -275,7 +298,8 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
275
298
|
connectForecastNulls,
|
|
276
299
|
historyOnTop,
|
|
277
300
|
showEndDots,
|
|
278
|
-
|
|
301
|
+
dotLonePoints,
|
|
302
|
+
formatDate: formatDateProp,
|
|
279
303
|
labelFormatter,
|
|
280
304
|
onLegendClick,
|
|
281
305
|
margin,
|
|
@@ -289,6 +313,8 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
289
313
|
showChartAxesLegend = true,
|
|
290
314
|
xAxisLabel,
|
|
291
315
|
yAxisLabel,
|
|
316
|
+
xAxisType,
|
|
317
|
+
xTicks,
|
|
292
318
|
showActiveDots = true,
|
|
293
319
|
overlayElements,
|
|
294
320
|
hiddenSeries,
|
|
@@ -311,9 +337,13 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
311
337
|
maxVisibleItems,
|
|
312
338
|
preventDeselection,
|
|
313
339
|
legendVariant = 'default',
|
|
340
|
+
legendOrder,
|
|
314
341
|
legendWidth = 1000,
|
|
315
342
|
legendMarginLeft = 0,
|
|
316
343
|
} = props;
|
|
344
|
+
// A number axis has no dates to format: its default prints the number.
|
|
345
|
+
const formatDateFn =
|
|
346
|
+
formatDateProp ?? (xAxisType === 'number' ? String : formatDate);
|
|
317
347
|
|
|
318
348
|
const [shouldAnimate, setShouldAnimate] = useState(false);
|
|
319
349
|
const lineDataInitializedRef = useRef(false);
|
|
@@ -464,7 +494,17 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
464
494
|
const renderTooltipContent = (props: any) => {
|
|
465
495
|
// Filter payload to exclude items with null/undefined values
|
|
466
496
|
// This prevents showing stale data when hovering on dates without data points
|
|
467
|
-
const
|
|
497
|
+
const normalized = normalizeTooltipPayload(props.payload);
|
|
498
|
+
// With `legendOrder`, the rows read in the legend's order (recharts lists
|
|
499
|
+
// every history line before the forecasts).
|
|
500
|
+
const filteredPayload =
|
|
501
|
+
legendOrder && normalized
|
|
502
|
+
? [...normalized].sort(
|
|
503
|
+
(a, b) =>
|
|
504
|
+
orderRank(legendOrder, a.dataKey) -
|
|
505
|
+
orderRank(legendOrder, b.dataKey),
|
|
506
|
+
)
|
|
507
|
+
: normalized;
|
|
468
508
|
|
|
469
509
|
// If no valid payload items, render ChartTooltipContent with active=false and empty payload
|
|
470
510
|
// This allows ChartTooltipContent to clear its lastTooltipData state
|
|
@@ -560,6 +600,55 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
560
600
|
}));
|
|
561
601
|
}, [forecastData, excludeLegendIds, showLegend]);
|
|
562
602
|
|
|
603
|
+
// The plain legend: every line in `legendOrder` (else histories, then
|
|
604
|
+
// forecasts), forecasts drawn dashed when their lines are.
|
|
605
|
+
const plainLegend = useMemo((): SeriesLegendItem[] => {
|
|
606
|
+
if (!showLegend || legendVariant !== 'plain') return [];
|
|
607
|
+
// Without historyData the chart draws the single `historical` line;
|
|
608
|
+
// it is listed when it has a value to draw.
|
|
609
|
+
const singleHistory =
|
|
610
|
+
!historyData?.length &&
|
|
611
|
+
chartData.some(row => row[HISTORY_SERIES_KEY] != null)
|
|
612
|
+
? [
|
|
613
|
+
{
|
|
614
|
+
dataKey: HISTORY_SERIES_KEY,
|
|
615
|
+
label: 'Historical Data',
|
|
616
|
+
color: historicalLineColor ?? (isDarkTheme ? '#ffffff' : '#000000'),
|
|
617
|
+
},
|
|
618
|
+
]
|
|
619
|
+
: [];
|
|
620
|
+
const all: SeriesLegendItem[] = [
|
|
621
|
+
...singleHistory,
|
|
622
|
+
...(historyLegend ?? []).map(h => ({
|
|
623
|
+
dataKey: h.dataKey,
|
|
624
|
+
label: h.value,
|
|
625
|
+
color: h.color,
|
|
626
|
+
})),
|
|
627
|
+
...legendPayload.map(p => ({
|
|
628
|
+
dataKey: p.dataKey,
|
|
629
|
+
label: p.value,
|
|
630
|
+
color: p.color,
|
|
631
|
+
dashed: forecastLineStyle === 'dashed',
|
|
632
|
+
})),
|
|
633
|
+
];
|
|
634
|
+
if (!legendOrder) return all;
|
|
635
|
+
return [...all].sort(
|
|
636
|
+
(a, b) =>
|
|
637
|
+
orderRank(legendOrder, a.dataKey) - orderRank(legendOrder, b.dataKey),
|
|
638
|
+
);
|
|
639
|
+
}, [
|
|
640
|
+
showLegend,
|
|
641
|
+
legendVariant,
|
|
642
|
+
historyData,
|
|
643
|
+
chartData,
|
|
644
|
+
historicalLineColor,
|
|
645
|
+
isDarkTheme,
|
|
646
|
+
historyLegend,
|
|
647
|
+
legendPayload,
|
|
648
|
+
forecastLineStyle,
|
|
649
|
+
legendOrder,
|
|
650
|
+
]);
|
|
651
|
+
|
|
563
652
|
useEffect(() => {
|
|
564
653
|
if (disableAnimation) {
|
|
565
654
|
setShouldAnimate(false);
|
|
@@ -721,6 +810,8 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
721
810
|
yAxisClassName={yAxisClassName}
|
|
722
811
|
xAxisLabel={xAxisLabel}
|
|
723
812
|
yAxisLabel={yAxisLabel}
|
|
813
|
+
xAxisType={xAxisType}
|
|
814
|
+
xTicks={xTicks}
|
|
724
815
|
xMin={xMin}
|
|
725
816
|
xMax={xMax}
|
|
726
817
|
yMin={yMin}
|
|
@@ -776,7 +867,7 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
776
867
|
<QuantileBands
|
|
777
868
|
hiddenBands={hiddenSeries}
|
|
778
869
|
quantileBandKey={quantileBandKey}
|
|
779
|
-
animate
|
|
870
|
+
animate={!disableAnimation}
|
|
780
871
|
animationDuration={150}
|
|
781
872
|
animationBegin={0}
|
|
782
873
|
customBands={quantileBands}
|
|
@@ -800,6 +891,7 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
800
891
|
connectForecastNulls={connectForecastNulls}
|
|
801
892
|
historyOnTop={historyOnTop}
|
|
802
893
|
showEndDots={showEndDots}
|
|
894
|
+
dotLonePoints={dotLonePoints}
|
|
803
895
|
/>
|
|
804
896
|
|
|
805
897
|
{showTooltip && (
|
|
@@ -820,7 +912,27 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
820
912
|
|
|
821
913
|
<div className={cn(S.footer, footerClassName)}>
|
|
822
914
|
{showLegend &&
|
|
823
|
-
(legendVariant === '
|
|
915
|
+
(legendVariant === 'plain' ? (
|
|
916
|
+
<SeriesLegend
|
|
917
|
+
className={cn(S.plainLegend, legendClassName)}
|
|
918
|
+
items={plainLegend}
|
|
919
|
+
hiddenSeries={hiddenSeries}
|
|
920
|
+
onToggle={
|
|
921
|
+
onLegendClick
|
|
922
|
+
? dataKey => {
|
|
923
|
+
const index = plainLegend.findIndex(
|
|
924
|
+
item => item.dataKey === dataKey,
|
|
925
|
+
);
|
|
926
|
+
onLegendClick(
|
|
927
|
+
{ dataKey, value: plainLegend[index]?.label } as never,
|
|
928
|
+
index,
|
|
929
|
+
{},
|
|
930
|
+
);
|
|
931
|
+
}
|
|
932
|
+
: undefined
|
|
933
|
+
}
|
|
934
|
+
/>
|
|
935
|
+
) : legendVariant === 'svg' ? (
|
|
824
936
|
<LegendSvg
|
|
825
937
|
payload={[
|
|
826
938
|
...(historyLegend ?? []).map(p => ({ ...p, solid: true })),
|
|
@@ -863,6 +975,13 @@ BaseChartWrapperContent.displayName = 'BaseChartWrapperContent';
|
|
|
863
975
|
* Base chart wrapper component with common structure
|
|
864
976
|
* Handles container, grid, axes, tooltip, and legend
|
|
865
977
|
*/
|
|
978
|
+
|
|
979
|
+
/** A key's place in `order`; keys not in it go last, in their own order. */
|
|
980
|
+
function orderRank(order: readonly string[], key: unknown): number {
|
|
981
|
+
const i = typeof key === 'string' ? order.indexOf(key) : -1;
|
|
982
|
+
return i === -1 ? order.length : i;
|
|
983
|
+
}
|
|
984
|
+
|
|
866
985
|
export function BaseChartWrapper(props: BaseChartWrapperProps) {
|
|
867
986
|
const { renderId, chartData, loading, hasCombinedData, legendVariant } =
|
|
868
987
|
props;
|
|
@@ -8,6 +8,10 @@ import {
|
|
|
8
8
|
generateRoundedYTicks,
|
|
9
9
|
} from '../tools/formatters';
|
|
10
10
|
|
|
11
|
+
/** Axis room for a title beside the ticks. */
|
|
12
|
+
const X_AXIS_HEIGHT_WITH_LABEL = 44;
|
|
13
|
+
const Y_AXIS_WIDTH_WITH_LABEL = 72;
|
|
14
|
+
|
|
11
15
|
interface ChartAxesProps {
|
|
12
16
|
xAxisClassName?: string;
|
|
13
17
|
yAxisClassName?: string;
|
|
@@ -20,6 +24,13 @@ interface ChartAxesProps {
|
|
|
20
24
|
yMin?: number;
|
|
21
25
|
yMax?: number;
|
|
22
26
|
autoScaleYAxis?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* `category` (default) gives every row one slot; `number` places the rows
|
|
29
|
+
* by their `date` read as a number, from the smallest to the largest.
|
|
30
|
+
*/
|
|
31
|
+
xAxisType?: 'category' | 'number';
|
|
32
|
+
/** The x values to label; without it recharts picks them. */
|
|
33
|
+
xTicks?: ReadonlyArray<string | number>;
|
|
23
34
|
/**
|
|
24
35
|
* Draw no axes. A hidden x-axis is kept, so the tooltip still names the
|
|
25
36
|
* row's date. The y-axis keeps fixed `yMin`/`yMax` (with `autoScaleYAxis`
|
|
@@ -29,6 +40,8 @@ interface ChartAxesProps {
|
|
|
29
40
|
hide?: boolean;
|
|
30
41
|
}
|
|
31
42
|
|
|
43
|
+
const numericDate = (row: { date?: unknown }) => Number(row.date);
|
|
44
|
+
|
|
32
45
|
export function ChartAxes({
|
|
33
46
|
xAxisClassName,
|
|
34
47
|
yAxisClassName,
|
|
@@ -43,6 +56,8 @@ export function ChartAxes({
|
|
|
43
56
|
yMin,
|
|
44
57
|
yMax,
|
|
45
58
|
autoScaleYAxis = false,
|
|
59
|
+
xAxisType = 'category',
|
|
60
|
+
xTicks,
|
|
46
61
|
hide = false,
|
|
47
62
|
}: ChartAxesProps) {
|
|
48
63
|
const tickColor = 'var(--muted-foreground)';
|
|
@@ -96,19 +111,27 @@ export function ChartAxes({
|
|
|
96
111
|
<>
|
|
97
112
|
<XAxis
|
|
98
113
|
className={xAxisClassName}
|
|
99
|
-
|
|
114
|
+
// A number axis reads the row's `date` as a number (rows keep it as
|
|
115
|
+
// text, like every other chart's).
|
|
116
|
+
dataKey={xAxisType === 'number' ? numericDate : 'date'}
|
|
117
|
+
type={xAxisType}
|
|
100
118
|
hide={hide}
|
|
101
119
|
tickLine={false}
|
|
102
120
|
axisLine={false}
|
|
103
121
|
tickMargin={8}
|
|
104
|
-
minTickGap={32}
|
|
122
|
+
minTickGap={xTicks ? 8 : 32}
|
|
105
123
|
tick={{ fill: tickColor }}
|
|
106
124
|
tickFormatter={value => formatDate(value as string)}
|
|
107
|
-
|
|
108
|
-
|
|
125
|
+
// Room for the title under the ticks, as BarChart and
|
|
126
|
+
// StackedAreaChart keep.
|
|
127
|
+
{...(xAxisLabel && { height: X_AXIS_HEIGHT_WITH_LABEL })}
|
|
128
|
+
{...(xTicks && { ticks: [...xTicks] })}
|
|
129
|
+
{...(xMin !== undefined && xMax !== undefined
|
|
130
|
+
? { domain: [xMin, xMax] }
|
|
131
|
+
: xAxisType === 'number' && { domain: ['dataMin', 'dataMax'] })}
|
|
109
132
|
>
|
|
110
133
|
{xAxisLabel && (
|
|
111
|
-
<Label value={xAxisLabel} position="insideBottom" offset={
|
|
134
|
+
<Label value={xAxisLabel} position="insideBottom" offset={0} />
|
|
112
135
|
)}
|
|
113
136
|
</XAxis>
|
|
114
137
|
{hide && !autoScaleYAxis && yMin !== undefined && yMax !== undefined && (
|
|
@@ -123,6 +146,7 @@ export function ChartAxes({
|
|
|
123
146
|
minTickGap={32}
|
|
124
147
|
tick={{ fill: tickColor }}
|
|
125
148
|
tickFormatter={formatNumber}
|
|
149
|
+
{...(yAxisLabel && { width: Y_AXIS_WIDTH_WITH_LABEL })}
|
|
126
150
|
{...(yDomain && { domain: yDomain })}
|
|
127
151
|
{...(yTicks && { ticks: yTicks })}
|
|
128
152
|
allowDataOverflow={!autoScaleYAxis}
|
|
@@ -84,9 +84,12 @@ export function ChartTooltipContent({
|
|
|
84
84
|
const [item] = payloadArray;
|
|
85
85
|
const key = `${labelKey || item.dataKey || item.name || 'value'}`;
|
|
86
86
|
const itemConfig = getPayloadConfigFromPayload(config, item, key);
|
|
87
|
+
// A number x (a number axis) is the label as much as a text one is.
|
|
87
88
|
const value =
|
|
88
|
-
!labelKey &&
|
|
89
|
-
|
|
89
|
+
!labelKey &&
|
|
90
|
+
(typeof displayLabel === 'string' || typeof displayLabel === 'number')
|
|
91
|
+
? config[String(displayLabel) as keyof typeof config]?.label ||
|
|
92
|
+
displayLabel
|
|
90
93
|
: itemConfig?.label;
|
|
91
94
|
|
|
92
95
|
if (labelFormatter) {
|
|
@@ -97,7 +100,7 @@ export function ChartTooltipContent({
|
|
|
97
100
|
);
|
|
98
101
|
}
|
|
99
102
|
|
|
100
|
-
if (
|
|
103
|
+
if (value == null || value === '') {
|
|
101
104
|
return null;
|
|
102
105
|
}
|
|
103
106
|
|
|
@@ -58,6 +58,7 @@ export function QuantileBands({
|
|
|
58
58
|
fill={fillColor}
|
|
59
59
|
// fillOpacity={band.opacity}
|
|
60
60
|
legendType={showLegend === false ? 'none' : undefined}
|
|
61
|
+
tooltipType={band.showInTooltip === false ? 'none' : undefined}
|
|
61
62
|
connectNulls={band.connectNulls ?? true}
|
|
62
63
|
dot={false}
|
|
63
64
|
activeDot={false}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
.root
|
|
2
|
+
flex-wrap wrap
|
|
3
|
+
row-gap 0.5rem
|
|
4
|
+
margin-top 0.75rem
|
|
5
|
+
|
|
6
|
+
.button
|
|
7
|
+
appearance none
|
|
8
|
+
background none
|
|
9
|
+
border 0
|
|
10
|
+
padding 0
|
|
11
|
+
font inherit
|
|
12
|
+
color inherit
|
|
13
|
+
cursor pointer
|
|
14
|
+
|
|
15
|
+
.dimmed
|
|
16
|
+
opacity 0.4
|
|
17
|
+
|
|
18
|
+
// A short line in the series' colour (currentColor), dashed for a forecast.
|
|
19
|
+
.swatch
|
|
20
|
+
width 14px
|
|
21
|
+
height 0
|
|
22
|
+
flex-shrink 0
|
|
23
|
+
border-top 2px solid currentColor
|
|
24
|
+
|
|
25
|
+
.dashed
|
|
26
|
+
border-top-style dashed
|
|
27
|
+
|
|
28
|
+
// A bar's or an area's swatch.
|
|
29
|
+
.square
|
|
30
|
+
width 0.5rem
|
|
31
|
+
height 0.5rem
|
|
32
|
+
border-top 0
|
|
33
|
+
border-radius 2px
|
|
34
|
+
background currentColor
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// This file is automatically generated.
|
|
2
|
+
// Please do not change this file!
|
|
3
|
+
interface CssExports {
|
|
4
|
+
button: string;
|
|
5
|
+
dashed: string;
|
|
6
|
+
dimmed: string;
|
|
7
|
+
root: string;
|
|
8
|
+
square: string;
|
|
9
|
+
swatch: string;
|
|
10
|
+
}
|
|
11
|
+
export const cssExports: CssExports;
|
|
12
|
+
export default cssExports;
|