@zsherman/charts 0.0.0-stage → 0.1.1

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 (180) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/LICENSE +373 -0
  3. package/README.md +13 -2
  4. package/THIRD_PARTY_NOTICES.md +33 -0
  5. package/dist/dom/bar-list/bar-list.js +386 -0
  6. package/dist/dom/bar-list/bar-list.styles.js +171 -0
  7. package/dist/dom/bar-list/index.js +1 -0
  8. package/dist/dom/chart/chart.js +716 -0
  9. package/dist/dom/chart/chart.styles.js +165 -0
  10. package/dist/dom/chart/index.js +1 -0
  11. package/dist/dom/chart-grid/chart-grid.js +67 -0
  12. package/dist/dom/chart-grid/index.js +1 -0
  13. package/dist/dom/donut/donut.js +565 -0
  14. package/dist/dom/donut/donut.styles.js +111 -0
  15. package/dist/dom/donut/index.js +1 -0
  16. package/dist/dom/heat-graph/calendar.js +118 -0
  17. package/dist/dom/heat-graph/heat-graph.js +423 -0
  18. package/dist/dom/heat-graph/heat-graph.styles.js +168 -0
  19. package/dist/dom/heat-graph/index.js +1 -0
  20. package/dist/dom/heat-matrix/heat-matrix.js +346 -0
  21. package/dist/dom/heat-matrix/heat-matrix.styles.js +155 -0
  22. package/dist/dom/heat-matrix/index.js +1 -0
  23. package/dist/dom/heat-matrix/model.js +41 -0
  24. package/dist/dom/index.js +13 -0
  25. package/dist/dom/internal/colors.js +44 -0
  26. package/dist/dom/internal/floating-tip.js +83 -0
  27. package/dist/dom/internal/format.js +43 -0
  28. package/dist/dom/internal/grid-context.js +5 -0
  29. package/dist/dom/internal/hidden.styles.js +16 -0
  30. package/dist/dom/internal/legend.js +112 -0
  31. package/dist/dom/internal/legend.styles.js +108 -0
  32. package/dist/dom/internal/markers.js +101 -0
  33. package/dist/dom/internal/options.js +300 -0
  34. package/dist/dom/internal/place.js +27 -0
  35. package/dist/dom/internal/scheme-watch.js +73 -0
  36. package/dist/dom/internal/share.js +28 -0
  37. package/dist/dom/internal/types.js +0 -0
  38. package/dist/dom/row-chart/index.js +1 -0
  39. package/dist/dom/row-chart/model.js +59 -0
  40. package/dist/dom/row-chart/row-chart.js +691 -0
  41. package/dist/dom/row-chart/row-chart.styles.js +242 -0
  42. package/dist/dom/segmented-bar/index.js +1 -0
  43. package/dist/dom/segmented-bar/segmented-bar.js +280 -0
  44. package/dist/dom/segmented-bar/segmented-bar.styles.js +88 -0
  45. package/dist/dom/sparkline/index.js +1 -0
  46. package/dist/dom/sparkline/sparkline.js +145 -0
  47. package/dist/dom/stacked-histogram/histogram.js +137 -0
  48. package/dist/dom/stacked-histogram/index.js +1 -0
  49. package/dist/dom/stacked-histogram/stacked-histogram.js +761 -0
  50. package/dist/dom/stacked-histogram/stacked-histogram.styles.js +211 -0
  51. package/dist/dom/state-timeline/index.js +1 -0
  52. package/dist/dom/state-timeline/model.js +166 -0
  53. package/dist/dom/state-timeline/state-timeline.js +729 -0
  54. package/dist/dom/state-timeline/state-timeline.styles.js +171 -0
  55. package/dist/dom/status-history/index.js +1 -0
  56. package/dist/dom/status-history/status-history.js +542 -0
  57. package/dist/dom/status-history/status-history.styles.js +171 -0
  58. package/dist/solid/bar-list/bar-list.jsx +139 -0
  59. package/dist/solid/bar-list/bar-list.styles.jsx +171 -0
  60. package/dist/solid/bar-list/index.jsx +1 -0
  61. package/dist/solid/chart/chart.jsx +454 -0
  62. package/dist/solid/chart/chart.styles.jsx +165 -0
  63. package/dist/solid/chart/index.jsx +1 -0
  64. package/dist/solid/chart-grid/chart-grid.jsx +46 -0
  65. package/dist/solid/chart-grid/index.jsx +1 -0
  66. package/dist/solid/donut/donut.jsx +291 -0
  67. package/dist/solid/donut/donut.styles.jsx +111 -0
  68. package/dist/solid/donut/index.jsx +1 -0
  69. package/dist/solid/heat-graph/calendar.jsx +118 -0
  70. package/dist/solid/heat-graph/heat-graph.jsx +228 -0
  71. package/dist/solid/heat-graph/heat-graph.styles.jsx +168 -0
  72. package/dist/solid/heat-graph/index.jsx +1 -0
  73. package/dist/solid/heat-matrix/heat-matrix.jsx +165 -0
  74. package/dist/solid/heat-matrix/heat-matrix.styles.jsx +155 -0
  75. package/dist/solid/heat-matrix/index.jsx +1 -0
  76. package/dist/solid/heat-matrix/model.jsx +41 -0
  77. package/dist/solid/index.jsx +13 -0
  78. package/dist/solid/internal/colors.jsx +44 -0
  79. package/dist/solid/internal/floating-tip.jsx +68 -0
  80. package/dist/solid/internal/format.jsx +43 -0
  81. package/dist/solid/internal/grid-context.jsx +5 -0
  82. package/dist/solid/internal/hidden.styles.jsx +16 -0
  83. package/dist/solid/internal/legend.jsx +35 -0
  84. package/dist/solid/internal/legend.styles.jsx +108 -0
  85. package/dist/solid/internal/markers.jsx +101 -0
  86. package/dist/solid/internal/options.jsx +300 -0
  87. package/dist/solid/internal/place.jsx +27 -0
  88. package/dist/solid/internal/scheme-watch.jsx +73 -0
  89. package/dist/solid/internal/share.jsx +28 -0
  90. package/dist/solid/internal/types.jsx +0 -0
  91. package/dist/solid/row-chart/index.jsx +1 -0
  92. package/dist/solid/row-chart/model.jsx +59 -0
  93. package/dist/solid/row-chart/row-chart.jsx +312 -0
  94. package/dist/solid/row-chart/row-chart.styles.jsx +242 -0
  95. package/dist/solid/segmented-bar/index.jsx +1 -0
  96. package/dist/solid/segmented-bar/segmented-bar.jsx +125 -0
  97. package/dist/solid/segmented-bar/segmented-bar.styles.jsx +88 -0
  98. package/dist/solid/sparkline/index.jsx +1 -0
  99. package/dist/solid/sparkline/sparkline.jsx +88 -0
  100. package/dist/solid/stacked-histogram/histogram.jsx +137 -0
  101. package/dist/solid/stacked-histogram/index.jsx +1 -0
  102. package/dist/solid/stacked-histogram/stacked-histogram.jsx +381 -0
  103. package/dist/solid/stacked-histogram/stacked-histogram.styles.jsx +211 -0
  104. package/dist/solid/state-timeline/index.jsx +1 -0
  105. package/dist/solid/state-timeline/model.jsx +166 -0
  106. package/dist/solid/state-timeline/state-timeline.jsx +387 -0
  107. package/dist/solid/state-timeline/state-timeline.styles.jsx +171 -0
  108. package/dist/solid/status-history/index.jsx +1 -0
  109. package/dist/solid/status-history/status-history.jsx +279 -0
  110. package/dist/solid/status-history/status-history.styles.jsx +171 -0
  111. package/dist/styles.css +1 -0
  112. package/dist/types/bar-list/bar-list.d.ts +91 -0
  113. package/dist/types/bar-list/bar-list.styles.d.ts +148 -0
  114. package/dist/types/bar-list/index.d.ts +3 -0
  115. package/dist/types/chart/chart.d.ts +98 -0
  116. package/dist/types/chart/chart.styles.d.ts +145 -0
  117. package/dist/types/chart/index.d.ts +3 -0
  118. package/dist/types/chart-grid/chart-grid.d.ts +35 -0
  119. package/dist/types/chart-grid/index.d.ts +2 -0
  120. package/dist/types/donut/donut.d.ts +124 -0
  121. package/dist/types/donut/donut.styles.d.ts +96 -0
  122. package/dist/types/donut/index.d.ts +3 -0
  123. package/dist/types/heat-graph/calendar.d.ts +61 -0
  124. package/dist/types/heat-graph/heat-graph.d.ts +82 -0
  125. package/dist/types/heat-graph/heat-graph.styles.d.ts +141 -0
  126. package/dist/types/heat-graph/index.d.ts +4 -0
  127. package/dist/types/heat-matrix/heat-matrix.d.ts +65 -0
  128. package/dist/types/heat-matrix/heat-matrix.styles.d.ts +135 -0
  129. package/dist/types/heat-matrix/index.d.ts +3 -0
  130. package/dist/types/heat-matrix/model.d.ts +15 -0
  131. package/dist/types/index.d.ts +12 -0
  132. package/dist/types/internal/colors.d.ts +22 -0
  133. package/dist/types/internal/floating-tip.d.ts +15 -0
  134. package/dist/types/internal/format.d.ts +7 -0
  135. package/dist/types/internal/grid-context.d.ts +9 -0
  136. package/dist/types/internal/hidden.styles.d.ts +15 -0
  137. package/dist/types/internal/legend.d.ts +18 -0
  138. package/dist/types/internal/legend.styles.d.ts +96 -0
  139. package/dist/types/internal/markers.d.ts +16 -0
  140. package/dist/types/internal/options.d.ts +59 -0
  141. package/dist/types/internal/place.d.ts +20 -0
  142. package/dist/types/internal/scheme-watch.d.ts +12 -0
  143. package/dist/types/internal/share.d.ts +19 -0
  144. package/dist/types/internal/types.d.ts +163 -0
  145. package/dist/types/row-chart/index.d.ts +4 -0
  146. package/dist/types/row-chart/model.d.ts +50 -0
  147. package/dist/types/row-chart/row-chart.d.ts +148 -0
  148. package/dist/types/row-chart/row-chart.styles.d.ts +216 -0
  149. package/dist/types/segmented-bar/index.d.ts +3 -0
  150. package/dist/types/segmented-bar/segmented-bar.d.ts +65 -0
  151. package/dist/types/segmented-bar/segmented-bar.styles.d.ts +75 -0
  152. package/dist/types/sparkline/index.d.ts +3 -0
  153. package/dist/types/sparkline/sparkline.d.ts +38 -0
  154. package/dist/types/stacked-histogram/histogram.d.ts +48 -0
  155. package/dist/types/stacked-histogram/index.d.ts +4 -0
  156. package/dist/types/stacked-histogram/stacked-histogram.d.ts +73 -0
  157. package/dist/types/stacked-histogram/stacked-histogram.styles.d.ts +187 -0
  158. package/dist/types/state-timeline/index.d.ts +3 -0
  159. package/dist/types/state-timeline/model.d.ts +63 -0
  160. package/dist/types/state-timeline/state-timeline.d.ts +108 -0
  161. package/dist/types/state-timeline/state-timeline.styles.d.ts +151 -0
  162. package/dist/types/status-history/index.d.ts +3 -0
  163. package/dist/types/status-history/status-history.d.ts +72 -0
  164. package/dist/types/status-history/status-history.styles.d.ts +150 -0
  165. package/docs/api.json +1346 -0
  166. package/docs/bar-list.md +190 -0
  167. package/docs/chart-grid.md +77 -0
  168. package/docs/chart.md +355 -0
  169. package/docs/donut.md +182 -0
  170. package/docs/heat-graph.md +121 -0
  171. package/docs/heat-matrix.md +108 -0
  172. package/docs/llms.txt +68 -0
  173. package/docs/row-chart.md +193 -0
  174. package/docs/search-index.json +291 -0
  175. package/docs/segmented-bar.md +128 -0
  176. package/docs/sparkline.md +82 -0
  177. package/docs/stacked-histogram.md +123 -0
  178. package/docs/state-timeline.md +141 -0
  179. package/docs/status-history.md +121 -0
  180. package/package.json +109 -4
