repometry 0.4.1 → 0.5.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 (190) hide show
  1. package/README.md +4 -0
  2. package/package.json +1 -1
  3. package/target/main/resources/report/json/report-v2.schema.json +13 -1
  4. package/target/main/resources/viewer/files.css +7 -3
  5. package/target/main/resources/viewer/styles.css +2 -2
  6. package/target/main/ts/api/git/git.d.ts +1 -0
  7. package/target/main/ts/api/git/git.js +12 -0
  8. package/target/main/ts/api/git/inspection-source.js +2 -8
  9. package/target/main/ts/api/git/local.d.ts +1 -1
  10. package/target/main/ts/api/git/local.js +39 -5
  11. package/target/main/ts/api/process/docker-tool-runner.d.ts +13 -0
  12. package/target/main/ts/api/process/docker-tool-runner.js +108 -0
  13. package/target/main/ts/api/process/failure.d.ts +1 -0
  14. package/target/main/ts/api/process/failure.js +12 -0
  15. package/target/main/ts/api/process/plugin.d.ts +10 -0
  16. package/target/main/ts/api/process/plugin.js +96 -0
  17. package/target/main/ts/compose/analyze-snapshot.d.ts +3 -2
  18. package/target/main/ts/compose/analyze-snapshot.js +82 -29
  19. package/target/main/ts/compose/inspect.d.ts +5 -0
  20. package/target/main/ts/compose/inspect.js +10 -6
  21. package/target/main/ts/core/engine/execute.d.ts +3 -1
  22. package/target/main/ts/core/engine/execute.js +14 -2
  23. package/target/main/ts/core/engine/operation-cache.d.ts +14 -0
  24. package/target/main/ts/core/engine/operation-cache.js +66 -0
  25. package/target/main/ts/core/engine/plan.d.ts +1 -0
  26. package/target/main/ts/core/engine/plan.js +22 -3
  27. package/target/main/ts/core/engine/plugin.d.ts +8 -1
  28. package/target/main/ts/core/engine/plugin.js +1 -3
  29. package/target/main/ts/core/engine/report-products.js +2 -13
  30. package/target/main/ts/core/model/clone-source.js +1 -1
  31. package/target/main/ts/core/model/code-structure.d.ts +3 -0
  32. package/target/main/ts/core/model/code-structure.js +4 -1
  33. package/target/main/ts/core/model/finding-ignore.js +1 -1
  34. package/target/main/ts/core/model/finding.js +1 -1
  35. package/target/main/ts/core/model/line-coverage.d.ts +5 -1
  36. package/target/main/ts/core/model/line-coverage.js +14 -11
  37. package/target/main/ts/core/model/measurement.js +1 -1
  38. package/target/main/ts/core/model/plugin-data.d.ts +2 -0
  39. package/target/main/ts/core/model/plugin-data.js +15 -1
  40. package/target/main/ts/core/model/quality-summary.js +1 -1
  41. package/target/main/ts/core/model/scanner-import.js +1 -1
  42. package/target/main/ts/core/model/snapshot.d.ts +1 -0
  43. package/target/main/ts/core/model/snapshot.js +5 -1
  44. package/target/main/ts/core/model/source.d.ts +3 -0
  45. package/target/main/ts/core/model/source.js +15 -0
  46. package/target/main/ts/core/ports/tool-runner.d.ts +18 -0
  47. package/target/main/ts/core/ports/tool-runner.js +2 -0
  48. package/target/main/ts/plugin.d.ts +7 -1
  49. package/target/main/ts/plugin.js +3 -1
  50. package/target/main/ts/plugins/coverage/coverage.d.ts +5 -2
  51. package/target/main/ts/plugins/coverage/coverage.js +35 -10
  52. package/target/main/ts/plugins/language-js/function-facts.d.ts +1 -0
  53. package/target/main/ts/plugins/language-js/function-facts.js +30 -4
  54. package/target/main/ts/report/html/app/viewer-chart-bars.d.ts +11 -0
  55. package/target/main/ts/report/html/app/viewer-chart-bars.js +54 -0
  56. package/target/main/ts/report/html/app/viewer-chart-label.d.ts +15 -0
  57. package/target/main/ts/report/html/app/viewer-chart-label.js +43 -0
  58. package/target/main/ts/report/html/app/viewer-chart-line.d.ts +24 -0
  59. package/target/main/ts/report/html/app/viewer-chart-line.js +89 -0
  60. package/target/main/ts/report/html/app/viewer-chart-points.d.ts +49 -0
  61. package/target/main/ts/report/html/app/viewer-chart-points.js +74 -0
  62. package/target/main/ts/report/html/app/viewer-chart-scale.d.ts +21 -0
  63. package/target/main/ts/report/html/app/viewer-chart-scale.js +93 -0
  64. package/target/main/ts/report/html/app/viewer-chart-svg.d.ts +28 -0
  65. package/target/main/ts/report/html/app/viewer-chart-svg.js +41 -0
  66. package/target/main/ts/report/html/app/viewer-chart-time.d.ts +26 -0
  67. package/target/main/ts/report/html/app/viewer-chart-time.js +150 -0
  68. package/target/main/ts/report/html/app/viewer-chart-tip.d.ts +4 -0
  69. package/target/main/ts/report/html/app/viewer-chart-tip.js +210 -0
  70. package/target/main/ts/report/html/app/viewer-chart-x.d.ts +12 -0
  71. package/target/main/ts/report/html/app/viewer-chart-x.js +50 -0
  72. package/target/main/ts/report/html/app/viewer-chart.d.ts +4 -98
  73. package/target/main/ts/report/html/app/viewer-chart.js +16 -677
  74. package/target/main/ts/report/html/app/viewer-code-map.d.ts +27 -0
  75. package/target/main/ts/report/html/app/viewer-code-map.js +175 -0
  76. package/target/main/ts/report/html/app/viewer-columns.d.ts +38 -0
  77. package/target/main/ts/report/html/app/viewer-columns.js +92 -0
  78. package/target/main/ts/report/html/app/viewer-commit-picker.d.ts +12 -0
  79. package/target/main/ts/report/html/app/viewer-commit-picker.js +229 -0
  80. package/target/main/ts/report/html/app/viewer-commits.d.ts +5 -9
  81. package/target/main/ts/report/html/app/viewer-commits.js +17 -216
  82. package/target/main/ts/report/html/app/viewer-controls.d.ts +43 -0
  83. package/target/main/ts/report/html/app/viewer-controls.js +141 -0
  84. package/target/main/ts/report/html/app/viewer-copy.d.ts +17 -0
  85. package/target/main/ts/report/html/app/viewer-copy.js +64 -0
  86. package/target/main/ts/report/html/app/viewer-diagnostics.d.ts +4 -0
  87. package/target/main/ts/report/html/app/viewer-diagnostics.js +60 -0
  88. package/target/main/ts/report/html/app/viewer-dom.d.ts +27 -0
  89. package/target/main/ts/report/html/app/viewer-dom.js +99 -0
  90. package/target/main/ts/report/html/app/viewer-file-aside.d.ts +19 -0
  91. package/target/main/ts/report/html/app/viewer-file-aside.js +124 -0
  92. package/target/main/ts/report/html/app/viewer-file-figures.d.ts +15 -0
  93. package/target/main/ts/report/html/app/viewer-file-figures.js +60 -0
  94. package/target/main/ts/report/html/app/viewer-file-source.d.ts +7 -0
  95. package/target/main/ts/report/html/app/viewer-file-source.js +119 -0
  96. package/target/main/ts/report/html/app/viewer-files.d.ts +2 -118
  97. package/target/main/ts/report/html/app/viewer-files.js +24 -2184
  98. package/target/main/ts/report/html/app/viewer-filter.d.ts +3 -51
  99. package/target/main/ts/report/html/app/viewer-filter.js +111 -450
  100. package/target/main/ts/report/html/app/viewer-hotspots.d.ts +4 -0
  101. package/target/main/ts/report/html/app/viewer-hotspots.js +131 -0
  102. package/target/main/ts/report/html/app/viewer-inspection.d.ts +69 -0
  103. package/target/main/ts/report/html/app/viewer-inspection.js +36 -0
  104. package/target/main/ts/report/html/app/viewer-issue-group.d.ts +27 -0
  105. package/target/main/ts/report/html/app/viewer-issue-group.js +174 -0
  106. package/target/main/ts/report/html/app/viewer-issues.d.ts +4 -0
  107. package/target/main/ts/report/html/app/viewer-issues.js +173 -0
  108. package/target/main/ts/report/html/app/viewer-language-bar.d.ts +9 -0
  109. package/target/main/ts/report/html/app/viewer-language-bar.js +72 -0
  110. package/target/main/ts/report/html/app/viewer-languages.d.ts +27 -0
  111. package/target/main/ts/report/html/app/viewer-languages.js +93 -0
  112. package/target/main/ts/report/html/app/viewer-layout.d.ts +14 -0
  113. package/target/main/ts/report/html/app/viewer-layout.js +134 -0
  114. package/target/main/ts/report/html/app/viewer-line-coverage.d.ts +47 -0
  115. package/target/main/ts/report/html/app/viewer-line-coverage.js +121 -0
  116. package/target/main/ts/report/html/app/viewer-locations.d.ts +16 -0
  117. package/target/main/ts/report/html/app/viewer-locations.js +31 -0
  118. package/target/main/ts/report/html/app/viewer-map-scale.d.ts +26 -0
  119. package/target/main/ts/report/html/app/viewer-map-scale.js +147 -0
  120. package/target/main/ts/report/html/app/viewer-map-shape.d.ts +42 -0
  121. package/target/main/ts/report/html/app/viewer-map-shape.js +102 -0
  122. package/target/main/ts/report/html/app/viewer-metric-format.d.ts +23 -0
  123. package/target/main/ts/report/html/app/viewer-metric-format.js +93 -0
  124. package/target/main/ts/report/html/app/viewer-metric-registry.d.ts +0 -20
  125. package/target/main/ts/report/html/app/viewer-metric-registry.js +0 -77
  126. package/target/main/ts/report/html/app/viewer-metrics-view.d.ts +4 -0
  127. package/target/main/ts/report/html/app/viewer-metrics-view.js +162 -0
  128. package/target/main/ts/report/html/app/viewer-metrics.d.ts +6 -1
  129. package/target/main/ts/report/html/app/viewer-metrics.js +53 -35
  130. package/target/main/ts/report/html/app/viewer-overview-cards.d.ts +12 -0
  131. package/target/main/ts/report/html/app/viewer-overview-cards.js +112 -0
  132. package/target/main/ts/report/html/app/viewer-overview-hotspots.d.ts +9 -0
  133. package/target/main/ts/report/html/app/viewer-overview-hotspots.js +173 -0
  134. package/target/main/ts/report/html/app/viewer-overview-lead.d.ts +12 -0
  135. package/target/main/ts/report/html/app/viewer-overview-lead.js +116 -0
  136. package/target/main/ts/report/html/app/viewer-profile-badges.d.ts +11 -0
  137. package/target/main/ts/report/html/app/viewer-profile-badges.js +119 -0
  138. package/target/main/ts/report/html/app/viewer-profile-table.d.ts +29 -0
  139. package/target/main/ts/report/html/app/viewer-profile-table.js +124 -0
  140. package/target/main/ts/report/html/app/viewer-profile.d.ts +2 -6
  141. package/target/main/ts/report/html/app/viewer-profile.js +6 -248
  142. package/target/main/ts/report/html/app/viewer-pull-sections.d.ts +4 -0
  143. package/target/main/ts/report/html/app/viewer-pull-sections.js +118 -0
  144. package/target/main/ts/report/html/app/viewer-pull-trend.d.ts +5 -0
  145. package/target/main/ts/report/html/app/viewer-pull-trend.js +63 -0
  146. package/target/main/ts/report/html/app/viewer-pulls.d.ts +0 -1
  147. package/target/main/ts/report/html/app/viewer-pulls.js +28 -178
  148. package/target/main/ts/report/html/app/viewer-query-field.d.ts +19 -0
  149. package/target/main/ts/report/html/app/viewer-query-field.js +203 -0
  150. package/target/main/ts/report/html/app/viewer-route.d.ts +34 -0
  151. package/target/main/ts/report/html/app/viewer-route.js +55 -0
  152. package/target/main/ts/report/html/app/viewer-shell.d.ts +20 -0
  153. package/target/main/ts/report/html/app/viewer-shell.js +116 -0
  154. package/target/main/ts/report/html/app/viewer-source-view.d.ts +49 -0
  155. package/target/main/ts/report/html/app/viewer-source-view.js +150 -0
  156. package/target/main/ts/report/html/app/viewer-source.d.ts +0 -84
  157. package/target/main/ts/report/html/app/viewer-source.js +7 -363
  158. package/target/main/ts/report/html/app/viewer-suggest.d.ts +13 -0
  159. package/target/main/ts/report/html/app/viewer-suggest.js +91 -0
  160. package/target/main/ts/report/html/app/viewer-sunburst.d.ts +5 -0
  161. package/target/main/ts/report/html/app/viewer-sunburst.js +40 -0
  162. package/target/main/ts/report/html/app/viewer-table-cells.d.ts +25 -0
  163. package/target/main/ts/report/html/app/viewer-table-cells.js +129 -0
  164. package/target/main/ts/report/html/app/viewer-table-rows.d.ts +54 -0
  165. package/target/main/ts/report/html/app/viewer-table-rows.js +95 -0
  166. package/target/main/ts/report/html/app/viewer-tokenizer.d.ts +16 -0
  167. package/target/main/ts/report/html/app/viewer-tokenizer.js +192 -0
  168. package/target/main/ts/report/html/app/viewer-totals.d.ts +14 -0
  169. package/target/main/ts/report/html/app/viewer-totals.js +89 -0
  170. package/target/main/ts/report/html/app/viewer-treemap.d.ts +24 -0
  171. package/target/main/ts/report/html/app/viewer-treemap.js +112 -0
  172. package/target/main/ts/report/html/app/viewer-trend-data.d.ts +49 -0
  173. package/target/main/ts/report/html/app/viewer-trend-data.js +105 -0
  174. package/target/main/ts/report/html/app/viewer-trend-nav.d.ts +5 -0
  175. package/target/main/ts/report/html/app/viewer-trend-nav.js +85 -0
  176. package/target/main/ts/report/html/app/viewer-trend-points.d.ts +30 -0
  177. package/target/main/ts/report/html/app/viewer-trend-points.js +128 -0
  178. package/target/main/ts/report/html/app/viewer-trend-series.d.ts +26 -0
  179. package/target/main/ts/report/html/app/viewer-trend-series.js +98 -0
  180. package/target/main/ts/report/html/app/viewer-trends.d.ts +2 -70
  181. package/target/main/ts/report/html/app/viewer-trends.js +41 -392
  182. package/target/main/ts/report/html/app/viewer.js +152 -663
  183. package/target/main/ts/report/html/site/badges.js +41 -18
  184. package/target/main/ts/report/json/store/local-store.js +2 -12
  185. package/target/main/ts/report/json/store/operation-cache.d.ts +8 -0
  186. package/target/main/ts/report/json/store/operation-cache.js +32 -0
  187. package/target/main/ts/report/json/store/replace-file.d.ts +1 -0
  188. package/target/main/ts/report/json/store/replace-file.js +14 -0
  189. package/target/main/ts/report.d.ts +1 -0
  190. package/target/main/ts/report.js +1 -0
