repometry 0.0.0-stage → 0.1.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 (280) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +27 -2
  3. package/package.json +63 -4
  4. package/target/main/resources/report/json/compact-json-v1.schema.json +24 -0
  5. package/target/main/resources/report/json/comparison-v1.schema.json +65 -0
  6. package/target/main/resources/report/json/config-v1.schema.json +34 -0
  7. package/target/main/resources/report/json/diff-coverage-v1.schema.json +55 -0
  8. package/target/main/resources/report/json/file-metrics-v1.schema.json +134 -0
  9. package/target/main/resources/report/json/inspection-context-v1.schema.json +172 -0
  10. package/target/main/resources/report/json/metabranch-v1.schema.json +9 -0
  11. package/target/main/resources/report/json/metric-catalog-v1.schema.json +31 -0
  12. package/target/main/resources/report/json/public-history-v1.schema.json +50 -0
  13. package/target/main/resources/report/json/pull-review-v1.schema.json +97 -0
  14. package/target/main/resources/report/json/quality-history-v1.schema.json +51 -0
  15. package/target/main/resources/report/json/report-v1.schema.json +65 -0
  16. package/target/main/resources/report/json/report-v2.schema.json +238 -0
  17. package/target/main/resources/report/json/repository-summary-v1.schema.json +181 -0
  18. package/target/main/resources/report/json/viewer-details-v1.schema.json +343 -0
  19. package/target/main/resources/report/json/viewer-inspection-v1.schema.json +13 -0
  20. package/target/main/resources/viewer/files.css +578 -0
  21. package/target/main/resources/viewer/index.html +130 -0
  22. package/target/main/resources/viewer/projects.html +52 -0
  23. package/target/main/resources/viewer/styles.css +586 -0
  24. package/target/main/resources/viewer/viewer-pulls.css +64 -0
  25. package/target/main/ts/api/gh/github-pr-comment.d.ts +4 -0
  26. package/target/main/ts/api/gh/github-pr-comment.js +107 -0
  27. package/target/main/ts/api/gh/github.d.ts +6 -0
  28. package/target/main/ts/api/gh/github.js +91 -0
  29. package/target/main/ts/api/git/blob-batch.d.ts +10 -0
  30. package/target/main/ts/api/git/blob-batch.js +105 -0
  31. package/target/main/ts/api/git/changes.d.ts +22 -0
  32. package/target/main/ts/api/git/changes.js +107 -0
  33. package/target/main/ts/api/git/commit-metadata.d.ts +3 -0
  34. package/target/main/ts/api/git/commit-metadata.js +54 -0
  35. package/target/main/ts/api/git/git-churn.d.ts +9 -0
  36. package/target/main/ts/api/git/git-churn.js +70 -0
  37. package/target/main/ts/api/git/git-reader.d.ts +16 -0
  38. package/target/main/ts/api/git/git-reader.js +73 -0
  39. package/target/main/ts/api/git/git.d.ts +21 -0
  40. package/target/main/ts/api/git/git.js +152 -0
  41. package/target/main/ts/api/git/inspection-source.d.ts +15 -0
  42. package/target/main/ts/api/git/inspection-source.js +30 -0
  43. package/target/main/ts/api/git/local-sources.d.ts +5 -0
  44. package/target/main/ts/api/git/local-sources.js +46 -0
  45. package/target/main/ts/api/git/local.d.ts +3 -0
  46. package/target/main/ts/api/git/local.js +35 -0
  47. package/target/main/ts/api/git/metabranch-worktree.d.ts +1 -0
  48. package/target/main/ts/api/git/metabranch-worktree.js +34 -0
  49. package/target/main/ts/api/git/source-cache.d.ts +3 -0
  50. package/target/main/ts/api/git/source-cache.js +56 -0
  51. package/target/main/ts/api/import/finding-import.d.ts +9 -0
  52. package/target/main/ts/api/import/finding-import.js +226 -0
  53. package/target/main/ts/api/import/lcov-files.d.ts +5 -0
  54. package/target/main/ts/api/import/lcov-files.js +13 -0
  55. package/target/main/ts/api/import/profile-file.d.ts +4 -0
  56. package/target/main/ts/api/import/profile-file.js +16 -0
  57. package/target/main/ts/api/worker/remote-executor.d.ts +2 -0
  58. package/target/main/ts/api/worker/remote-executor.js +45 -0
  59. package/target/main/ts/api/worker/worker-protocol.d.ts +7 -0
  60. package/target/main/ts/api/worker/worker-protocol.js +101 -0
  61. package/target/main/ts/api/worker/worker.d.ts +5 -0
  62. package/target/main/ts/api/worker/worker.js +85 -0
  63. package/target/main/ts/cli/arguments.d.ts +28 -0
  64. package/target/main/ts/cli/arguments.js +110 -0
  65. package/target/main/ts/cli/config.d.ts +14 -0
  66. package/target/main/ts/cli/config.js +60 -0
  67. package/target/main/ts/cli/github-pr-comment.d.ts +1 -0
  68. package/target/main/ts/cli/github-pr-comment.js +12 -0
  69. package/target/main/ts/cli/imports.d.ts +7 -0
  70. package/target/main/ts/cli/imports.js +29 -0
  71. package/target/main/ts/cli/main.d.ts +2 -0
  72. package/target/main/ts/cli/main.js +378 -0
  73. package/target/main/ts/cli/reporting.d.ts +18 -0
  74. package/target/main/ts/cli/reporting.js +108 -0
  75. package/target/main/ts/cli/revisions.d.ts +6 -0
  76. package/target/main/ts/cli/revisions.js +15 -0
  77. package/target/main/ts/cli/store.d.ts +13 -0
  78. package/target/main/ts/cli/store.js +38 -0
  79. package/target/main/ts/compose/analyze-snapshot.d.ts +4 -0
  80. package/target/main/ts/compose/analyze-snapshot.js +163 -0
  81. package/target/main/ts/compose/attach-churn.d.ts +2 -0
  82. package/target/main/ts/compose/attach-churn.js +37 -0
  83. package/target/main/ts/compose/diff-coverage.d.ts +20 -0
  84. package/target/main/ts/compose/diff-coverage.js +56 -0
  85. package/target/main/ts/compose/inspect.d.ts +23 -0
  86. package/target/main/ts/compose/inspect.js +44 -0
  87. package/target/main/ts/compose/plugin-list.d.ts +6 -0
  88. package/target/main/ts/compose/plugin-list.js +9 -0
  89. package/target/main/ts/compose/prepare-metabranch.d.ts +9 -0
  90. package/target/main/ts/compose/prepare-metabranch.js +16 -0
  91. package/target/main/ts/compose/review.d.ts +16 -0
  92. package/target/main/ts/compose/review.js +38 -0
  93. package/target/main/ts/compose/sources.d.ts +3 -0
  94. package/target/main/ts/compose/sources.js +29 -0
  95. package/target/main/ts/core/engine/execute.d.ts +10 -0
  96. package/target/main/ts/core/engine/execute.js +49 -0
  97. package/target/main/ts/core/engine/manifest.d.ts +4 -0
  98. package/target/main/ts/core/engine/manifest.js +104 -0
  99. package/target/main/ts/core/engine/plan.d.ts +22 -0
  100. package/target/main/ts/core/engine/plan.js +106 -0
  101. package/target/main/ts/core/engine/plugin.d.ts +59 -0
  102. package/target/main/ts/core/engine/plugin.js +4 -0
  103. package/target/main/ts/core/engine/report-products.d.ts +16 -0
  104. package/target/main/ts/core/engine/report-products.js +85 -0
  105. package/target/main/ts/core/model/changes.d.ts +8 -0
  106. package/target/main/ts/core/model/changes.js +1 -0
  107. package/target/main/ts/core/model/churn.d.ts +11 -0
  108. package/target/main/ts/core/model/churn.js +5 -0
  109. package/target/main/ts/core/model/clone-source.d.ts +16 -0
  110. package/target/main/ts/core/model/clone-source.js +14 -0
  111. package/target/main/ts/core/model/code-structure.d.ts +30 -0
  112. package/target/main/ts/core/model/code-structure.js +31 -0
  113. package/target/main/ts/core/model/comparison.d.ts +24 -0
  114. package/target/main/ts/core/model/comparison.js +1 -0
  115. package/target/main/ts/core/model/diff-coverage.d.ts +23 -0
  116. package/target/main/ts/core/model/diff-coverage.js +1 -0
  117. package/target/main/ts/core/model/finding-ignore.d.ts +8 -0
  118. package/target/main/ts/core/model/finding-ignore.js +35 -0
  119. package/target/main/ts/core/model/finding.d.ts +28 -0
  120. package/target/main/ts/core/model/finding.js +29 -0
  121. package/target/main/ts/core/model/ignore-rule.d.ts +11 -0
  122. package/target/main/ts/core/model/ignore-rule.js +1 -0
  123. package/target/main/ts/core/model/measurement.d.ts +27 -0
  124. package/target/main/ts/core/model/measurement.js +24 -0
  125. package/target/main/ts/core/model/metric-descriptor.d.ts +17 -0
  126. package/target/main/ts/core/model/metric-descriptor.js +1 -0
  127. package/target/main/ts/core/model/plugin-data.d.ts +25 -0
  128. package/target/main/ts/core/model/plugin-data.js +1 -0
  129. package/target/main/ts/core/model/pull-review.d.ts +48 -0
  130. package/target/main/ts/core/model/pull-review.js +1 -0
  131. package/target/main/ts/core/model/quality-summary.d.ts +42 -0
  132. package/target/main/ts/core/model/quality-summary.js +64 -0
  133. package/target/main/ts/core/model/report.d.ts +32 -0
  134. package/target/main/ts/core/model/report.js +5 -0
  135. package/target/main/ts/core/model/scanner-import.d.ts +39 -0
  136. package/target/main/ts/core/model/scanner-import.js +28 -0
  137. package/target/main/ts/core/model/snapshot.d.ts +15 -0
  138. package/target/main/ts/core/model/snapshot.js +50 -0
  139. package/target/main/ts/core/model/source-locator.d.ts +7 -0
  140. package/target/main/ts/core/model/source-locator.js +1 -0
  141. package/target/main/ts/core/model/source.d.ts +23 -0
  142. package/target/main/ts/core/model/source.js +1 -0
  143. package/target/main/ts/core/ports/report.d.ts +9 -0
  144. package/target/main/ts/core/ports/report.js +1 -0
  145. package/target/main/ts/core/ports/source-provider.d.ts +14 -0
  146. package/target/main/ts/core/ports/source-provider.js +4 -0
  147. package/target/main/ts/core/profile/exclusions.d.ts +11 -0
  148. package/target/main/ts/core/profile/exclusions.js +57 -0
  149. package/target/main/ts/core/profile/maintainability.d.ts +31 -0
  150. package/target/main/ts/core/profile/maintainability.js +9 -0
  151. package/target/main/ts/core/profile/profile.d.ts +4 -0
  152. package/target/main/ts/core/profile/profile.js +10 -0
  153. package/target/main/ts/core/usecases/compare.d.ts +6 -0
  154. package/target/main/ts/core/usecases/compare.js +145 -0
  155. package/target/main/ts/core/usecases/diff-coverage-policy.d.ts +6 -0
  156. package/target/main/ts/core/usecases/diff-coverage-policy.js +10 -0
  157. package/target/main/ts/core/usecases/diff-coverage.d.ts +40 -0
  158. package/target/main/ts/core/usecases/diff-coverage.js +15 -0
  159. package/target/main/ts/core/usecases/inspect.d.ts +38 -0
  160. package/target/main/ts/core/usecases/inspect.js +41 -0
  161. package/target/main/ts/core/usecases/review.d.ts +31 -0
  162. package/target/main/ts/core/usecases/review.js +128 -0
  163. package/target/main/ts/index.d.ts +35 -0
  164. package/target/main/ts/index.js +17 -0
  165. package/target/main/ts/plugin.d.ts +14 -0
  166. package/target/main/ts/plugin.js +5 -0
  167. package/target/main/ts/plugins/coverage/coverage.d.ts +23 -0
  168. package/target/main/ts/plugins/coverage/coverage.js +120 -0
  169. package/target/main/ts/plugins/coverage/diff-coverage.d.ts +7 -0
  170. package/target/main/ts/plugins/coverage/diff-coverage.js +89 -0
  171. package/target/main/ts/plugins/duplication/duplication.d.ts +4 -0
  172. package/target/main/ts/plugins/duplication/duplication.js +93 -0
  173. package/target/main/ts/plugins/language-js/babel-provider.d.ts +10 -0
  174. package/target/main/ts/plugins/language-js/babel-provider.js +46 -0
  175. package/target/main/ts/plugins/language-js/clone-candidates.d.ts +3 -0
  176. package/target/main/ts/plugins/language-js/clone-candidates.js +64 -0
  177. package/target/main/ts/plugins/language-js/code-lines.d.ts +6 -0
  178. package/target/main/ts/plugins/language-js/code-lines.js +35 -0
  179. package/target/main/ts/plugins/language-js/function-facts.d.ts +8 -0
  180. package/target/main/ts/plugins/language-js/function-facts.js +60 -0
  181. package/target/main/ts/plugins/language-js/structural-observations.d.ts +8 -0
  182. package/target/main/ts/plugins/language-js/structural-observations.js +79 -0
  183. package/target/main/ts/plugins/maintainability/maintainability.d.ts +9 -0
  184. package/target/main/ts/plugins/maintainability/maintainability.js +42 -0
  185. package/target/main/ts/plugins/maintainability/structural-smells.d.ts +3 -0
  186. package/target/main/ts/plugins/maintainability/structural-smells.js +15 -0
  187. package/target/main/ts/plugins/source/forges.d.ts +4 -0
  188. package/target/main/ts/plugins/source/forges.js +79 -0
  189. package/target/main/ts/plugins/structure/structure.d.ts +2 -0
  190. package/target/main/ts/plugins/structure/structure.js +37 -0
  191. package/target/main/ts/report/html/app/viewer-access.d.ts +19 -0
  192. package/target/main/ts/report/html/app/viewer-access.js +25 -0
  193. package/target/main/ts/report/html/app/viewer-chart.d.ts +101 -0
  194. package/target/main/ts/report/html/app/viewer-chart.js +711 -0
  195. package/target/main/ts/report/html/app/viewer-commits.d.ts +36 -0
  196. package/target/main/ts/report/html/app/viewer-commits.js +280 -0
  197. package/target/main/ts/report/html/app/viewer-data.d.ts +23 -0
  198. package/target/main/ts/report/html/app/viewer-data.js +105 -0
  199. package/target/main/ts/report/html/app/viewer-files.d.ts +117 -0
  200. package/target/main/ts/report/html/app/viewer-files.js +2087 -0
  201. package/target/main/ts/report/html/app/viewer-filter.d.ts +113 -0
  202. package/target/main/ts/report/html/app/viewer-filter.js +492 -0
  203. package/target/main/ts/report/html/app/viewer-metric-registry.d.ts +68 -0
  204. package/target/main/ts/report/html/app/viewer-metric-registry.js +206 -0
  205. package/target/main/ts/report/html/app/viewer-metrics.d.ts +44 -0
  206. package/target/main/ts/report/html/app/viewer-metrics.js +172 -0
  207. package/target/main/ts/report/html/app/viewer-profile.d.ts +17 -0
  208. package/target/main/ts/report/html/app/viewer-profile.js +401 -0
  209. package/target/main/ts/report/html/app/viewer-projects.d.ts +1 -0
  210. package/target/main/ts/report/html/app/viewer-projects.js +90 -0
  211. package/target/main/ts/report/html/app/viewer-pulls.d.ts +3 -0
  212. package/target/main/ts/report/html/app/viewer-pulls.js +242 -0
  213. package/target/main/ts/report/html/app/viewer-source.d.ts +75 -0
  214. package/target/main/ts/report/html/app/viewer-source.js +313 -0
  215. package/target/main/ts/report/html/app/viewer-trends.d.ts +74 -0
  216. package/target/main/ts/report/html/app/viewer-trends.js +413 -0
  217. package/target/main/ts/report/html/app/viewer.d.ts +1 -0
  218. package/target/main/ts/report/html/app/viewer.js +794 -0
  219. package/target/main/ts/report/html/site/badges.d.ts +14 -0
  220. package/target/main/ts/report/html/site/badges.js +39 -0
  221. package/target/main/ts/report/html/site/export-site.d.ts +21 -0
  222. package/target/main/ts/report/html/site/export-site.js +157 -0
  223. package/target/main/ts/report/html/site/exported-server.d.ts +6 -0
  224. package/target/main/ts/report/html/site/exported-server.js +70 -0
  225. package/target/main/ts/report/html/site/local-server.d.ts +7 -0
  226. package/target/main/ts/report/html/site/local-server.js +64 -0
  227. package/target/main/ts/report/html/site/projects.d.ts +3 -0
  228. package/target/main/ts/report/html/site/projects.js +79 -0
  229. package/target/main/ts/report/html/site/server.d.ts +4 -0
  230. package/target/main/ts/report/html/site/server.js +64 -0
  231. package/target/main/ts/report/html/site/source-route.d.ts +6 -0
  232. package/target/main/ts/report/html/site/source-route.js +22 -0
  233. package/target/main/ts/report/json/projections/commit-summary.d.ts +11 -0
  234. package/target/main/ts/report/json/projections/commit-summary.js +118 -0
  235. package/target/main/ts/report/json/projections/components.d.ts +4 -0
  236. package/target/main/ts/report/json/projections/components.js +29 -0
  237. package/target/main/ts/report/json/projections/file-metrics.d.ts +5 -0
  238. package/target/main/ts/report/json/projections/file-metrics.js +243 -0
  239. package/target/main/ts/report/json/projections/inspection-context.d.ts +6 -0
  240. package/target/main/ts/report/json/projections/inspection-context.js +92 -0
  241. package/target/main/ts/report/json/projections/inspection-details.d.ts +11 -0
  242. package/target/main/ts/report/json/projections/inspection-details.js +69 -0
  243. package/target/main/ts/report/json/projections/metric-values.d.ts +7 -0
  244. package/target/main/ts/report/json/projections/metric-values.js +57 -0
  245. package/target/main/ts/report/json/projections/public-history.d.ts +4 -0
  246. package/target/main/ts/report/json/projections/public-history.js +16 -0
  247. package/target/main/ts/report/json/store/components.d.ts +2 -0
  248. package/target/main/ts/report/json/store/components.js +31 -0
  249. package/target/main/ts/report/json/store/local-store.d.ts +13 -0
  250. package/target/main/ts/report/json/store/local-store.js +179 -0
  251. package/target/main/ts/report/json/store/pull-review-store.d.ts +3 -0
  252. package/target/main/ts/report/json/store/pull-review-store.js +51 -0
  253. package/target/main/ts/report/json/validation/metabranch.d.ts +4 -0
  254. package/target/main/ts/report/json/validation/metabranch.js +7 -0
  255. package/target/main/ts/report/json/validation/report.d.ts +15 -0
  256. package/target/main/ts/report/json/validation/report.js +64 -0
  257. package/target/main/ts/report/json/validation/summary-validation.d.ts +4 -0
  258. package/target/main/ts/report/json/validation/summary-validation.js +27 -0
  259. package/target/main/ts/report/json/wire/compact-json.d.ts +11 -0
  260. package/target/main/ts/report/json/wire/compact-json.js +76 -0
  261. package/target/main/ts/report/json/wire/component-model.d.ts +9 -0
  262. package/target/main/ts/report/json/wire/component-model.js +1 -0
  263. package/target/main/ts/report/json/wire/domain-types.d.ts +2 -0
  264. package/target/main/ts/report/json/wire/domain-types.js +1 -0
  265. package/target/main/ts/report/json/wire/file-metrics-model.d.ts +77 -0
  266. package/target/main/ts/report/json/wire/file-metrics-model.js +1 -0
  267. package/target/main/ts/report/json/wire/inspection-context-model.d.ts +26 -0
  268. package/target/main/ts/report/json/wire/inspection-context-model.js +1 -0
  269. package/target/main/ts/report/json/wire/inspection-details-model.d.ts +82 -0
  270. package/target/main/ts/report/json/wire/inspection-details-model.js +1 -0
  271. package/target/main/ts/report/json/wire/project-model.d.ts +7 -0
  272. package/target/main/ts/report/json/wire/project-model.js +1 -0
  273. package/target/main/ts/report/json/wire/public-history-model.d.ts +42 -0
  274. package/target/main/ts/report/json/wire/public-history-model.js +6 -0
  275. package/target/main/ts/report/json/wire/pull-review-model.d.ts +7 -0
  276. package/target/main/ts/report/json/wire/pull-review-model.js +1 -0
  277. package/target/main/ts/report/json/wire/summary-model.d.ts +45 -0
  278. package/target/main/ts/report/json/wire/summary-model.js +1 -0
  279. package/target/main/ts/report.d.ts +11 -0
  280. package/target/main/ts/report.js +5 -0
