@sybilion/uilib 1.7.2 → 1.8.0

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.
Files changed (85) hide show
  1. package/dist/agent-glossary/content-prompt.generated.ts +2 -0
  2. package/dist/agent-glossary/content.generated.ts +2 -0
  3. package/dist/agent-glossary/content.md +2 -0
  4. package/dist/agent-glossary/content.prompt.md +2 -0
  5. package/dist/agent-glossary/workspace.generated.ts +1 -0
  6. package/dist/agent-glossary/workspace.index.md +1 -0
  7. package/dist/agent-glossary/workspace.md +1 -0
  8. package/dist/esm/src/components/ui/AnalysesSelector/AnalysesSelector.js +37 -20
  9. package/dist/esm/src/components/ui/Chart/Chart.styl.js +2 -2
  10. package/dist/esm/src/components/ui/Chart/components/BaseChartWrapper.js +46 -51
  11. package/dist/esm/src/components/ui/Chart/components/ChartContainer.js +2 -2
  12. package/dist/esm/src/components/ui/Chart/components/ChartGrid.js +2 -2
  13. package/dist/esm/src/components/ui/Chart/components/ChartLegendContent.js +2 -2
  14. package/dist/esm/src/components/ui/Chart/components/ChartLegendItem.js +2 -2
  15. package/dist/esm/src/components/ui/Chart/components/ChartTooltipContent.js +2 -2
  16. package/dist/esm/src/components/ui/Chart/components/ChartTooltipItem.js +3 -3
  17. package/dist/esm/src/components/ui/Chart/components/CustomChartLegend/CustomChartLegend.js +8 -3
  18. package/dist/esm/src/components/ui/Chart/components/LegendSvg/LegendSvg.js +1 -1
  19. package/dist/esm/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.helpers.js +17 -6
  20. package/dist/esm/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.js +40 -10
  21. package/dist/esm/src/components/ui/ChartAreaInteractive/ChartLines.js +43 -13
  22. package/dist/esm/src/components/ui/ChartAreaInteractive/chartHistories.js +44 -0
  23. package/dist/esm/src/components/ui/ChartAreaInteractive/overlays/useChartYRange.js +32 -7
  24. package/dist/esm/src/components/ui/Page/PageTabs/PageTabs.styl.js +2 -2
  25. package/dist/esm/src/components/ui/Page/PageXScroll/PageXScroll.js +50 -15
  26. package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.js +212 -0
  27. package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.js +123 -0
  28. package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.styl.js +7 -0
  29. package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.js +29 -0
  30. package/dist/esm/src/index.js +3 -1
  31. package/dist/esm/src/utils/chartConnectionPoint.js +31 -16
  32. package/dist/esm/types/src/components/ui/AnalysesSelector/AnalysesSelector.d.ts +1 -1
  33. package/dist/esm/types/src/components/ui/AnalysesSelector/AnalysesSelector.types.d.ts +12 -0
  34. package/dist/esm/types/src/components/ui/AnalysesSelector/index.d.ts +1 -1
  35. package/dist/esm/types/src/components/ui/Chart/components/BaseChartWrapper.d.ts +8 -0
  36. package/dist/esm/types/src/components/ui/Chart/components/CustomChartLegend/CustomChartLegend.d.ts +8 -1
  37. package/dist/esm/types/src/components/ui/Chart/components/LegendSvg/LegendSvg.d.ts +2 -0
  38. package/dist/esm/types/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.d.ts +1 -1
  39. package/dist/esm/types/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.helpers.d.ts +5 -0
  40. package/dist/esm/types/src/components/ui/ChartAreaInteractive/ChartLines.d.ts +19 -1
  41. package/dist/esm/types/src/components/ui/ChartAreaInteractive/chartHistories.d.ts +40 -0
  42. package/dist/esm/types/src/components/ui/ChartAreaInteractive/index.d.ts +1 -0
  43. package/dist/esm/types/src/components/ui/ChartAreaInteractive/overlays/useChartYRange.d.ts +6 -1
  44. package/dist/esm/types/src/components/ui/Page/PageXScroll/PageXScroll.d.ts +1 -1
  45. package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.d.ts +3 -0
  46. package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.d.ts +65 -0
  47. package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.d.ts +20 -0
  48. package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.types.d.ts +85 -0
  49. package/dist/esm/types/src/components/ui/StackedAreaChart/index.d.ts +2 -0
  50. package/dist/esm/types/src/docs/pages/StackedAreaChartPage.d.ts +1 -0
  51. package/dist/esm/types/src/index.d.ts +1 -0
  52. package/dist/esm/types/src/utils/chartConnectionPoint.d.ts +11 -3
  53. package/dist/esm/types/tests/chartHistories.test.d.ts +1 -0
  54. package/dist/esm/types/tests/pageXScroll.test.d.ts +1 -0
  55. package/dist/esm/types/tests/stackedAreaChart.test.d.ts +1 -0
  56. package/package.json +1 -1
  57. package/src/components/ui/AnalysesSelector/AnalysesSelector.tsx +50 -22
  58. package/src/components/ui/AnalysesSelector/AnalysesSelector.types.ts +13 -0
  59. package/src/components/ui/AnalysesSelector/index.ts +1 -0
  60. package/src/components/ui/Chart/components/BaseChartWrapper.tsx +71 -52
  61. package/src/components/ui/Chart/components/CustomChartLegend/CustomChartLegend.tsx +21 -1
  62. package/src/components/ui/Chart/components/LegendSvg/LegendSvg.tsx +3 -1
  63. package/src/components/ui/ChartAreaInteractive/AGENT.md +2 -0
  64. package/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.helpers.ts +27 -5
  65. package/src/components/ui/ChartAreaInteractive/ChartAreaInteractive.tsx +64 -10
  66. package/src/components/ui/ChartAreaInteractive/ChartLines.tsx +86 -30
  67. package/src/components/ui/ChartAreaInteractive/chartHistories.ts +63 -0
  68. package/src/components/ui/ChartAreaInteractive/index.ts +1 -0
  69. package/src/components/ui/ChartAreaInteractive/overlays/useChartYRange.ts +52 -6
  70. package/src/components/ui/Page/AGENT.md +2 -0
  71. package/src/components/ui/Page/PageTabs/PageTabs.styl +8 -0
  72. package/src/components/ui/Page/PageXScroll/PageXScroll.tsx +57 -13
  73. package/src/components/ui/StackedAreaChart/AGENT.md +26 -0
  74. package/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.ts +268 -0
  75. package/src/components/ui/StackedAreaChart/StackedAreaChart.styl +32 -0
  76. package/src/components/ui/StackedAreaChart/StackedAreaChart.styl.d.ts +13 -0
  77. package/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.tsx +82 -0
  78. package/src/components/ui/StackedAreaChart/StackedAreaChart.tsx +416 -0
  79. package/src/components/ui/StackedAreaChart/StackedAreaChart.types.ts +97 -0
  80. package/src/components/ui/StackedAreaChart/index.ts +2 -0
  81. package/src/docs/pages/ChartAreaInteractivePage.tsx +67 -0
  82. package/src/docs/pages/StackedAreaChartPage.tsx +141 -0
  83. package/src/docs/registry.ts +6 -0
  84. package/src/index.ts +1 -0
  85. package/src/utils/chartConnectionPoint.ts +41 -18