@@ -0,0 +1,49 @@
1
+ import type { ProfileBoundary } from '../../json/wire/inspection-context-model.js';
2
+ import type { Layout } from './viewer-chart-svg.js';
3
+ export interface ChartPoint {
4
+ readonly value: number | null;
5
+ /** Axis label used only when the points have no commit times. */
6
+ readonly label: string;
7
+ /** Accessible name of the point. */
8
+ readonly title: string;
9
+ readonly href: string;
10
+ readonly current: boolean;
11
+ readonly partial: boolean;
12
+ readonly boundary?: ProfileBoundary | null;
13
+ /** Commit time in milliseconds; the x axis is a time scale when every point has one. */
14
+ readonly time: number | null;
15
+ /** Hover card lines: a heading, then details. */
16
+ readonly tip: ChartTip;
17
+ /** A green flag on the best value of the series, a red one at half-mast on the worst. */
18
+ readonly flag?: 'best' | 'worst' | undefined;
19
+ }
20
+ export interface ChartTip {
21
+ readonly heading: string;
22
+ readonly commit: string;
23
+ /** The commit's message, first line: what the chart's step was. */
24
+ readonly subject?: string;
25
+ readonly value: string;
26
+ readonly change?: {
27
+ readonly text: string;
28
+ readonly direction: string;
29
+ } | null;
30
+ readonly notes: readonly string[];
31
+ }
32
+ export type Flag = NonNullable<ChartPoint['flag']>;
33
+ /** The values the points measured, in order, without the gaps. */
34
+ export declare function measuredValues(values: readonly (number | null)[]): number[];
35
+ /** Runs of consecutive measured points, broken only where a measurement is missing; a profile change is marked, not cut. */
36
+ export declare function segments(values: readonly (number | null)[]): number[][];
37
+ /**
38
+ * Code keeps its state until the next commit, so the line holds each value and steps at the commit that changed it;
39
+ * a slope would invent values for the days between commits. A run of equal values is one horizontal stroke, so ten
40
+ * thousand quiet commits cost a single segment.
41
+ */
42
+ export declare function stepPath(segment: readonly number[], valueAt: (index: number) => number, x: (index: number) => number, y: (value: number) => number): string;
43
+ /** A dashed line where the profile changed between two inspections. */
44
+ export declare function markBoundaries(svg: SVGSVGElement, layout: Layout, points: readonly ChartPoint[]): void;
45
+ /** A tiny flag planted in the point: the pennant of the logo on a short pole, both in the flag's colour. */
46
+ export declare function flagMark(kind: Flag, x: number, y: number): SVGGElement;
47
+ export declare function flagIcon(kind: Flag): SVGSVGElement;
48
+ /** A point's link to its commit, which holds its marks. */
49
+ export declare function pointLink(point: ChartPoint, index: number): SVGAElement;
@@ -0,0 +1,74 @@
1
+ import { line, setAttributes, svgNode } from './viewer-chart-svg.js';
2
+ /** The values the points measured, in order, without the gaps. */
3
+ export function measuredValues(values) {
4
+ return values.flatMap(value => value === null ? [] : [value]);
5
+ }
6
+ /** Runs of consecutive measured points, broken only where a measurement is missing; a profile change is marked, not cut. */
7
+ export function segments(values) {
8
+ const result = [];
9
+ let run = [];
10
+ for (const [index, value] of values.entries()) {
11
+ if (value !== null) {
12
+ run.push(index);
13
+ continue;
14
+ }
15
+ if (run.length)
16
+ result.push(run);
17
+ run = [];
18
+ }
19
+ if (run.length)
20
+ result.push(run);
21
+ return result.filter(segment => segment.length > 1);
22
+ }
23
+ /**
24
+ * Code keeps its state until the next commit, so the line holds each value and steps at the commit that changed it;
25
+ * a slope would invent values for the days between commits. A run of equal values is one horizontal stroke, so ten
26
+ * thousand quiet commits cost a single segment.
27
+ */
28
+ export function stepPath(segment, valueAt, x, y) {
29
+ return segment.map((index, order) => {
30
+ const value = valueAt(index);
31
+ if (!order)
32
+ return `M${x(index).toFixed(1)},${y(value).toFixed(1)}`;
33
+ const last = order === segment.length - 1;
34
+ return value === valueAt(segment[order - 1]) && !last ? '' : `H${x(index).toFixed(1)}V${y(value).toFixed(1)}`;
35
+ }).join('');
36
+ }
37
+ /** A dashed line where the profile changed between two inspections. */
38
+ export function markBoundaries(svg, layout, points) {
39
+ for (const [index, point] of points.entries()) {
40
+ if (index === 0 || !point.boundary)
41
+ continue;
42
+ const x = layout.xs[index];
43
+ svg.append(line(`tc-profile-${point.boundary}`, { x1: x, x2: x, y1: layout.top, y2: layout.top + layout.plotHeight }));
44
+ }
45
+ }
46
+ /** A tiny flag planted in the point: the pennant of the logo on a short pole, both in the flag's colour. */
47
+ export function flagMark(kind, x, y) {
48
+ const group = svgNode('g', `tc-flag tc-flag-${kind}`);
49
+ const foot = y - 4;
50
+ const head = foot - 12;
51
+ const pole = svgNode('rect');
52
+ setAttributes(pole, { x: x - .75, y: head, width: 1.5, height: foot - head });
53
+ const pennant = svgNode('path');
54
+ pennant.setAttribute('d', `M${(x + .75).toFixed(1)},${head.toFixed(1)}L${(x + 10).toFixed(1)},${(head - .4).toFixed(1)}L${(x + .75).toFixed(1)},${(head + 5.5).toFixed(1)}Z`);
55
+ group.append(pole, pennant);
56
+ return group;
57
+ }
58
+ export function flagIcon(kind) {
59
+ const icon = svgNode('svg', `tc-flag tc-flag-${kind}`);
60
+ icon.setAttribute('viewBox', '0 -1 11 14');
61
+ icon.setAttribute('aria-hidden', 'true');
62
+ icon.append(flagMark(kind, 1, 16));
63
+ return icon;
64
+ }
65
+ /** A point's link to its commit, which holds its marks. */
66
+ export function pointLink(point, index) {
67
+ const link = svgNode('a', 'tc-point');
68
+ link.setAttribute('href', point.href);
69
+ link.setAttribute('aria-label', point.title);
70
+ link.dataset.index = String(index);
71
+ if (point.current)
72
+ link.setAttribute('aria-current', 'true');
73
+ return link;
74
+ }
@@ -0,0 +1,21 @@
1
+ import type { Layout } from './viewer-chart-svg.js';
2
+ export interface YScale {
3
+ readonly values: readonly number[];
4
+ /** Values are ratios within [0, 1]. */
5
+ readonly ratio: boolean;
6
+ /** About how many grid lines the scale has. */
7
+ readonly count: number;
8
+ readonly format: (value: number) => string;
9
+ /** Ticks fall on whole multiples of this unit. */
10
+ readonly tickUnit: number;
11
+ /** The scale holds zero and marks it: the base of diverging bars. */
12
+ readonly zero: boolean;
13
+ /** Labels for a second scale at the right edge, one per grid line. */
14
+ readonly rightLabels?: ((ticks: number) => readonly string[]) | undefined;
15
+ }
16
+ /** Draws the value axis and its grid, sets the plot's left edge and width, and returns the value-to-y mapping. */
17
+ export declare function yAxis(svg: SVGSVGElement, layout: Layout, scale: YScale): (value: number) => number;
18
+ /** A round step near the given one: 1, 2, 2.5 or 5 times a power of ten. */
19
+ export declare function niceStep(raw: number): number;
20
+ /** Evenly spaced round ticks that cover the data with a margin. */
21
+ export declare function niceTicks(values: readonly number[], ratio: boolean, count: number): number[];
@@ -0,0 +1,93 @@
1
+ import { line, plotRight, text } from './viewer-chart-svg.js';
2
+ let measure;
3
+ /** Rendered width of a 13 px tick label; an estimate where the page cannot measure text. */
4
+ function tickWidth(label) {
5
+ if (measure === undefined) {
6
+ measure = typeof document === 'undefined' ? null : document.createElement('canvas').getContext('2d');
7
+ if (measure)
8
+ measure.font = `13px ${getComputedStyle(document.documentElement).fontFamily}`;
9
+ }
10
+ return measure ? measure.measureText(label).width : label.length * 8.6;
11
+ }
12
+ function widest(labels) {
13
+ return Math.ceil(Math.max(...labels.map(tickWidth)));
14
+ }
15
+ /** The ticks' words: percentages with as many decimals as the step needs, zero plainly on a diverging scale. */
16
+ function tickTexts(ticks, { ratio, zero, format }) {
17
+ const tickStep = ticks.length > 1 ? ticks[1] - ticks[0] : 1;
18
+ const digits = Math.min(3, Math.max(0, -Math.floor(Math.log10(tickStep * 100) + 1e-9)));
19
+ const tickPercent = new Intl.NumberFormat(undefined, { style: 'percent', minimumFractionDigits: digits, maximumFractionDigits: digits });
20
+ const word = ratio ? (tick) => tickPercent.format(tick) : format;
21
+ return ticks.map(tick => zero && tick === 0 ? '0' : word(tick));
22
+ }
23
+ /** Draws the value axis and its grid, sets the plot's left edge and width, and returns the value-to-y mapping. */
24
+ export function yAxis(svg, layout, scale) {
25
+ const { values, ratio, count, tickUnit, zero } = scale;
26
+ const ticks = niceTicks((zero ? [...values, 0] : values).map(value => value / tickUnit), ratio, count).map(tick => tick * tickUnit);
27
+ const labels = tickTexts(ticks, scale);
28
+ layout.left = widest(labels) + 12;
29
+ // A second scale takes a label column at the right, one label per grid line.
30
+ const secondLabels = scale.rightLabels?.(ticks.length) ?? [];
31
+ const reserve = secondLabels.length ? widest(secondLabels) + 12 - plotRight : 0;
32
+ layout.plotWidth = Math.max(40, layout.width - layout.left - plotRight - reserve);
33
+ const low = ticks[0];
34
+ const high = ticks.at(-1);
35
+ const y = (value) => layout.top + layout.plotHeight - (high === low ? .5 : (value - low) / (high - low)) * layout.plotHeight;
36
+ const across = (at) => ({ x1: layout.left, x2: layout.left + layout.plotWidth, y1: at, y2: at });
37
+ for (const [index, tick] of ticks.entries()) {
38
+ const base = zero ? tick === 0 : index === 0;
39
+ const label = text(labels[index], 'tc-tick', layout.left - 8, y(tick) + 4.5);
40
+ label.setAttribute('text-anchor', 'end');
41
+ svg.append(line(base ? 'tc-base' : 'tc-grid', across(y(tick))), label);
42
+ const second = secondLabels[index];
43
+ if (second !== undefined)
44
+ svg.append(text(second, 'tc-tick tc-tick-secondary', layout.left + layout.plotWidth + 8, y(tick) + 4.5));
45
+ }
46
+ if (zero && low !== 0)
47
+ svg.append(line('tc-grid', across(y(low))));
48
+ return y;
49
+ }
50
+ function paddedRange(values, ratio) {
51
+ let low = Math.min(...values);
52
+ let high = Math.max(...values);
53
+ // A minimum visible range keeps noise flat: 2 percentage points for ratios, 10% of the value for counts.
54
+ const minimum = ratio ? .02 : Math.max(Math.abs(high) * .1, 2);
55
+ if (high - low < minimum) {
56
+ const middle = (high + low) / 2;
57
+ low = middle - minimum / 2;
58
+ high = middle + minimum / 2;
59
+ }
60
+ const span = high - low;
61
+ low -= span * .12;
62
+ high += span * .12;
63
+ if (Math.min(...values) >= 0)
64
+ low = Math.max(0, low);
65
+ if (ratio && Math.max(...values) <= 1)
66
+ high = Math.min(1, high);
67
+ return [low, high];
68
+ }
69
+ /** A round step near the given one: 1, 2, 2.5 or 5 times a power of ten. */
70
+ export function niceStep(raw) {
71
+ if (!(raw > 0))
72
+ return 1;
73
+ const power = 10 ** Math.floor(Math.log10(raw));
74
+ const fraction = raw / power;
75
+ return ([1, 2, 2.5, 5].find(factor => fraction <= factor) ?? 10) * power;
76
+ }
77
+ /** Evenly spaced round ticks that cover the data with a margin. */
78
+ export function niceTicks(values, ratio, count) {
79
+ if (values.length === 0)
80
+ return ratio ? [0, .5, 1] : [0, 1];
81
+ const [low, high] = paddedRange(values, ratio);
82
+ const raw = (high - low) / count;
83
+ const magnitude = 10 ** Math.floor(Math.log10(raw));
84
+ const step = [1, 2, 2.5, 5, 10].map(factor => factor * magnitude).find(candidate => candidate >= raw) ?? raw;
85
+ const integral = !ratio && values.every(Number.isInteger);
86
+ const unit = integral ? Math.max(1, step) : step;
87
+ const first = Math.floor(low / unit) * unit;
88
+ const last = Math.ceil(high / unit) * unit;
89
+ const ticks = [];
90
+ for (let tick = first; tick <= last + unit / 2; tick += unit)
91
+ ticks.push(Number(tick.toPrecision(12)));
92
+ return ticks;
93
+ }
@@ -0,0 +1,28 @@
1
+ /** The chart's SVG building blocks and the plot's layout, shared by its axes, marks and hover card. */
2
+ export interface Layout {
3
+ readonly width: number;
4
+ readonly height: number;
5
+ readonly top: number;
6
+ readonly plotHeight: number;
7
+ left: number;
8
+ plotWidth: number;
9
+ /** Horizontal position of each point. */
10
+ xs: number[];
11
+ /** Y of each point's mark, for the hover card. */
12
+ ys: (number | null)[];
13
+ }
14
+ export declare const plotTop = 34;
15
+ export declare const plotBottom = 30;
16
+ export declare const plotRight = 18;
17
+ /** Keeps the first and last marks off the plot edges. */
18
+ export declare const inset = 12;
19
+ export declare function svgNode<K extends keyof SVGElementTagNameMap>(tag: K, className?: string): SVGElementTagNameMap[K];
20
+ /** Sets numeric attributes, rounded to a tenth of a pixel. */
21
+ export declare function setAttributes(node: SVGElement, attributes: Record<string, number>): void;
22
+ export declare function text(content: string, className: string, x: number, y: number): SVGTextElement;
23
+ /** A label centred on its anchor. */
24
+ export declare function centredText(content: string, className: string, x: number, y: number): SVGTextElement;
25
+ /** Where a centred label stands so that it stays inside the plot. */
26
+ export declare function clampLabel(x: number, label: string, { left, plotWidth }: Pick<Layout, 'left' | 'plotWidth'>): number;
27
+ /** A line across the plot, at a height or at a point. */
28
+ export declare function line(className: string, attributes: Record<string, number>): SVGLineElement;
@@ -0,0 +1,41 @@
1
+ /** The chart's SVG building blocks and the plot's layout, shared by its axes, marks and hover card. */
2
+ export const plotTop = 34;
3
+ export const plotBottom = 30;
4
+ export const plotRight = 18;
5
+ /** Keeps the first and last marks off the plot edges. */
6
+ export const inset = 12;
7
+ const svgNamespace = 'http://www.w3.org/2000/svg';
8
+ export function svgNode(tag, className) {
9
+ const node = document.createElementNS(svgNamespace, tag);
10
+ if (className)
11
+ node.setAttribute('class', className);
12
+ return node;
13
+ }
14
+ /** Sets numeric attributes, rounded to a tenth of a pixel. */
15
+ export function setAttributes(node, attributes) {
16
+ for (const [name, value] of Object.entries(attributes))
17
+ node.setAttribute(name, String(Number(value.toFixed(1))));
18
+ }
19
+ export function text(content, className, x, y) {
20
+ const node = svgNode('text', className);
21
+ node.textContent = content;
22
+ setAttributes(node, { x, y });
23
+ return node;
24
+ }
25
+ /** A label centred on its anchor. */
26
+ export function centredText(content, className, x, y) {
27
+ const node = text(content, className, x, y);
28
+ node.setAttribute('text-anchor', 'middle');
29
+ return node;
30
+ }
31
+ /** Where a centred label stands so that it stays inside the plot. */
32
+ export function clampLabel(x, label, { left, plotWidth }) {
33
+ const half = label.length * 3.6 + 2;
34
+ return Math.min(Math.max(x, left + half), left + plotWidth - half);
35
+ }
36
+ /** A line across the plot, at a height or at a point. */
37
+ export function line(className, attributes) {
38
+ const node = svgNode('line', className);
39
+ setAttributes(node, attributes);
40
+ return node;
41
+ }
@@ -0,0 +1,26 @@
1
+ import type { Layout } from './viewer-chart-svg.js';
2
+ export type TickUnit = 'minute' | 'hour' | 'day' | 'month' | 'year';
3
+ export interface TimeTick {
4
+ readonly time: number;
5
+ readonly label: string;
6
+ readonly unit: TickUnit;
7
+ }
8
+ /**
9
+ * Labels for ticks in time order. Each names its own unit, and the coarser units join it only where they change:
10
+ * Jul 2023, Nov, Dec, Jan 2024, Feb; 30 Jun, 1 Jul, 2; 23:00, 1 Jul, 01:00. The first tick always carries them.
11
+ */
12
+ export declare function tickLabels(times: readonly number[], unit: TickUnit): string[];
13
+ /**
14
+ * The labels under the ticks. Which ticks keep a label is settled first: one that would sit far off its tick at the
15
+ * edge, or touch the label before it, keeps only its mark. Only then are the labels written, so the first label shown
16
+ * carries the coarser units, and every later one carries them only where they change.
17
+ */
18
+ export declare function axisLabels(ticks: readonly {
19
+ time: number;
20
+ x: number;
21
+ }[], unit: TickUnit, layout: Pick<Layout, 'left' | 'plotWidth'>): {
22
+ at: number;
23
+ label: string;
24
+ }[];
25
+ /** Calendar-aligned ticks between two times, as many as fit the width without overlapping labels. */
26
+ export declare function timeTicks(start: number, end: number, width: number): TimeTick[];
@@ -0,0 +1,150 @@
1
+ import { clampLabel } from './viewer-chart-svg.js';
2
+ const monthLabel = new Intl.DateTimeFormat(undefined, { month: 'short' });
3
+ const monthYearLabel = new Intl.DateTimeFormat(undefined, { month: 'short', year: 'numeric' });
4
+ const dayLabel = new Intl.DateTimeFormat(undefined, { day: 'numeric', month: 'short' });
5
+ const dayYearLabel = new Intl.DateTimeFormat(undefined, { day: 'numeric', month: 'short', year: 'numeric' });
6
+ const hourLabel = new Intl.DateTimeFormat(undefined, { hour: '2-digit', minute: '2-digit' });
7
+ function changes(previous, date) {
8
+ const year = previous === null || previous.getFullYear() !== date.getFullYear();
9
+ const month = year || previous.getMonth() !== date.getMonth();
10
+ return { year, month, day: month || previous.getDate() !== date.getDate() };
11
+ }
12
+ /** A tick within a day names its time, and the date where the date changes; midnight is the date alone. */
13
+ function clockLabel(date, changed) {
14
+ if (!changed.day)
15
+ return hourLabel.format(date);
16
+ const days = (changed.year ? dayYearLabel : dayLabel).format(date);
17
+ return date.getHours() === 0 && date.getMinutes() === 0 ? days : `${days}, ${hourLabel.format(date)}`;
18
+ }
19
+ function dateLabel(date, changed) {
20
+ if (changed.year)
21
+ return dayYearLabel.format(date);
22
+ return changed.month ? dayLabel.format(date) : String(date.getDate());
23
+ }
24
+ const unitLabels = {
25
+ year: date => String(date.getFullYear()),
26
+ month: (date, changed) => (changed.year ? monthYearLabel : monthLabel).format(date),
27
+ day: dateLabel,
28
+ hour: clockLabel,
29
+ minute: clockLabel,
30
+ };
31
+ /**
32
+ * Labels for ticks in time order. Each names its own unit, and the coarser units join it only where they change:
33
+ * Jul 2023, Nov, Dec, Jan 2024, Feb; 30 Jun, 1 Jul, 2; 23:00, 1 Jul, 01:00. The first tick always carries them.
34
+ */
35
+ export function tickLabels(times, unit) {
36
+ let previous = null;
37
+ return times.map(time => {
38
+ const date = new Date(time);
39
+ const changed = changes(previous, date);
40
+ previous = date;
41
+ return unitLabels[unit](date, changed);
42
+ });
43
+ }
44
+ /**
45
+ * The labels under the ticks. Which ticks keep a label is settled first: one that would sit far off its tick at the
46
+ * edge, or touch the label before it, keeps only its mark. Only then are the labels written, so the first label shown
47
+ * carries the coarser units, and every later one carries them only where they change.
48
+ */
49
+ export function axisLabels(ticks, unit, layout) {
50
+ let shown = [...ticks];
51
+ for (;;) {
52
+ const labels = tickLabels(shown.map(tick => tick.time), unit);
53
+ let previous = -Infinity;
54
+ const placed = [];
55
+ const drop = shown.findIndex((tick, index) => {
56
+ const label = labels[index];
57
+ const at = clampLabel(tick.x, label, layout);
58
+ const half = label.length * 3.7;
59
+ if (Math.abs(at - tick.x) > 12 || at - half < previous + 10)
60
+ return true;
61
+ previous = at + half;
62
+ placed.push({ at, label });
63
+ return false;
64
+ });
65
+ if (drop < 0)
66
+ return placed;
67
+ shown = shown.filter((_, index) => index !== drop);
68
+ }
69
+ }
70
+ const minute = 60e3;
71
+ const hour = 60 * minute;
72
+ const day = 24 * hour;
73
+ const steps = [
74
+ { unit: 'minute', count: 5, size: 5 * minute }, { unit: 'minute', count: 10, size: 10 * minute },
75
+ { unit: 'minute', count: 15, size: 15 * minute }, { unit: 'minute', count: 30, size: 30 * minute },
76
+ { unit: 'hour', count: 1, size: hour }, { unit: 'hour', count: 3, size: 3 * hour }, { unit: 'hour', count: 6, size: 6 * hour },
77
+ { unit: 'hour', count: 12, size: 12 * hour }, { unit: 'day', count: 1, size: day }, { unit: 'day', count: 2, size: 2 * day },
78
+ { unit: 'day', count: 7, size: 7 * day }, { unit: 'day', count: 14, size: 14 * day }, { unit: 'month', count: 1, size: 30.4 * day },
79
+ { unit: 'month', count: 3, size: 91 * day }, { unit: 'month', count: 6, size: 182 * day }, { unit: 'year', count: 1, size: 365 * day },
80
+ { unit: 'year', count: 2, size: 730 * day }, { unit: 'year', count: 5, size: 1826 * day },
81
+ ];
82
+ /** Calendar-aligned ticks between two times, as many as fit the width without overlapping labels. */
83
+ export function timeTicks(start, end, width) {
84
+ for (const step of steps) {
85
+ const periods = (end - start) / step.size;
86
+ if (periods > Math.max(2, width / 64))
87
+ continue;
88
+ const ticks = calendarTicks(start, end, step.unit, step.count);
89
+ // Sized by the typical label: the few that carry a year or a date beside them are placed with care by the axis.
90
+ const lengths = ticks.map(tick => tick.label.length).sort((x, y) => x - y);
91
+ const widest = (lengths[Math.floor(lengths.length / 2)] ?? 0) * 7.4 + 14;
92
+ if (ticks.length < 2 || width / Math.max(1, periods) >= widest)
93
+ return ticks;
94
+ }
95
+ return calendarTicks(start, end, 'year', 10);
96
+ }
97
+ /** How coarse each unit is: a tick of a unit starts at zero in every finer one. */
98
+ const coarseness = { minute: 0, hour: 1, day: 2, month: 3, year: 4 };
99
+ function startOf(time, unit) {
100
+ const cursor = new Date(time);
101
+ cursor.setSeconds(0, 0);
102
+ const level = coarseness[unit];
103
+ if (level >= 1)
104
+ cursor.setMinutes(0);
105
+ if (level >= 2)
106
+ cursor.setHours(0);
107
+ if (level >= 3)
108
+ cursor.setDate(1);
109
+ if (level >= 4)
110
+ cursor.setMonth(0);
111
+ return cursor;
112
+ }
113
+ const advances = {
114
+ minute: cursor => cursor.setMinutes(cursor.getMinutes() + 1),
115
+ hour: cursor => cursor.setHours(cursor.getHours() + 1),
116
+ day: cursor => cursor.setDate(cursor.getDate() + 1),
117
+ month: cursor => cursor.setMonth(cursor.getMonth() + 1),
118
+ year: cursor => cursor.setFullYear(cursor.getFullYear() + 1),
119
+ };
120
+ function weekOf(date) {
121
+ return Math.floor(date.getTime() / (7 * day));
122
+ }
123
+ /** Weeks start on Mondays, every other one for two-week steps; days step from the first of the month, never to the 31st. */
124
+ function alignedDay(cursor, count) {
125
+ if (count !== 7 && count !== 14)
126
+ return (cursor.getDate() - 1) % count === 0 && cursor.getDate() < 31;
127
+ return cursor.getDay() === 1 && (count === 7 || weekOf(cursor) % 2 === 0);
128
+ }
129
+ const alignments = {
130
+ minute: (cursor, count) => cursor.getMinutes() % count === 0,
131
+ hour: (cursor, count) => cursor.getHours() % count === 0,
132
+ day: alignedDay,
133
+ month: (cursor, count) => cursor.getMonth() % count === 0,
134
+ year: (cursor, count) => cursor.getFullYear() % count === 0,
135
+ };
136
+ function calendarTicks(start, end, unit, count) {
137
+ const cursor = startOf(start, unit);
138
+ const advance = advances[unit];
139
+ const times = [];
140
+ let guard = 0;
141
+ while (cursor.getTime() < start && guard++ < 6000)
142
+ advance(cursor);
143
+ while (cursor.getTime() <= end && guard++ < 6000) {
144
+ if (alignments[unit](cursor, count))
145
+ times.push(cursor.getTime());
146
+ advance(cursor);
147
+ }
148
+ const labels = tickLabels(times, unit);
149
+ return times.map((time, index) => ({ time, label: labels[index], unit }));
150
+ }
@@ -0,0 +1,4 @@
1
+ import type { ChartPoint } from './viewer-chart-points.js';
2
+ import type { Layout } from './viewer-chart-svg.js';
3
+ /** One hover card for the whole chart: the point nearest the pointer, or the focused one. */
4
+ export declare function interact(svg: SVGSVGElement, tip: HTMLDivElement, layout: Layout, points: readonly ChartPoint[]): void;