@sybilion/uilib 1.8.4 → 1.8.5

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.
@@ -36,6 +36,7 @@ const AXIS_LINE = { stroke: 'var(--border)', strokeWidth: 1 };
36
36
  const X_AXIS_HEIGHT_WITH_LABEL = 44;
37
37
  const layerColor = (layer, index) => layer.color ?? getChartSeriesColor(index);
38
38
  const overlayColor = (overlay) => overlay.color ?? DEFAULT_OVERLAY_COLOR;
39
+ const LONE_DOT_RADIUS = 3;
39
40
  function LegendEntry({ swatch, label, extra, dimmed, onClick, }) {
40
41
  const content = (jsxs(Fragment, { children: [swatch, label, extra != null && jsx("span", { className: S.legendExtra, children: extra })] }));
41
42
  return onClick ? (jsx("button", { type: "button", className: cn(ChartS.legendItem, S.legendButton, dimmed && S.dimmed), "aria-pressed": !dimmed, onClick: onClick, children: content })) : (jsx("span", { className: ChartS.legendItem, children: content }));
@@ -68,6 +69,18 @@ const StackedAreaChart = forwardRef(({ className, data, xKey, layers, overlays =
68
69
  // does not change them.
69
70
  const weights = useMemo(() => (showWeights ? stackWeights(data, visibleKeys) : undefined), [showWeights, data, visibleKeys]);
70
71
  const asBars = rows.length < barsBelow;
72
+ // How many values each overlay has: a bridged line with one value draws
73
+ // nothing, so `dotLonePoints` dots it.
74
+ const overlayValueCounts = useMemo(() => {
75
+ const counts = {};
76
+ for (const overlay of overlays) {
77
+ counts[overlay.key] = data.filter(row => {
78
+ const value = row[overlay.key];
79
+ return typeof value === 'number' && Number.isFinite(value);
80
+ }).length;
81
+ }
82
+ return counts;
83
+ }, [data, overlays]);
71
84
  const compareKey = overlays.find(o => o.compare)?.key;
72
85
  const zonesOn = compareKey != null && !hidden.has(compareKey);
73
86
  const zones = useMemo(() => {
@@ -120,7 +133,17 @@ const StackedAreaChart = forwardRef(({ className, data, xKey, layers, overlays =
120
133
  width: yAxisLabel ? 72 : 56, tickLine: false, axisLine: AXIS_LINE, children: yAxisLabel && (jsx(Label, { value: yAxisLabel, angle: -90, position: "insideLeft" })) }), showTooltip && (jsx(ChartPlacedTooltip, { content: tooltip => (jsx(StackedTooltip, { active: tooltip.active, payload: tooltip.payload, label: tooltip.label, config: config, valueFormatter: tooltipValueFormatter, labelFormatter: tooltipLabelFormatter })) })), zones.map(zone => (jsx(ReferenceArea, { x1: zone.x1, x2: zone.x2, fill: ZONE_FILL[zone.kind], fillOpacity: 1, stroke: "none", ifOverflow: "hidden", zIndex: ZONE_Z_INDEX }, `${zone.kind}-${String(zone.x1)}`))), layers.map((layer, index) => asBars ? (jsx(Bar, { dataKey: layer.key, name: layer.key, fill: layerColor(layer, index), maxBarSize: 96, hide: hidden.has(layer.key), ...stackProps }, layer.key)) : (jsx(Area, { type: "monotone", dataKey: layer.key, name: layer.key, fill: layerColor(layer, index), fillOpacity: 1, hide: hidden.has(layer.key), ...stackProps }, layer.key))), margin &&
121
134
  (asBars ? (jsx(Bar, { dataKey: MARGIN_KEY, name: MARGIN_KEY, fill: marginColor, maxBarSize: 96, hide: !marginOn, ...stackProps }, MARGIN_KEY)) : (jsx(Area, { type: "monotone", dataKey: MARGIN_KEY, name: MARGIN_KEY, fill: marginColor, fillOpacity: 1, hide: !marginOn, ...stackProps }, MARGIN_KEY))), overlays.map(overlay => {
122
135
  const color = overlayColor(overlay);
123
- return (jsx(Line, { type: "linear", dataKey: overlay.key, name: overlay.key, stroke: color, strokeWidth: 1.75, dot: asBars ? { r: 4, fill: color, stroke: color } : false, activeDot: false, connectNulls: true, isAnimationActive: false, hide: hidden.has(overlay.key) }, overlay.key));
136
+ const lone = !asBars &&
137
+ overlay.dotLonePoints &&
138
+ overlayValueCounts[overlay.key] === 1;
139
+ return (jsx(Line, { type: "linear", dataKey: overlay.key, name: overlay.key, stroke: color, strokeWidth: 1.75, dot: asBars
140
+ ? { r: 4, fill: color, stroke: color }
141
+ : lone
142
+ ? (props) => typeof props.value === 'number' &&
143
+ Number.isFinite(props.value) &&
144
+ props.cx != null &&
145
+ props.cy != null ? (jsx("circle", { className: S.loneDot, cx: props.cx, cy: props.cy, r: LONE_DOT_RADIUS, fill: color }, `lone-${overlay.key}-${props.index}`)) : (jsx("g", {}, `lone-${overlay.key}-${props.index}`))
146
+ : false, activeDot: false, connectNulls: true, isAnimationActive: false, hide: hidden.has(overlay.key) }, overlay.key));
124
147
  })] }) }), showLegend && (jsxs("div", { className: cn(ChartS.legendContainer, S.legend), children: [layers.map((layer, index) => (jsx(LegendEntry, { swatch: jsx("span", { className: ChartS.legendIndicator, style: { backgroundColor: layerColor(layer, index) } }), label: layer.label ?? layer.key, extra: weights && !hidden.has(layer.key)
125
148
  ? `${weights[layer.key] ?? 0}%`
126
149
  : undefined, dimmed: hidden.has(layer.key), onClick: toggle && (() => toggle(layer.key)) }, layer.key))), marginOn && (jsx(LegendEntry, { swatch: jsx("span", { className: ChartS.legendIndicator, style: { backgroundColor: marginColor } }), label: marginLabel })), overlays.map(overlay => (jsx(LegendEntry, { swatch: jsx("span", { className: S.legendLine, style: { backgroundColor: overlayColor(overlay) } }), label: overlay.label ?? overlay.key, dimmed: hidden.has(overlay.key), onClick: toggle && (() => toggle(overlay.key)) }, overlay.key))), zoneKinds.map(kind => (jsx(LegendEntry, { swatch: jsx("span", { className: cn(ChartS.legendIndicator, S.legendZone), style: { backgroundColor: ZONE_FILL[kind] } }), label: zoneLabels[kind] }, kind)))] }))] }));