@@ -0,0 +1,141 @@
1
+ import { useState } from 'react';
2
+
3
+ import { NumberControl } from '#uilib/components/ui/NumberControl';
4
+ import { PageContentSection, SectionHeader } from '#uilib/components/ui/Page';
5
+ import type {
6
+ StackedAreaChartDatum,
7
+ StackedAreaChartLayer,
8
+ StackedAreaChartOverlay,
9
+ } from '#uilib/components/ui/StackedAreaChart';
10
+ import { StackedAreaChart } from '#uilib/components/ui/StackedAreaChart';
11
+ import StackedAreaChartGuidance from '#uilib/components/ui/StackedAreaChart/AGENT.md';
12
+
13
+ import { AppPageHeader } from '../components/AppPageHeader/AppPageHeader';
14
+ import { DocsGuidance } from '../components/DocsGuidance/DocsGuidance';
15
+ import { DocsHeaderActions } from '../docsHeaderActions';
16
+
17
+ /** Invented monthly cost layers per kg, three years from Jan 2023. */
18
+ const COST_ROWS: StackedAreaChartDatum[] = Array.from(
19
+ { length: 36 },
20
+ (_, i) => {
21
+ const year = 2023 + Math.floor(i / 12);
22
+ const month = String((i % 12) + 1).padStart(2, '0');
23
+ const wave = Math.sin(i / 4);
24
+ return {
25
+ date: `${year}-${month}-01`,
26
+ raw: Number((1.8 + 0.25 * wave + i * 0.01).toFixed(2)),
27
+ energy: Number((0.6 + 0.15 * Math.cos(i / 3)).toFixed(2)),
28
+ labour: Number((0.45 + i * 0.004).toFixed(2)),
29
+ freight: Number((0.2 + 0.05 * wave).toFixed(2)),
30
+ // A price every third month and in the last one, so the line and its
31
+ // shading run the full width.
32
+ paid:
33
+ i % 3 === 0 || i === 35
34
+ ? Number((3.3 + 0.35 * Math.sin(i / 5)).toFixed(2))
35
+ : null,
36
+ };
37
+ },
38
+ );
39
+
40
+ const COST_LAYERS: StackedAreaChartLayer[] = [
41
+ { key: 'raw', label: 'Raw material' },
42
+ { key: 'energy', label: 'Energy' },
43
+ { key: 'labour', label: 'Labour' },
44
+ { key: 'freight', label: 'Freight' },
45
+ ];
46
+
47
+ const PAID: StackedAreaChartOverlay[] = [
48
+ { key: 'paid', label: 'Prices paid', compare: true },
49
+ ];
50
+
51
+ const QUARTER_ROWS: StackedAreaChartDatum[] = [
52
+ {
53
+ quarter: 'Q1',
54
+ raw: 1.9,
55
+ energy: 0.7,
56
+ labour: 0.5,
57
+ freight: 0.2,
58
+ paid: 3.6,
59
+ },
60
+ {
61
+ quarter: 'Q2',
62
+ raw: 2.1,
63
+ energy: 0.6,
64
+ labour: 0.5,
65
+ freight: 0.25,
66
+ paid: 3.2,
67
+ },
68
+ ];
69
+
70
+ const formatPrice = (value: number) => `€${value.toFixed(2)}`;
71
+ const formatMonth = (value: string | number) =>
72
+ new Date(String(value)).toLocaleDateString('en-GB', {
73
+ month: 'short',
74
+ year: 'numeric',
75
+ timeZone: 'UTC',
76
+ });
77
+
78
+ export default function StackedAreaChartPage() {
79
+ const [marginPct, setMarginPct] = useState(8);
80
+
81
+ return (
82
+ <>
83
+ <AppPageHeader
84
+ breadcrumbs={[{ label: 'StackedAreaChart' }]}
85
+ title="StackedAreaChart"
86
+ subheader="Cost layers stacked over time, with a margin and lines over the stack."
87
+ actions={<DocsHeaderActions />}
88
+ />
89
+ <DocsGuidance source={StackedAreaChartGuidance} />
90
+ <PageContentSection>
91
+ <SectionHeader
92
+ title="Should cost"
93
+ description="Four cost layers, a margin on top and the prices paid. Green where the price is above the cost, red where it is below. Click a legend entry to hide it."
94
+ withMargin
95
+ />
96
+ <div
97
+ style={{
98
+ display: 'flex',
99
+ alignItems: 'center',
100
+ gap: 8,
101
+ marginBottom: 12,
102
+ }}
103
+ >
104
+ <span>Margin %</span>
105
+ <NumberControl
106
+ value={marginPct}
107
+ onChange={setMarginPct}
108
+ min={0}
109
+ max={50}
110
+ />
111
+ </div>
112
+ <StackedAreaChart
113
+ data={COST_ROWS}
114
+ xKey="date"
115
+ layers={COST_LAYERS}
116
+ overlays={PAID}
117
+ margin={{ pct: marginPct }}
118
+ yearTicks
119
+ valueFormatter={formatPrice}
120
+ tooltipLabelFormatter={formatMonth}
121
+ height={320}
122
+ />
123
+ </PageContentSection>
124
+ <PageContentSection>
125
+ <SectionHeader
126
+ title="Few points"
127
+ description="With fewer than three rows the stack draws as bars."
128
+ withMargin
129
+ />
130
+ <StackedAreaChart
131
+ data={QUARTER_ROWS}
132
+ xKey="quarter"
133
+ layers={COST_LAYERS}
134
+ overlays={PAID}
135
+ valueFormatter={formatPrice}
136
+ height={260}
137
+ />
138
+ </PageContentSection>
139
+ </>
140
+ );
141
+ }
@@ -224,6 +224,12 @@ export const DOC_REGISTRY: DocEntry[] = [
224
224
  section: 'Charts',
225
225
  load: () => import('./pages/BarChartPage'),
226
226
  },
