derive-dashboards 0.2.0 → 0.4.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 (70) hide show
  1. package/README.md +4 -0
  2. package/dist/cache.mjs +2 -1
  3. package/dist/cli/add.mjs +1 -1
  4. package/dist/cli/doctor.mjs +39 -24
  5. package/dist/core/document/display.d.mts +63 -0
  6. package/dist/core/document/display.mjs +139 -0
  7. package/dist/core/engine/version.d.mts +5 -4
  8. package/dist/core/engine/version.mjs +5 -4
  9. package/dist/core/error/codes.d.mts +2 -0
  10. package/dist/core/error/codes.mjs +2 -0
  11. package/dist/core/index.d.mts +9 -7
  12. package/dist/core/index.mjs +7 -5
  13. package/dist/core/model/validate/drill.mjs +6 -15
  14. package/dist/core/model/validate/fields.mjs +130 -0
  15. package/dist/core/model/validate/index.mjs +11 -3
  16. package/dist/core/model/validate/known-members.mjs +133 -0
  17. package/dist/core/model/validate/known-nested.mjs +85 -0
  18. package/dist/core/model/validate/known.mjs +94 -0
  19. package/dist/core/model/validate/source.mjs +8 -1
  20. package/dist/core/query/alias.mjs +11 -1
  21. package/dist/core/query/drill/selection.d.mts +7 -2
  22. package/dist/core/query/drill/selection.mjs +18 -15
  23. package/dist/core/result/columns.d.mts +10 -1
  24. package/dist/core/result/columns.mjs +21 -2
  25. package/dist/core/result/time-axis.d.mts +23 -0
  26. package/dist/core/result/time-axis.mjs +72 -0
  27. package/dist/core/result/types.d.mts +36 -1
  28. package/dist/core/schema/answer-parts.mjs +23 -1
  29. package/dist/core/schema/answers.d.mts +203 -0
  30. package/dist/core/schema/answers.mjs +1 -0
  31. package/dist/core/schema/tools.d.mts +2 -0
  32. package/dist/core/schema/tools.mjs +27 -1
  33. package/dist/core/time/bucket.mjs +8 -2
  34. package/dist/core/types/api.d.mts +1 -1
  35. package/dist/core/types/api.mjs +1 -1
  36. package/dist/drill-change.mjs +1 -1
  37. package/dist/drill-seal.mjs +1 -0
  38. package/dist/endpoints/index.d.mts +3 -0
  39. package/dist/execute.mjs +13 -7
  40. package/dist/index.mjs +6 -1
  41. package/dist/react/Dashboard.d.mts +4 -0
  42. package/dist/react/Dashboard.mjs +12 -1
  43. package/dist/react/Tile.mjs +11 -4
  44. package/dist/react/context.d.mts +2 -1
  45. package/dist/react/drill/Bars.mjs +2 -2
  46. package/dist/react/drill/props.mjs +2 -1
  47. package/dist/react/format.d.mts +6 -0
  48. package/dist/react/format.mjs +24 -18
  49. package/dist/react/index.d.mts +8 -4
  50. package/dist/react/index.mjs +8 -5
  51. package/dist/react/renderers/index.d.mts +9 -21
  52. package/dist/react/renderers/index.mjs +10 -7
  53. package/dist/react/renderers/list.d.mts +2 -1
  54. package/dist/react/renderers/list.mjs +8 -11
  55. package/dist/react/renderers/props.d.mts +26 -0
  56. package/dist/react/scope.d.mts +4 -0
  57. package/dist/react/scope.mjs +19 -2
  58. package/dist/react/series-points.d.mts +18 -0
  59. package/dist/react/series-points.mjs +19 -0
  60. package/dist/react/series-slots.d.mts +37 -0
  61. package/dist/react/series-slots.mjs +71 -0
  62. package/dist/react/series.d.mts +22 -12
  63. package/dist/react/series.mjs +80 -82
  64. package/dist/recharts/bar.mjs +5 -4
  65. package/dist/recharts/line.mjs +5 -4
  66. package/dist/recharts/parts.mjs +29 -14
  67. package/dist/recharts/table.mjs +10 -8
  68. package/dist/skills/derive-dashboards/SKILL.md +1 -1
  69. package/dist/store/apply.mjs +3 -1
  70. package/package.json +4 -4