@@ -1,7 +1,7 @@
1
1
  import styleInject from 'style-inject';
2
2
 
3
- var css_248z = ".StackedAreaChart_root__BkxHH{width:100%}.StackedAreaChart_legend__JVlx8{flex-wrap:wrap;margin-top:.75rem;row-gap:.5rem}.StackedAreaChart_legendButton__-nJsf{-webkit-appearance:none;-moz-appearance:none;appearance:none;background:none;border:0;color:inherit;cursor:pointer;font:inherit;padding:0}.StackedAreaChart_dimmed__L-fX7{opacity:.4}.StackedAreaChart_legendExtra__xGgeV{color:var(--muted-foreground);font-feature-settings:\"tnum\";font-variant-numeric:tabular-nums}.StackedAreaChart_legendLine__UDtNb{border-radius:1px;flex-shrink:0;height:2px;width:14px}.StackedAreaChart_legendZone__-47oB{border:1px solid var(--border)}";
4
- var S = {"root":"StackedAreaChart_root__BkxHH","legend":"StackedAreaChart_legend__JVlx8","legendButton":"StackedAreaChart_legendButton__-nJsf","dimmed":"StackedAreaChart_dimmed__L-fX7","legendExtra":"StackedAreaChart_legendExtra__xGgeV","legendLine":"StackedAreaChart_legendLine__UDtNb","legendZone":"StackedAreaChart_legendZone__-47oB"};
3
+ var css_248z = ".StackedAreaChart_root__BkxHH{width:100%}.StackedAreaChart_legend__JVlx8{flex-wrap:wrap;margin-top:.75rem;row-gap:.5rem}.StackedAreaChart_legendButton__-nJsf{-webkit-appearance:none;-moz-appearance:none;appearance:none;background:none;border:0;color:inherit;cursor:pointer;font:inherit;padding:0}.StackedAreaChart_dimmed__L-fX7{opacity:.4}.StackedAreaChart_legendExtra__xGgeV{color:var(--muted-foreground);font-feature-settings:\"tnum\";font-variant-numeric:tabular-nums}.StackedAreaChart_legendLine__UDtNb{border-radius:1px;flex-shrink:0;height:2px;width:14px}.StackedAreaChart_loneDot__08-V7{pointer-events:none}.StackedAreaChart_legendZone__-47oB{border:1px solid var(--border)}";
4
+ var S = {"root":"StackedAreaChart_root__BkxHH","legend":"StackedAreaChart_legend__JVlx8","legendButton":"StackedAreaChart_legendButton__-nJsf","dimmed":"StackedAreaChart_dimmed__L-fX7","legendExtra":"StackedAreaChart_legendExtra__xGgeV","legendLine":"StackedAreaChart_legendLine__UDtNb","loneDot":"StackedAreaChart_loneDot__08-V7","legendZone":"StackedAreaChart_legendZone__-47oB"};
5
5
  styleInject(css_248z);
