@sybilion/uilib 1.8.4 → 1.8.6
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 +27 -0
- package/dist/agent-glossary/content.generated.ts +27 -0
- package/dist/agent-glossary/content.index.md +1 -0
- package/dist/agent-glossary/content.md +27 -0
- package/dist/agent-glossary/content.prompt.md +27 -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/Chart/components/BaseChartWrapper.js +3 -1
- package/dist/esm/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.js +5 -1
- package/dist/esm/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.styl.js +2 -2
- package/dist/esm/src/components/ui/ChartAreaInteractive/ChartLines.js +15 -5
- package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.js +24 -1
- package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.styl.js +2 -2
- package/dist/esm/src/components/widgets/BacktestChart/BacktestChart.js +113 -0
- package/dist/esm/src/components/widgets/BacktestChart/BacktestChart.styl.js +7 -0
- package/dist/esm/src/components/widgets/BacktestChart/backtestChart.helpers.js +134 -0
- package/dist/esm/src/index.js +3 -1
- package/dist/esm/types/src/components/ui/Chart/components/BaseChartWrapper.d.ts +3 -2
- package/dist/esm/types/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.d.ts +2 -0
- package/dist/esm/types/src/components/ui/ChartAreaInteractive/ChartLines.d.ts +16 -1
- package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.types.d.ts +5 -0
- package/dist/esm/types/src/components/widgets/BacktestChart/BacktestChart.d.ts +37 -0
- package/dist/esm/types/src/components/widgets/BacktestChart/backtestChart.helpers.d.ts +61 -0
- package/dist/esm/types/src/components/widgets/BacktestChart/index.d.ts +2 -0
- package/dist/esm/types/src/docs/pages/BacktestChartPage.d.ts +1 -0
- package/dist/esm/types/src/index.d.ts +1 -0
- package/dist/esm/types/tests/backtestChart.test.d.ts +1 -0
- package/package.json +1 -1
- package/src/components/ui/Chart/components/BaseChartWrapper.tsx +6 -2
- package/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.styl +4 -0
- package/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.styl.d.ts +1 -0
- package/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.tsx +7 -1
- package/src/components/ui/ChartAreaInteractive/ChartLines.tsx +30 -4
- package/src/components/ui/StackedAreaChart/AGENT.md +1 -0
- package/src/components/ui/StackedAreaChart/StackedAreaChart.styl +3 -0
- package/src/components/ui/StackedAreaChart/StackedAreaChart.styl.d.ts +1 -0
- package/src/components/ui/StackedAreaChart/StackedAreaChart.tsx +44 -1
- package/src/components/ui/StackedAreaChart/StackedAreaChart.types.ts +5 -0
- package/src/components/widgets/BacktestChart/AGENT.md +24 -0
- package/src/components/widgets/BacktestChart/BacktestChart.styl +23 -0
- package/src/components/widgets/BacktestChart/BacktestChart.styl.d.ts +9 -0
- package/src/components/widgets/BacktestChart/BacktestChart.tsx +247 -0
- package/src/components/widgets/BacktestChart/backtestChart.helpers.ts +172 -0
- package/src/components/widgets/BacktestChart/index.ts +8 -0
- package/src/docs/pages/BacktestChartPage.tsx +94 -0
- package/src/docs/pages/StackedAreaChartPage.tsx +30 -0
- package/src/docs/registry.ts +6 -0
- package/src/index.ts +1 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ComponentProps, ReactNode } from 'react';
|
|
2
2
|
import type { QuantileBandConfig } from '#uilib/components/ui/Chart/chartForecastVisualization.types';
|
|
3
3
|
import { ChartDataPoint } from '#uilib/components/ui/ChartAreaInteractive/ChartAreaInteractive.types';
|
|
4
|
-
import { ForecastItemData } from '#uilib/components/ui/ChartAreaInteractive/ChartLines';
|
|
4
|
+
import { ForecastItemData, type ForecastLineStyle } from '#uilib/components/ui/ChartAreaInteractive/ChartLines';
|
|
5
5
|
import { HistoryItemData } from '#uilib/components/ui/ChartAreaInteractive/chartHistories';
|
|
6
6
|
import { LegendPayload } from 'recharts/types/component/DefaultLegendContent';
|
|
7
7
|
import type { ChartConfig } from '../Chart.types';
|
|
@@ -125,7 +125,8 @@ export interface BaseChartWrapperProps {
|
|
|
125
125
|
/** When true, no placeholder UI for empty `chartData` (parent renders overlay). */
|
|
126
126
|
suppressNoDataState?: boolean;
|
|
127
127
|
onGridHeightChange?: (height: number) => void;
|
|
128
|
-
|
|
128
|
+
/** Every forecast line's style, unless the line sets its own `lineStyle`. */
|
|
129
|
+
forecastLineStyle?: ForecastLineStyle;
|
|
129
130
|
disableHistoricalAnimation?: boolean;
|
|
130
131
|
/** Clear all hidden series (legend “show all”). */
|
|
131
132
|
onShowAll?: () => void;
|
|
@@ -6,6 +6,8 @@ export type SeriesLegendItem = {
|
|
|
6
6
|
color: string;
|
|
7
7
|
/** Draw the swatch as a dashed line (a forecast line). */
|
|
8
8
|
dashed?: boolean;
|
|
9
|
+
/** Draw the swatch as a dotted line (a baseline, such as a value held flat). */
|
|
10
|
+
dotted?: boolean;
|
|
9
11
|
/** Draw the swatch as a filled square (a bar or an area) instead of a line. */
|
|
10
12
|
square?: boolean;
|
|
11
13
|
};
|
|
@@ -24,7 +24,22 @@ export type ForecastItemData = {
|
|
|
24
24
|
* `connectForecastNulls` decides.
|
|
25
25
|
*/
|
|
26
26
|
connectNulls?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* This line's style, over the chart's `forecastLineStyle`. `dotted` is for
|
|
29
|
+
* a baseline (a last value held flat), not a forecast. Read by the
|
|
30
|
+
* Recharts charts (ChartLines) and their plain legend only.
|
|
31
|
+
*/
|
|
32
|
+
lineStyle?: ForecastLineStyle;
|
|
33
|
+
/**
|
|
34
|
+
* Mark this line's last point with a dot, over the chart's `showEndDots`
|
|
35
|
+
* (a backtest's forecast lands on a dot; its baseline does not). Read by
|
|
36
|
+
* ChartLines only.
|
|
37
|
+
*/
|
|
38
|
+
endDot?: boolean;
|
|
27
39
|
};
|
|
40
|
+
export type ForecastLineStyle = 'dashed' | 'dotted' | 'solid';
|
|
41
|
+
/** The stroke pattern for a line style. */
|
|
42
|
+
export declare function lineStyleDasharray(style: ForecastLineStyle): string;
|
|
28
43
|
export type Dot = {
|
|
29
44
|
x: number;
|
|
30
45
|
y: number;
|
|
@@ -67,7 +82,7 @@ export declare const ChartLines: import("react").MemoExoticComponent<({ chartDat
|
|
|
67
82
|
shouldAnimate?: boolean;
|
|
68
83
|
historicalLineColor?: string;
|
|
69
84
|
showLegend?: boolean;
|
|
70
|
-
forecastLineStyle?:
|
|
85
|
+
forecastLineStyle?: ForecastLineStyle;
|
|
71
86
|
disableAnimation?: boolean;
|
|
72
87
|
disableHistoricalAnimation?: boolean;
|
|
73
88
|
/** Draw each history line across months it has no value for. */
|
|
@@ -21,6 +21,11 @@ export interface StackedAreaChartOverlay {
|
|
|
21
21
|
* line. At most one overlay should set it; the first one that does is used.
|
|
22
22
|
*/
|
|
23
23
|
compare?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Dot the line's only value. Gaps are bridged, so a line with a single
|
|
26
|
+
* value would otherwise draw nothing.
|
|
27
|
+
*/
|
|
28
|
+
dotLonePoints?: boolean;
|
|
24
29
|
}
|
|
25
30
|
export interface StackedAreaChartMargin {
|
|
26
31
|
/**
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
import { type BacktestOrigin, type BacktestPoint, type BacktestScore } from './backtestChart.helpers';
|
|
3
|
+
export type BacktestChartProps = {
|
|
4
|
+
/** The actual values, one per month. */
|
|
5
|
+
actuals: BacktestPoint[];
|
|
6
|
+
/** The past months forecasts were made from, each cut to one horizon. */
|
|
7
|
+
origins: BacktestOrigin[];
|
|
8
|
+
/**
|
|
9
|
+
* The score windows under the chart: the last N calendar months up to the
|
|
10
|
+
* latest actual (default: 24 and 12).
|
|
11
|
+
*/
|
|
12
|
+
windows?: number[];
|
|
13
|
+
actualLabel?: string;
|
|
14
|
+
forecastLabel?: string;
|
|
15
|
+
baselineLabel?: string;
|
|
16
|
+
actualColor?: string;
|
|
17
|
+
forecastColor?: string;
|
|
18
|
+
baselineColor?: string;
|
|
19
|
+
/** One score line; the default says "removes X% of the error". */
|
|
20
|
+
renderScore?: (score: BacktestScore) => ReactNode;
|
|
21
|
+
formatPercent?: (value: number) => string;
|
|
22
|
+
formatNumber?: (value: number) => string;
|
|
23
|
+
formatDate?: (value: string, detailed?: boolean) => string;
|
|
24
|
+
height?: number;
|
|
25
|
+
/** Draw the lines at once, with no animation. */
|
|
26
|
+
disableAnimation?: boolean;
|
|
27
|
+
isDarkTheme?: boolean;
|
|
28
|
+
renderId?: string;
|
|
29
|
+
className?: string;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* A backtest: the actual values (solid), and from each origin the forecast
|
|
33
|
+
* out to its horizon (dashed, with a dot where it lands) and the last value
|
|
34
|
+
* held flat over the same months (dotted, the baseline). Under it, for each
|
|
35
|
+
* window, how much of the baseline's error the forecast removes.
|
|
36
|
+
*/
|
|
37
|
+
export declare function BacktestChart({ actuals, origins, windows, actualLabel, forecastLabel, baselineLabel, actualColor, forecastColor, baselineColor, renderScore, formatPercent, formatNumber, formatDate, height, disableAnimation, isDarkTheme, renderId, className, }: BacktestChartProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { ChartDataPoint } from '#uilib/components/ui/ChartAreaInteractive/ChartAreaInteractive.types';
|
|
2
|
+
/** One month's value: `date` is a sortable ISO date (`2024-03-01`). */
|
|
3
|
+
export type BacktestPoint = {
|
|
4
|
+
date: string;
|
|
5
|
+
value: number;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* One backtest origin: a past month a forecast was made from.
|
|
9
|
+
* The baseline holds `value`, the last price known then, flat over the
|
|
10
|
+
* forecast's months.
|
|
11
|
+
*/
|
|
12
|
+
export type BacktestOrigin = {
|
|
13
|
+
/** The month the forecast was made in: the last month with a known value. */
|
|
14
|
+
date: string;
|
|
15
|
+
/** The value known at `date`, held flat as the baseline. */
|
|
16
|
+
value: number;
|
|
17
|
+
/**
|
|
18
|
+
* The forecast for the months after `date`, in date order. Its last point
|
|
19
|
+
* is where it lands: the month the scores compare with the actual value.
|
|
20
|
+
*/
|
|
21
|
+
forecast: BacktestPoint[];
|
|
22
|
+
};
|
|
23
|
+
/** One window's scores: mean absolute percentage error at the landing month. */
|
|
24
|
+
export type BacktestScore = {
|
|
25
|
+
/** The window: the last `months` calendar months up to the latest actual. */
|
|
26
|
+
months: number;
|
|
27
|
+
/** How many forecasts landed in the window with an actual to compare. */
|
|
28
|
+
count: number;
|
|
29
|
+
/** The forecast's MAPE, in percent. */
|
|
30
|
+
forecastError: number;
|
|
31
|
+
/** The held-flat baseline's MAPE, in percent. */
|
|
32
|
+
baselineError: number;
|
|
33
|
+
/**
|
|
34
|
+
* How much of the baseline's error the forecast removes, in percent
|
|
35
|
+
* (negative: the forecast has more error). Null when the baseline has none.
|
|
36
|
+
*/
|
|
37
|
+
errorRemoved: number | null;
|
|
38
|
+
};
|
|
39
|
+
/** The data key of origin `index`'s forecast line. */
|
|
40
|
+
export declare const backtestForecastKey: (index: number) => string;
|
|
41
|
+
/** The data key of origin `index`'s held-flat line. */
|
|
42
|
+
export declare const backtestBaselineKey: (index: number) => string;
|
|
43
|
+
export declare const backtestForecastId: (index: number) => number;
|
|
44
|
+
export declare const backtestBaselineId: (index: number) => number;
|
|
45
|
+
/**
|
|
46
|
+
* The chart's rows, one per calendar month from the first to the last, keyed
|
|
47
|
+
* `YYYY-MM-01` (a month with no value has a row of its own, so the x axis
|
|
48
|
+
* spaces months evenly): the actuals under `historical`, and for each origin
|
|
49
|
+
* its forecast (from the origin's value through every forecast month) and its
|
|
50
|
+
* baseline (the origin's value on the same months), each under its own key.
|
|
51
|
+
* A date that is not `YYYY-MM…` is left out.
|
|
52
|
+
*/
|
|
53
|
+
export declare function buildBacktestRows(actuals: BacktestPoint[], origins: BacktestOrigin[]): ChartDataPoint[];
|
|
54
|
+
/**
|
|
55
|
+
* The scores for the last `months` calendar months up to the latest actual:
|
|
56
|
+
* every origin whose forecast lands in one of them, against that month's
|
|
57
|
+
* actual. Dates match by calendar month (`2024-03-01` and `2024-03-31` are
|
|
58
|
+
* one month). Null when the actuals span fewer months than the window, or
|
|
59
|
+
* no forecast lands in it with a non-zero actual to compare.
|
|
60
|
+
*/
|
|
61
|
+
export declare function backtestScore(actuals: BacktestPoint[], origins: BacktestOrigin[], months: number): BacktestScore | null;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function BacktestChartPage(): import("react/jsx-runtime").JSX.Element;
|
|
@@ -81,4 +81,5 @@ export * from './components/widgets/DriverCard';
|
|
|
81
81
|
export * from './components/widgets/DriverMap';
|
|
82
82
|
export * from './components/widgets/DriversComparisonChart';
|
|
83
83
|
export * from './components/widgets/PerformanceChart';
|
|
84
|
+
export * from './components/widgets/BacktestChart';
|
|
84
85
|
export * from './components/widgets/SybilionAuthLayout';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@testing-library/jest-dom';
|
package/package.json
CHANGED
|
@@ -14,6 +14,7 @@ import { ChartDataPoint } from '#uilib/components/ui/ChartAreaInteractive/ChartA
|
|
|
14
14
|
import {
|
|
15
15
|
ChartLines,
|
|
16
16
|
ForecastItemData,
|
|
17
|
+
type ForecastLineStyle,
|
|
17
18
|
getForecastColor,
|
|
18
19
|
resolveHistoryLines,
|
|
19
20
|
} from '#uilib/components/ui/ChartAreaInteractive/ChartLines';
|
|
@@ -218,7 +219,8 @@ export interface BaseChartWrapperProps {
|
|
|
218
219
|
/** When true, no placeholder UI for empty `chartData` (parent renders overlay). */
|
|
219
220
|
suppressNoDataState?: boolean;
|
|
220
221
|
onGridHeightChange?: (height: number) => void;
|
|
221
|
-
|
|
222
|
+
/** Every forecast line's style, unless the line sets its own `lineStyle`. */
|
|
223
|
+
forecastLineStyle?: ForecastLineStyle;
|
|
222
224
|
disableHistoricalAnimation?: boolean;
|
|
223
225
|
/** Clear all hidden series (legend “show all”). */
|
|
224
226
|
onShowAll?: () => void;
|
|
@@ -597,6 +599,7 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
597
599
|
// tooltip: item.tooltip,
|
|
598
600
|
dimmed: item.dimmed,
|
|
599
601
|
updated_at: item.updated_at,
|
|
602
|
+
lineStyle: item.lineStyle,
|
|
600
603
|
}));
|
|
601
604
|
}, [forecastData, excludeLegendIds, showLegend]);
|
|
602
605
|
|
|
@@ -628,7 +631,8 @@ const BaseChartWrapperContent = forwardRef<
|
|
|
628
631
|
dataKey: p.dataKey,
|
|
629
632
|
label: p.value,
|
|
630
633
|
color: p.color,
|
|
631
|
-
dashed: forecastLineStyle === 'dashed',
|
|
634
|
+
dashed: (p.lineStyle ?? forecastLineStyle) === 'dashed',
|
|
635
|
+
dotted: (p.lineStyle ?? forecastLineStyle) === 'dotted',
|
|
632
636
|
})),
|
|
633
637
|
];
|
|
634
638
|
if (!legendOrder) return all;
|
|
@@ -11,6 +11,8 @@ export type SeriesLegendItem = {
|
|
|
11
11
|
color: string;
|
|
12
12
|
/** Draw the swatch as a dashed line (a forecast line). */
|
|
13
13
|
dashed?: boolean;
|
|
14
|
+
/** Draw the swatch as a dotted line (a baseline, such as a value held flat). */
|
|
15
|
+
dotted?: boolean;
|
|
14
16
|
/** Draw the swatch as a filled square (a bar or an area) instead of a line. */
|
|
15
17
|
square?: boolean;
|
|
16
18
|
};
|
|
@@ -41,7 +43,11 @@ export function SeriesLegend({
|
|
|
41
43
|
<span
|
|
42
44
|
className={cn(
|
|
43
45
|
S.swatch,
|
|
44
|
-
item.square
|
|
46
|
+
item.square
|
|
47
|
+
? S.square
|
|
48
|
+
: item.dotted
|
|
49
|
+
? S.dotted
|
|
50
|
+
: item.dashed && S.dashed,
|
|
45
51
|
)}
|
|
46
52
|
style={{ color: item.color }}
|
|
47
53
|
aria-hidden
|
|
@@ -35,8 +35,29 @@ export type ForecastItemData = {
|
|
|
35
35
|
* `connectForecastNulls` decides.
|
|
36
36
|
*/
|
|
37
37
|
connectNulls?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* This line's style, over the chart's `forecastLineStyle`. `dotted` is for
|
|
40
|
+
* a baseline (a last value held flat), not a forecast. Read by the
|
|
41
|
+
* Recharts charts (ChartLines) and their plain legend only.
|
|
42
|
+
*/
|
|
43
|
+
lineStyle?: ForecastLineStyle;
|
|
44
|
+
/**
|
|
45
|
+
* Mark this line's last point with a dot, over the chart's `showEndDots`
|
|
46
|
+
* (a backtest's forecast lands on a dot; its baseline does not). Read by
|
|
47
|
+
* ChartLines only.
|
|
48
|
+
*/
|
|
49
|
+
endDot?: boolean;
|
|
38
50
|
};
|
|
39
51
|
|
|
52
|
+
export type ForecastLineStyle = 'dashed' | 'dotted' | 'solid';
|
|
53
|
+
|
|
54
|
+
/** The stroke pattern for a line style. */
|
|
55
|
+
export function lineStyleDasharray(style: ForecastLineStyle): string {
|
|
56
|
+
if (style === 'dashed') return '4 2';
|
|
57
|
+
if (style === 'dotted') return '1 3';
|
|
58
|
+
return '0';
|
|
59
|
+
}
|
|
60
|
+
|
|
40
61
|
export type Dot = {
|
|
41
62
|
x: number;
|
|
42
63
|
y: number;
|
|
@@ -133,7 +154,7 @@ export const ChartLines = memo(
|
|
|
133
154
|
shouldAnimate?: boolean;
|
|
134
155
|
historicalLineColor?: string;
|
|
135
156
|
showLegend?: boolean;
|
|
136
|
-
forecastLineStyle?:
|
|
157
|
+
forecastLineStyle?: ForecastLineStyle;
|
|
137
158
|
disableAnimation?: boolean;
|
|
138
159
|
disableHistoricalAnimation?: boolean;
|
|
139
160
|
/** Draw each history line across months it has no value for. */
|
|
@@ -238,6 +259,7 @@ export const ChartLines = memo(
|
|
|
238
259
|
dataKey: string,
|
|
239
260
|
color: string,
|
|
240
261
|
connected: boolean,
|
|
262
|
+
endDot: boolean = showEndDots,
|
|
241
263
|
) => {
|
|
242
264
|
const i = props.index;
|
|
243
265
|
if (
|
|
@@ -248,7 +270,7 @@ export const ChartLines = memo(
|
|
|
248
270
|
) {
|
|
249
271
|
return null;
|
|
250
272
|
}
|
|
251
|
-
const isEnd =
|
|
273
|
+
const isEnd = endDot && lastIndex >= 0 && i === lastIndex;
|
|
252
274
|
const isAlone =
|
|
253
275
|
dotLonePoints &&
|
|
254
276
|
(connected
|
|
@@ -330,6 +352,8 @@ export const ChartLines = memo(
|
|
|
330
352
|
} = data;
|
|
331
353
|
const dataKey = `forecast_${id}`;
|
|
332
354
|
const connected = data.connectNulls ?? connectForecastNulls;
|
|
355
|
+
const endDot = data.endDot ?? showEndDots;
|
|
356
|
+
const lineStyle = data.lineStyle ?? forecastLineStyle;
|
|
333
357
|
|
|
334
358
|
return (
|
|
335
359
|
<Line
|
|
@@ -338,9 +362,10 @@ export const ChartLines = memo(
|
|
|
338
362
|
dataKey={dataKey}
|
|
339
363
|
stroke={color}
|
|
340
364
|
strokeWidth={1}
|
|
341
|
-
strokeDasharray={
|
|
365
|
+
strokeDasharray={lineStyleDasharray(lineStyle)}
|
|
366
|
+
strokeLinecap={lineStyle === 'dotted' ? 'round' : undefined}
|
|
342
367
|
dot={
|
|
343
|
-
|
|
368
|
+
endDot || dotLonePoints
|
|
344
369
|
? props =>
|
|
345
370
|
renderDot(
|
|
346
371
|
props,
|
|
@@ -348,6 +373,7 @@ export const ChartLines = memo(
|
|
|
348
373
|
dataKey,
|
|
349
374
|
color,
|
|
350
375
|
connected,
|
|
376
|
+
endDot,
|
|
351
377
|
)
|
|
352
378
|
: false
|
|
353
379
|
}
|
|
@@ -17,6 +17,7 @@ StackedAreaChart shows how a total is built from parts over time: cost layers st
|
|
|
17
17
|
- Expect zones to change at rows: a crossing between two rows shows at the next row. Hiding a layer or the compare line in the legend updates the margin, the zones and the weights to match what is drawn.
|
|
18
18
|
- Give each layer a `label`; the legend and tooltip use it.
|
|
19
19
|
- Keep `margin.pct` in the host's state and pass it on every change; the margin layer stays mounted, so typing does not redraw the chart.
|
|
20
|
+
- Set `dotLonePoints` on an overlay that can have a single value in the window (for example one price paid): a bridged line with one value draws nothing, so it gets a dot.
|
|
20
21
|
- Set `yearTicks` for multi-year monthly data so the axis reads one label per year.
|
|
21
22
|
- Pass `hiddenKeys` and `onToggleKey` when the host must remember which series are hidden.
|
|
22
23
|
- Name the axes with `xAxisLabel` and `yAxisLabel`; pass `tooltipValueFormatter` when the tooltip should read differently from the axis (for example with a unit).
|
|
@@ -71,6 +71,7 @@ const layerColor = (layer: StackedAreaChartLayer, index: number) =>
|
|
|
71
71
|
layer.color ?? getChartSeriesColor(index);
|
|
72
72
|
const overlayColor = (overlay: StackedAreaChartOverlay) =>
|
|
73
73
|
overlay.color ?? DEFAULT_OVERLAY_COLOR;
|
|
74
|
+
const LONE_DOT_RADIUS = 3;
|
|
74
75
|
|
|
75
76
|
function LegendEntry({
|
|
76
77
|
swatch,
|
|
@@ -178,6 +179,18 @@ const StackedAreaChart = forwardRef<HTMLDivElement, StackedAreaChartProps>(
|
|
|
178
179
|
[showWeights, data, visibleKeys],
|
|
179
180
|
);
|
|
180
181
|
const asBars = rows.length < barsBelow;
|
|
182
|
+
// How many values each overlay has: a bridged line with one value draws
|
|
183
|
+
// nothing, so `dotLonePoints` dots it.
|
|
184
|
+
const overlayValueCounts = useMemo(() => {
|
|
185
|
+
const counts: Record<string, number> = {};
|
|
186
|
+
for (const overlay of overlays) {
|
|
187
|
+
counts[overlay.key] = data.filter(row => {
|
|
188
|
+
const value = row[overlay.key];
|
|
189
|
+
return typeof value === 'number' && Number.isFinite(value);
|
|
190
|
+
}).length;
|
|
191
|
+
}
|
|
192
|
+
return counts;
|
|
193
|
+
}, [data, overlays]);
|
|
181
194
|
const compareKey = overlays.find(o => o.compare)?.key;
|
|
182
195
|
const zonesOn = compareKey != null && !hidden.has(compareKey);
|
|
183
196
|
const zones = useMemo(() => {
|
|
@@ -351,6 +364,10 @@ const StackedAreaChart = forwardRef<HTMLDivElement, StackedAreaChartProps>(
|
|
|
351
364
|
))}
|
|
352
365
|
{overlays.map(overlay => {
|
|
353
366
|
const color = overlayColor(overlay);
|
|
367
|
+
const lone =
|
|
368
|
+
!asBars &&
|
|
369
|
+
overlay.dotLonePoints &&
|
|
370
|
+
overlayValueCounts[overlay.key] === 1;
|
|
354
371
|
return (
|
|
355
372
|
<Line
|
|
356
373
|
key={overlay.key}
|
|
@@ -359,7 +376,33 @@ const StackedAreaChart = forwardRef<HTMLDivElement, StackedAreaChartProps>(
|
|
|
359
376
|
name={overlay.key}
|
|
360
377
|
stroke={color}
|
|
361
378
|
strokeWidth={1.75}
|
|
362
|
-
dot={
|
|
379
|
+
dot={
|
|
380
|
+
asBars
|
|
381
|
+
? { r: 4, fill: color, stroke: color }
|
|
382
|
+
: lone
|
|
383
|
+
? (props: {
|
|
384
|
+
cx?: number;
|
|
385
|
+
cy?: number;
|
|
386
|
+
index?: number;
|
|
387
|
+
value?: unknown;
|
|
388
|
+
}) =>
|
|
389
|
+
typeof props.value === 'number' &&
|
|
390
|
+
Number.isFinite(props.value) &&
|
|
391
|
+
props.cx != null &&
|
|
392
|
+
props.cy != null ? (
|
|
393
|
+
<circle
|
|
394
|
+
key={`lone-${overlay.key}-${props.index}`}
|
|
395
|
+
className={S.loneDot}
|
|
396
|
+
cx={props.cx}
|
|
397
|
+
cy={props.cy}
|
|
398
|
+
r={LONE_DOT_RADIUS}
|
|
399
|
+
fill={color}
|
|
400
|
+
/>
|
|
401
|
+
) : (
|
|
402
|
+
<g key={`lone-${overlay.key}-${props.index}`} />
|
|
403
|
+
)
|
|
404
|
+
: false
|
|
405
|
+
}
|
|
363
406
|
activeDot={false}
|
|
364
407
|
connectNulls
|
|
365
408
|
isAnimationActive={false}
|
|
@@ -27,6 +27,11 @@ export interface StackedAreaChartOverlay {
|
|
|
27
27
|
* line. At most one overlay should set it; the first one that does is used.
|
|
28
28
|
*/
|
|
29
29
|
compare?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Dot the line's only value. Gaps are bridged, so a line with a single
|
|
32
|
+
* value would otherwise draw nothing.
|
|
33
|
+
*/
|
|
34
|
+
dotLonePoints?: boolean;
|
|
30
35
|
}
|
|
31
36
|
|
|
32
37
|
export interface StackedAreaChartMargin {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
BacktestChart shows how a forecast did in the past against the simplest baseline, the last known value held flat. It draws the actual values (solid), and from each past month a forecast was made in (an origin) the forecast out to one horizon (dashed, with a dot where it lands) and the origin's value held flat over the same months (dotted grey). Under the chart, one line per window says how much of the baseline's error the forecast removes, with both mean absolute percentage errors (MAPE).
|
|
2
|
+
|
|
3
|
+
## When to use
|
|
4
|
+
|
|
5
|
+
- To show that a forecast beats knowing only the last price, for one horizon at a time.
|
|
6
|
+
|
|
7
|
+
## When not to use
|
|
8
|
+
|
|
9
|
+
- For the latest forecast with its range; use ChartAreaInteractive.
|
|
10
|
+
- For every horizon of every vintage at once; use PerformanceChart's spaghetti view.
|
|
11
|
+
|
|
12
|
+
## Do
|
|
13
|
+
|
|
14
|
+
- Cut each origin's `forecast` to the horizon shown: its last point is the landing month the scores use.
|
|
15
|
+
- Set an origin's `value` to the actual value at the origin month (the last price known when the forecast was made), and leave out an origin with none.
|
|
16
|
+
- `windows` (default 24 and 12) are the last N calendar months up to the latest actual; a window longer than the actuals, or with no landing in it, has no line.
|
|
17
|
+
- Dates are matched by month (`2024-03-01` and `2024-03-28` are one month), and the chart has one column per month, missing months included.
|
|
18
|
+
- `backtestScore(actuals, origins, months)` is the same computation, for a score shown elsewhere.
|
|
19
|
+
- Only forecasts are dashed: the baseline is dotted and the actuals solid.
|
|
20
|
+
|
|
21
|
+
## Requires
|
|
22
|
+
|
|
23
|
+
- `actuals`: one point per month, ISO dates (`2024-03-01`); a date that does not start `YYYY-MM` is left out.
|
|
24
|
+
- `origins`: `{ date, value, forecast: [{ date, value }] }`, forecast in date order.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
.root
|
|
2
|
+
display flex
|
|
3
|
+
flex-direction column
|
|
4
|
+
width 100%
|
|
5
|
+
|
|
6
|
+
.scores
|
|
7
|
+
margin 0.75rem 0 0
|
|
8
|
+
padding 0
|
|
9
|
+
list-style none
|
|
10
|
+
display flex
|
|
11
|
+
flex-direction column
|
|
12
|
+
gap 0.25rem
|
|
13
|
+
font-size 0.875rem
|
|
14
|
+
color var(--muted-foreground)
|
|
15
|
+
|
|
16
|
+
strong
|
|
17
|
+
color var(--foreground)
|
|
18
|
+
font-weight 500
|
|
19
|
+
|
|
20
|
+
// BaseChartWrapper fills its parent's height (min-height 100%); here the
|
|
21
|
+
// legend and the scores share that height, so the chart keeps its own.
|
|
22
|
+
.root .chart
|
|
23
|
+
min-height 0
|