@@ -0,0 +1,18 @@
1
+ import { CellFormat } from "./messages.mjs";
2
+ import { Drawn, Point, Series } from "./series.mjs";
3
+ import { ResultCell, TileAnswer } from "derive-dashboards/core";
4
+ /**
5
+ * The answer's row behind a point (or its place) of a series, for `drill.open(row,
6
+ * series.column.id)`; undefined for a point no row stands behind.
7
+ */
8
+ export declare const rowAt: (drawn: Drawn, point: number | Point, seriesKey: string) => number | undefined;
9
+ /** The answer's own cell behind a point of a series; none when no row stands behind it. */
10
+ export declare function pointCell(answer: Pick<TileAnswer, "columns" | "rows">, drawn: Drawn, at: {
11
+ point: number | Point;
12
+ series: Series;
13
+ }): ResultCell | undefined;
14
+ /** The answer's own cell behind a point of a series, as its column writes it; none with no row. */
15
+ export declare function pointText(answer: Pick<TileAnswer, "columns" | "rows">, drawn: Drawn, at: {
16
+ point: number | Point;
17
+ series: Series;
18
+ }, format?: CellFormat): string | undefined;
@@ -0,0 +1,19 @@
1
+ import { formatCell } from "./format.mjs";
2
+ // src/series-points.ts
3
+ /**
4
+ * The answer's row behind a point (or its place) of a series, for `drill.open(row,
5
+ * series.column.id)`; undefined for a point no row stands behind.
6
+ */
7
+ const rowAt = (drawn, point, seriesKey) => drawn.rowAt(point, seriesKey);
8
+ /** The answer's own cell behind a point of a series; none when no row stands behind it. */
9
+ function pointCell(answer, drawn, at) {
10
+ const row = drawn.rowAt(at.point, at.series.key);
11
+ const column = answer.columns.indexOf(at.series.column);
12
+ return row === void 0 ? void 0 : answer.rows[row]?.[column];
13
+ }
14
+ /** The answer's own cell behind a point of a series, as its column writes it; none with no row. */
15
+ function pointText(answer, drawn, at, format = {}) {
16
+ const cell = pointCell(answer, drawn, at);
17
+ return cell === void 0 ? void 0 : formatCell(cell, at.series.column, format);
18
+ }
19
+ export { pointCell, pointText, rowAt };
@@ -0,0 +1,37 @@
1
+ import { ResultCell, ResultColumn } from "derive-dashboards/core";
2
+ /** A series that is one value of the fields a chart splits by. */
3
+ export type SplitSeries = {
4
+ /** The columns the chart splits by, after its x axis. */
5
+ by: readonly ResultColumn[];
6
+ /** The series' key: its raw values, as `toSeries` keys them. */
7
+ key: string;
8
+ /** Its raw cells, one per column of `by`. */
9
+ cells: readonly ResultCell[];
10
+ };
11
+ /** A book of palette places: the same value of the same field, the same place, every time it is asked. */
12
+ export type SeriesSlots = {
13
+ of: (series: SplitSeries) => number;
14
+ };
15
+ /**
16
+ * A new book. A value whose field declares its values in the model takes its place among them,
17
+ * so its colour is the same on every dashboard and every load. Any other value takes the next
18
+ * free place of its field the first time it is drawn, and keeps it while the book lives: a
19
+ * dashboard keeps one book, so a filter never repaints a series and every tile agrees.
20
+ *
21
+ * Known gap: a field that declares no values has no place that outlives the page, so after a
22
+ * reload its values are placed again in the order they are first drawn. Declare the field's
23
+ * `values` in the model, or a widget's `display.colors`, to fix a colour for good. A chart
24
+ * split by two fields places each pair of values, and series that are measures go by position.
25
+ */
26
+ export declare function seriesSlots(): SeriesSlots;
27
+ /**
28
+ * The palette colour of each series of one chart, as an index into a palette of `size`
29
+ * colours; none for a series with no slot (Other). A series takes its slot's colour. Two slots
30
+ * a palette apart would take the same one, so when the chart has a colour to spare the series
31
+ * with the higher slot takes the first colour no series of the chart has: within one chart
32
+ * colours stay apart for as long as the palette allows, as they did when colour went by
33
+ * position. Only past the palette's size do two series share a colour.
34
+ */
35
+ export declare function chartColors(series: readonly {
36
+ slot?: number;
37
+ }[], size: number): (number | undefined)[];
@@ -0,0 +1,71 @@
1
+ // src/series-slots.ts
2
+ /** The place the model gives a value: its index among the values its field declares, in the model's order. */
3
+ function declaredSlot(series) {
4
+ const [column, ...more] = series.by;
5
+ const [cell] = series.cells;
6
+ if (column?.values === void 0 || more.length > 0 || typeof cell !== "string") return void 0;
7
+ const index = column.values.indexOf(cell);
8
+ return index === -1 ? void 0 : index;
9
+ }
10
+ /** The lowest place no value of the field has, at or after the places its declared values hold. */
11
+ function freeSlot(taken, from) {
12
+ const used = new Set(taken.values());
13
+ let slot = from;
14
+ while (used.has(slot)) slot += 1;
15
+ return slot;
16
+ }
17
+ /**
18
+ * A new book. A value whose field declares its values in the model takes its place among them,
19
+ * so its colour is the same on every dashboard and every load. Any other value takes the next
20
+ * free place of its field the first time it is drawn, and keeps it while the book lives: a
21
+ * dashboard keeps one book, so a filter never repaints a series and every tile agrees.
22
+ *
23
+ * Known gap: a field that declares no values has no place that outlives the page, so after a
24
+ * reload its values are placed again in the order they are first drawn. Declare the field's
25
+ * `values` in the model, or a widget's `display.colors`, to fix a colour for good. A chart
26
+ * split by two fields places each pair of values, and series that are measures go by position.
27
+ */
28
+ function seriesSlots() {
29
+ const fields = /* @__PURE__ */ new Map();
30
+ return { of(series) {
31
+ const field = series.by.map((c) => c.member ?? c.id).join("|");
32
+ const taken = fields.get(field) ?? /* @__PURE__ */ new Map();
33
+ fields.set(field, taken);
34
+ const had = declaredSlot(series) ?? taken.get(series.key);
35
+ if (had !== void 0) {
36
+ taken.set(series.key, had);
37
+ return had;
38
+ }
39
+ const slot = freeSlot(taken, series.by.length === 1 ? series.by[0]?.values?.length ?? 0 : 0);
40
+ taken.set(series.key, slot);
41
+ return slot;
42
+ } };
43
+ }
44
+ /**
45
+ * The palette colour of each series of one chart, as an index into a palette of `size`
46
+ * colours; none for a series with no slot (Other). A series takes its slot's colour. Two slots
47
+ * a palette apart would take the same one, so when the chart has a colour to spare the series
48
+ * with the higher slot takes the first colour no series of the chart has: within one chart
49
+ * colours stay apart for as long as the palette allows, as they did when colour went by
50
+ * position. Only past the palette's size do two series share a colour.
51
+ */
52
+ function chartColors(series, size) {
53
+ const bySlot = series.flatMap((s, at) => s.slot === void 0 ? [] : [{
54
+ at,
55
+ slot: s.slot
56
+ }]).toSorted((a, b) => a.slot - b.slot);
57
+ const colors = /* @__PURE__ */ new Map();
58
+ const used = /* @__PURE__ */ new Set();
59
+ const take = (at, color) => {
60
+ colors.set(at, color);
61
+ used.add(color);
62
+ };
63
+ const waiting = bySlot.filter(({ at, slot }) => {
64
+ if (used.has(slot % size)) return true;
65
+ take(at, slot % size);
66
+ return false;
67
+ });
68
+ for (const { at, slot } of waiting) take(at, Array.from({ length: size }, (_v, i) => i).find((i) => !used.has(i)) ?? slot % size);
69
+ return series.map((_s, at) => colors.get(at));
70
+ }
71
+ export { chartColors, seriesSlots };
@@ -1,5 +1,6 @@
1
1
  import { CellFormat } from "./messages.mjs";
