react-cheminfo 0.33.0 → 0.35.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 (248) hide show
  1. package/README.md +28 -28
  2. package/lib/about/core/about.d.ts +1 -30
  3. package/lib/about/core/about.d.ts.map +1 -1
  4. package/lib/about/core/about.js +0 -16
  5. package/lib/about/core/about.js.map +1 -1
  6. package/lib/about/core/index.d.ts +0 -1
  7. package/lib/about/core/index.d.ts.map +1 -1
  8. package/lib/about/core/index.js +0 -1
  9. package/lib/about/core/index.js.map +1 -1
  10. package/lib/about/ui/AboutBuild.d.ts +2 -5
  11. package/lib/about/ui/AboutBuild.d.ts.map +1 -1
  12. package/lib/about/ui/AboutBuild.js +7 -19
  13. package/lib/about/ui/AboutBuild.js.map +1 -1
  14. package/lib/about/ui/AboutPage.d.ts +2 -2
  15. package/lib/about/ui/AboutPage.d.ts.map +1 -1
  16. package/lib/about/ui/AboutPage.js +3 -14
  17. package/lib/about/ui/AboutPage.js.map +1 -1
  18. package/lib/about/ui/AboutVersion.d.ts +0 -4
  19. package/lib/about/ui/AboutVersion.d.ts.map +1 -1
  20. package/lib/about/ui/AboutVersion.js +3 -31
  21. package/lib/about/ui/AboutVersion.js.map +1 -1
  22. package/lib/axis/core/axisMarks.d.ts +54 -0
  23. package/lib/axis/core/axisMarks.d.ts.map +1 -0
  24. package/lib/axis/core/axisMarks.js +69 -0
  25. package/lib/axis/core/axisMarks.js.map +1 -0
  26. package/lib/axis/core/axisScale.d.ts +31 -0
  27. package/lib/axis/core/axisScale.d.ts.map +1 -0
  28. package/lib/axis/core/axisScale.js +31 -0
  29. package/lib/axis/core/axisScale.js.map +1 -0
  30. package/lib/axis/core/index.d.ts +16 -0
  31. package/lib/axis/core/index.d.ts.map +1 -0
  32. package/lib/axis/core/index.js +13 -0
  33. package/lib/axis/core/index.js.map +1 -0
  34. package/lib/axis/core/logAxisBounds.d.ts +78 -0
  35. package/lib/axis/core/logAxisBounds.d.ts.map +1 -0
  36. package/lib/axis/core/logAxisBounds.js +129 -0
  37. package/lib/axis/core/logAxisBounds.js.map +1 -0
  38. package/lib/axis/ui/AxisScaleControl.d.ts +66 -0
  39. package/lib/axis/ui/AxisScaleControl.d.ts.map +1 -0
  40. package/lib/axis/ui/AxisScaleControl.js +26 -0
  41. package/lib/axis/ui/AxisScaleControl.js.map +1 -0
  42. package/lib/axis/ui/index.d.ts +6 -0
  43. package/lib/axis/ui/index.d.ts.map +1 -0
  44. package/lib/axis/ui/index.js +5 -0
  45. package/lib/axis/ui/index.js.map +1 -0
  46. package/lib/core.d.ts +1 -0
  47. package/lib/core.d.ts.map +1 -1
  48. package/lib/core.js +1 -0
  49. package/lib/core.js.map +1 -1
  50. package/lib/delimited/core/index.d.ts +2 -0
  51. package/lib/delimited/core/index.d.ts.map +1 -1
  52. package/lib/delimited/core/index.js +1 -0
  53. package/lib/delimited/core/index.js.map +1 -1
  54. package/lib/delimited/core/tableRows.d.ts +49 -0
  55. package/lib/delimited/core/tableRows.d.ts.map +1 -0
  56. package/lib/delimited/core/tableRows.js +63 -0
  57. package/lib/delimited/core/tableRows.js.map +1 -0
  58. package/lib/delimited/ui/DelimitedTextPanel.d.ts +9 -0
  59. package/lib/delimited/ui/DelimitedTextPanel.d.ts.map +1 -1
  60. package/lib/delimited/ui/DelimitedTextPanel.js +3 -2
  61. package/lib/delimited/ui/DelimitedTextPanel.js.map +1 -1
  62. package/lib/delimited/ui/TableDataButton.d.ts +96 -0
  63. package/lib/delimited/ui/TableDataButton.d.ts.map +1 -0
  64. package/lib/delimited/ui/TableDataButton.js +39 -0
  65. package/lib/delimited/ui/TableDataButton.js.map +1 -0
  66. package/lib/delimited/ui/index.d.ts +2 -0
  67. package/lib/delimited/ui/index.d.ts.map +1 -1
  68. package/lib/delimited/ui/index.js +1 -0
  69. package/lib/delimited/ui/index.js.map +1 -1
  70. package/lib/download/core/index.d.ts +0 -5
  71. package/lib/download/core/index.d.ts.map +1 -1
  72. package/lib/download/core/index.js +0 -3
  73. package/lib/download/core/index.js.map +1 -1
  74. package/lib/download/ui/FigureDownload.d.ts +4 -55
  75. package/lib/download/ui/FigureDownload.d.ts.map +1 -1
  76. package/lib/download/ui/FigureDownload.js +6 -66
  77. package/lib/download/ui/FigureDownload.js.map +1 -1
  78. package/lib/download/ui/FigureDownloadPanel.d.ts +16 -2
  79. package/lib/download/ui/FigureDownloadPanel.d.ts.map +1 -1
  80. package/lib/download/ui/FigureDownloadPanel.js +11 -30
  81. package/lib/download/ui/FigureDownloadPanel.js.map +1 -1
  82. package/lib/download/ui/figureDownloadHint.d.ts +30 -0
  83. package/lib/download/ui/figureDownloadHint.d.ts.map +1 -0
  84. package/lib/download/ui/figureDownloadHint.js +33 -0
  85. package/lib/download/ui/figureDownloadHint.js.map +1 -0
  86. package/lib/download/ui/index.d.ts +3 -10
  87. package/lib/download/ui/index.d.ts.map +1 -1
  88. package/lib/download/ui/index.js +1 -5
  89. package/lib/download/ui/index.js.map +1 -1
  90. package/lib/download/ui/useFigureActions.d.ts +50 -0
  91. package/lib/download/ui/useFigureActions.d.ts.map +1 -0
  92. package/lib/download/ui/useFigureActions.js +58 -0
  93. package/lib/download/ui/useFigureActions.js.map +1 -0
  94. package/lib/download/ui/useFigureDownload.d.ts +85 -0
  95. package/lib/download/ui/useFigureDownload.d.ts.map +1 -0
  96. package/lib/download/ui/useFigureDownload.js +75 -0
  97. package/lib/download/ui/useFigureDownload.js.map +1 -0
  98. package/lib/ecosystem/core/sites.d.ts.map +1 -1
  99. package/lib/ecosystem/core/sites.js +0 -40
  100. package/lib/ecosystem/core/sites.js.map +1 -1
  101. package/lib/ecosystem/core/types.d.ts +0 -10
  102. package/lib/ecosystem/core/types.d.ts.map +1 -1
  103. package/lib/i18n/core/chromeCatalog.d.ts +1 -1
  104. package/lib/i18n/core/chromeCatalog.d.ts.map +1 -1
  105. package/lib/ir/core/irCommands.d.ts +2 -2
  106. package/lib/ir/core/irCommands.js +2 -2
  107. package/lib/ir/core/irCommands.js.map +1 -1
  108. package/lib/ir/ui/IrAboutDialog.d.ts.map +1 -1
  109. package/lib/ir/ui/IrAboutDialog.js +1 -1
  110. package/lib/ir/ui/IrAboutDialog.js.map +1 -1
  111. package/lib/ir/ui/IrCanvas.d.ts +6 -0
  112. package/lib/ir/ui/IrCanvas.d.ts.map +1 -1
  113. package/lib/ir/ui/IrCanvas.js +2 -2
  114. package/lib/ir/ui/IrCanvas.js.map +1 -1
  115. package/lib/ir/ui/IrShell.d.ts.map +1 -1
  116. package/lib/ir/ui/IrShell.js +16 -11
  117. package/lib/ir/ui/IrShell.js.map +1 -1
  118. package/lib/ir/ui/IrToolbar.d.ts +11 -1
  119. package/lib/ir/ui/IrToolbar.d.ts.map +1 -1
  120. package/lib/ir/ui/IrToolbar.js +12 -2
  121. package/lib/ir/ui/IrToolbar.js.map +1 -1
  122. package/lib/ir/ui/index.d.ts +1 -0
  123. package/lib/ir/ui/index.d.ts.map +1 -1
  124. package/lib/ir/ui/index.js.map +1 -1
  125. package/lib/locales/de.json +8 -4
  126. package/lib/locales/en.json +8 -4
  127. package/lib/locales/es.json +8 -4
  128. package/lib/locales/fr.json +8 -4
  129. package/lib/locales/it.json +8 -4
  130. package/lib/overlay/ui/OverlayPanel.d.ts +6 -0
  131. package/lib/overlay/ui/OverlayPanel.d.ts.map +1 -1
  132. package/lib/overlay/ui/OverlayPanel.js +2 -2
  133. package/lib/overlay/ui/OverlayPanel.js.map +1 -1
  134. package/lib/overlay/ui/overlayPanelStyles.d.ts +2 -1
  135. package/lib/overlay/ui/overlayPanelStyles.d.ts.map +1 -1
  136. package/lib/overlay/ui/overlayPanelStyles.js +3 -1
  137. package/lib/overlay/ui/overlayPanelStyles.js.map +1 -1
  138. package/lib/periodic/core/index.d.ts +1 -1
  139. package/lib/periodic/core/index.d.ts.map +1 -1
  140. package/lib/periodic/core/index.js +1 -1
  141. package/lib/periodic/core/index.js.map +1 -1
  142. package/lib/periodic/core/layout.d.ts +18 -0
  143. package/lib/periodic/core/layout.d.ts.map +1 -1
  144. package/lib/periodic/core/layout.js +9 -0
  145. package/lib/periodic/core/layout.js.map +1 -1
  146. package/lib/periodic/ui/ElementCell.d.ts +0 -6
  147. package/lib/periodic/ui/ElementCell.d.ts.map +1 -1
  148. package/lib/periodic/ui/ElementCell.js +35 -19
  149. package/lib/periodic/ui/ElementCell.js.map +1 -1
  150. package/lib/periodic/ui/PeriodicTable.d.ts +12 -1
  151. package/lib/periodic/ui/PeriodicTable.d.ts.map +1 -1
  152. package/lib/periodic/ui/PeriodicTable.js +34 -15
  153. package/lib/periodic/ui/PeriodicTable.js.map +1 -1
  154. package/lib/ui.d.ts +1 -0
  155. package/lib/ui.d.ts.map +1 -1
  156. package/lib/ui.js +1 -0
  157. package/lib/ui.js.map +1 -1
  158. package/package.json +1 -1
  159. package/src/about/core/about.ts +1 -51
  160. package/src/about/core/index.ts +0 -1
  161. package/src/about/ui/AboutBuild.tsx +8 -37
  162. package/src/about/ui/AboutPage.tsx +8 -64
  163. package/src/about/ui/AboutVersion.tsx +5 -45
  164. package/src/axis/core/axisMarks.ts +114 -0
  165. package/src/axis/core/axisScale.ts +41 -0
  166. package/src/axis/core/index.ts +20 -0
  167. package/src/axis/core/logAxisBounds.ts +198 -0
  168. package/src/axis/ui/AxisScaleControl.tsx +103 -0
  169. package/src/axis/ui/index.ts +9 -0
  170. package/src/core.ts +1 -0
  171. package/src/delimited/core/index.ts +2 -0
  172. package/src/delimited/core/tableRows.ts +93 -0
  173. package/src/delimited/ui/DelimitedTextPanel.tsx +25 -9
  174. package/src/delimited/ui/TableDataButton.tsx +162 -0
  175. package/src/delimited/ui/index.ts +2 -0
  176. package/src/download/core/index.ts +0 -25
  177. package/src/download/ui/FigureDownload.tsx +11 -148
  178. package/src/download/ui/FigureDownloadPanel.tsx +46 -45
  179. package/src/download/ui/figureDownloadHint.ts +52 -0
  180. package/src/download/ui/index.ts +6 -10
  181. package/src/download/ui/useFigureActions.ts +113 -0
  182. package/src/download/ui/useFigureDownload.tsx +191 -0
  183. package/src/ecosystem/core/sites.ts +0 -40
  184. package/src/ecosystem/core/types.ts +0 -10
  185. package/src/ir/core/irCommands.ts +2 -2
  186. package/src/ir/ui/IrAboutDialog.tsx +0 -13
  187. package/src/ir/ui/IrCanvas.tsx +8 -2
  188. package/src/ir/ui/IrShell.tsx +23 -25
  189. package/src/ir/ui/IrToolbar.tsx +36 -6
  190. package/src/ir/ui/index.ts +1 -0
  191. package/src/locales/de.json +8 -4
  192. package/src/locales/en.json +8 -4
  193. package/src/locales/es.json +8 -4
  194. package/src/locales/fr.json +8 -4
  195. package/src/locales/it.json +8 -4
  196. package/src/overlay/ui/OverlayPanel.tsx +8 -2
  197. package/src/overlay/ui/overlayPanelStyles.ts +3 -0
  198. package/src/periodic/core/index.ts +1 -0
  199. package/src/periodic/core/layout.ts +19 -0
  200. package/src/periodic/ui/ElementCell.tsx +35 -26
  201. package/src/periodic/ui/PeriodicTable.tsx +55 -16
  202. package/src/ui.ts +1 -0
  203. package/styles/chrome.css +71 -0
  204. package/lib/about/core/repository.d.ts +0 -14
  205. package/lib/about/core/repository.d.ts.map +0 -1
  206. package/lib/about/core/repository.js +0 -21
  207. package/lib/about/core/repository.js.map +0 -1
  208. package/lib/download/core/exportStyles.d.ts +0 -22
  209. package/lib/download/core/exportStyles.d.ts.map +0 -1
  210. package/lib/download/core/exportStyles.js +0 -19
  211. package/lib/download/core/exportStyles.js.map +0 -1
  212. package/lib/download/core/imageResolution.d.ts +0 -88
  213. package/lib/download/core/imageResolution.d.ts.map +0 -1
  214. package/lib/download/core/imageResolution.js +0 -77
  215. package/lib/download/core/imageResolution.js.map +0 -1
  216. package/lib/download/core/pictureExport.d.ts +0 -93
  217. package/lib/download/core/pictureExport.d.ts.map +0 -1
  218. package/lib/download/core/pictureExport.js +0 -116
  219. package/lib/download/core/pictureExport.js.map +0 -1
  220. package/lib/download/ui/ExportDialogFrame.d.ts +0 -46
  221. package/lib/download/ui/ExportDialogFrame.d.ts.map +0 -1
  222. package/lib/download/ui/ExportDialogFrame.js +0 -24
  223. package/lib/download/ui/ExportDialogFrame.js.map +0 -1
  224. package/lib/download/ui/ExportImageDialog.d.ts +0 -47
  225. package/lib/download/ui/ExportImageDialog.d.ts.map +0 -1
  226. package/lib/download/ui/ExportImageDialog.js +0 -25
  227. package/lib/download/ui/ExportImageDialog.js.map +0 -1
  228. package/lib/download/ui/ExportNameField.d.ts +0 -29
  229. package/lib/download/ui/ExportNameField.d.ts.map +0 -1
  230. package/lib/download/ui/ExportNameField.js +0 -99
  231. package/lib/download/ui/ExportNameField.js.map +0 -1
  232. package/lib/download/ui/ExportPictureSection.d.ts +0 -38
  233. package/lib/download/ui/ExportPictureSection.d.ts.map +0 -1
  234. package/lib/download/ui/ExportPictureSection.js +0 -48
  235. package/lib/download/ui/ExportPictureSection.js.map +0 -1
  236. package/lib/download/ui/useDrawingBox.d.ts +0 -23
  237. package/lib/download/ui/useDrawingBox.d.ts.map +0 -1
  238. package/lib/download/ui/useDrawingBox.js +0 -21
  239. package/lib/download/ui/useDrawingBox.js.map +0 -1
  240. package/src/about/core/repository.ts +0 -22
  241. package/src/download/core/exportStyles.ts +0 -20
  242. package/src/download/core/imageResolution.ts +0 -118
  243. package/src/download/core/pictureExport.ts +0 -207
  244. package/src/download/ui/ExportDialogFrame.tsx +0 -86
  245. package/src/download/ui/ExportImageDialog.tsx +0 -91
  246. package/src/download/ui/ExportNameField.tsx +0 -148
  247. package/src/download/ui/ExportPictureSection.tsx +0 -142
  248. package/src/download/ui/useDrawingBox.ts +0 -37
