@sybilion/uilib 1.8.3 → 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.
Files changed (47) hide show
  1. package/dist/esm/src/components/ui/BarChart/BarChart.js +39 -7
  2. package/dist/esm/src/components/ui/Chart/components/BaseChartWrapper.js +71 -6
  3. package/dist/esm/src/components/ui/Chart/components/BaseChartWrapper.styl.js +2 -2
  4. package/dist/esm/src/components/ui/Chart/components/ChartAxes.js +11 -3
  5. package/dist/esm/src/components/ui/Chart/components/ChartTooltipContent.js +6 -3
  6. package/dist/esm/src/components/ui/Chart/components/QuantileBands.js +1 -1
  7. package/dist/esm/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.js +20 -0
  8. package/dist/esm/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.styl.js +7 -0
  9. package/dist/esm/src/components/ui/ChartAreaInteractive/ChartLines.js +49 -13
  10. package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.js +45 -11
  11. package/dist/esm/src/components/ui/StackedAreaChart/StackedAreaChart.js +15 -8
  12. package/dist/esm/types/src/components/ui/BarChart/BarChart.types.d.ts +22 -0
  13. package/dist/esm/types/src/components/ui/Chart/chartForecastVisualization.types.d.ts +5 -0
  14. package/dist/esm/types/src/components/ui/Chart/components/BaseChartWrapper.d.ts +22 -5
  15. package/dist/esm/types/src/components/ui/Chart/components/ChartAxes.d.ts +8 -1
  16. package/dist/esm/types/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.d.ts +22 -0
  17. package/dist/esm/types/src/components/ui/Chart/components/SeriesLegend/index.d.ts +1 -0
  18. package/dist/esm/types/src/components/ui/ChartAreaInteractive/ChartLines.d.ts +10 -1
  19. package/dist/esm/types/src/components/ui/ChartAreaInteractive/chartHistories.d.ts +5 -0
  20. package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.d.ts +10 -3
  21. package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.d.ts +1 -1
  22. package/dist/esm/types/src/components/ui/StackedAreaChart/StackedAreaChart.types.d.ts +9 -0
  23. package/dist/esm/types/tests/viewChartGaps.test.d.ts +1 -0
  24. package/package.json +1 -1
  25. package/src/components/ui/BarChart/AGENT.md +4 -0
  26. package/src/components/ui/BarChart/BarChart.styl.d.ts +1 -1
  27. package/src/components/ui/BarChart/BarChart.tsx +107 -21
  28. package/src/components/ui/BarChart/BarChart.types.ts +22 -0
  29. package/src/components/ui/Chart/AGENT.md +3 -0
  30. package/src/components/ui/Chart/chartForecastVisualization.types.ts +5 -0
  31. package/src/components/ui/Chart/components/BaseChartWrapper.styl +5 -0
  32. package/src/components/ui/Chart/components/BaseChartWrapper.styl.d.ts +1 -0
  33. package/src/components/ui/Chart/components/BaseChartWrapper.tsx +124 -5
  34. package/src/components/ui/Chart/components/ChartAxes.tsx +29 -5
  35. package/src/components/ui/Chart/components/ChartTooltipContent.tsx +6 -3
  36. package/src/components/ui/Chart/components/QuantileBands.tsx +1 -0
  37. package/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.styl +34 -0
  38. package/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.styl.d.ts +12 -0
  39. package/src/components/ui/Chart/components/SeriesLegend/SeriesLegend.tsx +70 -0
  40. package/src/components/ui/Chart/components/SeriesLegend/index.ts +1 -0
  41. package/src/components/ui/ChartAreaInteractive/ChartLines.tsx +110 -40
  42. package/src/components/ui/ChartAreaInteractive/chartHistories.ts +5 -0
  43. package/src/components/ui/StackedAreaChart/AGENT.md +1 -0
  44. package/src/components/ui/StackedAreaChart/StackedAreaChart.helpers.ts +54 -13
  45. package/src/components/ui/StackedAreaChart/StackedAreaChart.tooltip.tsx +1 -1
  46. package/src/components/ui/StackedAreaChart/StackedAreaChart.tsx +36 -12
  47. package/src/components/ui/StackedAreaChart/StackedAreaChart.types.ts +9 -0
@@ -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
- legendVariant?: 'default' | 'svg';
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
- formatDate: formatDateFn = formatDate,
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 filteredPayload = normalizeTooltipPayload(props.payload);
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 === 'svg' ? (
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
- dataKey="date"
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
- {...(xMin !== undefined &&
108
- xMax !== undefined && { domain: [xMin, xMax] })}
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={-5} />
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 && typeof displayLabel === 'string'
89
- ? config[displayLabel as keyof typeof config]?.label || displayLabel
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 (!value) {
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;
@@ -0,0 +1,70 @@
1
+ import cn from 'classnames';
2
+ import type { ReactNode } from 'react';
3
+
4
+ import ChartS from '../../Chart.styl';
5
+ import S from './SeriesLegend.styl';
6
+
7
+ export type SeriesLegendItem = {
8
+ /** The series' data key; the legend hands it back on a click. */
9
+ dataKey: string;
10
+ label: ReactNode;
11
+ color: string;
12
+ /** Draw the swatch as a dashed line (a forecast line). */
13
+ dashed?: boolean;
14
+ /** Draw the swatch as a filled square (a bar or an area) instead of a line. */
15
+ square?: boolean;
16
+ };
17
+
18
+ /**
19
+ * A plain legend: one entry per series, in the order given, each a button
20
+ * that hides or shows its series. A hidden series' entry is dimmed.
21
+ */
22
+ export function SeriesLegend({
23
+ items,
24
+ hiddenSeries,
25
+ onToggle,
26
+ className,
27
+ }: {
28
+ items: SeriesLegendItem[];
29
+ hiddenSeries?: ReadonlySet<string>;
30
+ /** Without it the entries are text, not buttons. */
31
+ onToggle?: (dataKey: string) => void;
32
+ className?: string;
33
+ }) {
34
+ if (items.length === 0) return null;
35
+ return (
36
+ <div className={cn(ChartS.legendContainer, S.root, className)}>
37
+ {items.map(item => {
38
+ const dimmed = hiddenSeries?.has(item.dataKey) ?? false;
39
+ const content = (
40
+ <>
41
+ <span
42
+ className={cn(
43
+ S.swatch,
44
+ item.square ? S.square : item.dashed && S.dashed,
45
+ )}
46
+ style={{ color: item.color }}
47
+ aria-hidden
48
+ />
49
+ {item.label}
50
+ </>
51
+ );
52
+ return onToggle ? (
53
+ <button
54
+ key={item.dataKey}
55
+ type="button"
56
+ className={cn(ChartS.legendItem, S.button, dimmed && S.dimmed)}
57
+ aria-pressed={!dimmed}
58
+ onClick={() => onToggle(item.dataKey)}
59
+ >
60
+ {content}
61
+ </button>
62
+ ) : (
63
+ <span key={item.dataKey} className={ChartS.legendItem}>
64
+ {content}
65
+ </span>
66
+ );
67
+ })}
68
+ </div>
69
+ );
70
+ }
@@ -0,0 +1 @@
1
+ export { SeriesLegend, type SeriesLegendItem } from './SeriesLegend';