2
- import { Receipt, ResultColumn, TileAnswer } from "derive-dashboards/core";
2
+ import { SeriesSlots } from "./series-slots.mjs";
3
+ import { ResultCell, ResultColumn, TileAnswer, TimeBucket } from "derive-dashboards/core";
3
4
  /** One point of a chart: its x label, its place (`#`, never an entry id) and a number per series key. */
4
5
  export type Point = {
5
6
  x: string;
@@ -7,12 +8,15 @@ export type Point = {
7
8
  /**
8
9
  * A drawn series: its key in each point, its label and the column it reads. `axis` is the side
9
10
  * of the chart its scale is read on: values in another unit than the first get the right axis.
11
+ * `slot` is its place in the palette, which follows the series and not its position: a value of
12
+ * a field keeps its slot whatever else is drawn (`seriesSlots`). Other has none: it is the grey.
10
13
  */
11
14
  export type Series = {
12
15
  key: string;
13
16
  label: string;
14
17
  column: ResultColumn;
15
18
  axis: "left" | "right";
19
+ slot?: number;
16
20
  };
17
21
  /** The series key a dimension's Other draws as. */
18
22
  export declare const OTHER_KEY = "other";
@@ -24,8 +28,11 @@ export declare const OTHER_KEY = "other";
24
28
  * Values in a second unit are read on a right axis; a third unit, or more values than a split can
25
29
  * draw, are not drawn and are named in `notDrawn`, never silently dropped. Points and series are
26
30
  * keyed by raw values, so two that read alike stay apart. Totals are not drawn.
31
+ *
32
+ * `slots` is the dashboard's book of palette places, so a value has one colour on every tile;
33
+ * left out, the chart keeps a book of its own.
27
34
  */
28
- export declare function toSeries(answer: Pick<TileAnswer, "columns" | "rows">, format?: CellFormat): Drawn;
35
+ export declare function toSeries(answer: Answer, format?: CellFormat, slots?: SeriesSlots): Drawn;
29
36
  /**
30
37
  * What a chart draws, and the way back from any chart kit's point to the answer's row:
31
38
  * `rowAt(point, seriesKey)` gives the row's index in `answer.rows` (for a drill), or undefined
@@ -36,19 +43,22 @@ export type Drawn = {
36
43
  series: Series[];
37
44
  split: boolean;
38
45
  notDrawn: ResultColumn[];
46
+ /**
47
+ * When the x axis is time: each point's bucket as instants, in the points' order, `partial`
48
+ * on one not whole (the week still running). A chart draws time on these; `x` only names it.
49
+ */
50
+ buckets?: readonly (TimeBucket | undefined)[];
51
+ /** Whether a point stands for the values a limit folded together: its x is Other. */
52
+ isOther: (point: number) => boolean;
53
+ /** A point's raw x cell, as the answer has it; null for a point with none. What a point is of, however it reads. */
54
+ xAt: (point: number) => ResultCell;
39
55
  rowAt: (point: number | Point, seriesKey: string) => number | undefined;
40
56
  };
41
- /**
42
- * The row of the answer behind a point of any chart kit's series, by the point's place (or the
43
- * point) and the series key; undefined for a point no row stands behind. Pass it to
44
- * `drill.open(row, series.column.id)` to open what is behind the point.
45
- */
46
- export declare const rowAt: (drawn: Drawn, point: number | Point, seriesKey: string) => number | undefined;
57
+ type Answer = Pick<TileAnswer, "columns" | "rows" | "time">;
47
58
  /**
48
59
  * Whether the answer's last time bucket is still running: the range ends before the bucket
49
60
  * does, as this week does on its Monday. Its value is not final, so a chart marks it rather
50
- * than draw it as a fall.
61
+ * than draw it as a fall. The server says so (`answer.time`), by the engine's own calendar.
51
62
  */
52
- export declare function lastBucketOpen(answer: Pick<TileAnswer, "columns" | "rows"> & {
53
- receipt: Pick<Receipt, "range" | "calendar">;
54
- }): boolean;
63
+ export declare function lastBucketOpen(answer: Pick<TileAnswer, "columns" | "rows" | "time">): boolean;
64
+ export {};
@@ -1,11 +1,13 @@
1
1
  import { formatCell, plotValue } from "./format.mjs";
2
2
  import { ENGLISH } from "./english.mjs";
3
- import { isMark } from "derive-dashboards/core";
3
+ import { seriesSlots } from "./series-slots.mjs";
4
+ import { isMark, roleOf } from "derive-dashboards/core";
4
5
  // src/series.ts
5
- const isValue = (c) => c.id.startsWith("m") || c.id.startsWith("c");
6
+ const isValue = (c) => roleOf(c) === "measure" || roleOf(c) === "calc";
6
7
  /** The series key a dimension's Other draws as. */
7
8
  const OTHER_KEY = "other";
8
- const isAll = (cell) => isMark(cell) && cell.mark === "all";
9
+ /** Whether a cell is the given mark: Other, or the total row's All. */
10
+ const isMarked = (cell, mark) => isMark(cell) && cell.mark === mark;
9
11
  /** What a value is read against: two columns share an axis only when this is equal. */
10
12
  const scaleOf = (c) => JSON.stringify([
11
13
  c.unit ?? null,
@@ -20,45 +22,63 @@ const scaleOf = (c) => JSON.stringify([
20
22
  * Values in a second unit are read on a right axis; a third unit, or more values than a split can
21
23
  * draw, are not drawn and are named in `notDrawn`, never silently dropped. Points and series are
22
24
  * keyed by raw values, so two that read alike stay apart. Totals are not drawn.
25
+ *
26
+ * `slots` is the dashboard's book of palette places, so a value has one colour on every tile;
27
+ * left out, the chart keeps a book of its own.
23
28
  */
24
- function toSeries(answer, format = {}) {
29
+ function toSeries(answer, format = {}, slots) {
25
30
  const at = /* @__PURE__ */ new Map();
26
- const drawn = drawSeries(answer, format, (point, key, row) => {
31
+ const shape = shapeOf(answer, format, slots ?? seriesSlots(), (point, key, row) => {
27
32
  at.set(`${String(point)}|${key}`, row);
28
33
  });
34
+ const { xs, ...drawn } = drawSeries(answer, shape);
29
35
  return {
30
36
  ...drawn,
37
+ ...bucketsOf(answer, shape.keys, xs),
38
+ isOther: (point) => isMarked(xs[point] ?? null, "other"),
39
+ xAt: (point) => xs[point] ?? null,
31
40
  rowAt: (point, key) => {
32
41
  const index = typeof point === "number" ? point : drawn.points.indexOf(point);
33
42
  return at.get(`${String(index)}|${key}`) ?? at.get(`${String(index)}|*`);
34
43
  }
35
44
  };
36
45
  }
46
+ /** Each point's time bucket, when the x axis is the answer's time column. */
47
+ function bucketsOf(answer, keys, xs) {
48
+ const x = answer.columns[keys[0] ?? -1];
49
+ if (answer.time === void 0 || x === void 0 || roleOf(x) !== "time") return {};
50
+ const named = new Map(answer.time.buckets.map((b) => [b.label, b]));
51
+ return { buckets: xs.map((cell) => typeof cell === "string" ? named.get(cell) : void 0) };
52
+ }
37
53
  /** The values to draw: calcs when there are any (what the widget was built to see), else measures. */
38
54
  function valuesOf(columns) {
39
55
  const all = columns.filter((c) => isValue(c));
40
- const calcs = all.filter((c) => c.id.startsWith("c"));
56
+ const calcs = all.filter((c) => roleOf(c) === "calc");
41
57
  return calcs.length > 0 ? calcs : all;
42
58
  }
43
- function drawSeries(answer, format, seen) {
59
+ /** How an answer is read into a chart: its key columns, how a key reads, and the book of slots. */
60
+ function shapeOf(answer, format, slots, seen) {
44
61
  const keys = answer.columns.flatMap((c, i) => isValue(c) ? [] : [i]);
45
- const values = valuesOf(answer.columns);
46
- const rows = answer.rows.map((row, index) => ({
47
- row,
48
- index
49
- })).filter(({ row }) => !row.some((cell) => isAll(cell)));
50
62
  const keyText = (cell, column) => {
51
63
  if (cell !== null && cell !== "") return formatCell(cell, column, format);
52
64
  const words = format.messages ?? ENGLISH;
53
65
  return column === void 0 ? words.empty : words.drillNone(column.label);
54
66
  };
55
- const shape = {
67
+ return {
56
68
  answer,
57
69
  keys,
58
70
  keyText,
59
- seen
71
+ seen,
72
+ slots
60
73
  };
61
- return keys.length < 2 ? plainSeries(rows, values, shape) : splitSeries(rows, values, shape);
74
+ }
75
+ function drawSeries(answer, shape) {
76
+ const values = valuesOf(answer.columns);
77
+ const rows = answer.rows.map((row, index) => ({
78
+ row,
79
+ index
80
+ })).filter(({ row }) => !row.some((cell) => isMarked(cell, "all")));
81
+ return shape.keys.length < 2 ? plainSeries(rows, values, shape) : splitSeries(rows, values, shape);
62
82
  }
63
83
  /** The x label of a row, from its first key. */
64
84
  const xOf = (shape, row) => {
@@ -70,25 +90,29 @@ function plainSeries(rows, values, shape) {
70
90
  const scales = [...new Set(values.map((c) => scaleOf(c)))];
71
91
  const side = (c) => scales.indexOf(scaleOf(c));
72
92
  const drawn = values.filter((c) => side(c) < 2);
73
- const series = drawn.map((column) => ({
93
+ const series = drawn.map((column, slot) => ({
74
94
  key: column.id,
75
95
  label: column.label,
76
96
  column,
77
- axis: side(column) === 0 ? "left" : "right"
97
+ axis: side(column) === 0 ? "left" : "right",
98
+ slot
78
99
  }));
100
+ const points = rows.map(({ row, index }, i) => {
101
+ shape.seen(i, "*", index);
102
+ const cells = drawn.map((c) => [c.id, plotValue(row[shape.answer.columns.indexOf(c)] ?? null)]);
103
+ return {
104
+ x: xOf(shape, row),
105
+ "#": i,
106
+ ...Object.fromEntries(cells)
107
+ };
108
+ });
109
+ const xs = rows.map(({ row }) => row[shape.keys[0] ?? -1] ?? null);
79
110
  return {
80
- points: rows.map(({ row, index }, i) => {
81
- shape.seen(i, "*", index);
82
- const cells = drawn.map((c) => [c.id, plotValue(row[shape.answer.columns.indexOf(c)] ?? null)]);
83
- return {
84
- x: xOf(shape, row),
85
- "#": i,
86
- ...Object.fromEntries(cells)
87
- };
88
- }),
111
+ points,
89
112
  series,
90
113
  split: false,
91
- notDrawn: values.filter((c) => side(c) >= 2)
114
+ notDrawn: values.filter((c) => side(c) >= 2),
115
+ xs
92
116
  };
93
117
  }
94
118
  /** One value split into a series per combination of the keys after the first; other values are not drawn. */
@@ -98,18 +122,25 @@ function splitSeries(rows, values, shape) {
98
122
  points: [],
99
123
  series: [],
100
124
  split: true,
101
- notDrawn: []
125
+ notDrawn: [],
126
+ xs: []
102
127
  };
103
128
  const { answer, keys, keyText } = shape;
104
129
  const valueAt = answer.columns.indexOf(first);
105
130
  const byX = /* @__PURE__ */ new Map();
131
+ const xs = [];
106
132
  const names = /* @__PURE__ */ new Map();
107
133
  for (const { row, index } of rows) {
108
- const splitCells = keys.slice(1).map((i) => row[i] ?? null);
109
- const key = splitKey(splitCells);
110
- const label = splitCells.map((cell, i) => keyText(cell, answer.columns[keys[i + 1] ?? -1]));
111
- names.set(key, label.join(" · "));
112
- const xKey = JSON.stringify(row[keys[0] ?? 0] ?? null);
134
+ const cells = keys.slice(1).map((i) => row[i] ?? null);
135
+ const key = splitKey(cells);
136
+ const label = cells.map((cell, i) => keyText(cell, answer.columns[keys[i + 1] ?? -1]));
137
+ names.set(key, {
138
+ label: label.join(" · "),
139
+ cells
140
+ });
141
+ const x = row[keys[0] ?? 0] ?? null;
142
+ const xKey = JSON.stringify(x);
143
+ if (!byX.has(xKey)) xs.push(x);
113
144
  const point = byX.get(xKey) ?? {
114
145
  x: xOf(shape, row),
115
146
  "#": byX.size
@@ -118,17 +149,24 @@ function splitSeries(rows, values, shape) {
118
149
  byX.set(xKey, point);
119
150
  shape.seen(Number(point["#"]), key, index);
120
151
  }
121
- const series = [...names].map(([key, label]) => ({
152
+ const by = keys.slice(1).flatMap((i) => answer.columns[i] ?? []);
153
+ const series = [...names].map(([key, { label, cells }]) => ({
122
154
  key,
123
155
  label,
124
156
  column: first,
125
- axis: "left"
157
+ axis: "left",
158
+ ...key === "other" ? {} : { slot: shape.slots.of({
159
+ by,
160
+ key,
161
+ cells
162
+ }) }
126
163
  })).toSorted((a, b) => Number(a.key === OTHER_KEY) - Number(b.key === OTHER_KEY));
127
164
  return {
128
165
  points: [...byX.values()],
129
166
  series,
130
167
  split: true,
131
- notDrawn: rest
168
+ notDrawn: rest,
169
+ xs
132
170
  };
133
171
  }
134
172
  /** A series' key from its split cells: Other stays "other", any value is its raw JSON. */
@@ -138,54 +176,14 @@ function splitKey(cells) {
138
176
  return `s${JSON.stringify(cells.length === 1 ? only ?? null : cells)}`;
139
177
  }
140
178
  /**
141
- * The row of the answer behind a point of any chart kit's series, by the point's place (or the
142
- * point) and the series key; undefined for a point no row stands behind. Pass it to
143
- * `drill.open(row, series.column.id)` to open what is behind the point.
144
- */
145
- const rowAt = (drawn, point, seriesKey) => drawn.rowAt(point, seriesKey);
146
- const DAY = 864e5;
147
- const DAYS = {
148
- day: 1,
149
- week: 7
150
- };
151
- const MONTHS = {
152
- month: 1,
153
- quarter: 3,
154
- year: 12
155
- };
156
- /** A calendar date ("2026-11-16") as the UTC midnight that names it. */
157
- const dateOf = (text) => {
158
- const [year = 0, month = 1, day = 1] = text.slice(0, 10).split("-").map(Number);
159
- return Date.UTC(year, month - 1, day);
160
- };
161
- /** Where a bucket that starts on a calendar date ends, at its grain; none for a grain it cannot step. */
162
- function bucketEnd(start, grain) {
163
- const days = DAYS[grain];
164
- if (days !== void 0) return start + days * DAY;
165
- const months = MONTHS[grain];
166
- if (months === void 0) return void 0;
167
- const at = new Date(start);
168
- return Date.UTC(at.getUTCFullYear(), at.getUTCMonth() + months, at.getUTCDate());
169
- }
170
- /** An instant as the calendar day it falls on in a zone, named as a bucket names its day. */
171
- const dayIn = (instant, timeZone) => new Intl.DateTimeFormat("en-CA", {
172
- timeZone,
173
- year: "numeric",
174
- month: "2-digit",
175
- day: "2-digit"
176
- }).format(new Date(instant));
177
- /**
178
179
  * Whether the answer's last time bucket is still running: the range ends before the bucket
179
180
  * does, as this week does on its Monday. Its value is not final, so a chart marks it rather
180
- * than draw it as a fall.
181
+ * than draw it as a fall. The server says so (`answer.time`), by the engine's own calendar.
181
182
  */
182
183
  function lastBucketOpen(answer) {
183
- const grain = answer.columns[0]?.grain;
184
- const to = answer.receipt.range?.to;
185
- const last = answer.rows.findLast((row) => typeof row[0] === "string")?.[0];
186
- if (grain === void 0 || to === void 0 || typeof last !== "string") return false;
187
- if (!/^\d{4}-\d{2}-\d{2}/u.test(last)) return false;
188
- const end = bucketEnd(dateOf(last), grain);
189
- return end !== void 0 && dateOf(dayIn(to, answer.receipt.calendar.timezone)) + DAY < end;
184
+ const at = answer.columns.findIndex((c) => roleOf(c) === "time");
185
+ const last = answer.rows.findLast((row) => typeof row[at] === "string")?.[at];
186
+ const bucket = answer.time?.buckets.find((b) => b.label === last);
187
+ return bucket?.partial === "end" || bucket?.partial === "both";
190
188
  }
191
- export { OTHER_KEY, lastBucketOpen, rowAt, toSeries };
189
+ export { OTHER_KEY, lastBucketOpen, toSeries };
@@ -1,6 +1,6 @@
1
1
  import { yAxis } from "./axis.mjs";
2
2
  import { openPoint } from "./marks.mjs";
3
- import { CHART, TOOLTIP, color, legendText, runningLabel, tooltipFormat, tooltipLabel } from "./parts.mjs";
3
+ import { CHART, TOOLTIP, legendText, runningLabel, seriesColors, tooltipFormat, tooltipLabel } from "./parts.mjs";
4
4
  import { toSeries } from "./series.mjs";
5
5
  import { Beside } from "./table.mjs";
6
6
  import { useState } from "react";
@@ -81,8 +81,9 @@ function barAxes(overTime, values, points) {
81
81
  function BarTile(props) {
82
82
  const { answer, locale, drill } = props;
83
83
  const [focused, setFocused] = useState(null);
84
- const drawn = toSeries(answer, props);
84
+ const drawn = toSeries(answer, props, props.slots);
85
85
  const { points, series, split } = drawn;
86
+ const painted = seriesColors(props, series);
86
87
  const overTime = answer.columns[0]?.type === "time" || answer.columns[0]?.grain !== void 0;
87
88
  const running = overTime ? runningLabel(props, points) : void 0;
88
89
  const values = yAxis(series, points, locale, split);
@@ -113,10 +114,10 @@ function BarTile(props) {
113
114
  itemSorter: null,
114
115
  formatter: legendText
115
116
  }) : null,
116
- series.map((s, i) => /* @__PURE__ */ jsx(Bar, {
117
+ painted.map(({ series: s, color }, i) => /* @__PURE__ */ jsx(Bar, {
117
118
  dataKey: s.key,
118
119
  name: s.label,
119
- fill: color(props, s, i),
120
+ fill: color,
120
121
  ...split ? {
121
122
  stackId: "split",
122
123
  stroke: "var(--dd-surface)",
@@ -1,6 +1,6 @@
1
1
  import { yAxes } from "./axes.mjs";
2
2
  import { openPoint } from "./marks.mjs";
3
- import { CHART, TOOLTIP, color, dashOf, legendText, runningLabel, tooltipFormat, tooltipLabel } from "./parts.mjs";
3
+ import { CHART, TOOLTIP, dashOf, legendText, runningLabel, seriesColors, tooltipFormat, tooltipLabel } from "./parts.mjs";
4
4
  import { toSeries } from "./series.mjs";
5
5
  import { Beside } from "./table.mjs";
6
6
  import { useState } from "react";
@@ -50,8 +50,9 @@ function runningArea(props, points) {
50
50
  function LineTile(props) {
51
51
  const { answer, locale, drill } = props;
52
52
  const [focused, setFocused] = useState(null);
53
- const drawn = toSeries(answer, props);
53
+ const drawn = toSeries(answer, props, props.slots);
54
54
  const { points, series } = drawn;
55
+ const painted = seriesColors(props, series);
55
56
  const onClick = lineClick(drill, drawn);
56
57
  const running = runningLabel(props, points);
57
58
  return /* @__PURE__ */ jsxs("div", {
@@ -87,11 +88,11 @@ function LineTile(props) {
87
88
  itemSorter: null,
88
89
  formatter: legendText
89
90
  }) : null,
90
- series.map((s, i) => /* @__PURE__ */ jsx(Line, {
91
+ painted.map(({ series: s, color }, i) => /* @__PURE__ */ jsx(Line, {
91
92
  dataKey: s.key,
92
93
  name: s.label,
93
94
  yAxisId: s.axis,
94
- stroke: color(props, s, i),
95
+ stroke: color,
95
96
  strokeWidth: 2,
96
97
  ...dashOf(i),
97
98
  legendType: "plainline",
@@ -1,24 +1,35 @@
1
1
  import { OTHER_KEY } from "./series.mjs";
2
2
  import { cellText } from "./table.mjs";
3
- import { lastBucketOpen } from "derive-dashboards/react";
3
+ import { chartColors, lastBucketOpen } from "derive-dashboards/react";
4
4
  import { jsx } from "react/jsx-runtime";
5
+ import { displayOf } from "derive-dashboards/core";
5
6
  // src/parts.tsx
6
- /** A series' colour: the palette in order, and Other always the quiet grey. */
7
7
  /** Legend words in the text color: a series' hue marks its swatch, never its name. */
8
8
  const legendText = (value) => /* @__PURE__ */ jsx("span", {
9
9
  className: "dd-chart-legend",
10
10
  children: value
11
11
  });
12
- /** A widget's own colors by series label (`display.colors`), else the palette in order. */
13
- const colorsOf = (props) => {
14
- const colors = props.widget.display?.["colors"];
15
- return typeof colors === "object" && colors !== null ? new Map(Object.entries(colors)) : /* @__PURE__ */ new Map();
16
- };
17
- const color = (props, s, i) => {
18
- const own = colorsOf(props).get(s.label);
19
- if (typeof own === "string") return own;
20
- return s.key === OTHER_KEY ? "var(--dd-other)" : `var(--dd-series-${i % 6 + 1})`;
21
- };
12
+ /** A widget's own colour for a series, by its label (`display.colors`). */
13
+ const ownColor = (props, s) => displayOf("line", props.widget.display).colors?.[s.label];
14
+ /** The palette's colours, `--dd-series-1` to `--dd-series-6`. */
15
+ const PALETTE = 6;
16
+ /**
17
+ * Each series with its colour, in the series' order: the widget's own for it, else Other's quiet grey,
18
+ * else the palette's at the series' slot, which follows the series and not where it is drawn. In
19
+ * one chart two series share a colour only past the palette's six (`chartColors`).
20
+ */
21
+ function seriesColors(props, series) {
22
+ const placed = series.map((s, at) => s.key === OTHER_KEY ? {} : { slot: s.slot ?? at });
23
+ const palette = chartColors(placed, PALETTE);
24
+ return series.map((s, at) => {
25
+ const place = palette[at];
26
+ const fallback = place === void 0 ? "var(--dd-other)" : `var(--dd-series-${place + 1})`;
27
+ return {
28
+ series: s,
29
+ color: ownColor(props, s) ?? fallback
30
+ };
31
+ });
32
+ }
22
33
  /**
23
34
  * A tooltip's value: the answer's own cell behind the point, in its own series' column, so an
24
35
  * exact amount reads exactly and each series keeps its unit. Only the pixels use the float.
@@ -43,7 +54,11 @@ const DASHES = [
43
54
  "2 3",
44
55
  "10 3 2 3"
45
56
  ];
46
- /** The i-th line's dash, or none for the first: a solid line takes no dash at all. */
57
+ /**
58
+ * The i-th line's dash, or none for the first: a solid line takes no dash at all. The dash goes
59
+ * by where the line is drawn, not by its series' slot: its job is to tell the lines of one chart
60
+ * apart without colour, and two slots four apart would share a dash.
61
+ */
47
62
  const dashOf = (i) => {
48
63
  const dash = DASHES[i % DASHES.length];
49
64
  return dash === void 0 ? {} : { strokeDasharray: dash };
@@ -85,4 +100,4 @@ const CHART = {
85
100
  left: 0
86
101
  }
87
102
  };
88
- export { CHART, TOOLTIP, color, dashOf, legendText, runningLabel, tooltipFormat, tooltipLabel };
103
+ export { CHART, TOOLTIP, dashOf, legendText, runningLabel, seriesColors, tooltipFormat, tooltipLabel };
@@ -1,6 +1,6 @@
1
1
  import { NotDrawn } from "./axes.mjs";
2
2
  import { Marks } from "./marks.mjs";
3
- import { formatCell } from "derive-dashboards/react";
3
+ import { formatCell, pointText } from "derive-dashboards/react";
4
4
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
5
5
  // src/table.tsx
6
6
  /**
@@ -8,10 +8,12 @@ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
8
8
  * reads exactly and each series keeps its unit; the drawn number when no row stands behind it.
9
9
  */
10
10
  function cellText(props, drawn, at) {
11
- const { answer } = props;
12
- const row = typeof at.place === "number" ? drawn.rowAt(at.place, at.series.key) : void 0;
13
- const cell = row === void 0 ? void 0 : answer.rows[row]?.[answer.columns.indexOf(at.series.column)];
14
- if (cell !== void 0) return formatCell(cell, at.series.column, props);
11
+ const point = typeof at.place === "number" ? at.place : void 0;
12
+ const own = point === void 0 ? void 0 : pointText(props.answer, drawn, {
13
+ point,
14
+ series: at.series
15
+ }, props);
16
+ if (own !== void 0) return own;
15
17
  if (typeof at.value === "number") return formatCell(at.value, at.series.column, props);
16
18
  const v = at.value;
17
19
  return typeof v === "string" || typeof v === "boolean" || typeof v === "bigint" ? String(v) : "";
@@ -23,9 +25,9 @@ function cellText(props, drawn, at) {
23
25
  */
24
26
  function ChartTable({ drawn, props, kind }) {
25
27
  const { points, series } = drawn;
26
- return /* @__PURE__ */ jsxs("table", {
28
+ return /* @__PURE__ */ jsx("div", {
27
29
  className: "dd-sr-only",
28
- children: [
30
+ children: /* @__PURE__ */ jsxs("table", { children: [
29
31
  /* @__PURE__ */ jsx("caption", { children: props.messages.chartTable({
30
32
  title: props.widget.title,
31
33
  kind,
@@ -43,7 +45,7 @@ function ChartTable({ drawn, props, kind }) {
43
45
  series: s,
44
46
  value: point[s.key]
45
47
  }) }, s.key))] }, point.x)) })
46
- ]
48
+ ] })
47
49
  });
48
50
  }
49
51
  /** What sits beside every chart: its keyboard path, what it does not draw, and its data table. */
@@ -15,7 +15,7 @@ the `derive-dashboards` CLI. Docs: https://github.com/derive-to/dashboards/tree/
15
15
  Check, and say what you found:
16
16
  - Postgres (node-postgres `Pool`, or PGlite with a folder) is reachable. Other databases: a custom
17
17
  data layer (`docs/custom-data-layer.md`).
18
- - React 19 for the UI. Next.js: the provider and dashboards render from a `"use client"` file.
18
+ - React 18.3 or 19 for the UI (the filter bar that `add` copies into the app needs React 19). Next.js: the provider and dashboards render from a `"use client"` file.
19
19
  - How the app knows who is signed in: the handler needs a `viewer(request)` that returns the
20
20
  person (with tenant values when the model has tenant keys) or `null`.
21
21