@@ -14,17 +14,11 @@ import {
14
14
  buildLabel,
15
15
  buildStamp,
16
16
  buildSummary,
17
- releasedVersion,
18
17
  } from '../../build/core/buildInfo.ts';
19
- import { githubSources } from '../core/repository.ts';
20
18
 
21
19
  export interface AboutVersionProps {
22
20
  /** What the build published about itself, or `undefined` when it published none. */
23
21
  build: BuildInfo | undefined;
24
- /** Where the sources live, which is what the badge links into. */
25
- repository: string;
26
- /** Whether a visitor can open that repository, and so whether it is linked. */
27
- publicRepository: boolean;
28
22
  }
29
23
 
30
24
  /**
@@ -34,53 +28,24 @@ export interface AboutVersionProps {
34
28
  * @returns The badge, or `null` when the build says nothing worth showing.
35
29
  */
36
30
  export function AboutVersion(props: AboutVersionProps): ReactElement | null {
37
- const { build, repository, publicRepository } = props;
31
+ const { build } = props;
38
32
  const label = buildLabel(build);
39
33
  if (build === undefined || label === undefined) return null;
40
- const href = publicRepository ? addressOf(build, repository) : undefined;
41
- const summary = buildSummary(build);
42
34
  // The instant belongs on the badge rather than in its hover: a version
43
35
  // nobody can date says nothing about how old the page they read is.
44
36
  const badge = `${label} · ${buildStamp(build.builtAt)}`;
45
- if (href === undefined) {
46
- return (
47
- <span style={BADGE_STYLE} className="about-version" title={summary}>
48
- {badge}
49
- </span>
50
- );
51
- }
52
37
 
53
38
  return (
54
- <a
39
+ <span
40
+ style={BADGE_STYLE}
55
41
  className="about-version"
56
- href={href}
57
- title={summary}
58
- target="_blank"
59
- rel="noopener noreferrer"
60
- style={LINK_BADGE_STYLE}
42
+ title={buildSummary(build)}
61
43
  >
62
44
  {badge}
63
- </a>
45
+ </span>
64
46
  );
65
47
  }
