@zsherman/charts 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.
- package/CHANGELOG.md +7 -0
- package/LICENSE +373 -0
- package/README.md +13 -2
- package/THIRD_PARTY_NOTICES.md +33 -0
- package/dist/dom/bar-list/bar-list.js +386 -0
- package/dist/dom/bar-list/bar-list.styles.js +171 -0
- package/dist/dom/bar-list/index.js +1 -0
- package/dist/dom/chart/chart.js +716 -0
- package/dist/dom/chart/chart.styles.js +165 -0
- package/dist/dom/chart/index.js +1 -0
- package/dist/dom/chart-grid/chart-grid.js +67 -0
- package/dist/dom/chart-grid/index.js +1 -0
- package/dist/dom/donut/donut.js +565 -0
- package/dist/dom/donut/donut.styles.js +111 -0
- package/dist/dom/donut/index.js +1 -0
- package/dist/dom/heat-graph/calendar.js +118 -0
- package/dist/dom/heat-graph/heat-graph.js +423 -0
- package/dist/dom/heat-graph/heat-graph.styles.js +168 -0
- package/dist/dom/heat-graph/index.js +1 -0
- package/dist/dom/heat-matrix/heat-matrix.js +346 -0
- package/dist/dom/heat-matrix/heat-matrix.styles.js +155 -0
- package/dist/dom/heat-matrix/index.js +1 -0
- package/dist/dom/heat-matrix/model.js +41 -0
- package/dist/dom/index.js +13 -0
- package/dist/dom/internal/colors.js +44 -0
- package/dist/dom/internal/floating-tip.js +83 -0
- package/dist/dom/internal/format.js +43 -0
- package/dist/dom/internal/grid-context.js +5 -0
- package/dist/dom/internal/hidden.styles.js +16 -0
- package/dist/dom/internal/legend.js +112 -0
- package/dist/dom/internal/legend.styles.js +108 -0
- package/dist/dom/internal/markers.js +101 -0
- package/dist/dom/internal/options.js +300 -0
- package/dist/dom/internal/place.js +27 -0
- package/dist/dom/internal/scheme-watch.js +73 -0
- package/dist/dom/internal/share.js +28 -0
- package/dist/dom/internal/types.js +0 -0
- package/dist/dom/row-chart/index.js +1 -0
- package/dist/dom/row-chart/model.js +59 -0
- package/dist/dom/row-chart/row-chart.js +691 -0
- package/dist/dom/row-chart/row-chart.styles.js +242 -0
- package/dist/dom/segmented-bar/index.js +1 -0
- package/dist/dom/segmented-bar/segmented-bar.js +280 -0
- package/dist/dom/segmented-bar/segmented-bar.styles.js +88 -0
- package/dist/dom/sparkline/index.js +1 -0
- package/dist/dom/sparkline/sparkline.js +145 -0
- package/dist/dom/stacked-histogram/histogram.js +137 -0
- package/dist/dom/stacked-histogram/index.js +1 -0
- package/dist/dom/stacked-histogram/stacked-histogram.js +761 -0
- package/dist/dom/stacked-histogram/stacked-histogram.styles.js +211 -0
- package/dist/dom/state-timeline/index.js +1 -0
- package/dist/dom/state-timeline/model.js +166 -0
- package/dist/dom/state-timeline/state-timeline.js +729 -0
- package/dist/dom/state-timeline/state-timeline.styles.js +171 -0
- package/dist/dom/status-history/index.js +1 -0
- package/dist/dom/status-history/status-history.js +542 -0
- package/dist/dom/status-history/status-history.styles.js +171 -0
- package/dist/solid/bar-list/bar-list.jsx +139 -0
- package/dist/solid/bar-list/bar-list.styles.jsx +171 -0
- package/dist/solid/bar-list/index.jsx +1 -0
- package/dist/solid/chart/chart.jsx +454 -0
- package/dist/solid/chart/chart.styles.jsx +165 -0
- package/dist/solid/chart/index.jsx +1 -0
- package/dist/solid/chart-grid/chart-grid.jsx +46 -0
- package/dist/solid/chart-grid/index.jsx +1 -0
- package/dist/solid/donut/donut.jsx +291 -0
- package/dist/solid/donut/donut.styles.jsx +111 -0
- package/dist/solid/donut/index.jsx +1 -0
- package/dist/solid/heat-graph/calendar.jsx +118 -0
- package/dist/solid/heat-graph/heat-graph.jsx +228 -0
- package/dist/solid/heat-graph/heat-graph.styles.jsx +168 -0
- package/dist/solid/heat-graph/index.jsx +1 -0
- package/dist/solid/heat-matrix/heat-matrix.jsx +165 -0
- package/dist/solid/heat-matrix/heat-matrix.styles.jsx +155 -0
- package/dist/solid/heat-matrix/index.jsx +1 -0
- package/dist/solid/heat-matrix/model.jsx +41 -0
- package/dist/solid/index.jsx +13 -0
- package/dist/solid/internal/colors.jsx +44 -0
- package/dist/solid/internal/floating-tip.jsx +68 -0
- package/dist/solid/internal/format.jsx +43 -0
- package/dist/solid/internal/grid-context.jsx +5 -0
- package/dist/solid/internal/hidden.styles.jsx +16 -0
- package/dist/solid/internal/legend.jsx +35 -0
- package/dist/solid/internal/legend.styles.jsx +108 -0
- package/dist/solid/internal/markers.jsx +101 -0
- package/dist/solid/internal/options.jsx +300 -0
- package/dist/solid/internal/place.jsx +27 -0
- package/dist/solid/internal/scheme-watch.jsx +73 -0
- package/dist/solid/internal/share.jsx +28 -0
- package/dist/solid/internal/types.jsx +0 -0
- package/dist/solid/row-chart/index.jsx +1 -0
- package/dist/solid/row-chart/model.jsx +59 -0
- package/dist/solid/row-chart/row-chart.jsx +312 -0
- package/dist/solid/row-chart/row-chart.styles.jsx +242 -0
- package/dist/solid/segmented-bar/index.jsx +1 -0
- package/dist/solid/segmented-bar/segmented-bar.jsx +125 -0
- package/dist/solid/segmented-bar/segmented-bar.styles.jsx +88 -0
- package/dist/solid/sparkline/index.jsx +1 -0
- package/dist/solid/sparkline/sparkline.jsx +88 -0
- package/dist/solid/stacked-histogram/histogram.jsx +137 -0
- package/dist/solid/stacked-histogram/index.jsx +1 -0
- package/dist/solid/stacked-histogram/stacked-histogram.jsx +381 -0
- package/dist/solid/stacked-histogram/stacked-histogram.styles.jsx +211 -0
- package/dist/solid/state-timeline/index.jsx +1 -0
- package/dist/solid/state-timeline/model.jsx +166 -0
- package/dist/solid/state-timeline/state-timeline.jsx +387 -0
- package/dist/solid/state-timeline/state-timeline.styles.jsx +171 -0
- package/dist/solid/status-history/index.jsx +1 -0
- package/dist/solid/status-history/status-history.jsx +279 -0
- package/dist/solid/status-history/status-history.styles.jsx +171 -0
- package/dist/styles.css +1 -0
- package/dist/types/bar-list/bar-list.d.ts +91 -0
- package/dist/types/bar-list/bar-list.styles.d.ts +148 -0
- package/dist/types/bar-list/index.d.ts +3 -0
- package/dist/types/chart/chart.d.ts +98 -0
- package/dist/types/chart/chart.styles.d.ts +145 -0
- package/dist/types/chart/index.d.ts +3 -0
- package/dist/types/chart-grid/chart-grid.d.ts +35 -0
- package/dist/types/chart-grid/index.d.ts +2 -0
- package/dist/types/donut/donut.d.ts +124 -0
- package/dist/types/donut/donut.styles.d.ts +96 -0
- package/dist/types/donut/index.d.ts +3 -0
- package/dist/types/heat-graph/calendar.d.ts +61 -0
- package/dist/types/heat-graph/heat-graph.d.ts +82 -0
- package/dist/types/heat-graph/heat-graph.styles.d.ts +141 -0
- package/dist/types/heat-graph/index.d.ts +4 -0
- package/dist/types/heat-matrix/heat-matrix.d.ts +65 -0
- package/dist/types/heat-matrix/heat-matrix.styles.d.ts +135 -0
- package/dist/types/heat-matrix/index.d.ts +3 -0
- package/dist/types/heat-matrix/model.d.ts +15 -0
- package/dist/types/index.d.ts +12 -0
- package/dist/types/internal/colors.d.ts +22 -0
- package/dist/types/internal/floating-tip.d.ts +15 -0
- package/dist/types/internal/format.d.ts +7 -0
- package/dist/types/internal/grid-context.d.ts +9 -0
- package/dist/types/internal/hidden.styles.d.ts +15 -0
- package/dist/types/internal/legend.d.ts +18 -0
- package/dist/types/internal/legend.styles.d.ts +96 -0
- package/dist/types/internal/markers.d.ts +16 -0
- package/dist/types/internal/options.d.ts +59 -0
- package/dist/types/internal/place.d.ts +20 -0
- package/dist/types/internal/scheme-watch.d.ts +12 -0
- package/dist/types/internal/share.d.ts +19 -0
- package/dist/types/internal/types.d.ts +163 -0
- package/dist/types/row-chart/index.d.ts +4 -0
- package/dist/types/row-chart/model.d.ts +50 -0
- package/dist/types/row-chart/row-chart.d.ts +148 -0
- package/dist/types/row-chart/row-chart.styles.d.ts +216 -0
- package/dist/types/segmented-bar/index.d.ts +3 -0
- package/dist/types/segmented-bar/segmented-bar.d.ts +65 -0
- package/dist/types/segmented-bar/segmented-bar.styles.d.ts +75 -0
- package/dist/types/sparkline/index.d.ts +3 -0
- package/dist/types/sparkline/sparkline.d.ts +38 -0
- package/dist/types/stacked-histogram/histogram.d.ts +48 -0
- package/dist/types/stacked-histogram/index.d.ts +4 -0
- package/dist/types/stacked-histogram/stacked-histogram.d.ts +73 -0
- package/dist/types/stacked-histogram/stacked-histogram.styles.d.ts +187 -0
- package/dist/types/state-timeline/index.d.ts +3 -0
- package/dist/types/state-timeline/model.d.ts +63 -0
- package/dist/types/state-timeline/state-timeline.d.ts +108 -0
- package/dist/types/state-timeline/state-timeline.styles.d.ts +151 -0
- package/dist/types/status-history/index.d.ts +3 -0
- package/dist/types/status-history/status-history.d.ts +72 -0
- package/dist/types/status-history/status-history.styles.d.ts +150 -0
- package/docs/api.json +1346 -0
- package/docs/bar-list.md +190 -0
- package/docs/chart-grid.md +77 -0
- package/docs/chart.md +355 -0
- package/docs/donut.md +182 -0
- package/docs/heat-graph.md +121 -0
- package/docs/heat-matrix.md +108 -0
- package/docs/llms.txt +68 -0
- package/docs/row-chart.md +193 -0
- package/docs/search-index.json +291 -0
- package/docs/segmented-bar.md +128 -0
- package/docs/sparkline.md +82 -0
- package/docs/stacked-histogram.md +123 -0
- package/docs/state-timeline.md +141 -0
- package/docs/status-history.md +121 -0
- package/package.json +104 -4
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import "@stylexjs/stylex";
|
|
2
|
+
// src/segmented-bar/segmented-bar.styles.ts
|
|
3
|
+
const styles = {
|
|
4
|
+
root: {
|
|
5
|
+
kB7OPa: "x9f619",
|
|
6
|
+
k1xSpc: "xrvj5dj",
|
|
7
|
+
kOIVth: "xgtrjx6",
|
|
8
|
+
k7Eaqz: "xeuugli",
|
|
9
|
+
kMv6JI: "x13rcfk",
|
|
10
|
+
kGuDYH: "x409pna",
|
|
11
|
+
kLWn49: "xau0z5x",
|
|
12
|
+
kMwMTN: "x1yjpjgc",
|
|
13
|
+
$$css: true
|
|
14
|
+
},
|
|
15
|
+
figure: {
|
|
16
|
+
kVAEAm: "x1n2onr6",
|
|
17
|
+
k1xSpc: "xrvj5dj",
|
|
18
|
+
kOIVth: "x1xuqxfr",
|
|
19
|
+
k7Eaqz: "xeuugli",
|
|
20
|
+
$$css: true
|
|
21
|
+
},
|
|
22
|
+
bar: {
|
|
23
|
+
k1xSpc: "x78zum5",
|
|
24
|
+
kOIVth: "x195vfkc",
|
|
25
|
+
kZKoxP: "x17hm2qj",
|
|
26
|
+
kVQacm: "xb3r6kr",
|
|
27
|
+
kaIpWk: "x10hpsqq",
|
|
28
|
+
kWkggS: "x1l62bwj",
|
|
29
|
+
k3XXqK: "x1t137rt x815t2r",
|
|
30
|
+
kMeerF: "x1qgsegg",
|
|
31
|
+
kjBf7l: "x17fapuw",
|
|
32
|
+
$$css: true
|
|
33
|
+
},
|
|
34
|
+
segment: {
|
|
35
|
+
kCS8Yb: "x1r8uery",
|
|
36
|
+
kmuXW: "xs83m0k",
|
|
37
|
+
k7Eaqz: "x9041oi",
|
|
38
|
+
kWkggS: "xrmkqiu",
|
|
39
|
+
kAXs8y: "x1kzknox",
|
|
40
|
+
$$css: true
|
|
41
|
+
},
|
|
42
|
+
rest: {
|
|
43
|
+
kCS8Yb: "x1r8uery",
|
|
44
|
+
kmuXW: "xs83m0k",
|
|
45
|
+
$$css: true
|
|
46
|
+
},
|
|
47
|
+
markerLabel: {
|
|
48
|
+
kVAEAm: "x1n2onr6",
|
|
49
|
+
kZKoxP: "xg2ss61",
|
|
50
|
+
kGuDYH: "xb5fjo1",
|
|
51
|
+
kMwMTN: "xe2mk2a",
|
|
52
|
+
kcqcaj: "xss6m8b",
|
|
53
|
+
$$css: true
|
|
54
|
+
},
|
|
55
|
+
markerText: {
|
|
56
|
+
kVAEAm: "x10l6tqk",
|
|
57
|
+
k87sOh: "x13vifvy",
|
|
58
|
+
khDVqt: "xuxw1ft",
|
|
59
|
+
$$css: true
|
|
60
|
+
},
|
|
61
|
+
tick: {
|
|
62
|
+
kVAEAm: "x10l6tqk",
|
|
63
|
+
krVfgx: "x48lskh",
|
|
64
|
+
kzqmXN: "xfo62xy",
|
|
65
|
+
kZKoxP: "x1e27r4u",
|
|
66
|
+
keTefX: "x1hb08if",
|
|
67
|
+
kaIpWk: "x1nj7uno",
|
|
68
|
+
kWkggS: "x1878pr6 xwvh9j7",
|
|
69
|
+
kGVxlE: "x1bjtdd6",
|
|
70
|
+
kAXs8y: "x1kzknox",
|
|
71
|
+
$$css: true
|
|
72
|
+
},
|
|
73
|
+
legendGrid: {
|
|
74
|
+
kumcoG: "x1ymqfbz",
|
|
75
|
+
k1C7PZ: "x1lcxfya",
|
|
76
|
+
$$css: true
|
|
77
|
+
},
|
|
78
|
+
legendRow: {
|
|
79
|
+
kg3NbH: "xnjsko4",
|
|
80
|
+
$$css: true
|
|
81
|
+
},
|
|
82
|
+
empty: {
|
|
83
|
+
kogj98: "x1ghz6dp",
|
|
84
|
+
kMwMTN: "xe2mk2a",
|
|
85
|
+
$$css: true
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
export { styles };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Sparkline } from "./sparkline.jsx";
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { cssColor, wash } from "../internal/colors.jsx";
|
|
2
|
+
import "@stylexjs/stylex";
|
|
3
|
+
import { Show, createMemo, omit } from "solid-js";
|
|
4
|
+
// src/sparkline/sparkline.tsx
|
|
5
|
+
const isAligned = (data) => Array.isArray(data[0]) || ArrayBuffer.isView(data[0]);
|
|
6
|
+
/** The viewBox is 100 × 100, stretched to the box; strokes don't scale with it. */
|
|
7
|
+
const BOX = 100;
|
|
8
|
+
/**
|
|
9
|
+
* A word-sized trend line with no axes, legend or tooltip, for a stat tile or a table cell. It shows the
|
|
10
|
+
* shape, not values: put the number it supports next to it. A small SVG (no canvas, no uPlot), drawn on the
|
|
11
|
+
* server and themed by CSS.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* <Sparkline label="Revenue, last 30 days: up 12%" data={daily} />
|
|
15
|
+
*/
|
|
16
|
+
function Sparkline(props) {
|
|
17
|
+
const rest = omit(props, "label", "data", "height", "color", "area", "dot", "width", "class");
|
|
18
|
+
const stroke = () => cssColor(props.color ?? "chart-1", 0);
|
|
19
|
+
const height = () => props.height ?? 32;
|
|
20
|
+
const shape = createMemo(() => {
|
|
21
|
+
const d = props.data;
|
|
22
|
+
const xs = isAligned(d) ? d[0] : Array.from(d, (_v, i) => i);
|
|
23
|
+
const ys = isAligned(d) ? d[1] ?? [] : d;
|
|
24
|
+
let xMin = Infinity;
|
|
25
|
+
let xMax = -Infinity;
|
|
26
|
+
let yMin = Infinity;
|
|
27
|
+
let yMax = -Infinity;
|
|
28
|
+
for (let i = 0; i < xs.length; i++) {
|
|
29
|
+
const y = ys[i];
|
|
30
|
+
if (y == null || !Number.isFinite(y)) continue;
|
|
31
|
+
xMin = Math.min(xMin, xs[i]);
|
|
32
|
+
xMax = Math.max(xMax, xs[i]);
|
|
33
|
+
yMin = Math.min(yMin, y);
|
|
34
|
+
yMax = Math.max(yMax, y);
|
|
35
|
+
}
|
|
36
|
+
if (!Number.isFinite(yMin)) return void 0;
|
|
37
|
+
const inset = ((props.width ?? 1.5) / 2 + 1) / height() * BOX;
|
|
38
|
+
const px = (x) => xMax === xMin ? BOX / 2 : (x - xMin) / (xMax - xMin) * BOX;
|
|
39
|
+
const py = (y) => yMax === yMin ? BOX / 2 : inset + (1 - (y - yMin) / (yMax - yMin)) * (BOX - 2 * inset);
|
|
40
|
+
const runs = [];
|
|
41
|
+
let run = [];
|
|
42
|
+
for (let i = 0; i < xs.length; i++) {
|
|
43
|
+
const y = ys[i];
|
|
44
|
+
if (y == null || !Number.isFinite(y)) {
|
|
45
|
+
if (run.length) runs.push(run);
|
|
46
|
+
run = [];
|
|
47
|
+
} else run.push([px(xs[i]), py(y)]);
|
|
48
|
+
}
|
|
49
|
+
if (run.length) runs.push(run);
|
|
50
|
+
const n = (v) => Math.round(v * 100) / 100;
|
|
51
|
+
const path = (r) => r.map(([x, y], i) => `${i ? "L" : "M"}${n(x)} ${n(y)}`).join("");
|
|
52
|
+
const last = runs.at(-1)?.at(-1);
|
|
53
|
+
return {
|
|
54
|
+
line: runs.map(path).join(""),
|
|
55
|
+
area: runs.map((r) => `${path(r)}L${n(r.at(-1)[0])} ${BOX}L${n(r[0][0])} ${BOX}Z`).join(""),
|
|
56
|
+
last: last && {
|
|
57
|
+
left: `${n(last[0])}%`,
|
|
58
|
+
top: `${n(last[1])}%`
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
return <div {...rest} role="img" aria-label={props.label} data-slot="sparkline" class={[{ className: "x1n2onr6 x1lliihq x9f619 xh8yej3 xeuugli" }.className, props.class]} style={{
|
|
63
|
+
height: `${height()}px`,
|
|
64
|
+
"--swatch": stroke()
|
|
65
|
+
}}>
|
|
66
|
+
<Show when={shape()}>
|
|
67
|
+
{(s) => <>
|
|
68
|
+
<svg aria-hidden="true" viewBox={`0 0 ${BOX} ${BOX}`} preserveAspectRatio="none" class="x1lliihq xh8yej3 x5yr21d x1rea2x4">
|
|
69
|
+
<Show when={props.area}>
|
|
70
|
+
<path data-slot="sparkline-area" d={s().area} style={{
|
|
71
|
+
fill: wash(stroke(), 12),
|
|
72
|
+
stroke: "none"
|
|
73
|
+
}} />
|
|
74
|
+
</Show>
|
|
75
|
+
<path data-slot="sparkline-line" d={s().line} vector-effect="non-scaling-stroke" stroke-linejoin="round" stroke-linecap="round" style={{
|
|
76
|
+
fill: "none",
|
|
77
|
+
stroke: stroke(),
|
|
78
|
+
"stroke-width": `${props.width ?? 1.5}px`
|
|
79
|
+
}} />
|
|
80
|
+
</svg>
|
|
81
|
+
<Show when={props.dot && s().last}>
|
|
82
|
+
{(at) => <span data-slot="sparkline-dot" aria-hidden="true" class="x10l6tqk x1v4s8kt xols6we x16rqkct xrmkqiu xacm4fh x11lhmoz x1kzknox" style={at()} />}
|
|
83
|
+
</Show>
|
|
84
|
+
</>}
|
|
85
|
+
</Show>
|
|
86
|
+
</div>;
|
|
87
|
+
}
|
|
88
|
+
export { Sparkline };
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// src/stacked-histogram/histogram.ts
|
|
2
|
+
/** The buckets inside the range, oldest first (they're expected sorted). */
|
|
3
|
+
function visibleBuckets(buckets, range) {
|
|
4
|
+
return buckets.filter((b) => b.time >= range.from && b.time < range.to);
|
|
5
|
+
}
|
|
6
|
+
/** The range the buckets cover: the first one's start to the last one's end. */
|
|
7
|
+
function extent(buckets, bucketMs) {
|
|
8
|
+
const first = buckets[0];
|
|
9
|
+
const last = buckets.at(-1);
|
|
10
|
+
return first && last ? {
|
|
11
|
+
from: first.time,
|
|
12
|
+
to: last.time + bucketMs
|
|
13
|
+
} : void 0;
|
|
14
|
+
}
|
|
15
|
+
const count = (v) => v !== void 0 && Number.isFinite(v) && v > 0 ? v : 0;
|
|
16
|
+
function totalOf(bucket, keys) {
|
|
17
|
+
let sum = 0;
|
|
18
|
+
for (const k of keys) sum += count(bucket.values[k]);
|
|
19
|
+
return sum;
|
|
20
|
+
}
|
|
21
|
+
/** Each series' sum over the buckets from index `a` to `b` (inclusive). */
|
|
22
|
+
function sums(buckets, keys, a, b) {
|
|
23
|
+
const out = keys.map(() => 0);
|
|
24
|
+
for (let i = Math.max(0, a); i <= Math.min(b, buckets.length - 1); i++) keys.forEach((k, s) => out[s] += count(buckets[i].values[k]));
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
/** The bucket under a time: the last one starting at or before it, else the first. -1 with none. */
|
|
28
|
+
function indexAt(buckets, time) {
|
|
29
|
+
if (!buckets.length) return -1;
|
|
30
|
+
let lo = 0;
|
|
31
|
+
let hi = buckets.length - 1;
|
|
32
|
+
while (lo < hi) {
|
|
33
|
+
const mid = lo + hi + 1 >> 1;
|
|
34
|
+
if (buckets[mid].time <= time) lo = mid;
|
|
35
|
+
else hi = mid - 1;
|
|
36
|
+
}
|
|
37
|
+
return lo;
|
|
38
|
+
}
|
|
39
|
+
const r = (v) => Math.round(v * 100) / 100;
|
|
40
|
+
/**
|
|
41
|
+
* One SVG path per series, in a viewBox one unit per bucket wide and `height` pixels tall (stretched across
|
|
42
|
+
* the width; the heights aren't). Series stack from the first, on the baseline, up; a pixel of space
|
|
43
|
+
* separates a bucket's segments so each reads as its own.
|
|
44
|
+
*/
|
|
45
|
+
function barPaths(buckets, keys, range, bucketMs, height) {
|
|
46
|
+
const width = Math.max(1, (range.to - range.from) / bucketMs);
|
|
47
|
+
let max = 0;
|
|
48
|
+
for (const b of buckets) max = Math.max(max, totalOf(b, keys));
|
|
49
|
+
const scale = max > 0 ? (height - 1) / max : 0;
|
|
50
|
+
const gap = .15;
|
|
51
|
+
const paths = keys.map(() => "");
|
|
52
|
+
for (const b of buckets) {
|
|
53
|
+
const x = r((b.time - range.from) / bucketMs + gap / 2);
|
|
54
|
+
let base = 0;
|
|
55
|
+
keys.forEach((k, s) => {
|
|
56
|
+
const h = count(b.values[k]) * scale;
|
|
57
|
+
if (h <= 0) return;
|
|
58
|
+
const lift = base > 0 && h > 1.5 ? 1 : 0;
|
|
59
|
+
const y0 = height - base - lift;
|
|
60
|
+
const y1 = height - base - h;
|
|
61
|
+
paths[s] += `M${x} ${r(y1)}h0.85V${r(y0)}h-0.85Z`;
|
|
62
|
+
base += h;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
width,
|
|
67
|
+
max,
|
|
68
|
+
paths
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
const SECOND = 1e3;
|
|
72
|
+
const MINUTE = 60 * SECOND;
|
|
73
|
+
const HOUR = 60 * MINUTE;
|
|
74
|
+
const DAY = 24 * HOUR;
|
|
75
|
+
const STEPS = [
|
|
76
|
+
SECOND,
|
|
77
|
+
5 * SECOND,
|
|
78
|
+
15 * SECOND,
|
|
79
|
+
30 * SECOND,
|
|
80
|
+
MINUTE,
|
|
81
|
+
2 * MINUTE,
|
|
82
|
+
5 * MINUTE,
|
|
83
|
+
10 * MINUTE,
|
|
84
|
+
15 * MINUTE,
|
|
85
|
+
30 * MINUTE,
|
|
86
|
+
HOUR,
|
|
87
|
+
2 * HOUR,
|
|
88
|
+
3 * HOUR,
|
|
89
|
+
6 * HOUR,
|
|
90
|
+
12 * HOUR,
|
|
91
|
+
DAY,
|
|
92
|
+
2 * DAY,
|
|
93
|
+
7 * DAY,
|
|
94
|
+
14 * DAY
|
|
95
|
+
];
|
|
96
|
+
/**
|
|
97
|
+
* A time zone's offset from UTC at a moment, in milliseconds: the runtime's own zone without one. Ticks
|
|
98
|
+
* fall on that zone's round hours and midnights.
|
|
99
|
+
*/
|
|
100
|
+
function zoneOffset(time, timeZone) {
|
|
101
|
+
if (!timeZone) return -new Date(time).getTimezoneOffset() * MINUTE;
|
|
102
|
+
const parts = new Intl.DateTimeFormat("en-US", {
|
|
103
|
+
timeZone,
|
|
104
|
+
hourCycle: "h23",
|
|
105
|
+
year: "numeric",
|
|
106
|
+
month: "numeric",
|
|
107
|
+
day: "numeric",
|
|
108
|
+
hour: "numeric",
|
|
109
|
+
minute: "numeric",
|
|
110
|
+
second: "numeric"
|
|
111
|
+
}).formatToParts(time);
|
|
112
|
+
const n = (type) => Number(parts.find((p) => p.type === type)?.value ?? 0);
|
|
113
|
+
return Date.UTC(n("year"), n("month") - 1, n("day"), n("hour"), n("minute"), n("second")) - Math.floor(time / SECOND) * SECOND;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Round times across the range, at most `max` of them: on the minute, the hour or midnight in the time
|
|
117
|
+
* zone, the step the smallest that fits. `day` marks a tick at midnight, which is written as a date.
|
|
118
|
+
*/
|
|
119
|
+
function timeTicks(range, max, timeZone) {
|
|
120
|
+
const span = range.to - range.from;
|
|
121
|
+
if (span <= 0 || max < 1) return {
|
|
122
|
+
step: 0,
|
|
123
|
+
ticks: []
|
|
124
|
+
};
|
|
125
|
+
const step = STEPS.find((s) => span / s <= max) ?? Math.ceil(span / max / (7 * DAY)) * 7 * DAY;
|
|
126
|
+
const offset = zoneOffset(range.from, timeZone);
|
|
127
|
+
const ticks = [];
|
|
128
|
+
for (let t = Math.ceil((range.from + offset) / step) * step - offset; t <= range.to; t += step) ticks.push({
|
|
129
|
+
time: t,
|
|
130
|
+
day: (t + offset) % DAY === 0
|
|
131
|
+
});
|
|
132
|
+
return {
|
|
133
|
+
step,
|
|
134
|
+
ticks
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
export { barPaths, extent, indexAt, sums, timeTicks, totalOf, visibleBuckets, zoneOffset };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { StackedHistogram } from "./stacked-histogram.jsx";
|