@@ -0,0 +1,43 @@
1
+ // src/internal/format.ts
2
+ /** Default number format: full under 10,000, compact above (12.9K), in the reader's locale. */
3
+ function numberFormat(locale, max = 0) {
4
+ const full = new Intl.NumberFormat(locale, { maximumFractionDigits: 2 });
5
+ const compact = new Intl.NumberFormat(locale, {
6
+ notation: "compact",
7
+ maximumFractionDigits: 1
8
+ });
9
+ return (value) => (Math.abs(max) >= 1e4 ? compact : full).format(value);
10
+ }
11
+ /** Default x format for the tooltip heading and readout. */
12
+ function xFormat(x, locale, span) {
13
+ if (x?.format) return x.format;
14
+ const categories = x?.categories;
15
+ if (categories) return (value) => categories[Math.round(value)] ?? String(value);
16
+ if (x?.time ?? true) {
17
+ const options = span < 3600 ? {
18
+ hour: "numeric",
19
+ minute: "2-digit",
20
+ second: "2-digit"
21
+ } : span < 86400 ? {
22
+ hour: "numeric",
23
+ minute: "2-digit"
24
+ } : span < 5184e3 ? {
25
+ month: "short",
26
+ day: "numeric",
27
+ hour: "numeric",
28
+ minute: "2-digit"
29
+ } : {
30
+ year: "numeric",
31
+ month: "short",
32
+ day: "numeric"
33
+ };
34
+ const date = new Intl.DateTimeFormat(locale, options);
35
+ return (value) => date.format(/* @__PURE__ */ new Date(value * 1e3));
36
+ }
37
+ return numberFormat(locale);
38
+ }
39
+ /** The formatter for one series' values. */
40
+ function valueFormat(series, y, fallback) {
41
+ return series?.format ?? y?.format ?? fallback;
42
+ }
43
+ export { numberFormat, valueFormat, xFormat };
@@ -0,0 +1,5 @@
1
+ import { createContext, useContext } from "solid-js";
2
+ // src/internal/grid-context.ts
3
+ const ChartGridContext = createContext(null);
4
+ const useChartGrid = () => useContext(ChartGridContext);
5
+ export { ChartGridContext, useChartGrid };
@@ -0,0 +1,16 @@
1
+ import "@stylexjs/stylex";
2
+ // src/internal/hidden.styles.ts
3
+ /** Text for assistive technology only: a row's full name, the values behind a chart drawn without its legend. */
4
+ const hidden = { visually: {
5
+ kVAEAm: "x10l6tqk",
6
+ kzqmXN: "x1i1rx1s",
7
+ kZKoxP: "xjm9jq1",
8
+ kogj98: "xkdpibf",
9
+ kmVPX3: "x1717udv",
10
+ kVQacm: "xb3r6kr",
11
+ kMcinP: "xeh89do",
12
+ khDVqt: "xuxw1ft",
13
+ kMzoRj: "xc342km",
14
+ $$css: true
15
+ } };
16
+ export { hidden };
@@ -0,0 +1,112 @@
1
+ import { legend } from "./legend.styles.js";
2
+ import { createComponent, getNextElement, getNextMarker, insert, memo, runHydrationEvents, scope, spread, template } from "@solidjs/web";
3
+ import * as stylex from "@stylexjs/stylex";
4
+ import "solid-js";
5
+ // src/internal/legend.tsx
6
+ var _tmpl$ = /* @__PURE__ */ template(`<span>`);
7
+ var _tmpl$2 = /* @__PURE__ */ template(`<span><!$><!/><!$><!/>`);
8
+ /** A category's color mark: decorative, the label beside it names the category. */
9
+ function Swatch(props) {
10
+ var _el$ = getNextElement(_tmpl$);
11
+ spread(_el$, [
12
+ {
13
+ "aria-hidden": "true",
14
+ "data-slot": "chart-swatch"
15
+ },
16
+ () => {
17
+ return stylex.attrs(legend.swatch);
18
+ },
19
+ { get style() {
20
+ return { "--swatch": props.color };
21
+ } }
22
+ ], false);
23
+ runHydrationEvents();
24
+ return _el$;
25
+ }
26
+ /**
27
+ * The inside of a legend row: swatch, label (and a note under it), value and share. The row itself (an
28
+ * `li`, or a button inside one) is the caller's. `hidden` keeps the text from assistive technology, for a
29
+ * row that names itself in words.
30
+ */
31
+ function LegendEntry(props) {
32
+ return [
33
+ createComponent(Swatch, { get color() {
34
+ return props.color;
35
+ } }),
36
+ (() => {
37
+ var _el$2 = getNextElement(_tmpl$2);
38
+ var _el$3 = _el$2.firstChild;
39
+ var [_el$4, _el$5] = getNextMarker(_el$3.nextSibling);
40
+ var _el$6 = _el$4.nextSibling;
41
+ var [_el$7, _el$8] = getNextMarker(_el$6.nextSibling);
42
+ spread(_el$2, [{
43
+ get "aria-hidden"() {
44
+ return props.hidden ? "true" : void 0;
45
+ },
46
+ "data-slot": "chart-legend-label"
47
+ }, () => {
48
+ return stylex.attrs(legend.label);
49
+ }], true);
50
+ insert(_el$2, scope(() => {
51
+ return props.label;
52
+ }), _el$4, _el$5);
53
+ insert(_el$2, scope((() => {
54
+ var _c$ = memo(() => {
55
+ return !!props.note;
56
+ });
57
+ return () => {
58
+ return _c$() ? (() => {
59
+ var _el$10 = getNextElement(_tmpl$);
60
+ spread(_el$10, [{ "data-slot": "chart-legend-note" }, () => {
61
+ return stylex.attrs(legend.note);
62
+ }], true);
63
+ insert(_el$10, scope(() => {
64
+ return props.note;
65
+ }));
66
+ runHydrationEvents();
67
+ return _el$10;
68
+ })() : void 0;
69
+ };
70
+ })()), _el$7, _el$8);
71
+ runHydrationEvents();
72
+ return _el$2;
73
+ })(),
74
+ (() => {
75
+ var _el$9 = getNextElement(_tmpl$);
76
+ spread(_el$9, [{
77
+ get "aria-hidden"() {
78
+ return props.hidden ? "true" : void 0;
79
+ },
80
+ "data-slot": "chart-legend-value"
81
+ }, () => {
82
+ return stylex.attrs(legend.value);
83
+ }], true);
84
+ insert(_el$9, scope(() => {
85
+ return props.value;
86
+ }));
87
+ runHydrationEvents();
88
+ return _el$9;
89
+ })(),
90
+ memo(() => {
91
+ return memo(() => {
92
+ return props.share !== void 0;
93
+ })() ? (() => {
94
+ var _el$11 = getNextElement(_tmpl$);
95
+ spread(_el$11, [{
96
+ get "aria-hidden"() {
97
+ return props.hidden ? "true" : void 0;
98
+ },
99
+ "data-slot": "chart-legend-share"
100
+ }, () => {
101
+ return stylex.attrs(legend.share);
102
+ }], true);
103
+ insert(_el$11, scope(() => {
104
+ return props.share;
105
+ }));
106
+ runHydrationEvents();
107
+ return _el$11;
108
+ })() : void 0;
109
+ })
110
+ ];
111
+ }
112
+ export { LegendEntry, Swatch };
@@ -0,0 +1,108 @@
1
+ import "@stylexjs/stylex";
2
+ // src/internal/legend.styles.ts
3
+ /**
4
+ * The legend list Donut and Segmented bar share: one row per category with its swatch, label, value and
5
+ * share. Values and shares sit in fixed columns at the end, in tabular figures, so they line up row to row.
6
+ */
7
+ const legend = {
8
+ list: {
9
+ k1xSpc: "xrvj5dj",
10
+ kfiyM8: "x10ukxgv",
11
+ kOIVth: "xpowot6",
12
+ k7Eaqz: "xeuugli",
13
+ kogj98: "x1ghz6dp",
14
+ kmVPX3: "x1717udv",
15
+ kB88ic: "xe8uvvx",
16
+ kMv6JI: "x13rcfk",
17
+ kGuDYH: "x409pna",
18
+ kLWn49: "xau0z5x",
19
+ kMwMTN: "x1yjpjgc",
20
+ $$css: true
21
+ },
22
+ row: {
23
+ kB7OPa: "x9f619",
24
+ k1xSpc: "x78zum5",
25
+ kGNEyG: "x1cy8zhl",
26
+ kOIVth: "x8rwxmg",
27
+ kzqmXN: "xh8yej3",
28
+ k7Eaqz: "xeuugli",
29
+ kAzted: "x1pw4g4d",
30
+ k8WAf4: "x1ucdaca",
31
+ kg3NbH: "x1d6xsf",
32
+ kaIpWk: "x1b6xorz",
33
+ k9WMMc: "x1yc453h",
34
+ $$css: true
35
+ },
36
+ interactive: {
37
+ kWkggS: "xjbqb8w x1duwuep",
38
+ k1ekBW: "x15406qy",
39
+ kIyJzY: "xctwqe3 x12w9bfk",
40
+ $$css: true
41
+ },
42
+ button: {
43
+ kVAEAm: "x1n2onr6",
44
+ kogj98: "x1ghz6dp",
45
+ kMzoRj: "xc342km",
46
+ kMv6JI: "xjb2p0i",
47
+ kGuDYH: "x1qlqyl8",
48
+ kLWn49: "x15bjb6t",
49
+ kMwMTN: "x1heor9g",
50
+ kkrTdU: "x1ypdohk",
51
+ k3XXqK: "x1t137rt x9v5kkp",
52
+ kMeerF: "x1de99jn",
53
+ kInvED: "x2ssjo2",
54
+ kjBf7l: "xi1ptex x1x84bn5",
55
+ $$css: true
56
+ },
57
+ selected: {
58
+ k63SB2: "x1vmg6u2",
59
+ kWkggS: "x3yl11l",
60
+ k3XXqK: "x815t2r",
61
+ kMeerF: "xcthrzx",
62
+ kjBf7l: "x1x84bn5",
63
+ $$css: true
64
+ },
65
+ swatch: {
66
+ kmuXW: "x2lah0s",
67
+ k1xSpc: "x1rg5ohu",
68
+ kzqmXN: "x1e7hgle",
69
+ kZKoxP: "x1qa65zt",
70
+ keoZOQ: "x1mlafj8",
71
+ kaIpWk: "x1cum3z5",
72
+ kWkggS: "xrmkqiu",
73
+ kAXs8y: "x1kzknox",
74
+ $$css: true
75
+ },
76
+ label: {
77
+ kUk6DE: "x12lumcd",
78
+ k7Eaqz: "xeuugli",
79
+ kHjlTd: "x1mzt3pk",
80
+ $$css: true
81
+ },
82
+ note: {
83
+ k1xSpc: "x1lliihq",
84
+ keoZOQ: "x14c0qtv",
85
+ kGuDYH: "xb5fjo1",
86
+ kMwMTN: "xe2mk2a",
87
+ $$css: true
88
+ },
89
+ value: {
90
+ kmuXW: "x2lah0s",
91
+ kcqcaj: "xss6m8b",
92
+ k63SB2: "x1vmg6u2",
93
+ khDVqt: "xuxw1ft",
94
+ kQ0QjF: "xa7awgv",
95
+ $$css: true
96
+ },
97
+ share: {
98
+ kmuXW: "x2lah0s",
99
+ k7Eaqz: "x1k8l6nd",
100
+ k9WMMc: "xp4054r",
101
+ kcqcaj: "xss6m8b",
102
+ kMwMTN: "xe2mk2a",
103
+ khDVqt: "xuxw1ft",
104
+ kQ0QjF: "xa7awgv",
105
+ $$css: true
106
+ }
107
+ };
108
+ export { legend };
@@ -0,0 +1,101 @@
1
+ import { wash } from "./colors.js";
2
+ // src/internal/markers.ts
3
+ const TONES = {
4
+ neutral: "var(--sui-text-muted)",
5
+ accent: "var(--sui-accent)",
6
+ success: "var(--sui-success)",
7
+ warning: "var(--sui-warning)",
8
+ danger: "var(--sui-danger)"
9
+ };
10
+ const isBand = (m) => typeof m.value !== "number";
11
+ /** A marker's two ends (the same value twice for a line). */
12
+ const ends = (m) => typeof m.value === "number" ? [m.value, m.value] : m.value;
13
+ /** The y values markers need in view: the y scale's range takes them in. */
14
+ function markerExtent(markers) {
15
+ return (markers ?? []).filter((m) => m.axis === "y").flatMap(ends);
16
+ }
17
+ function markersPlugin(markers, theme) {
18
+ const colored = markers.map((m) => {
19
+ const css = TONES[m.tone ?? "neutral"];
20
+ return {
21
+ marker: m,
22
+ line: theme.resolve(css),
23
+ band: theme.resolve(wash(css, m.tone && m.tone !== "neutral" ? 12 : 8))
24
+ };
25
+ });
26
+ /** A marker's span in canvas pixels along its axis, clamped to the plot. */
27
+ const span = (u, m) => {
28
+ const [a, b] = ends(m);
29
+ const p1 = u.valToPos(a, m.axis, true);
30
+ const p2 = u.valToPos(b, m.axis, true);
31
+ return [Math.min(p1, p2), Math.max(p1, p2)];
32
+ };
33
+ /** Labels drawn this frame (bands first, then lines), so later ones step around them. */
34
+ let placed = [];
35
+ const draw = (u, bands) => {
36
+ if (bands) placed = [];
37
+ const { ctx } = u;
38
+ const { left, top, width, height } = u.bbox;
39
+ const ratio = devicePixelRatio || 1;
40
+ ctx.save();
41
+ ctx.beginPath();
42
+ ctx.rect(left, top, width, height);
43
+ ctx.clip();
44
+ for (const { marker: m, line, band } of colored) {
45
+ if (isBand(m) !== bands) continue;
46
+ const [from, to] = span(u, m);
47
+ if (bands) {
48
+ ctx.fillStyle = band;
49
+ if (m.axis === "y") ctx.fillRect(left, from, width, to - from);
50
+ else ctx.fillRect(from, top, to - from, height);
51
+ } else {
52
+ ctx.strokeStyle = line;
53
+ ctx.lineWidth = 1.5 * ratio;
54
+ ctx.setLineDash((m.dash ?? [6, 4]).map((d) => d * ratio));
55
+ ctx.beginPath();
56
+ if (m.axis === "y") {
57
+ ctx.moveTo(left, from);
58
+ ctx.lineTo(left + width, from);
59
+ } else {
60
+ ctx.moveTo(from, top);
61
+ ctx.lineTo(from, top + height);
62
+ }
63
+ ctx.stroke();
64
+ ctx.setLineDash([]);
65
+ }
66
+ if (!m.label) continue;
67
+ const [size, ...family] = theme.font.split(" ");
68
+ const fontPx = parseFloat(size ?? "12") * ratio;
69
+ ctx.font = `500 ${fontPx}px ${family.join(" ")}`;
70
+ ctx.fillStyle = theme.text;
71
+ const pad = 4 * ratio;
72
+ const w = ctx.measureText(m.label).width;
73
+ const h = fontPx * 1.25;
74
+ let x;
75
+ let y;
76
+ if (m.axis === "y") {
77
+ x = left + width - pad - w;
78
+ y = from - pad - h;
79
+ } else {
80
+ x = bands || from - pad - w < left ? from + pad : from - pad - w;
81
+ y = top + pad;
82
+ }
83
+ while (placed.some((r) => x < r.x + r.w && x + w > r.x && y < r.y + r.h && y + h > r.y)) y += h;
84
+ placed.push({
85
+ x,
86
+ y,
87
+ w,
88
+ h
89
+ });
90
+ ctx.textAlign = "left";
91
+ ctx.textBaseline = "top";
92
+ ctx.fillText(m.label, x, y);
93
+ }
94
+ ctx.restore();
95
+ };
96
+ return { hooks: {
97
+ drawAxes: [(u) => draw(u, true)],
98
+ draw: [(u) => draw(u, false)]
99
+ } };
100
+ }
101
+ export { markerExtent, markersPlugin };
@@ -0,0 +1,300 @@
1
+ import { markerExtent } from "./markers.js";
2
+ import uPlot from "uplot";
3
+ // src/internal/options.ts
4
+ /**
5
+ * Props → uPlot options. Pure apart from uPlot's path builders: colors arrive resolved (canvas can't
6
+ * read tokens), so a theme change is a rebuild with new colors.
7
+ */
8
+ const BAR_MAX = 24;
9
+ /** How far the pointer may be from a line (in CSS pixels) to focus it. */
10
+ const FOCUS_PROX = 16;
11
+ /** The other series' opacity while one is focused. */
12
+ const FOCUS_ALPHA = .25;
13
+ const BAR_GAP = 2;
14
+ /** The smallest step between neighbouring x values (1 when there's only one). */
15
+ function xStep(xs) {
16
+ let step = Infinity;
17
+ for (let i = 1; i < xs.length; i++) {
18
+ const a = xs[i - 1];
19
+ const b = xs[i];
20
+ if (a != null && b != null && b - a > 0 && b - a < step) step = b - a;
21
+ }
22
+ return Number.isFinite(step) ? step : 1;
23
+ }
24
+ /**
25
+ * Stacked series: each visible series sits on the sum of the visible ones before it (hidden ones add
26
+ * nothing, so the stack closes up when one is toggled off). `null` counts as 0 in the stack.
27
+ */
28
+ function stackData(data, hidden) {
29
+ const [xs, ...ys] = data;
30
+ const len = xs?.length ?? 0;
31
+ const sums = Array.from({ length: len }, () => 0);
32
+ const out = ys.map((series, s) => {
33
+ const row = Array.from({ length: len }, () => null);
34
+ for (let i = 0; i < len; i++) {
35
+ if (!hidden.has(s)) sums[i] += series[i] ?? 0;
36
+ row[i] = sums[i];
37
+ }
38
+ return row;
39
+ });
40
+ return [xs ?? [], ...out];
41
+ }
42
+ /**
43
+ * Grouped bars: each bar series gets a slot inside its x value's group, at most 24px wide with 2px
44
+ * between neighbours, the group centred on the value. uPlot reads the offsets (`disp.x0`) and width
45
+ * (`disp.size`) in data units at draw time, so they follow zoom and resize.
46
+ */
47
+ /** uPlot's `BarsPathBuilderFacetUnit.PixelPercent` (a const enum, so not importable here). */
48
+ const PLOT_FRACTION = 2;
49
+ /**
50
+ * Grouped bars: each bar series gets a slot inside its x value's group, at most 24px wide with 2px
51
+ * between neighbours, the group centred on the value. uPlot reads the offsets (`disp.x0`) and width
52
+ * (`disp.size`) as fractions of the plot's width at draw time, so they follow zoom and resize on any x
53
+ * scale (its data-unit sizes assume a scale that starts near zero, which a time axis doesn't).
54
+ */
55
+ function groupedBars(slot, count) {
56
+ const geometry = (u) => {
57
+ const xs = u.data[0];
58
+ const step = xStep(xs);
59
+ const x0 = xs[0] ?? 0;
60
+ const plot = u.bbox.width / (devicePixelRatio || 1);
61
+ const pxPerStep = Math.abs(u.valToPos(x0 + step, "x") - u.valToPos(x0, "x")) || 1;
62
+ const bar = Math.max(1, Math.min(BAR_MAX, (pxPerStep * .7 - (count - 1) * BAR_GAP) / count));
63
+ return {
64
+ plot,
65
+ bar,
66
+ offset: -(count * bar + (count - 1) * BAR_GAP) / 2 + slot * (bar + BAR_GAP)
67
+ };
68
+ };
69
+ return {
70
+ x0: {
71
+ unit: PLOT_FRACTION,
72
+ values: (u) => {
73
+ const { plot, offset } = geometry(u);
74
+ return Array.from(u.data[0], (x) => (u.valToPos(x, "x") + offset) / plot);
75
+ }
76
+ },
77
+ size: {
78
+ unit: PLOT_FRACTION,
79
+ values: (u) => {
80
+ const { plot, bar } = geometry(u);
81
+ return [bar / plot];
82
+ }
83
+ }
84
+ };
85
+ }
86
+ function buildOptions(input) {
87
+ const { series, x, y, theme } = input;
88
+ const categories = x?.categories;
89
+ const time = x?.time ?? !categories;
90
+ const barCount = series.filter((s) => s.type === "bar").length;
91
+ const hasBars = barCount > 0;
92
+ const lastVisible = series.reduce((last, _s, i) => input.hidden.has(i) ? last : i, -1);
93
+ let barSlot = 0;
94
+ const uSeries = [{}];
95
+ series.forEach((s, i) => {
96
+ const type = s.type ?? "line";
97
+ const stroke = input.strokes[i];
98
+ const out = {
99
+ label: s.label,
100
+ stroke,
101
+ show: !input.hidden.has(i),
102
+ spanGaps: false,
103
+ points: {
104
+ show: s.points ?? false,
105
+ size: 8,
106
+ width: 2,
107
+ stroke: theme.surface,
108
+ fill: stroke
109
+ }
110
+ };
111
+ if (type === "bar") {
112
+ const grouped = !input.stacked && barCount > 1;
113
+ out.fill = input.fills[i] ?? stroke;
114
+ if (input.stacked) {
115
+ out.stroke = theme.surface;
116
+ out.width = BAR_GAP;
117
+ } else out.width = 0;
118
+ out.points = { show: false };
119
+ out.paths = uPlot.paths.bars({
120
+ size: [.6, BAR_MAX],
121
+ gap: 0,
122
+ radius: input.stacked && i !== lastVisible ? 0 : [.2, 0],
123
+ ...grouped ? { disp: groupedBars(barSlot++, barCount) } : {}
124
+ });
125
+ } else {
126
+ out.width = s.width ?? 2;
127
+ if (s.dash) out.dash = s.dash;
128
+ if (type === "area" || input.stacked) out.fill = input.fills[i];
129
+ }
130
+ uSeries.push(out);
131
+ });
132
+ const bands = input.stacked ? series.slice(1).map((_s, i) => ({ series: [i + 2, i + 1] })) : [];
133
+ const axis = (a, gridDefault) => ({
134
+ show: a?.show ?? true,
135
+ stroke: theme.text,
136
+ font: theme.font,
137
+ labelFont: theme.labelFont,
138
+ grid: {
139
+ show: a?.grid ?? gridDefault,
140
+ stroke: theme.grid,
141
+ width: 1
142
+ },
143
+ ticks: { show: false },
144
+ ...a?.label ? {
145
+ label: a.label,
146
+ labelSize: 20,
147
+ labelGap: 4
148
+ } : {},
149
+ ...a?.size ? { size: a.size } : {},
150
+ ...a?.steps ? { incrs: a.steps } : {}
151
+ });
152
+ const xAxis = axis(x, false);
153
+ xAxis.border = {
154
+ show: true,
155
+ stroke: theme.baseline,
156
+ width: 1
157
+ };
158
+ xAxis.filter = (u, splits) => {
159
+ const right = u.bbox.width / devicePixelRatio;
160
+ return splits.map((v) => v != null && right - u.valToPos(v, "x") < 24 ? null : v);
161
+ };
162
+ if (!x?.size) xAxis.size = (_u, values) => xAxisHeight(values, x?.label ? 20 : 0);
163
+ if (categories) {
164
+ xAxis.values = (_u, ticks) => ticks.map((t) => Number.isInteger(t) ? x?.format?.(t) ?? categories[t] ?? "" : "");
165
+ xAxis.incrs = x?.steps ?? [1];
166
+ } else if (x?.format) {
167
+ const format = x.format;
168
+ xAxis.values = (_u, ticks) => ticks.map((t) => t == null ? "" : format(t));
169
+ } else if (hasBars && !time) xAxis.incrs = x?.steps ?? [1];
170
+ if (hasBars && time && !categories && !x?.steps) {
171
+ xAxis.incrs = (u) => [
172
+ 1,
173
+ 2,
174
+ 7,
175
+ 14,
176
+ 30,
177
+ 60,
178
+ 90,
179
+ 180,
180
+ 365
181
+ ].map((k) => k * xStep(u.data[0]));
182
+ xAxis.splits = (u, _axis, min, max, incr) => {
183
+ const xs = u.data[0];
184
+ const every = Math.max(1, Math.round(incr / xStep(xs)));
185
+ return Array.from(xs).filter((v, i) => i % every === 0 && v >= min && v <= max);
186
+ };
187
+ }
188
+ const yAxis = axis(y, true);
189
+ yAxis.size = y?.size ?? ((u, values) => yAxisWidth(u, values, theme.font));
190
+ const yFormat = y?.format;
191
+ yAxis.values = (_u, ticks) => ticks.map((t) => t == null ? "" : yFormat ? yFormat(t) : input.yTicks(t, ticks));
192
+ let xRange = x?.range;
193
+ if (!xRange && (hasBars || categories)) xRange = (u, min, max) => {
194
+ const half = categories ? .5 : xStep(u.data[0]) / 2;
195
+ return hasBars || categories ? [min - half, max + half] : [min, max];
196
+ };
197
+ return {
198
+ width: input.width,
199
+ height: input.height,
200
+ series: uSeries,
201
+ bands,
202
+ axes: [xAxis, yAxis],
203
+ scales: {
204
+ x: {
205
+ time,
206
+ auto: true,
207
+ ...xRange ? { range: xRange } : {}
208
+ },
209
+ y: {
210
+ auto: !y?.range,
211
+ ...y?.range ? { range: y.range } : { range: yRange(hasBars || input.stacked, markerExtent(input.markers)) }
212
+ }
213
+ },
214
+ legend: { show: false },
215
+ focus: { alpha: FOCUS_ALPHA },
216
+ cursor: {
217
+ show: input.cursor,
218
+ x: true,
219
+ y: false,
220
+ points: {
221
+ size: (_u, i) => series[i - 1]?.type === "bar" ? 0 : 8,
222
+ width: 2,
223
+ stroke: theme.surface,
224
+ fill: (_u, i) => input.strokes[i - 1] ?? theme.text
225
+ },
226
+ drag: {
227
+ x: input.brush !== false,
228
+ y: false,
229
+ setScale: input.brush === "zoom"
230
+ },
231
+ focus: input.focus ? {
232
+ prox: FOCUS_PROX,
233
+ dist: (_u, i, _idx, valPos, curPos) => series[i - 1]?.type === "bar" ? Infinity : valPos - curPos
234
+ } : { prox: -1 },
235
+ ...input.syncKey ? { sync: {
236
+ key: input.syncKey,
237
+ setSeries: false,
238
+ scales: ["x", null]
239
+ } } : {}
240
+ },
241
+ select: {
242
+ show: input.brush !== false,
243
+ left: 0,
244
+ top: 0,
245
+ width: 0,
246
+ height: 0
247
+ },
248
+ padding: input.padding,
249
+ hooks: input.hooks,
250
+ plugins: input.plugins
251
+ };
252
+ }
253
+ /**
254
+ * The y range when the axis has no fixed one: bars and stacks grow from zero to a round top; lines get
255
+ * uPlot's padded fit. Either way it takes in the y markers, so a target above the data stays in view.
256
+ */
257
+ function yRange(fromZero, extra) {
258
+ return (_u, dataMin, dataMax) => {
259
+ const min = Math.min(dataMin, ...extra);
260
+ const max = Math.max(dataMax, ...extra);
261
+ if (fromZero) return [Math.min(0, min), max === min ? max + 1 : niceMax(max)];
262
+ const [lo, hi] = uPlot.rangeNum(min, max, .1, true);
263
+ return [lo ?? min, hi ?? max];
264
+ };
265
+ }
266
+ /** A round top for a zero-based scale (bars, stacks): 1, 2 or 5 times a power of ten above the max. */
267
+ function niceMax(max) {
268
+ if (max <= 0) return max;
269
+ const pow = 10 ** Math.floor(Math.log10(max));
270
+ for (const m of [
271
+ 1,
272
+ 1.2,
273
+ 1.5,
274
+ 2,
275
+ 2.5,
276
+ 3,
277
+ 4,
278
+ 5,
279
+ 6,
280
+ 8,
281
+ 10
282
+ ]) if (m * pow >= max) return m * pow;
283
+ return max;
284
+ }
285
+ /** Tick labels may hold a second line (`"12am\n11/1"`): 16px a line, plus room for the tick gap. */
286
+ function xAxisHeight(values, label) {
287
+ return Math.max(1, ...(values ?? []).map((v) => String(v ?? "").split("\n").length)) * 16 + 10 + label;
288
+ }
289
+ let measure = null;
290
+ /** The y axis takes the width of its widest label (uPlot's default is a fixed 50px). */
291
+ function yAxisWidth(u, values, font) {
292
+ if (!values?.length) return 24;
293
+ measure ??= document.createElement("canvas").getContext("2d");
294
+ if (!measure) return 50;
295
+ measure.font = font;
296
+ const widest = Math.max(...values.map((v) => measure.measureText(v).width));
297
+ const label = u.axes[1]?.label ? 24 : 0;
298
+ return Math.ceil(widest) + 12 + label;
299
+ }
300
+ export { FOCUS_ALPHA, buildOptions, stackData, xStep };