@@ -0,0 +1,711 @@
1
+ // Line and bar charts for the viewer. Drawn as SVG at the container's pixel width,
2
+ // so labels keep their size on every screen. Colours come from CSS classes and theme tokens.
3
+ const flagNotes = { best: 'Best of the shown inspections', worst: 'Worst of the shown inspections' };
4
+ const svgNamespace = 'http://www.w3.org/2000/svg';
5
+ const observed = new WeakMap();
6
+ const observer = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(entries => {
7
+ for (const entry of entries) {
8
+ const record = observed.get(entry.target);
9
+ const width = Math.round(entry.contentRect.width);
10
+ if (record && width > 0 && width !== record.width) {
11
+ record.width = width;
12
+ record.draw(width);
13
+ }
14
+ }
15
+ });
16
+ /** Draws into the container now and again whenever its width changes. */
17
+ function mount(container, draw) {
18
+ const width = Math.round(container.clientWidth);
19
+ observed.set(container, { width, draw });
20
+ observer?.observe(container);
21
+ draw(width || 640);
22
+ }
23
+ export function renderLineChart(container, points, options) {
24
+ mount(container, width => draw(container, points, width, options.height ?? (options.compact ? 200 : 248), (svg, layout) => lineChart(svg, layout, points, options)));
25
+ }
26
+ /** Diverging bars around zero, one per point; a point without a value gets a mark on the zero line. */
27
+ export function renderBarChart(container, points, options) {
28
+ mount(container, width => draw(container, points, width, options.compact ? 200 : 248, (svg, layout) => barChart(svg, layout, points, options)));
29
+ }
30
+ const top = 34;
31
+ const bottom = 30;
32
+ const right = 18;
33
+ /** Keeps the first and last marks off the plot edges. */
34
+ const inset = 12;
35
+ function draw(container, points, width, height, body) {
36
+ const layout = { width, height, top, plotHeight: height - top - bottom, left: 0, plotWidth: 0, xs: [], ys: [] };
37
+ const svg = svgElement('svg', 'tc');
38
+ svg.setAttribute('width', String(width));
39
+ svg.setAttribute('height', String(height));
40
+ svg.setAttribute('viewBox', `0 0 ${width} ${height}`);
41
+ body(svg, layout);
42
+ const tip = document.createElement('div');
43
+ tip.className = 'tc-tip';
44
+ tip.hidden = true;
45
+ tip.setAttribute('aria-hidden', 'true');
46
+ container.classList.add('chart-interactive');
47
+ container.replaceChildren(svg, tip);
48
+ interact(svg, tip, layout, points);
49
+ }
50
+ /**
51
+ * Positions points on the x axis and draws it. When every point has a commit time the axis is time: equal periods take
52
+ * equal width, and the ticks step by one calendar period (a day, a week, a month, a quarter, a year) chosen to fit the
53
+ * width. Without times the points stand evenly in order under their own labels.
54
+ */
55
+ function placeX(svg, layout, points) {
56
+ const { left, plotWidth, top: plotTop, plotHeight, height } = layout;
57
+ const from = left + inset;
58
+ const span = Math.max(1, plotWidth - inset * 2);
59
+ const times = points.map(point => point.time);
60
+ const timed = points.length > 0 && times.every((time) => time !== null);
61
+ const axis = plotTop + plotHeight;
62
+ if (timed) {
63
+ const low = Math.min(...times);
64
+ const high = Math.max(...times);
65
+ const at = (time) => high === low ? from + span / 2 : from + (time - low) / (high - low) * span;
66
+ layout.xs = times.map(at);
67
+ if (high === low)
68
+ return;
69
+ const ticks = timeTicks(low, high, span);
70
+ for (const tick of ticks) {
71
+ const mark = svgElement('line', 'tc-xtick');
72
+ setAttributes(mark, { x1: at(tick.time), x2: at(tick.time), y1: axis, y2: axis + 4 });
73
+ svg.append(mark);
74
+ }
75
+ for (const label of axisLabels(ticks.map(tick => ({ time: tick.time, x: at(tick.time) })), ticks[0]?.unit ?? 'day', layout)) {
76
+ const node = text(label.label, 'tc-label', label.at, height - 9);
77
+ node.setAttribute('text-anchor', 'middle');
78
+ svg.append(node);
79
+ }
80
+ return;
81
+ }
82
+ const step = points.length > 1 ? span / (points.length - 1) : 0;
83
+ layout.xs = points.map((_, index) => points.length > 1 ? from + index * step : from + span / 2);
84
+ const labelWidth = Math.max(...points.map(point => point.label.length)) * 7.6 + 10;
85
+ const every = step > 0 ? Math.max(1, Math.ceil(labelWidth / step)) : 1;
86
+ const last = points.length - 1;
87
+ for (const [index, point] of points.entries()) {
88
+ const near = index !== last && last - index < every;
89
+ if ((index % every !== 0 || near) && index !== last)
90
+ continue;
91
+ const label = text(point.label, 'tc-label', clampLabel(layout.xs[index], point.label, layout), height - 9);
92
+ label.setAttribute('text-anchor', 'middle');
93
+ svg.append(label);
94
+ }
95
+ }
96
+ const monthLabel = new Intl.DateTimeFormat(undefined, { month: 'short' });
97
+ const monthYearLabel = new Intl.DateTimeFormat(undefined, { month: 'short', year: 'numeric' });
98
+ const dayLabel = new Intl.DateTimeFormat(undefined, { day: 'numeric', month: 'short' });
99
+ const dayYearLabel = new Intl.DateTimeFormat(undefined, { day: 'numeric', month: 'short', year: 'numeric' });
100
+ const hourLabel = new Intl.DateTimeFormat(undefined, { hour: '2-digit', minute: '2-digit' });
101
+ /**
102
+ * Labels for ticks in time order. Each names its own unit, and the coarser units join it only where they change:
103
+ * Jul 2023, Nov, Dec, Jan 2024, Feb; 30 Jun, 1 Jul, 2; 23:00, 1 Jul, 01:00. The first tick always carries them.
104
+ */
105
+ export function tickLabels(times, unit) {
106
+ let previous = null;
107
+ return times.map(time => {
108
+ const date = new Date(time);
109
+ const year = previous === null || previous.getFullYear() !== date.getFullYear();
110
+ const month = year || previous.getMonth() !== date.getMonth();
111
+ const today = month || previous.getDate() !== date.getDate();
112
+ previous = date;
113
+ if (unit === 'year')
114
+ return String(date.getFullYear());
115
+ if (unit === 'month')
116
+ return (year ? monthYearLabel : monthLabel).format(date);
117
+ if (unit === 'day')
118
+ return year ? dayYearLabel.format(date) : month ? dayLabel.format(date) : String(date.getDate());
119
+ if (!today)
120
+ return hourLabel.format(date);
121
+ const days = (year ? dayYearLabel : dayLabel).format(date);
122
+ return date.getHours() === 0 && date.getMinutes() === 0 ? days : `${days}, ${hourLabel.format(date)}`;
123
+ });
124
+ }
125
+ /**
126
+ * The labels under the ticks. Which ticks keep a label is settled first: one that would sit far off its tick at the
127
+ * edge, or touch the label before it, keeps only its mark. Only then are the labels written, so the first label shown
128
+ * carries the coarser units, and every later one carries them only where they change.
129
+ */
130
+ export function axisLabels(ticks, unit, layout) {
131
+ let shown = [...ticks];
132
+ for (;;) {
133
+ const labels = tickLabels(shown.map(tick => tick.time), unit);
134
+ let previous = -Infinity;
135
+ const placed = [];
136
+ const drop = shown.findIndex((tick, index) => {
137
+ const label = labels[index];
138
+ const at = clampLabel(tick.x, label, layout);
139
+ const half = label.length * 3.7;
140
+ if (Math.abs(at - tick.x) > 12 || at - half < previous + 10)
141
+ return true;
142
+ previous = at + half;
143
+ placed.push({ at, label });
144
+ return false;
145
+ });
146
+ if (drop < 0)
147
+ return placed;
148
+ shown = shown.filter((_, index) => index !== drop);
149
+ }
150
+ }
151
+ function clampLabel(x, label, { left, plotWidth }) {
152
+ const half = label.length * 3.6 + 2;
153
+ return Math.min(Math.max(x, left + half), left + plotWidth - half);
154
+ }
155
+ const minute = 60e3;
156
+ const hour = 60 * minute;
157
+ const day = 24 * hour;
158
+ const steps = [
159
+ { unit: 'minute', count: 5, size: 5 * minute }, { unit: 'minute', count: 10, size: 10 * minute },
160
+ { unit: 'minute', count: 15, size: 15 * minute }, { unit: 'minute', count: 30, size: 30 * minute },
161
+ { unit: 'hour', count: 1, size: hour }, { unit: 'hour', count: 3, size: 3 * hour }, { unit: 'hour', count: 6, size: 6 * hour },
162
+ { unit: 'hour', count: 12, size: 12 * hour }, { unit: 'day', count: 1, size: day }, { unit: 'day', count: 2, size: 2 * day },
163
+ { unit: 'day', count: 7, size: 7 * day }, { unit: 'day', count: 14, size: 14 * day }, { unit: 'month', count: 1, size: 30.4 * day },
164
+ { unit: 'month', count: 3, size: 91 * day }, { unit: 'month', count: 6, size: 182 * day }, { unit: 'year', count: 1, size: 365 * day },
165
+ { unit: 'year', count: 2, size: 730 * day }, { unit: 'year', count: 5, size: 1826 * day },
166
+ ];
167
+ /** Calendar-aligned ticks between two times, as many as fit the width without overlapping labels. */
168
+ export function timeTicks(start, end, width) {
169
+ for (const step of steps) {
170
+ if ((end - start) / step.size > Math.max(2, width / 64))
171
+ continue;
172
+ const ticks = calendarTicks(start, end, step.unit, step.count);
173
+ // Sized by the typical label: the few that carry a year or a date beside them are placed with care by the axis.
174
+ const lengths = ticks.map(tick => tick.label.length).sort((x, y) => x - y);
175
+ const widest = (lengths[Math.floor(lengths.length / 2)] ?? 0) * 7.4 + 14;
176
+ if (ticks.length < 2 || (width / Math.max(1, (end - start) / step.size)) >= widest)
177
+ return ticks;
178
+ }
179
+ return calendarTicks(start, end, 'year', 10);
180
+ }
181
+ function calendarTicks(start, end, unit, count) {
182
+ const ticks = [];
183
+ const cursor = new Date(start);
184
+ cursor.setSeconds(0, 0);
185
+ if (unit !== 'minute')
186
+ cursor.setMinutes(0);
187
+ if (unit !== 'minute' && unit !== 'hour')
188
+ cursor.setHours(0);
189
+ if (unit === 'month' || unit === 'year')
190
+ cursor.setDate(1);
191
+ if (unit === 'year')
192
+ cursor.setMonth(0);
193
+ const advance = () => {
194
+ if (unit === 'minute')
195
+ cursor.setMinutes(cursor.getMinutes() + 1);
196
+ else if (unit === 'hour')
197
+ cursor.setHours(cursor.getHours() + 1);
198
+ else if (unit === 'day')
199
+ cursor.setDate(cursor.getDate() + 1);
200
+ else if (unit === 'month')
201
+ cursor.setMonth(cursor.getMonth() + 1);
202
+ else
203
+ cursor.setFullYear(cursor.getFullYear() + 1);
204
+ };
205
+ const aligned = () => unit === 'minute' ? cursor.getMinutes() % count === 0
206
+ : unit === 'hour' ? cursor.getHours() % count === 0
207
+ : unit === 'day' ? (count === 7 || count === 14 ? cursor.getDay() === 1 && (count === 7 || weekOf(cursor) % 2 === 0) : (cursor.getDate() - 1) % count === 0 && cursor.getDate() < 31)
208
+ : unit === 'month' ? cursor.getMonth() % count === 0 : cursor.getFullYear() % count === 0;
209
+ let guard = 0;
210
+ while (cursor.getTime() < start && guard++ < 6000)
211
+ advance();
212
+ while (cursor.getTime() <= end && guard++ < 6000) {
213
+ if (aligned()) {
214
+ const time = cursor.getTime();
215
+ ticks.push({ time, label: '', unit });
216
+ }
217
+ advance();
218
+ }
219
+ const labels = tickLabels(ticks.map(tick => tick.time), unit);
220
+ return ticks.map((tick, index) => ({ time: tick.time, label: labels[index], unit }));
221
+ }
222
+ function weekOf(date) {
223
+ return Math.floor(date.getTime() / (7 * day));
224
+ }
225
+ function yAxis(svg, layout, values, ratio, count, format, tickUnit, zero, rightLabels) {
226
+ const ticks = niceTicks((zero ? [...values, 0] : values).map(value => value / tickUnit), ratio, count).map(tick => tick * tickUnit);
227
+ const tickStep = ticks.length > 1 ? ticks[1] - ticks[0] : 1;
228
+ const digits = Math.min(3, Math.max(0, -Math.floor(Math.log10(tickStep * 100) + 1e-9)));
229
+ const tickPercent = new Intl.NumberFormat(undefined, { style: 'percent', minimumFractionDigits: digits, maximumFractionDigits: digits });
230
+ const labels = ticks.map(tick => zero && tick === 0 ? '0' : ratio ? tickPercent.format(tick) : format(tick));
231
+ layout.left = Math.ceil(Math.max(...labels.map(tickWidth))) + 12;
232
+ // A second scale takes a label column at the right, one label per grid line.
233
+ const secondLabels = rightLabels?.(ticks.length) ?? [];
234
+ const reserve = secondLabels.length ? Math.ceil(Math.max(...secondLabels.map(tickWidth))) + 12 - right : 0;
235
+ layout.plotWidth = Math.max(40, layout.width - layout.left - right - reserve);
236
+ const low = ticks[0];
237
+ const high = ticks.at(-1);
238
+ const y = (value) => layout.top + layout.plotHeight - (high === low ? .5 : (value - low) / (high - low)) * layout.plotHeight;
239
+ for (const [index, tick] of ticks.entries()) {
240
+ const line = svgElement('line', (zero ? tick === 0 : index === 0) ? 'tc-base' : 'tc-grid');
241
+ setAttributes(line, { x1: layout.left, x2: layout.left + layout.plotWidth, y1: y(tick), y2: y(tick) });
242
+ const label = text(labels[index], 'tc-tick', layout.left - 8, y(tick) + 4.5);
243
+ label.setAttribute('text-anchor', 'end');
244
+ svg.append(line, label);
245
+ const second = secondLabels[index];
246
+ if (second !== undefined)
247
+ svg.append(text(second, 'tc-tick tc-tick-secondary', layout.left + layout.plotWidth + 8, y(tick) + 4.5));
248
+ }
249
+ if (zero && low !== 0) {
250
+ const base = svgElement('line', 'tc-grid');
251
+ setAttributes(base, { x1: layout.left, x2: layout.left + layout.plotWidth, y1: y(low), y2: y(low) });
252
+ svg.append(base);
253
+ }
254
+ return y;
255
+ }
256
+ let measure;
257
+ /** Rendered width of a 13 px tick label; an estimate where the page cannot measure text. */
258
+ function tickWidth(label) {
259
+ if (measure === undefined) {
260
+ measure = typeof document === 'undefined' ? null : document.createElement('canvas').getContext('2d');
261
+ if (measure)
262
+ measure.font = `13px ${getComputedStyle(document.documentElement).fontFamily}`;
263
+ }
264
+ return measure ? measure.measureText(label).width : label.length * 8.6;
265
+ }
266
+ function lineChart(svg, layout, points, { format, ratio, compact, tickUnit = 1, labelCurrent = true, secondary }) {
267
+ const values = points.flatMap(point => point.value === null ? [] : [point.value]);
268
+ // The second scale starts at zero and shares the main grid lines: each line carries one round value of each.
269
+ const secondValues = secondary?.values.flatMap(value => value === null ? [] : [value]) ?? [];
270
+ let secondHigh = 0;
271
+ const rightLabels = secondary && secondValues.length ? (ticks) => {
272
+ const unit = secondary.tickUnit ?? 1;
273
+ const step = niceStep(Math.max(...secondValues, 0) / unit / Math.max(1, ticks - 1)) * unit;
274
+ secondHigh = step * (ticks - 1);
275
+ return Array.from({ length: ticks }, (_, index) => index === 0 ? '0' : secondary.format(step * index));
276
+ } : undefined;
277
+ const y = yAxis(svg, layout, values, ratio, compact ? 3 : 4, format, tickUnit, false, rightLabels);
278
+ placeX(svg, layout, points);
279
+ const x = (index) => layout.xs[index];
280
+ markBoundaries(svg, layout, points);
281
+ if (secondary && secondHigh > 0) {
282
+ const y2 = (value) => layout.top + layout.plotHeight - value / secondHigh * layout.plotHeight;
283
+ for (const segment of segments(secondary.values, points)) {
284
+ const path = svgElement('path', 'tc-line tc-line-secondary');
285
+ path.setAttribute('d', segment.map((index, order) => {
286
+ const value = secondary.values[index];
287
+ if (!order)
288
+ return `M${x(index).toFixed(1)},${y2(value).toFixed(1)}`;
289
+ const last = order === segment.length - 1;
290
+ return value === secondary.values[segment[order - 1]] && !last ? '' : `H${x(index).toFixed(1)}V${y2(value).toFixed(1)}`;
291
+ }).join(''));
292
+ svg.append(path);
293
+ }
294
+ }
295
+ for (const segment of segments(points.map(point => point.value), points)) {
296
+ const path = svgElement('path', 'tc-line');
297
+ // Code keeps its state until the next commit, so the line holds each value and steps at the commit that changed it;
298
+ // a slope would invent values for the days between commits.
299
+ // A run of equal values is one horizontal stroke, so ten thousand quiet commits cost a single segment.
300
+ path.setAttribute('d', segment.map((index, order) => {
301
+ const value = points[index].value;
302
+ if (!order)
303
+ return `M${x(index).toFixed(1)},${y(value).toFixed(1)}`;
304
+ const last = order === segment.length - 1;
305
+ return value === points[segment[order - 1]].value && !last ? '' : `H${x(index).toFixed(1)}V${y(value).toFixed(1)}`;
306
+ }).join(''));
307
+ svg.append(path);
308
+ }
309
+ const lastIndex = points.length - 1;
310
+ const axis = layout.top + layout.plotHeight;
311
+ layout.ys = points.map(point => point.value === null ? null : y(point.value));
312
+ // A dot per commit is noise once commits sit close together: the line carries the curve, and a dot marks only what
313
+ // says something (the selected and the last commit, flags, and a measurement with no neighbour to draw a line to).
314
+ // Hover and the keyboard move one dot along the line instead. Only a short, sparse series keeps a dot per commit.
315
+ const sparse = points.length <= 12 && layout.xs.every((at, index) => index === 0 || at - layout.xs[index - 1] >= 24);
316
+ const lonely = (index) => points[index - 1]?.value == null && points[index + 1]?.value == null;
317
+ // One value is labelled: the selected commit's, or the last one when the selection is off the chart.
318
+ const selectedIndex = points.findIndex(point => point.current && point.value !== null);
319
+ const labelled = labelCurrent && selectedIndex >= 0 ? selectedIndex : lastIndex;
320
+ for (const [index, point] of points.entries()) {
321
+ const marked = sparse || point.current || Boolean(point.flag || point.boundary) || index === lastIndex || (point.value !== null && lonely(index));
322
+ if (!marked)
323
+ continue;
324
+ const link = pointLink(point, index);
325
+ if (point.value === null) {
326
+ const gap = svgElement('line', 'tc-missing-mark');
327
+ setAttributes(gap, { x1: x(index), x2: x(index), y1: axis - 7, y2: axis });
328
+ link.append(gap);
329
+ }
330
+ else {
331
+ const dot = svgElement('circle', point.current ? 'tc-dot tc-dot-current' : 'tc-dot');
332
+ if (point.partial)
333
+ dot.classList.add('tc-dot-partial');
334
+ setAttributes(dot, { cx: x(index), cy: y(point.value), r: point.current ? 5 : 3.5 });
335
+ if (point.flag)
336
+ link.append(flagMark(point.flag, x(index), y(point.value)));
337
+ link.append(dot);
338
+ if (index === labelled) {
339
+ // A flag on this point or one just beside it takes the space above, so the value moves left of the pole.
340
+ // The label runs left from its anchor; it steps left of every flag it would cross, so it never covers one.
341
+ const width = format(point.value).length * 7.2;
342
+ const top = y(point.value) - 11;
343
+ let anchor = x(index);
344
+ let poles = [];
345
+ for (let pass = 0; pass < 4; pass += 1) {
346
+ const left = anchor - width - 4;
347
+ const crossed = points.flatMap((other, at) => {
348
+ if (!other.flag || other.value === null)
349
+ return [];
350
+ const foot = y(other.value) - 4;
351
+ return x(at) >= left && x(at) <= anchor + 11 && top > foot - 22 && top - 14 < foot ? [x(at)] : [];
352
+ });
353
+ if (!crossed.length || Math.min(...crossed) - 7 >= anchor)
354
+ break;
355
+ poles = crossed;
356
+ anchor = Math.min(...crossed) - 7;
357
+ }
358
+ // A label pushed far from its point would sit on the line; it goes under the point instead, where a flag never is.
359
+ const below = x(index) - anchor > 40 && y(point.value) + 22 <= axis;
360
+ if (below)
361
+ anchor = x(index) + (index === lastIndex ? 4 : 0);
362
+ const value = text(format(point.value), point.current ? 'tc-value tc-value-current' : 'tc-value', anchor, below ? y(point.value) + 21 : top);
363
+ value.setAttribute('text-anchor', (poles.length && !below) || (index === lastIndex && points.length > 1) ? 'end' : 'middle');
364
+ link.append(value);
365
+ }
366
+ }
367
+ svg.append(link);
368
+ }
369
+ }
370
+ function barChart(svg, layout, points, { format, compact, upIsWorse, tickUnit }) {
371
+ svg.classList.add('tc-bars');
372
+ const values = points.flatMap(point => point.value === null ? [] : [point.value]);
373
+ const y = yAxis(svg, layout, values, false, compact ? 3 : 4, format, tickUnit ?? 1, true);
374
+ placeX(svg, layout, points);
375
+ markBoundaries(svg, layout, points);
376
+ const x = (index) => layout.xs[index];
377
+ const gap = (index) => Math.min(index > 0 ? x(index) - x(index - 1) : Infinity, index < points.length - 1 ? x(index + 1) - x(index) : Infinity);
378
+ const valueWidth = (value) => format(value).length * 7.6;
379
+ layout.ys = points.map(point => point.value === null ? null : y(point.value));
380
+ // In a long history the zero line already says "no change" and "not measured"; only bars that moved are drawn.
381
+ const dense = layout.xs.some((at, index) => index > 0 && at - layout.xs[index - 1] < 7);
382
+ for (const [index, point] of points.entries()) {
383
+ const link = pointLink(point, index);
384
+ const room = gap(index);
385
+ if (dense && !point.current && (point.value === null || point.value === 0)) {
386
+ svg.append(link);
387
+ continue;
388
+ }
389
+ const barWidth = Math.max(2, Math.min(28, Number.isFinite(room) ? room * .62 : 28));
390
+ if (point.value === null) {
391
+ const missing = svgElement('line', 'tc-missing-mark');
392
+ setAttributes(missing, { x1: x(index), x2: x(index), y1: y(0) - 3.5, y2: y(0) + 3.5 });
393
+ link.append(missing);
394
+ }
395
+ else if (point.value !== 0) {
396
+ const worse = upIsWorse ? point.value > 0 : point.value < 0;
397
+ const bar = svgElement('rect', `tc-bar ${worse ? 'tc-bar-worse' : 'tc-bar-better'}${point.current ? ' tc-bar-current' : ''}`);
398
+ const top0 = Math.min(y(point.value), y(0));
399
+ setAttributes(bar, { x: x(index) - barWidth / 2, y: top0, width: barWidth, height: Math.max(1, Math.abs(y(point.value) - y(0))) });
400
+ link.append(bar);
401
+ if (point.current || valueWidth(point.value) < room) {
402
+ const above = point.value > 0;
403
+ const label = text(format(point.value), point.current ? 'tc-value tc-value-current' : 'tc-value', x(index), above ? y(point.value) - 7 : y(point.value) + 16);
404
+ label.setAttribute('text-anchor', 'middle');
405
+ link.append(label);
406
+ }
407
+ }
408
+ else {
409
+ const flat = svgElement('line', 'tc-zero-mark');
410
+ setAttributes(flat, { x1: x(index) - barWidth / 2, x2: x(index) + barWidth / 2, y1: y(0), y2: y(0) });
411
+ link.append(flat);
412
+ }
413
+ svg.append(link);
414
+ }
415
+ }
416
+ /** A tiny flag planted in the point: the pennant of the logo on a short pole, both in the flag's colour. */
417
+ function flagMark(kind, x, y) {
418
+ const group = svgElement('g', `tc-flag tc-flag-${kind}`);
419
+ const foot = y - 4;
420
+ const head = foot - 12;
421
+ const pole = svgElement('rect');
422
+ setAttributes(pole, { x: x - .75, y: head, width: 1.5, height: foot - head });
423
+ const pennant = svgElement('path');
424
+ 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`);
425
+ group.append(pole, pennant);
426
+ return group;
427
+ }
428
+ function flagIcon(kind) {
429
+ const icon = svgElement('svg', `tc-flag tc-flag-${kind}`);
430
+ icon.setAttribute('viewBox', '0 -1 11 14');
431
+ icon.setAttribute('aria-hidden', 'true');
432
+ icon.append(flagMark(kind, 1, 16));
433
+ return icon;
434
+ }
435
+ function pointLink(point, index) {
436
+ const link = svgElement('a', 'tc-point');
437
+ link.setAttribute('href', point.href);
438
+ link.setAttribute('aria-label', point.title);
439
+ link.dataset.index = String(index);
440
+ if (point.current)
441
+ link.setAttribute('aria-current', 'true');
442
+ return link;
443
+ }
444
+ function card(point, brief) {
445
+ // Each card opens its commit: in a cluster the card is how one of several commits at a point is picked.
446
+ const entry = document.createElement(point.href ? 'a' : 'div');
447
+ entry.className = 'tc-tip-entry';
448
+ if (entry instanceof HTMLAnchorElement)
449
+ entry.href = point.href;
450
+ const heading = document.createElement('strong');
451
+ heading.textContent = point.tip.heading;
452
+ const commit = document.createElement('code');
453
+ commit.textContent = point.tip.commit;
454
+ const value = document.createElement('span');
455
+ value.className = 'tc-tip-value';
456
+ value.textContent = point.tip.value;
457
+ if (point.tip.change && !(brief && point.tip.change.direction === 'same')) {
458
+ const change = document.createElement('span');
459
+ change.className = 'tc-tip-change';
460
+ change.dataset.direction = point.tip.change.direction;
461
+ change.textContent = point.tip.change.text;
462
+ value.append(' ', change);
463
+ }
464
+ entry.append(heading, commit);
465
+ if (point.tip.subject)
466
+ entry.append(Object.assign(document.createElement('span'), { className: 'tc-tip-subject', textContent: point.tip.subject }));
467
+ entry.append(value);
468
+ if (point.flag) {
469
+ const line = document.createElement('span');
470
+ line.className = 'tc-tip-flag';
471
+ line.append(flagIcon(point.flag), flagNotes[point.flag]);
472
+ entry.append(line);
473
+ }
474
+ for (const note of point.tip.notes) {
475
+ const line = document.createElement('span');
476
+ line.className = 'tc-tip-note';
477
+ line.textContent = note;
478
+ entry.append(line);
479
+ }
480
+ return entry;
481
+ }
482
+ /** One hover card for the whole chart: the point nearest the pointer, or the focused one. */
483
+ function interact(svg, tip, layout, points) {
484
+ if (points.length === 0)
485
+ return;
486
+ const guide = svgElement('line', 'tc-guide');
487
+ guide.setAttribute('visibility', 'hidden');
488
+ svg.insertBefore(guide, svg.firstChild);
489
+ // One dot follows the pointer or the keyboard along the line, so the chart needs no element per commit.
490
+ const pointer = svgElement('circle', 'tc-dot tc-hover-dot');
491
+ pointer.setAttribute('r', '4.5');
492
+ pointer.setAttribute('visibility', 'hidden');
493
+ svg.append(pointer);
494
+ let active = -1;
495
+ let side = null;
496
+ let pending;
497
+ let lastX = 0;
498
+ const nearest = (clientX) => {
499
+ const box = svg.getBoundingClientRect();
500
+ const px = (clientX - box.left) * (layout.width / Math.max(1, box.width));
501
+ let best = 0;
502
+ for (const [index, position] of layout.xs.entries()) {
503
+ // Prefer the later point on ties, so the newest of a cluster is reachable.
504
+ if (Math.abs(position - px) <= Math.abs(layout.xs[best] - px))
505
+ best = index;
506
+ }
507
+ return best;
508
+ };
509
+ const show = (index) => {
510
+ if (index === active)
511
+ return;
512
+ active = index;
513
+ const point = points[index];
514
+ const x = layout.xs[index];
515
+ setAttributes(guide, { x1: x, x2: x, y1: layout.top - 6, y2: layout.top + layout.plotHeight });
516
+ guide.setAttribute('visibility', 'visible');
517
+ svg.querySelectorAll('.tc-point.is-active').forEach(node => node.classList.remove('is-active'));
518
+ const own = svg.querySelector(`.tc-point[data-index="${index}"]`);
519
+ own?.classList.add('is-active');
520
+ const markY = layout.ys[index];
521
+ if (markY == null || own?.querySelector('.tc-dot') || svg.classList.contains('tc-bars'))
522
+ pointer.setAttribute('visibility', 'hidden');
523
+ else {
524
+ setAttributes(pointer, { cx: x, cy: markY });
525
+ pointer.setAttribute('visibility', 'visible');
526
+ }
527
+ tip.replaceChildren();
528
+ // Marks closer than a few pixels overlap, so the card lists every commit under the pointer, newest first.
529
+ const cluster = points.flatMap((_, other) => Math.abs(layout.xs[other] - x) < 4 ? [other] : []).reverse();
530
+ // The card scrolls, so a cluster lists its commits whole up to a bound a hand can still scroll through.
531
+ const listed = 50;
532
+ for (const member of cluster.slice(0, listed))
533
+ tip.append(card(points[member], cluster.length > 1));
534
+ if (cluster.length > listed) {
535
+ const more = document.createElement('span');
536
+ more.className = 'tc-tip-note';
537
+ more.textContent = `${cluster.length - listed} more commits at this point`;
538
+ tip.append(more);
539
+ }
540
+ tip.scrollTop = 0;
541
+ // Only a card of several commits takes the pointer; a single card stays out of the way of scanning along the chart.
542
+ tip.classList.toggle('tc-tip-held', cluster.length > 1);
543
+ tip.hidden = false;
544
+ const scale = svg.getBoundingClientRect().width / Math.max(1, layout.width) || 1;
545
+ const width = tip.offsetWidth;
546
+ const room = svg.clientWidth || layout.width;
547
+ // The card keeps the side it opened on while it fits, so it does not jump across the pointer heading for it.
548
+ const right = x * scale + 14;
549
+ const leftSide = x * scale - 14 - width;
550
+ side = side === 'left' && leftSide >= 0 ? 'left' : right + width <= room ? 'right' : 'left';
551
+ tip.style.left = `${Math.max(0, side === 'right' ? right : leftSide)}px`;
552
+ tip.style.top = `${Math.max(0, Math.min((markY ?? layout.top + layout.plotHeight) * scale - tip.offsetHeight / 2, layout.top * scale + layout.plotHeight * scale - tip.offsetHeight))}px`;
553
+ };
554
+ const hide = () => {
555
+ clearTimeout(pending);
556
+ side = null;
557
+ active = -1;
558
+ tip.hidden = true;
559
+ guide.setAttribute('visibility', 'hidden');
560
+ pointer.setAttribute('visibility', 'hidden');
561
+ svg.querySelectorAll('.tc-point.is-active').forEach(node => node.classList.remove('is-active'));
562
+ };
563
+ // A pointer heading for the card may cross other commits on the way. While it moves towards the card, the switch
564
+ // waits a moment; reaching the card keeps it, so its commits can be scrolled and picked.
565
+ svg.addEventListener('pointermove', event => {
566
+ const candidate = nearest(event.clientX);
567
+ const dx = event.clientX - lastX;
568
+ lastX = event.clientX;
569
+ clearTimeout(pending);
570
+ if (candidate === active)
571
+ return;
572
+ const box = tip.hidden || !tip.classList.contains('tc-tip-held') ? null : tip.getBoundingClientRect();
573
+ const heading = box !== null && (side === 'right' ? dx > 0 && event.clientX < box.left : dx < 0 && event.clientX > box.right);
574
+ if (heading)
575
+ pending = setTimeout(() => show(candidate), 220);
576
+ else
577
+ show(candidate);
578
+ });
579
+ svg.addEventListener('pointerleave', event => {
580
+ if (!tip.contains(event.relatedTarget))
581
+ hide();
582
+ });
583
+ tip.addEventListener('pointerenter', () => clearTimeout(pending));
584
+ tip.addEventListener('pointerleave', event => {
585
+ if (!svg.contains(event.relatedTarget))
586
+ hide();
587
+ });
588
+ svg.addEventListener('click', event => {
589
+ if (event.target.closest('.tc-point'))
590
+ return;
591
+ const href = points[nearest(event.clientX)]?.href;
592
+ if (href)
593
+ location.assign(href);
594
+ });
595
+ svg.addEventListener('focusin', event => {
596
+ const index = Number(event.target.closest('.tc-point')?.dataset.index);
597
+ if (Number.isInteger(index))
598
+ show(index);
599
+ });
600
+ svg.addEventListener('focusout', hide);
601
+ // The chart itself takes focus: arrows step through commits, Home and End jump to the ends, Enter opens the commit.
602
+ svg.tabIndex = 0;
603
+ svg.setAttribute('aria-keyshortcuts', 'ArrowLeft ArrowRight Home End Enter');
604
+ svg.setAttribute('aria-label', `${points.length} ${points.length === 1 ? 'commit' : 'commits'}; arrow keys move between them, Enter opens one`);
605
+ svg.addEventListener('focus', () => {
606
+ const current = points.findIndex(point => point.current);
607
+ show(current >= 0 ? current : points.length - 1);
608
+ });
609
+ svg.addEventListener('keydown', event => {
610
+ const last = points.length - 1;
611
+ const from = active < 0 ? last : active;
612
+ const next = event.key === 'ArrowLeft' ? Math.max(0, from - 1) : event.key === 'ArrowRight' ? Math.min(last, from + 1)
613
+ : event.key === 'Home' ? 0 : event.key === 'End' ? last : null;
614
+ if (next !== null) {
615
+ event.preventDefault();
616
+ active = -1;
617
+ show(next);
618
+ }
619
+ else if (event.key === 'Enter' && active >= 0) {
620
+ event.preventDefault();
621
+ location.assign(points[active].href);
622
+ }
623
+ });
624
+ }
625
+ /** Runs of consecutive measured points, broken where a measurement is missing. */
626
+ function segments(values, points) {
627
+ const result = [];
628
+ let run = [];
629
+ for (const [index, value] of values.entries()) {
630
+ if (value === null || points[index]?.boundary === 'changed') {
631
+ if (run.length)
632
+ result.push(run);
633
+ run = [];
634
+ }
635
+ if (value !== null)
636
+ run.push(index);
637
+ }
638
+ if (run.length)
639
+ result.push(run);
640
+ return result.filter(segment => segment.length > 1);
641
+ }
642
+ function markBoundaries(svg, layout, points) {
643
+ for (const [index, point] of points.entries()) {
644
+ if (index === 0 || !point.boundary)
645
+ continue;
646
+ const x = layout.xs[index];
647
+ const line = svgElement('line', `tc-profile-${point.boundary}`);
648
+ setAttributes(line, { x1: x, x2: x, y1: layout.top, y2: layout.top + layout.plotHeight });
649
+ svg.append(line);
650
+ }
651
+ }
652
+ function paddedRange(values, ratio) {
653
+ let low = Math.min(...values);
654
+ let high = Math.max(...values);
655
+ // A minimum visible range keeps noise flat: 2 percentage points for ratios, 10% of the value for counts.
656
+ const minimum = ratio ? .02 : Math.max(Math.abs(high) * .1, 2);
657
+ if (high - low < minimum) {
658
+ const middle = (high + low) / 2;
659
+ low = middle - minimum / 2;
660
+ high = middle + minimum / 2;
661
+ }
662
+ const span = high - low;
663
+ low -= span * .12;
664
+ high += span * .12;
665
+ if (Math.min(...values) >= 0)
666
+ low = Math.max(0, low);
667
+ if (ratio && Math.max(...values) <= 1)
668
+ high = Math.min(1, high);
669
+ return [low, high];
670
+ }
671
+ /** Evenly spaced round ticks that cover the data with a margin. */
672
+ /** A round step near the given one: 1, 2, 2.5 or 5 times a power of ten. */
673
+ function niceStep(raw) {
674
+ if (!(raw > 0))
675
+ return 1;
676
+ const power = 10 ** Math.floor(Math.log10(raw));
677
+ const fraction = raw / power;
678
+ return (fraction <= 1 ? 1 : fraction <= 2 ? 2 : fraction <= 2.5 ? 2.5 : fraction <= 5 ? 5 : 10) * power;
679
+ }
680
+ export function niceTicks(values, ratio, count) {
681
+ if (values.length === 0)
682
+ return ratio ? [0, .5, 1] : [0, 1];
683
+ const [low, high] = paddedRange(values, ratio);
684
+ const raw = (high - low) / count;
685
+ const magnitude = 10 ** Math.floor(Math.log10(raw));
686
+ const step = [1, 2, 2.5, 5, 10].map(factor => factor * magnitude).find(candidate => candidate >= raw) ?? raw;
687
+ const integral = !ratio && values.every(Number.isInteger);
688
+ const unit = integral ? Math.max(1, step) : step;
689
+ const first = Math.floor(low / unit) * unit;
690
+ const last = Math.ceil(high / unit) * unit;
691
+ const ticks = [];
692
+ for (let tick = first; tick <= last + unit / 2; tick += unit)
693
+ ticks.push(Number(tick.toPrecision(12)));
694
+ return ticks;
695
+ }
696
+ function text(content, className, x, y) {
697
+ const node = svgElement('text', className);
698
+ node.textContent = content;
699
+ setAttributes(node, { x, y });
700
+ return node;
701
+ }
702
+ function setAttributes(node, attributes) {
703
+ for (const [name, value] of Object.entries(attributes))
704
+ node.setAttribute(name, String(Number(value.toFixed(1))));
705
+ }
706
+ function svgElement(tag, className) {
707
+ const node = document.createElementNS(svgNamespace, tag);
708
+ if (className)
709
+ node.setAttribute('class', className);
710
+ return node;
711
+ }