227
+ {
228
+ slug: 'stacked-area-chart',
229
+ title: 'StackedAreaChart',
230
+ section: 'Charts',
231
+ load: () => import('./pages/StackedAreaChartPage'),
232
+ },
227
233
  {
228
234
  slug: 'chat',
229
235
  title: 'Chat',
package/src/index.ts CHANGED
@@ -27,6 +27,7 @@ export * from './components/ui/Chart';
27
27
  export * from './components/ui/ChartAreaInteractive';
28
28
  export * from './components/ui/DonutChart';
29
29
  export * from './components/ui/BarChart';
30
+ export * from './components/ui/StackedAreaChart';
30
31
  export * from './components/ui/Checkbox';
31
32
  export * from './components/ui/Confirm';
32
33
  export * from './components/ui/DatePicker';
@@ -7,13 +7,26 @@ import type {
7
7
  TimeSeriesPoint,
8
8
  } from '#uilib/components/ui/Chart/chartForecastVisualization.types';
9
9
  import type { ChartDataPoint } from '#uilib/components/ui/ChartAreaInteractive/ChartAreaInteractive.types';
10
+ import { HISTORY_SERIES_KEY } from '#uilib/components/ui/ChartAreaInteractive/chartHistories';
10
11
 
11
12
  export type EnsureChartForecastBridgeOptions = {
12
13
  forecastSeriesIds?: number[];
13
14
  /** When true, returns `chartData` unchanged */
14
15
  disabled?: boolean;
16
+ /**
17
+ * The history line each forecast joins, by forecast id: a data key such as
18
+ * `historical` or `historical_<id>`, or null to leave that forecast unjoined.
19
+ * Defaults to the single `historical` line for every forecast.
20
+ */
21
+ historyKeyFor?: (forecastId: number) => string | null;
15
22
  };
16
23
 
24
+ /** A history line's value on a row, or null when it has none. */
25
+ function historyValueAt(row: ChartDataPoint, key: string): number | null {
26
+ const v = row[key];
27
+ return v == null || Array.isArray(v) ? null : (v as number);
28
+ }
29
+
17
30
  const FORECAST_KEY_RE = /^forecast_(\d+)$/;
18
31
 
19
32
  function collectForecastSeriesIds(chartData: ChartDataPoint[]): number[] {
@@ -43,6 +56,7 @@ function hasForecastValue(point: ChartDataPoint, id: number): boolean {
43
56
  function findFirstForecastDateForBridge(
44
57
  rows: ChartDataPoint[],
45
58
  id: number,
59
+ historyKey: string,
46
60
  ): string | null {
47
61
  const dataKey = forecastSeriesKey(id);
48
62
  const withForecast = rows
@@ -60,11 +74,12 @@ function findFirstForecastDateForBridge(
60
74
  getPreviousMonth(next.date),
61
75
  );
62
76
  const forecastVal = row[dataKey];
77
+ const historyVal = historyValueAt(row, historyKey);
63
78
  const isSyntheticBridge =
64
79
  normalizeToMonthStart(row.date) === expectedBridgeDate &&
65
- row.historical != null &&
80
+ historyVal != null &&
66
81
  typeof forecastVal === 'number' &&
67
- forecastVal === row.historical;
82
+ forecastVal === historyVal;
68
83
 
69
84
  if (!isSyntheticBridge) {
70
85
  return row.date;
@@ -79,7 +94,8 @@ function findFirstForecastDateForBridge(
79
94
  /**
80
95
  * Injects `forecast_{id}` on the row before each series’ first forecast month (historical value)
81
96
  * so dashed forecast lines meet the historical line. Matches sybilion-client bridge behavior.
82
- * Never overwrites an existing forecast value on that row.
97
+ * Never overwrites an existing forecast value on that row. With several history lines,
98
+ * `historyKeyFor` says which one each forecast joins (null: none).
83
99
  */
84
100
  export function ensureChartForecastBridge(
85
101
  chartData: ChartDataPoint[],
@@ -103,9 +119,18 @@ export function ensureChartForecastBridge(
103
119
 
104
120
  const rows = sorted.map(p => ({ ...p }));
105
121
 
122
+ const historyKeyFor = options?.historyKeyFor ?? (() => HISTORY_SERIES_KEY);
123
+
106
124
  for (const id of seriesIds) {
125
+ const historyKey = historyKeyFor(id);
126
+ if (historyKey == null) continue;
127
+
107
128
  const dataKey = forecastSeriesKey(id);
108
- const firstForecastDate = findFirstForecastDateForBridge(rows, id);
129
+ const firstForecastDate = findFirstForecastDateForBridge(
130
+ rows,
131
+ id,
132
+ historyKey,
133
+ );
109
134
  if (!firstForecastDate) continue;
110
135
 
111
136
  const prevMonthRaw = getPreviousMonth(firstForecastDate);
@@ -117,12 +142,8 @@ export function ensureChartForecastBridge(
117
142
 
118
143
  if (prevMonthIdx >= 0) {
119
144
  const existing = rows[prevMonthIdx][dataKey];
120
- const hPrev = rows[prevMonthIdx].historical;
121
- if (
122
- (existing === undefined || existing === null) &&
123
- hPrev !== undefined &&
124
- hPrev !== null
125
- ) {
145
+ const hPrev = historyValueAt(rows[prevMonthIdx], historyKey);
146
+ if ((existing === undefined || existing === null) && hPrev !== null) {
126
147
  rows[prevMonthIdx] = {
127
148
  ...rows[prevMonthIdx],
128
149
  [dataKey]: hPrev,
@@ -130,7 +151,7 @@ export function ensureChartForecastBridge(
130
151
  }
131
152
  }
132
153
 
133
- const lastHist = findLastValidHistoricalChartPoint(rows);
154
+ const lastHist = findLastValidHistoricalChartPoint(rows, historyKey);
134
155
  if (!lastHist) continue;
135
156
 
136
157
  const normLast = normalizeToMonthStart(lastHist.date);
@@ -143,8 +164,8 @@ export function ensureChartForecastBridge(
143
164
  const existingAtLast = rows[idxLast][dataKey];
144
165
  if (existingAtLast !== undefined && existingAtLast !== null) continue;
145
166
 
146
- const hLast = rows[idxLast].historical;
147
- if (hLast !== undefined && hLast !== null) {
167
+ const hLast = historyValueAt(rows[idxLast], historyKey);
168
+ if (hLast !== null) {
148
169
  rows[idxLast] = { ...rows[idxLast], [dataKey]: hLast };
149
170
  }
150
171
  }
@@ -165,15 +186,17 @@ export function findLastValidHistoricalPoint(
165
186
  }
166
187
 
167
188
  /**
168
- * Find the last valid historical data point from ChartDataPoint array
189
+ * Find the last valid historical data point from ChartDataPoint array, on the
190
+ * single `historical` line or on the history line under `key`.
169
191
  */
170
192
  export function findLastValidHistoricalChartPoint(
171
193
  historical: ChartDataPoint[],
194
+ key: string = HISTORY_SERIES_KEY,
172
195
  ): ChartDataPoint | null {
173
- const validPoints = historical.filter(
174
- point => point.historical !== null && point.historical !== undefined,
175
- );
176
- return validPoints.length > 0 ? validPoints[validPoints.length - 1] : null;
196
+ for (let i = historical.length - 1; i >= 0; i--) {
197
+ if (historyValueAt(historical[i], key) !== null) return historical[i];
198
+ }
199
+ return null;
177
200
  }
178
201
 
179
202
  /**