@fundar/data-chart-telling 0.0.5 → 0.0.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.
@@ -31,7 +31,7 @@
31
31
  y,
32
32
  z,
33
33
  styles = {},
34
- segments = [],
34
+ segments = {},
35
35
  scales = {},
36
36
  margins,
37
37
  markers = [],
@@ -30,7 +30,7 @@
30
30
  y,
31
31
  z,
32
32
  styles = {},
33
- segments = [],
33
+ segments = {},
34
34
  markers = [],
35
35
  scales = {},
36
36
  margins,
@@ -27,7 +27,7 @@
27
27
  y,
28
28
  z,
29
29
  styles = {},
30
- segments = [],
30
+ segments = {},
31
31
  scales = {},
32
32
  margins,
33
33
  markers = [],
@@ -25,7 +25,7 @@
25
25
  y,
26
26
  z,
27
27
  styles = {},
28
- segments = [],
28
+ segments = {},
29
29
  markers = [],
30
30
  scales = {},
31
31
  margins,
@@ -40,7 +40,7 @@
40
40
  styles = {},
41
41
  scales = {},
42
42
  margins,
43
- segments = [],
43
+ segments = {},
44
44
  markers = [],
45
45
  tooltip = undefined,
46
46
  }: PlotProps<TData, BarSegmentStyle> = $props();
@@ -1,9 +1,9 @@
1
1
  import type { AxisValue, Series } from '../../types/plots/common';
2
2
  import type { Segment, Segments, LineSegmentStyle, VisualGroup } from '../../types/plots/styles';
3
3
  /**
4
- * Returns the flat {@link Segment} list for one series by merging all groups.
5
- * Within each group, series-specific segments are added before `'default'`
6
- * segments, so series-specific rules win on tie-breaks.
4
+ * Returns the flat {@link Segment} list for one series.
5
+ * Series-specific segments are added before `'default'` segments so that
6
+ * series-specific catch-alls win over default catch-alls on tie-breaks.
7
7
  */
8
8
  export declare function resolveSegmentsForSeries<TStyle>(segments: Segments<TStyle>, seriesName: string): Segment<TStyle>[];
9
9
  /** Returns true when `xVal` falls inside any of a segment's x ranges. */
@@ -1,21 +1,14 @@
1
1
  import { resolveAccessor } from './accessors';
2
2
  const DEFAULT_KEY = 'default';
3
3
  /**
4
- * Returns the flat {@link Segment} list for one series by merging all groups.
5
- * Within each group, series-specific segments are added before `'default'`
6
- * segments, so series-specific rules win on tie-breaks.
4
+ * Returns the flat {@link Segment} list for one series.
5
+ * Series-specific segments are added before `'default'` segments so that
6
+ * series-specific catch-alls win over default catch-alls on tie-breaks.
7
7
  */
8
8
  export function resolveSegmentsForSeries(segments, seriesName) {
9
- const result = [];
10
- for (const group of segments) {
11
- const specific = group[seriesName];
12
- const defaults = group[DEFAULT_KEY];
13
- if (specific)
14
- result.push(...specific);
15
- if (defaults)
16
- result.push(...defaults);
17
- }
18
- return result;
9
+ const specific = segments[seriesName] ?? [];
10
+ const defaults = segments[DEFAULT_KEY] ?? [];
11
+ return [...specific, ...defaults];
19
12
  }
20
13
  /** Returns true when `xVal` falls inside any of a segment's x ranges. */
21
14
  export function matchesSegmentX(xVal, seg) {
@@ -71,10 +64,8 @@ export function validateSegments(segments) {
71
64
  `only the first catch-all's style will be used.`);
72
65
  }
73
66
  };
74
- for (const group of segments) {
75
- for (const [key, segs] of Object.entries(group)) {
76
- check(key === DEFAULT_KEY ? '(default)' : `"${key}"`, segs);
77
- }
67
+ for (const [key, segs] of Object.entries(segments)) {
68
+ check(key === DEFAULT_KEY ? '(default)' : `"${key}"`, segs);
78
69
  }
79
70
  }
80
71
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -146,7 +137,15 @@ function resolveSeriesSegments(series, segs) {
146
137
  defaultVisual.push({ data: bridged, style: {} });
147
138
  }