6
6
 
7
7
  export { S as default };
@@ -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
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sybilion/uilib",
3
- "version": "1.8.4",
3
+ "version": "1.8.5",
4
4
  "description": "Sybilion Design System — React UI components (Vite + Stylus)",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -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).
@@ -28,5 +28,8 @@
28
28
  flex-shrink 0
29
29
  border-radius 1px
30
30
 
31
+ .loneDot
32
+ pointer-events none
33
+
31
34
  .legendZone
32
35
  border 1px solid var(--border)
@@ -7,6 +7,7 @@ interface CssExports {
7
7
  legendExtra: string;
8
8
  legendLine: string;
9
9
  legendZone: string;
10
+ loneDot: string;
10
11
  root: string;
11
12
  }
12
13
  export const cssExports: CssExports;
@@ -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={asBars ? { r: 4, fill: color, stroke: color } : false}
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 {
@@ -48,6 +48,20 @@ const PAID: StackedAreaChartOverlay[] = [
48
48
  { key: 'paid', label: 'Prices paid', compare: true },
49
49
  ];
50
50
 
51
+ // One price paid in the window, next to a market line.
52
+ const ONE_PAID_ROWS: StackedAreaChartDatum[] = COST_ROWS.slice(24).map(
53
+ (row, i) => ({
54
+ ...row,
55
+ paid: i === 7 ? 3.4 : null,
56
+ market: Number((3.1 + 0.2 * Math.sin(i / 2)).toFixed(2)),
57
+ }),
58
+ );
59
+
60
+ const ONE_PAID_OVERLAYS: StackedAreaChartOverlay[] = [
61
+ { key: 'market', label: 'Market', color: 'var(--muted-foreground)' },
62
+ { key: 'paid', label: 'Prices paid', dotLonePoints: true },
63
+ ];
64
+
51
65
  const QUARTER_ROWS: StackedAreaChartDatum[] = [
52
66
  {
53
67
  quarter: 'Q1',
@@ -121,6 +135,22 @@ export default function StackedAreaChartPage() {
121
135
  height={320}
122
136
  />
123
137
  </PageContentSection>
138
+ <PageContentSection>
139
+ <SectionHeader
140
+ title="A lone price"
141
+ description="A market line, and a single price paid shown as a dot with dotLonePoints (a line with one value draws nothing)."
142
+ withMargin
143
+ />
144
+ <StackedAreaChart
145
+ data={ONE_PAID_ROWS}
146
+ xKey="date"
147
+ layers={COST_LAYERS}
148
+ overlays={ONE_PAID_OVERLAYS}
149
+ valueFormatter={formatPrice}
150
+ tooltipLabelFormatter={formatMonth}
151
+ height={260}
152
+ />
153
+ </PageContentSection>
124
154
  <PageContentSection>
125
155
  <SectionHeader
126
156
  title="Few points"