66
48
 
67
- /**
68
- * What the badge opens: the release it names, or the commit it was built from.
69
- * @param build - What the build published about itself.
70
- * @param repository - Where the sources live.
71
- * @returns The address, or `undefined` when nothing inside the repository can
72
- * be addressed.
73
- */
74
- function addressOf(build: BuildInfo, repository: string): string | undefined {
75
- const sources = githubSources(repository);
76
- if (sources === undefined) return undefined;
77
- const version = releasedVersion(build);
78
- if (version !== undefined) return `${sources}/releases/tag/v${version}`;
79
- return build.commit === undefined
80
- ? undefined
81
- : `${sources}/commit/${build.commit}`;
82
- }
83
-
84
49
  const BADGE_STYLE = {
85
50
  flex: '0 0 auto',
86
51
  fontFamily: 'var(--font-mono, ui-monospace, monospace)',
@@ -88,8 +53,3 @@ const BADGE_STYLE = {
88
53
  color: 'var(--text-muted)',
89
54
  whiteSpace: 'nowrap',
90
55
  } as const satisfies CSSProperties;
91
-
92
- const LINK_BADGE_STYLE = {
93
- ...BADGE_STYLE,
94
- color: 'var(--accent)',
95
- } as const satisfies CSSProperties;
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Where a figure's marks land inside its box, so something else can count them.
3
+ *
4
+ * `emptiestCorner` picks the corner a floating card can sit in without covering
5
+ * the cluster the reader came for, and it needs screen positions to do it. A
6
+ * chart drawn by a library builds its scales during its own render, where a
7
+ * caller cannot reach them, so the positions are worked out here from the same
8
+ * values and the same two transforms the axes use.
9
+ *
10
+ * It is an estimate, which is all a corner needs: the plot area is inset from
11
+ * the box by the margins, and a mark a few pixels from a corner counts for that
12
+ * corner either way.
13
+ */
14
+
15
+ import type { AxisScale } from './axisScale.ts';
16
+
17
+ /** A point of the figure, in the quantities its axes carry. */
18
+ export interface AxisPoint {
19
+ x: number;
20
+ y: number;
21
+ }
22
+
23
+ /** Pixel positions of every mark, as `emptiestCorner` reads them. */
24
+ export interface AxisMarkPositions {
25
+ x: Float64Array;
26
+ y: Float64Array;
27
+ }
28
+
29
+ /** What {@link axisMarkPositions} needs beyond the points and the box. */
30
+ export interface AxisMarkPositionsOptions {
31
+ /**
32
+ * Width of the linear region of a logarithmic axis — d3's symmetric-log
33
+ * `constant`. Pass `0` for a true logarithmic axis.
34
+ * @default 1
35
+ */
36
+ constant?: number;
37
+ }
38
+
39
+ /**
40
+ * Place every point in the box the figure is drawn in.
41
+ * @param points - The points the figure draws.
42
+ * @param box - Its size, in pixels, normally from `useContainerSize`.
43
+ * @param box.width - How wide the figure is drawn.
44
+ * @param box.height - How tall it is drawn.
45
+ * @param scale - The scale each axis is drawn on, which the positions have to
46
+ * follow to land where the marks do.
47
+ * @param scale.y - How the vertical axis is read.
48
+ * @param scale.x - How the horizontal axis is read.
49
+ * @param options - See {@link AxisMarkPositionsOptions}.
50
+ * @returns One position per point, in the order they were given.
51
+ */
52
+ export function axisMarkPositions(
53
+ points: readonly AxisPoint[],
54
+ box: { width: number; height: number },
55
+ scale: { x: AxisScale; y: AxisScale },
56
+ options: AxisMarkPositionsOptions = {},
57
+ ): AxisMarkPositions {
58
+ const { constant = 1 } = options;
59
+ const x = new Float64Array(points.length);
60
+ const y = new Float64Array(points.length);
61
+
62
+ place(points, 'x', scale.x === 'log', constant, box.width, x);
63
+ place(points, 'y', scale.y === 'log', constant, box.height, y);
64
+ // The vertical axis grows upwards while the box is measured from its top.
65
+ for (let index = 0; index < y.length; index++) {
66
+ y[index] = box.height - (y[index] as number);
67
+ }
68
+ return { x, y };
69
+ }
70
+
71
+ function place(
72
+ points: readonly AxisPoint[],
73
+ axis: 'x' | 'y',
74
+ logarithmic: boolean,
75
+ constant: number,
76
+ extent: number,
77
+ into: Float64Array,
78
+ ): void {
79
+ let min = Infinity;
80
+ let max = -Infinity;
81
+ for (let index = 0; index < points.length; index++) {
82
+ const value = transform(
83
+ (points[index] as AxisPoint)[axis],
84
+ logarithmic,
85
+ constant,
86
+ );
87
+ into[index] = value;
88
+ if (value < min) min = value;
89
+ if (value > max) max = value;
90
+ }
91
+
92
+ const span = max - min;
93
+ for (let index = 0; index < into.length; index++) {
94
+ const value = (into[index] as number) - min;
95
+ into[index] = span === 0 ? extent / 2 : (value / span) * extent;
96
+ }
97
+ }
98
+
99
+ /**
100
+ * d3's symmetric log, which is the one a genuine zero survives.
101
+ * @param value - The value to place.
102
+ * @param logarithmic - Whether the axis is read logarithmically at all.
103
+ * @param constant - Width of its linear region.
104
+ * @returns The value in the units the axis is spaced in.
105
+ */
106
+ function transform(
107
+ value: number,
108
+ logarithmic: boolean,
109
+ constant: number,
110
+ ): number {
111
+ if (!logarithmic) return value;
112
+ const width = constant > 0 ? constant : 1;
113
+ return Math.sign(value) * Math.log1p(Math.abs(value) / width);
114
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Which of the two scales a quantity is read on, and who decided.
3
+ *
4
+ * Every quantity has a scale it is usually read on — a concentration spanning
5
+ * ten decades is read logarithmically, a first ionisation energy linearly — and
6
+ * a reader may overrule it. The two are kept apart rather than collapsed into
7
+ * one boolean, because a figure that forgets which it is holding gets both of
8
+ * the following wrong: picking another quantity has to bring that quantity's
9
+ * own scale rather than carry the last one over, and a link has to stay silent
10
+ * about a scale nobody chose, so that it keeps opening the way the quantity is
11
+ * read even after the usual scale is reconsidered.
12
+ */
13
+
14
+ /** The two scales an axis is drawn on. */
15
+ export type AxisScale = 'linear' | 'log';
16
+
17
+ /** A reader's choice, or `null` while they have not made one. */
18
+ export type AxisScaleChoice = AxisScale | null;
19
+
20
+ /**
21
+ * The scale an axis is actually drawn on.
22
+ * @param usual - How the quantity on the axis is normally read.
23
+ * @param chosen - What the reader asked for, or `null` to leave it to the
24
+ * quantity.
25
+ * @returns The scale to draw.
26
+ */
27
+ export function resolveAxisScale(
28
+ usual: AxisScale,
29
+ chosen: AxisScaleChoice,
30
+ ): AxisScale {
31
+ return chosen ?? usual;
32
+ }
33
+
34
+ /**
35
+ * Narrow an arbitrary string — one off a query parameter — to a scale.
36
+ * @param value - Candidate scale.
37
+ * @returns True when it is one of the two.
38
+ */
39
+ export function isAxisScale(value: string): value is AxisScale {
40
+ return value === 'linear' || value === 'log';
41
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * How an axis is read: linearly or logarithmically, who decided, where it
3
+ * starts and ends, and where its marks land.
4
+ *
5
+ * It is a domain of its own rather than part of any chart, because the library
6
+ * draws figures with three different renderers and the sites draw more with
7
+ * libraries of their own — and the two things that go wrong on a logarithmic
8
+ * axis go wrong the same way in all of them.
9
+ */
10
+
11
+ export type {
12
+ AxisMarkPositions,
13
+ AxisMarkPositionsOptions,
14
+ AxisPoint,
15
+ } from './axisMarks.ts';
16
+ export { axisMarkPositions } from './axisMarks.ts';
17
+ export type { AxisScale, AxisScaleChoice } from './axisScale.ts';
18
+ export { isAxisScale, resolveAxisScale } from './axisScale.ts';
19
+ export type { LogAxisBounds, LogAxisBoundsOptions } from './logAxisBounds.ts';
20
+ export { logAxisBounds } from './logAxisBounds.ts';
@@ -0,0 +1,198 @@
1
+ /**
2
+ * The ends of a logarithmic axis, and the values it is graduated at.
3
+ *
4
+ * Both have to be stated rather than left to the charting library, and for two
5
+ * separate reasons.
6
+ *
7
+ * The ends, because a library that finds them itself may not look at every
8
+ * value. nivo is the measured case: it sorts and takes a true minimum and
9
+ * maximum only for a linear axis, and on a log or symmetric-log one `'auto'`
10
+ * resolves to the first value the data carries and the last — so the domain
11
+ * comes out in plotting order, upside down whenever the first point outweighs
12
+ * the last, and cutting off everything past the last point drawn.
13
+ *
14
+ * The graduations, because d3 rules a symmetric-log axis at evenly *spaced*
15
+ * values while placing them logarithmically, so the top of a wide axis collects
16
+ * every graduation it has into one unreadable line. Ruling it by decade spreads
17
+ * them evenly, which is also how such a quantity is read.
18
+ */
19
+
20
+ /** What {@link logAxisBounds} answers. */
21
+ export interface LogAxisBounds {
22
+ /** The low end of the axis. */
23
+ min: number;
24
+ /** The high end. */
25
+ max: number;
26
+ /**
27
+ * The values to rule it at, or `undefined` for an axis spanning too few
28
+ * decades to be worth ruling by them — there the library's own linear
29
+ * graduations read better, and there is no pile-up to undo.
30
+ */
31
+ tickValues?: number[];
32
+ }
33
+
34
+ /** What {@link logAxisBounds} needs beyond the values. */
35
+ export interface LogAxisBoundsOptions {
36
+ /**
37
+ * Width of the linear region at the bottom of a symmetric-log axis — d3's
38
+ * `constant`. Everything smaller than it is placed almost on the zero line,
39
+ * so no graduation is written below it; a graduation there would land on top
40
+ * of the one before it, which is the pile-up this exists to prevent.
41
+ *
42
+ * Pass `0` for a true logarithmic axis, which has no such region — and which
43
+ * cannot place a zero or a negative at all, so those values are then left out
44
+ * of the ends rather than dragging them to nothing.
45
+ * @default 1
46
+ */
47
+ constant?: number;
48
+ /**
49
+ * Whether the ends are rounded outwards to whole powers of ten. An axis that
50
+ * ends on its own extreme values reads as though those were round numbers;
51
+ * one that ends on decades says what it is. Worth it on a true logarithmic
52
+ * axis, where every graduation is a decade anyway.
53
+ * @default false — the ends are the data's own
54
+ */
55
+ snapToDecades?: boolean;
56
+ /**
57
+ * How many decades the axis may span at most, counted down from its top. A
58
+ * quantity that reaches zero asymptotically has no meaningful bottom, and an
59
+ * axis given one spends most of its height on values nobody is reading.
60
+ * Only consulted with `snapToDecades`.
61
+ * @default Infinity — the axis spans whatever the data does
62
+ */
63
+ maxDecades?: number;
64
+ /**
65
+ * How many graduations the axis may carry. More than this is a smear however
66
+ * they are spread, so the decades are stepped over rather than written out.
67
+ * @default 10
68
+ */
69
+ mostTicks?: number;
70
+ }
71
+
72
+ /** Under this many decades the library's own graduations read better. */
73
+ const RULE_PAST_DECADES = 3;
74
+
75
+ /**
76
+ * Where a logarithmic axis starts and ends, and where it is graduated.
77
+ * @param values - Every value plotted on the axis. Anything not finite is
78
+ * ignored, so a sheet's gaps need not be filtered out first.
79
+ * @param options - See {@link LogAxisBoundsOptions}.
80
+ * @returns The ends and the graduations, or `null` when no value can be drawn
81
+ * and the caller should leave the axis to its library.
82
+ */
83
+ export function logAxisBounds(
84
+ values: Iterable<number>,
85
+ options: LogAxisBoundsOptions = {},
86
+ ): LogAxisBounds | null {
87
+ const { constant = 1, snapToDecades = false } = options;
88
+ const { maxDecades = Infinity, mostTicks = 10 } = options;
89
+ // A true logarithmic axis has no room for a zero or a negative, so they are
90
+ // not what its bottom end is taken from.
91
+ const positiveOnly = constant === 0;
92
+
93
+ let min = Infinity;
94
+ let max = -Infinity;
95
+ for (const value of values) {
96
+ if (!Number.isFinite(value)) continue;
97
+ if (positiveOnly && value <= 0) continue;
98
+ if (value < min) min = value;
99
+ if (value > max) max = value;
100
+ }
101
+ if (min > max) return null;
102
+
103
+ if (snapToDecades) {
104
+ const top = Math.ceil(Math.log10(Math.abs(max) || 1));
105
+ const bottom = Math.max(
106
+ Math.floor(Math.log10(Math.abs(min) || 1)),
107
+ top - maxDecades,
108
+ );
109
+ return withTicks(decade(bottom), decade(top), constant, mostTicks);
110
+ }
111
+
112
+ // A lone value, or a column of identical ones, spans nothing: reach to the
113
+ // origin so it is read against a zero rather than filling the axis.
114
+ if (min === max) {
115
+ if (min === 0) return { min: -1, max: 1 };
116
+ return { min: Math.min(min, 0), max: Math.max(max, 0) };
117
+ }
118
+
119
+ return withTicks(min, max, constant, mostTicks);
120
+ }
121
+
122
+ /**
123
+ * The ends, with the graduations when they are worth writing.
124
+ * @param min
125
+ * @param max
126
+ * @param constant
127
+ * @param mostTicks
128
+ */
129
+ function withTicks(
130
+ min: number,
131
+ max: number,
132
+ constant: number,
133
+ mostTicks: number,
134
+ ): LogAxisBounds {
135
+ const ticks = decadesBetween(min, max, constant, mostTicks);
136
+ return ticks === undefined ? { min, max } : { min, max, tickValues: ticks };
137
+ }
138
+
139
+ /**
140
+ * Zero, and the powers of ten the axis both reaches and can separate.
141
+ * @param min - The low end of the axis.
142
+ * @param max - Its high end.
143
+ * @param constant - Width of the linear region; see the options.
144
+ * @param mostTicks - How many graduations the axis may carry.
145
+ * @returns The values to rule it at, or nothing to leave it to the library.
146
+ */
147
+ function decadesBetween(
148
+ min: number,
149
+ max: number,
150
+ constant: number,
151
+ mostTicks: number,
152
+ ): number[] | undefined {
153
+ const largest = Math.max(Math.abs(min), Math.abs(max));
154
+ if (largest === 0) return undefined;
155
+
156
+ // Below the linear region every decade lands on the same pixel, so the axis
157
+ // is ruled from there up rather than from its smallest value.
158
+ const floor = constant > 0 ? Math.log10(constant) : smallestDecade(min, max);
159
+ const bottom = Math.ceil(floor);
160
+ const top = Math.floor(Math.log10(largest));
161
+ // Under a few decades the library's own graduations read better — but only
162
+ // where it has linear ones to offer. On a true logarithmic axis every
163
+ // graduation is a decade, so a short one is still ruled by decade.
164
+ if (constant > 0 && top - bottom + 1 < RULE_PAST_DECADES) return undefined;
165
+
166
+ const step = Math.ceil((top - bottom + 1) / mostTicks);
167
+ const ticks = min <= 0 && max >= 0 ? [0] : [];
168
+ for (let power = bottom; power <= top; power += step) {
169
+ const tick = decade(power);
170
+ if (tick >= min && tick <= max) ticks.push(tick);
171
+ }
172
+ if (constant === 0) return ticks;
173
+ return ticks.length < RULE_PAST_DECADES ? undefined : ticks;
174
+ }
175
+
176
+ /**
177
+ * A whole power of ten.
178
+ * @param power - The exponent, a whole number.
179
+ * @returns Ten to that power, the same bits on every engine.
180
+ */
181
+ function decade(power: number): number {
182
+ // Read as a decimal rather than computed: `10 ** power` goes through pow,
183
+ // which is not bit-exact across V8 versions — node 22 answers
184
+ // 0.00009999999999999999 for -4 and 1.0000000000000001e-20 for -20 — while a
185
+ // decimal literal is the correctly-rounded double everywhere.
186
+ return Number(`1e${power}`);
187
+ }
188
+
189
+ /**
190
+ * The decade of the smaller end, for an axis with no linear region.
191
+ * @param min - The low end of the axis.
192
+ * @param max - Its high end.
193
+ * @returns The base-ten logarithm of the smaller end that is not zero.
194
+ */
195
+ function smallestDecade(min: number, max: number): number {
196
+ const ends = [Math.abs(min), Math.abs(max)].filter((end) => end > 0);
197
+ return Math.log10(Math.min(...ends));
198
+ }
@@ -0,0 +1,103 @@
1
+ /**
2
+ * The control that moves one axis between its two scales.
3
+ *
4
+ * It belongs on the figure rather than in the page around it: the scale is not
5
+ * a property of the page but of the picture, a reader decides to change it
6
+ * while looking at the picture, and a figure handed out on its own — embedded
7
+ * in a course page, pasted into a deck — takes its controls with it. So it is
8
+ * written as a control of an `OverlayBar`, floating in whichever corner the
9
+ * marks leave emptiest.
10
+ *
11
+ * Its two segments are named here rather than by each site, because `Linear`
12
+ * and `Log` are chrome: one translation reaches every figure in the family.
13
+ */
14
+
15
+ import type { ReactElement } from 'react';
16
+
17
+ import type { HelpContent } from '../../help/ui/HelpBody.tsx';
18
+ import { useChromeT } from '../../i18n/ui/useT.ts';
19
+ import type { OverlayOption } from '../../overlay/ui/OverlayRow.tsx';
20
+ import { OverlaySegmented } from '../../overlay/ui/OverlaySegmented.tsx';
21
+ import type { AxisScale } from '../core/axisScale.ts';
22
+
23
+ /** Which axis a control belongs to, which is how it names itself. */
24
+ export type AxisScaleAxis = 'x' | 'y';
25
+
26
+ /** What {@link AxisScaleControl} needs. */
27
+ export interface AxisScaleControlProps {
28
+ /** The scale in force — what `resolveAxisScale` answered, not the choice. */
29
+ value: AxisScale;
30
+ /** Called with the scale the reader picked. */
31
+ onChange: (scale: AxisScale) => void;
32
+ /**
33
+ * Which axis it sets, which names it `X scale` or `Y scale`. Leave it out on
34
+ * a figure where only one axis offers the choice: the control is then simply
35
+ * `Scale`, because naming the axis is only worth the width when there are two
36
+ * of them to tell apart.
37
+ * @default undefined — the control is named `Scale`
38
+ */
39
+ axis?: AxisScaleAxis;
40
+ /**
41
+ * What it is called, overriding both of the above. For a figure whose axis
42
+ * has a name the reader already knows it by.
43
+ * @default the chrome's own word, in the language of the page
44
+ */
45
+ label?: string;
46
+ /**
47
+ * What the two scales do, in a sentence.
48
+ * @default the chrome's own explanation
49
+ */
50
+ help?: HelpContent;
51
+ /**
52
+ * Whether the name is announced but not written.
53
+ * @default false
54
+ */
55
+ hideLabel?: boolean;
56
+ /**
57
+ * Whether the control is greyed and unreachable.
58
+ * @default false
59
+ */
60
+ disabled?: boolean;
61
+ /**
62
+ * Value of the `data-testid` attribute.
63
+ * @default undefined
64
+ */
65
+ testId?: string;
66
+ }
67
+
68
+ /**
69
+ * The two scales, the one in force pressed.
70
+ * @param props - See {@link AxisScaleControlProps}.
71
+ * @returns The named pair of segments.
72
+ */
73
+ export function AxisScaleControl(props: AxisScaleControlProps): ReactElement {
74
+ const { value, onChange, axis, label, help, testId } = props;
75
+ const { hideLabel = false, disabled = false } = props;
76
+ const t = useChromeT();
77
+
78
+ const options: ReadonlyArray<OverlayOption<AxisScale>> = [
79
+ { value: 'linear', label: t('axis.linear') },
80
+ { value: 'log', label: t('axis.logarithmic') },
81
+ ];
82
+
83
+ return (
84
+ <OverlaySegmented<AxisScale>
85
+ label={label ?? t(nameOf(axis))}
86
+ help={help ?? { title: t('axis.scale'), body: t('axis.scaleHelp') }}
87
+ hideLabel={hideLabel}
88
+ disabled={disabled}
89
+ value={value}
90
+ options={options}
91
+ onChange={onChange}
92
+ testId={testId}
93
+ />
94
+ );
95
+ }
96
+
97
+ function nameOf(
98
+ axis: AxisScaleAxis | undefined,
99
+ ): 'axis.scale' | 'axis.xScale' | 'axis.yScale' {
100
+ if (axis === 'x') return 'axis.xScale';
101
+ if (axis === 'y') return 'axis.yScale';
102
+ return 'axis.scale';
103
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * The controls that say how an axis is read.
3
+ */
4
+
5
+ export type {
6
+ AxisScaleAxis,
7
+ AxisScaleControlProps,
8
+ } from './AxisScaleControl.tsx';
9
+ export { AxisScaleControl } from './AxisScaleControl.tsx';
package/src/core.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './about/core/index.ts';
2
+ export * from './axis/core/index.ts';
2
3
  export * from './build/core/index.ts';
3
4
  export * from './chart/core/index.ts';
4
5
  export * from './citation/core/index.ts';
@@ -8,3 +8,5 @@ export type { ReadDelimitedOptions } from './readDelimited.ts';
8
8
  export { detectDelimiter, readDelimited } from './readDelimited.ts';
9
9
  export type { ToDelimitedOptions } from './toDelimited.ts';
10
10
  export { escapeCell, toDelimited } from './toDelimited.ts';
11
+ export type { TableRecord, TableRows, TableRowsOptions } from './tableRows.ts';
12
+ export { columnsOf, tableRows } from './tableRows.ts';
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Turning a list of records into the cells a table is handed over as.
3
+ *
4
+ * Most pages hold their data as one object per row — a query result, a list of
5
+ * peaks, a row of properties — while a delimited table is cells. Every site
6
+ * that exports such a list wrote the same two loops, so they are here once.
7
+ */
8
+
9
+ /** One record, as a page holds it before it becomes a row of cells. */
10
+ export type TableRecord = Record<string, unknown>;
11
+
12
+ /** How a record becomes cells. */
13
+ export interface TableRowsOptions {
14
+ /**
15
+ * The columns to write, in order. A column no record carries comes out
16
+ * empty, which is what lets one caller pin the order of a table whose first
17
+ * record happens to be missing a field.
18
+ * @default the keys of the first record, in the order it declares them
19
+ */
20
+ columns?: readonly string[];
21
+ /**
22
+ * How a value becomes a cell.
23
+ * @default `String(value)`, with `null` and `undefined` written as nothing
24
+ */
25
+ format?: (value: unknown, column: string) => string;
26
+ }
27
+
28
+ /** The columns, and the cells under them. */
29
+ export interface TableRows {
30
+ /** Column names, in the order the cells are written. */
31
+ header: string[];
32
+ /** One array of cells per record. */
33
+ rows: string[][];
34
+ }
35
+
36
+ /**
37
+ * The records as the header and cells of a table.
38
+ * @param records - The records, in the order they are shown.
39
+ * @param options - See {@link TableRowsOptions}.
40
+ * @returns See {@link TableRows}.
41
+ */
42
+ export function tableRows(
43
+ records: readonly TableRecord[],
44
+ options: TableRowsOptions = {},
45
+ ): TableRows {
46
+ const { columns, format = defaultCell } = options;
47
+ const header = [...(columns ?? columnsOf(records))];
48
+ const rows: string[][] = [];
49
+ for (const record of records) {
50
+ const cells: string[] = [];
51
+ for (const column of header) cells.push(format(record[column], column));
52
+ rows.push(cells);
53
+ }
54
+ return { header, rows };
55
+ }
56
+
57
+ /**
58
+ * Every column the records carry, each named once, in the order first seen.
59
+ *
60
+ * Every record is read rather than only the first, because a list whose later
61
+ * records carry a field the first one happens to lack would otherwise be
62
+ * handed over with that column silently missing.
63
+ * @param records - The records.
64
+ * @returns The column names.
65
+ */
66
+ export function columnsOf(records: readonly TableRecord[]): string[] {
67
+ const columns: string[] = [];
68
+ const seen = new Set<string>();
69
+ for (const record of records) {
70
+ for (const column of Object.keys(record)) {
71
+ if (seen.has(column)) continue;
72
+ seen.add(column);
73
+ columns.push(column);
74
+ }
75
+ }
76
+ return columns;
77
+ }
78
+
79
+ function defaultCell(value: unknown): string {
80
+ if (value === null || value === undefined) return '';
81
+ if (typeof value === 'string') return value;
82
+ if (
83
+ typeof value === 'number' ||
84
+ typeof value === 'bigint' ||
85
+ typeof value === 'boolean'
86
+ ) {
87
+ return String(value);
88
+ }
89
+ // Anything else left in a cell has no honest text form, and `[object Object]`
90
+ // is the one answer a spreadsheet can do nothing with. JSON at least says
91
+ // what was there; a caller who wants better passes `format`.
92
+ return JSON.stringify(value) ?? '';
93
+ }