148
139
  else {
149
- userVisual.push({ data: run.data, style: segs[run.segIdx].style ?? {} });
140
+ const seg = segs[run.segIdx];
141
+ const segStyle = seg.style ?? {};
142
+ // Area-specific segments merge the catch-all as a baseline: catch-all
143
+ // keys fill in whatever the area segment omits; the area wins on conflict.
144
+ const isAreaSpecific = seg.areas != null && seg.areas.length > 0;
145
+ const resolvedStyle = isAreaSpecific && catchAll?.style
146
+ ? { ...catchAll.style, ...segStyle }
147
+ : segStyle;
148
+ userVisual.push({ data: run.data, style: resolvedStyle });
150
149
  // When the next run is also a user segment (no unmatched data between them),
151
150
  // insert a line-only connector so the lines join without a gap. Using a
152
151
  // separate mark (rather than appending a bridge point to the current run)
@@ -146,50 +146,29 @@ export type Segment<TStyle> = {
146
146
  areas?: Area[];
147
147
  style?: TStyle;
148
148
  };
149
- /** Per-series list of {@link Segment}s, keyed by series name. */
150
- export type SeriesAwareSegmentsMap<TStyle> = Record<string, Segment<TStyle>[]>;
151
- /**
152
- * A segment group that applies its segments to **every** series. Use the
153
- * `'default'` key to target all series at once.
154
- *
155
- * @example
156
- * ```ts
157
- * const group: DefaultSegmentsMap<LineSegmentStyle> = {
158
- * default: [{ areas: [{ x: { from: 2012, to: 2014 } }], style: { dots: { dotSymbol: 'circle' } } }],
159
- * };
160
- * ```
161
- */
162
- export type DefaultSegmentsMap<TStyle> = {
163
- default: Segment<TStyle>[];
164
- };
165
- /**
166
- * One element of a {@link Segments} array. Either a {@link DefaultSegmentsMap}
167
- * (applies its segments to every series via the `'default'` key) or a
168
- * {@link SeriesAwareSegmentsMap} (applies segments only to the named series).
169
- * Both forms can be mixed in the same array.
170
- */
171
- export type SegmentsStyle<TStyle> = DefaultSegmentsMap<TStyle> | SeriesAwareSegmentsMap<TStyle>;
172
149
  /**
173
150
  * The `segments` prop accepted by every plot kind.
174
151
  *
175
- * An array of {@link SegmentsStyle} groups processed in order. Each group maps
176
- * either `'default'` (all series) or specific series names to lists of
177
- * {@link Segment}s. Multiple groups let you organise segments by concern:
152
+ * A record mapping series names (or `'default'` for all series) to lists of
153
+ * {@link Segment}s. Segments in a named key apply only to that series;
154
+ * segments under `'default'` apply to every series.
178
155
  *
179
156
  * ```ts
180
- * segments: [
181
- * // Group 1 per-series stroke colours (catch-all baseline)
182
- * { female: [{ style: { stroke: { stroke: 'steelblue' } } }],
183
- * male: [{ style: { stroke: { stroke: 'tomato' } } }] },
184
- * // Group 2 – shared dot symbols for every series
185
- * { default: [{ areas: [{ x: { from: 2012, to: 2014 } }], style: { dots: { dotSymbol: 'circle' } } }] },
186
- * ]
157
+ * segments: {
158
+ * female: [{ style: { stroke: { stroke: 'tomato' } } }], // catch-all for female
159
+ * male: [{ style: { stroke: { stroke: 'steelblue' } } }], // catch-all for male
160
+ * default: [{ areas: [{ x: { from: 2012, to: 2014 } }], style: { dots: { dotSymbol: 'circle' } } }],
161
+ * }
187
162
  * ```
188
163
  *
189
164
  * Within each resolved series list, catch-alls (no `areas`) are evaluated last
190
165
  * so that area-specific segments always override them, regardless of position.
166
+ * Area-specific segments also inherit any style key absent from their own style
167
+ * by merging the catch-all as a baseline — the area's own style wins on conflict.
168
+ * This lets you set a series colour once in a catch-all and add per-range dots
169
+ * or dash patterns without repeating the colour on every area segment.
191
170
  */
192
- export type Segments<TStyle> = SegmentsStyle<TStyle>[];
171
+ export type Segments<TStyle> = Record<string, Segment<TStyle>[]>;
193
172
  /** One contiguous, styled slice of a series' data — ready to draw as its own mark. */
194
173
  export type VisualSegment<T extends Record<string, unknown>, TStyle = LineSegmentStyle> = {
195
174
  data: T[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fundar/data-chart-telling",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"