@sinnstack/ui 0.0.0-stage → 0.3.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 +89 -0
- package/LICENSE +373 -0
- package/README.md +188 -2
- package/docs/blocks.md +373 -0
- package/docs/data.md +128 -0
- package/docs/finishes.md +165 -0
- package/docs/motion.md +166 -0
- package/docs/primitives.md +510 -0
- package/docs/seo.md +191 -0
- package/docs/tokens.md +122 -0
- package/package.json +132 -3
- package/scripts/check-contrast.mjs +180 -0
- package/src/blocks/BlockRenderer.astro +60 -0
- package/src/blocks/CTABanner.astro +121 -0
- package/src/blocks/CTAPair.astro +110 -0
- package/src/blocks/CardGrid.astro +167 -0
- package/src/blocks/ConsentEmbed.astro +125 -0
- package/src/blocks/ConsentManager.astro +147 -0
- package/src/blocks/ConsentScript.astro +47 -0
- package/src/blocks/ContactCard.astro +164 -0
- package/src/blocks/ContactForm.astro +224 -0
- package/src/blocks/ErrorPage.astro +242 -0
- package/src/blocks/FAQ.astro +135 -0
- package/src/blocks/FeatureGrid.astro +148 -0
- package/src/blocks/Hero.astro +171 -0
- package/src/blocks/HeroCarousel.astro +220 -0
- package/src/blocks/HighlightCards.astro +107 -0
- package/src/blocks/LocationHours.astro +311 -0
- package/src/blocks/LogoCloud.astro +135 -0
- package/src/blocks/MaintenancePage.astro +173 -0
- package/src/blocks/MediaText.astro +177 -0
- package/src/blocks/PageHeader.astro +126 -0
- package/src/blocks/ProseSection.astro +98 -0
- package/src/blocks/Reviews.astro +194 -0
- package/src/blocks/RichText.astro +58 -0
- package/src/blocks/StatsBand.astro +117 -0
- package/src/blocks/Steps.astro +141 -0
- package/src/blocks/Testimonials.astro +144 -0
- package/src/blocks/Topbar.astro +158 -0
- package/src/blocks/cms/card-grid.ts +48 -0
- package/src/blocks/cms/consent.ts +16 -0
- package/src/blocks/cms/contact-card.ts +46 -0
- package/src/blocks/cms/contact-form.ts +37 -0
- package/src/blocks/cms/cta-banner.ts +27 -0
- package/src/blocks/cms/cta-pair.ts +30 -0
- package/src/blocks/cms/data-base.ts +5 -0
- package/src/blocks/cms/data.ts +257 -0
- package/src/blocks/cms/faq.ts +38 -0
- package/src/blocks/cms/feature-grid.ts +47 -0
- package/src/blocks/cms/fixtures.ts +591 -0
- package/src/blocks/cms/hero-carousel.ts +36 -0
- package/src/blocks/cms/hero.ts +33 -0
- package/src/blocks/cms/highlight-cards.ts +41 -0
- package/src/blocks/cms/images.ts +37 -0
- package/src/blocks/cms/index.ts +59 -0
- package/src/blocks/cms/location-hours.ts +67 -0
- package/src/blocks/cms/logo-cloud.ts +36 -0
- package/src/blocks/cms/media-text.ts +26 -0
- package/src/blocks/cms/page-header.ts +27 -0
- package/src/blocks/cms/pages.ts +35 -0
- package/src/blocks/cms/prose-section.ts +25 -0
- package/src/blocks/cms/reviews.ts +50 -0
- package/src/blocks/cms/schema-dsl.ts +134 -0
- package/src/blocks/cms/schema.ts +794 -0
- package/src/blocks/cms/shared.ts +63 -0
- package/src/blocks/cms/stats-band.ts +35 -0
- package/src/blocks/cms/steps.ts +40 -0
- package/src/blocks/cms/testimonials.ts +46 -0
- package/src/blocks/cms/topbar.ts +27 -0
- package/src/blocks/cms/types.ts +27 -0
- package/src/blocks/consent.ts +14 -0
- package/src/blocks/icons.ts +47 -0
- package/src/blocks/index.ts +42 -0
- package/src/blocks/internal/Actions.astro +35 -0
- package/src/blocks/internal/BlockHeader.astro +45 -0
- package/src/blocks/internal/BlockShell.astro +58 -0
- package/src/blocks/internal/CarouselScript.astro +7 -0
- package/src/blocks/internal/ConsentEmbedScript.astro +7 -0
- package/src/blocks/internal/ConsentManagerScript.astro +7 -0
- package/src/blocks/internal/HoursScript.astro +7 -0
- package/src/blocks/internal/Placeholder.astro +13 -0
- package/src/blocks/internal/RichTextNode.astro +133 -0
- package/src/blocks/internal/StandaloneDocument.astro +53 -0
- package/src/blocks/internal/consent.ts +96 -0
- package/src/blocks/internal/fields.ts +18 -0
- package/src/blocks/internal/registry-contract.ts +67 -0
- package/src/blocks/internal/reviews.ts +22 -0
- package/src/blocks/internal/slug.ts +12 -0
- package/src/blocks/internal/util.ts +23 -0
- package/src/blocks/lib/contact.ts +92 -0
- package/src/blocks/lib/error-pages.ts +119 -0
- package/src/blocks/lib/faq.ts +37 -0
- package/src/blocks/lib/hours.ts +201 -0
- package/src/blocks/lib/maintenance.ts +129 -0
- package/src/blocks/lib/markdown.ts +350 -0
- package/src/blocks/lib/rich-text.ts +132 -0
- package/src/blocks/registry.ts +79 -0
- package/src/blocks/scripts/carousel.ts +218 -0
- package/src/blocks/scripts/consent-embed.ts +105 -0
- package/src/blocks/scripts/consent-manager.ts +112 -0
- package/src/blocks/scripts/consent-store.ts +86 -0
- package/src/blocks/scripts/error-page.ts +71 -0
- package/src/blocks/scripts/hours.ts +107 -0
- package/src/blocks/scripts/maintenance.ts +109 -0
- package/src/blocks/styles/blocks.css +1320 -0
- package/src/blocks/styles/consent.css +102 -0
- package/src/blocks/styles/faq.css +28 -0
- package/src/blocks/styles/location.css +92 -0
- package/src/blocks/styles/prose-section.css +13 -0
- package/src/blocks/styles/quotes.css +123 -0
- package/src/blocks/styles/rich-text.css +14 -0
- package/src/blocks/types.ts +293 -0
- package/src/data/ChartAxis.astro +19 -0
- package/src/data/ChartContainer.astro +152 -0
- package/src/data/ChartLegend.astro +25 -0
- package/src/data/ChartSeries.astro +14 -0
- package/src/data/ChartTable.astro +17 -0
- package/src/data/ChartTooltip.astro +20 -0
- package/src/data/Sparkline.astro +55 -0
- package/src/data/cms.ts +130 -0
- package/src/data/index.ts +10 -0
- package/src/data/internal/scale.ts +107 -0
- package/src/data/lib/chart.ts +459 -0
- package/src/data/lib/render.ts +219 -0
- package/src/data/lib/sparkline.ts +117 -0
- package/src/data/schema.ts +65 -0
- package/src/data/scripts/chart.ts +324 -0
- package/src/data/styles/chart.css +392 -0
- package/src/finishes/aurora.css +75 -0
- package/src/finishes/autumn.css +87 -0
- package/src/finishes/blueprint.css +69 -0
- package/src/finishes/brutalist.css +55 -0
- package/src/finishes/clay.css +48 -0
- package/src/finishes/day-cycle.css +46 -0
- package/src/finishes/fog.css +71 -0
- package/src/finishes/glass.css +149 -0
- package/src/finishes/holographic.css +91 -0
- package/src/finishes/night.css +77 -0
- package/src/finishes/paper.css +58 -0
- package/src/finishes/polished-glass.css +159 -0
- package/src/finishes/rain.css +84 -0
- package/src/finishes/scripts/day-cycle.ts +30 -0
- package/src/finishes/scripts/holographic.ts +37 -0
- package/src/finishes/scripts/spotlight.ts +30 -0
- package/src/finishes/spotlight.css +41 -0
- package/src/finishes/spring.css +80 -0
- package/src/finishes/terminal.css +49 -0
- package/src/motion/Marquee.astro +90 -0
- package/src/motion/MotionProvider.astro +50 -0
- package/src/motion/PageTransitions.astro +61 -0
- package/src/motion/Parallax.astro +44 -0
- package/src/motion/ParallaxLayer.astro +31 -0
- package/src/motion/RevealOnScroll.astro +59 -0
- package/src/motion/SceneRegistry.astro +38 -0
- package/src/motion/ScrollScene.astro +81 -0
- package/src/motion/index.ts +12 -0
- package/src/motion/integration.ts +48 -0
- package/src/motion/runtime/page-transitions.ts +68 -0
- package/src/motion/runtime/runtime.ts +180 -0
- package/src/motion/runtime/scenes.ts +248 -0
- package/src/motion/runtime/types.ts +103 -0
- package/src/motion/styles/motion.css +332 -0
- package/src/motion/transitions.ts +74 -0
- package/src/motion/virtual.d.ts +4 -0
- package/src/primitives/Accordion.astro +129 -0
- package/src/primitives/Breadcrumbs.astro +50 -0
- package/src/primitives/BulletList.astro +69 -0
- package/src/primitives/Button.astro +73 -0
- package/src/primitives/ButtonGroup.astro +54 -0
- package/src/primitives/Callout.astro +137 -0
- package/src/primitives/Caption.astro +35 -0
- package/src/primitives/Card.astro +72 -0
- package/src/primitives/CardContent.astro +24 -0
- package/src/primitives/CardFooter.astro +36 -0
- package/src/primitives/CardHeader.astro +86 -0
- package/src/primitives/Checkbox.astro +38 -0
- package/src/primitives/ChoiceGroup.astro +23 -0
- package/src/primitives/Cluster.astro +49 -0
- package/src/primitives/Container.astro +44 -0
- package/src/primitives/DescriptionList.astro +83 -0
- package/src/primitives/Dialog.astro +38 -0
- package/src/primitives/Disclosure.astro +67 -0
- package/src/primitives/Divider.astro +93 -0
- package/src/primitives/Drawer.astro +33 -0
- package/src/primitives/Fieldset.astro +80 -0
- package/src/primitives/Flex.astro +64 -0
- package/src/primitives/FocusRing.astro +63 -0
- package/src/primitives/FocusTrap.astro +64 -0
- package/src/primitives/Footer.astro +155 -0
- package/src/primitives/Form.astro +68 -0
- package/src/primitives/FormDescription.astro +26 -0
- package/src/primitives/FormError.astro +106 -0
- package/src/primitives/FormField.astro +72 -0
- package/src/primitives/Grid.astro +64 -0
- package/src/primitives/Heading.astro +45 -0
- package/src/primitives/IconButton.astro +48 -0
- package/src/primitives/Image.astro +125 -0
- package/src/primitives/Input.astro +86 -0
- package/src/primitives/Label.astro +50 -0
- package/src/primitives/Link.astro +81 -0
- package/src/primitives/LinkButton.astro +66 -0
- package/src/primitives/List.astro +39 -0
- package/src/primitives/ListItem.astro +178 -0
- package/src/primitives/MediaObject.astro +66 -0
- package/src/primitives/MetricCard.astro +278 -0
- package/src/primitives/NavLink.astro +81 -0
- package/src/primitives/NavMenu.astro +109 -0
- package/src/primitives/Navbar.astro +197 -0
- package/src/primitives/Panel.astro +153 -0
- package/src/primitives/Picture.astro +167 -0
- package/src/primitives/Popover.astro +95 -0
- package/src/primitives/Prose.astro +45 -0
- package/src/primitives/Radio.astro +21 -0
- package/src/primitives/Rating.astro +135 -0
- package/src/primitives/ReducedMotion.astro +90 -0
- package/src/primitives/SegmentedControl.astro +132 -0
- package/src/primitives/Select.astro +109 -0
- package/src/primitives/Sheet.astro +45 -0
- package/src/primitives/SkipLink.astro +31 -0
- package/src/primitives/Slider.astro +186 -0
- package/src/primitives/Spacer.astro +35 -0
- package/src/primitives/Stack.astro +51 -0
- package/src/primitives/Surface.astro +70 -0
- package/src/primitives/Switch.astro +22 -0
- package/src/primitives/Table.astro +382 -0
- package/src/primitives/Tabs.astro +126 -0
- package/src/primitives/Text.astro +56 -0
- package/src/primitives/Textarea.astro +75 -0
- package/src/primitives/Thumbnail.astro +154 -0
- package/src/primitives/Toggle.astro +99 -0
- package/src/primitives/Tooltip.astro +50 -0
- package/src/primitives/VisuallyHidden.astro +29 -0
- package/src/primitives/index.ts +103 -0
- package/src/primitives/internal/AccordionScript.astro +7 -0
- package/src/primitives/internal/ChoiceField.astro +91 -0
- package/src/primitives/internal/FocusTrapScript.astro +8 -0
- package/src/primitives/internal/FormValidationScript.astro +10 -0
- package/src/primitives/internal/IndeterminateScript.astro +13 -0
- package/src/primitives/internal/ModalPanel.astro +114 -0
- package/src/primitives/internal/OverlayScript.astro +7 -0
- package/src/primitives/internal/ReducedMotionScript.astro +7 -0
- package/src/primitives/internal/SliderScript.astro +7 -0
- package/src/primitives/internal/SparklineScript.astro +7 -0
- package/src/primitives/internal/TableSortScript.astro +7 -0
- package/src/primitives/internal/TabsScript.astro +7 -0
- package/src/primitives/internal/TooltipScript.astro +7 -0
- package/src/primitives/internal/image-plan.ts +82 -0
- package/src/primitives/internal/image.ts +129 -0
- package/src/primitives/internal/slider.ts +170 -0
- package/src/primitives/lib/deprecate.ts +47 -0
- package/src/primitives/lib/focus-trap.ts +197 -0
- package/src/primitives/lib/focus.ts +60 -0
- package/src/primitives/lib/motion.ts +68 -0
- package/src/primitives/scripts/accordion.ts +123 -0
- package/src/primitives/scripts/form-validation.ts +225 -0
- package/src/primitives/scripts/overlay.ts +71 -0
- package/src/primitives/scripts/reduced-motion.ts +59 -0
- package/src/primitives/scripts/slider.ts +150 -0
- package/src/primitives/scripts/sparkline.ts +131 -0
- package/src/primitives/scripts/table-sort.ts +68 -0
- package/src/primitives/scripts/tabs.ts +151 -0
- package/src/primitives/scripts/tooltip.ts +28 -0
- package/src/primitives/styles/a11y.css +104 -0
- package/src/primitives/styles/accordion.css +53 -0
- package/src/primitives/styles/breadcrumbs.css +59 -0
- package/src/primitives/styles/bullet-list.css +89 -0
- package/src/primitives/styles/button.css +237 -0
- package/src/primitives/styles/choice.css +164 -0
- package/src/primitives/styles/cluster.css +24 -0
- package/src/primitives/styles/container.css +19 -0
- package/src/primitives/styles/data.css +849 -0
- package/src/primitives/styles/disclosure.css +140 -0
- package/src/primitives/styles/divider.css +50 -0
- package/src/primitives/styles/flex.css +42 -0
- package/src/primitives/styles/form.css +241 -0
- package/src/primitives/styles/grid.css +36 -0
- package/src/primitives/styles/media.css +247 -0
- package/src/primitives/styles/nav.css +792 -0
- package/src/primitives/styles/overlay.css +362 -0
- package/src/primitives/styles/prose.css +146 -0
- package/src/primitives/styles/scale.css +175 -0
- package/src/primitives/styles/segmented.css +165 -0
- package/src/primitives/styles/slider.css +170 -0
- package/src/primitives/styles/spacer.css +17 -0
- package/src/primitives/styles/stack.css +25 -0
- package/src/primitives/styles/surface.css +403 -0
- package/src/primitives/styles/tabs.css +223 -0
- package/src/primitives/styles/typography.css +144 -0
- package/src/primitives/styles/utilities.css +24 -0
- package/src/primitives/types.ts +385 -0
- package/src/primitives/utils.ts +254 -0
- package/src/seo/JSONLD.astro +35 -0
- package/src/seo/SEOHead.astro +47 -0
- package/src/seo/cms.ts +59 -0
- package/src/seo/index.ts +10 -0
- package/src/seo/integration.ts +268 -0
- package/src/seo/internal/url.ts +68 -0
- package/src/seo/lib/jsonld.ts +342 -0
- package/src/seo/lib/meta.ts +205 -0
- package/src/seo/lib/redirects.ts +293 -0
- package/src/seo/lib/robots.ts +33 -0
- package/src/seo/lib/rss.ts +98 -0
- package/src/seo/lib/sitemap.ts +105 -0
- package/src/seo/schema.ts +43 -0
- package/src/tokens/base.css +71 -0
- package/src/tokens/tokens.css +210 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* ChartAxis — labeled axis with ticks and gridlines, from the chart's scales.
|
|
4
|
+
* - y: nice round ticks; numbers in full under 10,000 and compact on an axis that reaches it
|
|
5
|
+
* (one style per axis); horizontal hairline gridlines (`grid={false}` to drop them).
|
|
6
|
+
* - x: the baseline, and tick labels that thin out by container width (every 2nd, 4th… label)
|
|
7
|
+
* so they never collide; time axes get calendar ticks ("Jan", "Apr", "Jan 2027").
|
|
8
|
+
* - `title` names the units ("Visits", "USD"); defaults to the chart's xTitle / yTitle.
|
|
9
|
+
* Place inside ChartContainer.
|
|
10
|
+
*/
|
|
11
|
+
import type { ChartModel } from './lib/chart';
|
|
12
|
+
import { renderXAxis, renderYAxis } from './lib/render';
|
|
13
|
+
|
|
14
|
+
type Props = { chart: ChartModel; axis: 'x' | 'y'; grid?: boolean; title?: string };
|
|
15
|
+
const { chart, axis, grid, title } = Astro.props;
|
|
16
|
+
const html = axis === 'y' ? renderYAxis(chart, { grid, title }) : renderXAxis(chart, { grid, title });
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
<Fragment set:html={html} />
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* ChartContainer — the frame every chart sits in: title, description, legend, the plot, the data
|
|
4
|
+
* table (the chart's accessible twin) and the source. Responsive with container queries.
|
|
5
|
+
*
|
|
6
|
+
* <ChartContainer chart={chart} title="Site visits per month" description="All locations, 2026"
|
|
7
|
+
* source={{ label: 'Analytics export', href: '/data' }}>
|
|
8
|
+
* <ChartLegend slot="legend" chart={chart} />
|
|
9
|
+
* <ChartAxis chart={chart} axis="y" /> <ChartAxis chart={chart} axis="x" />
|
|
10
|
+
* <ChartSeries chart={chart} /> <ChartTooltip chart={chart} />
|
|
11
|
+
* </ChartContainer>
|
|
12
|
+
*
|
|
13
|
+
* - With `chart`, it adds a sentence summary for screen readers and a "Show data table" table
|
|
14
|
+
* (with CSV download) under the plot. Every value is reachable without the tooltip.
|
|
15
|
+
* - `height` is the plot height; the x-axis band is added on top of it (no nested scrolling).
|
|
16
|
+
* - States: `loading` (skeleton), `refetching` (keeps the previous render, dimmed, no layout
|
|
17
|
+
* jump), `empty` and `error` (same footprint as the plot; slots `empty` / `error` for actions).
|
|
18
|
+
* - `surface`: the background the chart sits on (`raised` inside cards), so the 2px gaps and dot
|
|
19
|
+
* rings match it.
|
|
20
|
+
* - Filters belong in one row above the charts they scope, never inside a chart.
|
|
21
|
+
*/
|
|
22
|
+
import Heading from '../primitives/Heading.astro';
|
|
23
|
+
import type { HeadingLevel } from '../primitives/types';
|
|
24
|
+
import type { ChartModel } from './lib/chart';
|
|
25
|
+
import { renderTable } from './lib/render';
|
|
26
|
+
import './styles/chart.css';
|
|
27
|
+
import { renameProp } from '../primitives/lib/deprecate';
|
|
28
|
+
import { uid } from '../primitives/utils';
|
|
29
|
+
|
|
30
|
+
type Props = {
|
|
31
|
+
chart?: ChartModel;
|
|
32
|
+
title: string;
|
|
33
|
+
headingLevel?: HeadingLevel;
|
|
34
|
+
description?: string;
|
|
35
|
+
source?: string | { label: string; href?: string };
|
|
36
|
+
height?: 's' | 'm' | 'l' | 'xl';
|
|
37
|
+
/** Card background: `default`, `raised` or `subtle` (Surface's words). */
|
|
38
|
+
background?: 'default' | 'raised' | 'subtle';
|
|
39
|
+
/** @deprecated Use `background`. */
|
|
40
|
+
surface?: 'default' | 'raised' | 'subtle';
|
|
41
|
+
state?: 'ready' | 'loading' | 'refetching' | 'empty' | 'error';
|
|
42
|
+
emptyMessage?: string;
|
|
43
|
+
errorMessage?: string;
|
|
44
|
+
/** Show the data table under the chart: closed (default), open, or not at all. */
|
|
45
|
+
table?: boolean | 'open';
|
|
46
|
+
tableLabel?: string;
|
|
47
|
+
/** Offer a CSV download in the table view. */
|
|
48
|
+
download?: boolean;
|
|
49
|
+
id?: string;
|
|
50
|
+
class?: string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const {
|
|
54
|
+
chart,
|
|
55
|
+
title,
|
|
56
|
+
headingLevel = 3,
|
|
57
|
+
description,
|
|
58
|
+
source,
|
|
59
|
+
height = 'm',
|
|
60
|
+
background: backgroundProp,
|
|
61
|
+
surface,
|
|
62
|
+
state = 'ready',
|
|
63
|
+
emptyMessage = 'No data for this period.',
|
|
64
|
+
errorMessage = 'This chart couldn’t load.',
|
|
65
|
+
table = true,
|
|
66
|
+
tableLabel = 'Show data table',
|
|
67
|
+
download = true,
|
|
68
|
+
id,
|
|
69
|
+
class: className,
|
|
70
|
+
} = Astro.props;
|
|
71
|
+
// `data-surface` is the blocks' strong/tinted section attribute; the chart card says data-background.
|
|
72
|
+
const background = renameProp('ChartContainer', 'background', backgroundProp, 'surface', surface) ?? 'default';
|
|
73
|
+
|
|
74
|
+
const base = id ?? chart?.id ?? uid('sinn-chart');
|
|
75
|
+
const titleId = `${base}-title`;
|
|
76
|
+
const descId = description ? `${base}-desc` : undefined;
|
|
77
|
+
const summaryId = `${base}-summary`;
|
|
78
|
+
const ready = state === 'ready' || state === 'refetching';
|
|
79
|
+
const src = typeof source === 'string' ? { label: source } : source;
|
|
80
|
+
const fileName = title.replace(/[^\w\- ]+/g, '').trim().replace(/\s+/g, '-').toLowerCase() || 'chart-data';
|
|
81
|
+
const hasToolbar = Astro.slots.has('legend') || Astro.slots.has('toolbar');
|
|
82
|
+
if (chart?.warnings.length && import.meta.env?.DEV) console.warn(`[Chart] "${title}"\n - ${chart.warnings.join('\n - ')}`);
|
|
83
|
+
---
|
|
84
|
+
|
|
85
|
+
<figure
|
|
86
|
+
class:list={['sinn-chart', className]}
|
|
87
|
+
id={base}
|
|
88
|
+
data-height={height === 'm' ? undefined : height}
|
|
89
|
+
data-background={background === 'default' ? undefined : background}
|
|
90
|
+
data-state={state === 'ready' ? undefined : state}
|
|
91
|
+
data-type={chart?.type}
|
|
92
|
+
data-y-ch={chart?.yLabelCh}
|
|
93
|
+
data-bar-pct={chart?.type === 'bar' ? chart.barPct : undefined}
|
|
94
|
+
aria-labelledby={titleId}
|
|
95
|
+
aria-describedby={descId}
|
|
96
|
+
>
|
|
97
|
+
<figcaption class="sinn-chart__header">
|
|
98
|
+
<Heading level={headingLevel} size="m" weight="semibold" id={titleId} class="sinn-chart__title">{title}</Heading>
|
|
99
|
+
{description && <p class="sinn-chart__description" id={descId}>{description}</p>}
|
|
100
|
+
</figcaption>
|
|
101
|
+
{
|
|
102
|
+
hasToolbar && ready && (
|
|
103
|
+
<div class="sinn-chart__toolbar">
|
|
104
|
+
<slot name="legend" />
|
|
105
|
+
<slot name="toolbar" />
|
|
106
|
+
</div>
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
<div class="sinn-chart__body" aria-busy={state === 'loading' || state === 'refetching' ? 'true' : undefined}>
|
|
110
|
+
{
|
|
111
|
+
state === 'loading' ? (
|
|
112
|
+
<div class="sinn-chart__skeleton" role="status">
|
|
113
|
+
<span class="sinn-sr-only">Loading chart…</span>
|
|
114
|
+
</div>
|
|
115
|
+
) : state === 'empty' ? (
|
|
116
|
+
<div class="sinn-chart__state" data-kind="empty" role="status">
|
|
117
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" aria-hidden="true"><path d="M3.5 20.5h17M6.5 16v-3M11 16v-6M15.5 16v-4M20 16V8" /></svg>
|
|
118
|
+
<p>{emptyMessage}</p>
|
|
119
|
+
<slot name="empty" />
|
|
120
|
+
</div>
|
|
121
|
+
) : state === 'error' ? (
|
|
122
|
+
<div class="sinn-chart__state" data-kind="error" role="alert">
|
|
123
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.75" stroke-linecap="round" aria-hidden="true"><circle cx="12" cy="12" r="9" /><path d="M12 7.5v5.5" /><circle cx="12" cy="16.5" r="0.6" fill="currentColor" /></svg>
|
|
124
|
+
<p>{errorMessage}</p>
|
|
125
|
+
<slot name="error" />
|
|
126
|
+
</div>
|
|
127
|
+
) : (
|
|
128
|
+
<>
|
|
129
|
+
{chart && <p class="sinn-sr-only" id={summaryId}>{chart.summary}</p>}
|
|
130
|
+
<div class="sinn-chart__plot">
|
|
131
|
+
<slot />
|
|
132
|
+
</div>
|
|
133
|
+
</>
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
</div>
|
|
137
|
+
{
|
|
138
|
+
table && chart && ready && (
|
|
139
|
+
<details class="sinn-chart__data" open={table === 'open'}>
|
|
140
|
+
<summary>{tableLabel}</summary>
|
|
141
|
+
<Fragment set:html={renderTable(chart, { caption: title, hideCaption: true, download: download ? fileName : false })} />
|
|
142
|
+
</details>
|
|
143
|
+
)
|
|
144
|
+
}
|
|
145
|
+
{
|
|
146
|
+
src && (
|
|
147
|
+
<p class="sinn-chart__source">
|
|
148
|
+
Source: {src.href ? <a href={src.href}>{src.label}</a> : src.label}
|
|
149
|
+
</p>
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
</figure>
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* ChartLegend — names each series next to a key that mirrors its mark (a line for line charts, a
|
|
4
|
+
* rounded square for bars and areas). Shown for 2+ series; one series needs none (the title names
|
|
5
|
+
* it) unless `force`.
|
|
6
|
+
* - `toggle` (default): each entry is a button that shows/hides its series; Alt/Option-click shows
|
|
7
|
+
* only that one; "Show all" appears when something is hidden. Colors never change, the y scale
|
|
8
|
+
* stays put, and at least one series stays visible.
|
|
9
|
+
* Put it in ChartContainer's `legend` slot.
|
|
10
|
+
*/
|
|
11
|
+
import type { ChartModel } from './lib/chart';
|
|
12
|
+
import { renderLegend } from './lib/render';
|
|
13
|
+
|
|
14
|
+
type Props = { chart: ChartModel; toggle?: boolean; force?: boolean; label?: string };
|
|
15
|
+
const { chart, toggle = true, force, label } = Astro.props;
|
|
16
|
+
const html = renderLegend(chart, { toggle, force, label });
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
<Fragment set:html={html} />
|
|
20
|
+
{/* Top-level, not inside a condition: a <script> nested in an expression may be emitted inline
|
|
21
|
+
instead of bundled, which a strict CSP blocks. The module is shared with
|
|
22
|
+
ChartTooltip, loaded once, and does nothing when no toggles are on the page. */}
|
|
23
|
+
<script>
|
|
24
|
+
import './scripts/chart';
|
|
25
|
+
</script>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* ChartSeries — the marks: 2px lines (end dot with a surface ring), 10% area washes, or bars
|
|
4
|
+
* (≤ 24px, 4px rounded data end, square baseline, 2px surface gaps when grouped or stacked).
|
|
5
|
+
* Colors come from the validated series slots and follow the series, never its rank.
|
|
6
|
+
*/
|
|
7
|
+
import type { ChartModel } from './lib/chart';
|
|
8
|
+
import { renderSeries } from './lib/render';
|
|
9
|
+
|
|
10
|
+
type Props = { chart: ChartModel; endDots?: boolean };
|
|
11
|
+
const { chart, endDots } = Astro.props;
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
<Fragment set:html={renderSeries(chart, { endDots })} />
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* ChartTable — the chart's data as a real table: row headers for x, one column per series,
|
|
4
|
+
* values formatted like the axis and tooltip (tabular figures, right-aligned), scrollable and
|
|
5
|
+
* keyboard-focusable when wide. Optional CSV download of the raw values.
|
|
6
|
+
* ChartContainer renders it automatically (in "Show data table"); use it directly to place the
|
|
7
|
+
* table elsewhere or show it open.
|
|
8
|
+
*/
|
|
9
|
+
import type { ChartModel } from './lib/chart';
|
|
10
|
+
import { renderTable } from './lib/render';
|
|
11
|
+
import './styles/chart.css';
|
|
12
|
+
|
|
13
|
+
type Props = { chart: ChartModel; caption?: string; hideCaption?: boolean; download?: string | false };
|
|
14
|
+
const { chart, caption, hideCaption, download = false } = Astro.props;
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
<Fragment set:html={renderTable(chart, { caption, hideCaption, download })} />
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* ChartTooltip — exact values under the pointer or keyboard focus.
|
|
4
|
+
* - Lines/areas: a crosshair snaps to the nearest x; the tooltip lists every series there.
|
|
5
|
+
* - Bars: the band lifts; the bar under the pointer leads the list (stacked: plus the total).
|
|
6
|
+
* - Keyboard: Tab to the plot, ←/→ (Home/End, PageUp/PageDown), Esc; each step is announced.
|
|
7
|
+
* - Values lead (strong), names follow; series keyed with a short line, not a box.
|
|
8
|
+
* The tooltip enhances, never gates: every value is also in the table view.
|
|
9
|
+
*/
|
|
10
|
+
import type { ChartModel } from './lib/chart';
|
|
11
|
+
import { renderTooltip } from './lib/render';
|
|
12
|
+
|
|
13
|
+
type Props = { chart: ChartModel; label?: string };
|
|
14
|
+
const { chart, label } = Astro.props;
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
<Fragment set:html={renderTooltip(chart, { label })} />
|
|
18
|
+
<script>
|
|
19
|
+
import './scripts/chart';
|
|
20
|
+
</script>
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* Sparkline — a compact, axis-free trend inline with text or beside a metric.
|
|
4
|
+
*
|
|
5
|
+
* Visits are up this year <Sparkline values={monthly} labels={months} label="Visits" format="integer" />.
|
|
6
|
+
*
|
|
7
|
+
* - The line is the de-emphasis gray; the current (or max/min) point is the accent dot.
|
|
8
|
+
* - `size`: `inline` (sits in a line of text, 1em tall), `s`, `m`, or `fill` (full width).
|
|
9
|
+
* - `type`: line, area or bar. `reference` draws a target/average as a dashed hairline.
|
|
10
|
+
* - Read as one sentence by screen readers ("Visits: 1,240 in Jan to 1,390 in Dec (up 12%)…").
|
|
11
|
+
* - `interactive`: hover/focus readout of each value (←/→ on keyboard).
|
|
12
|
+
*/
|
|
13
|
+
import type { ValueFormat } from '../primitives/types';
|
|
14
|
+
import { defineSparkline } from './lib/sparkline';
|
|
15
|
+
import { renderSparkline } from './lib/render';
|
|
16
|
+
import './styles/chart.css';
|
|
17
|
+
|
|
18
|
+
type Props = {
|
|
19
|
+
values: Array<number | null | undefined>;
|
|
20
|
+
labels?: string[];
|
|
21
|
+
label?: string;
|
|
22
|
+
format?: ValueFormat;
|
|
23
|
+
type?: 'line' | 'area' | 'bar';
|
|
24
|
+
highlight?: 'last' | 'max' | 'min' | 'none';
|
|
25
|
+
reference?: number;
|
|
26
|
+
referenceLabel?: string;
|
|
27
|
+
zero?: boolean;
|
|
28
|
+
size?: 'inline' | 's' | 'm' | 'fill';
|
|
29
|
+
interactive?: boolean;
|
|
30
|
+
/** Series color slot for the line (default: the de-emphasis gray). */
|
|
31
|
+
slot?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 'muted';
|
|
32
|
+
locale?: string;
|
|
33
|
+
class?: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const { size = 'inline', interactive = false, slot, class: className, ...input } = Astro.props;
|
|
37
|
+
const model = defineSparkline(input);
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
<span
|
|
41
|
+
class:list={['sinn-spark', className]}
|
|
42
|
+
data-size={size === 'inline' ? undefined : size}
|
|
43
|
+
data-direction={model.direction}
|
|
44
|
+
role="img"
|
|
45
|
+
aria-label={model.summary}
|
|
46
|
+
tabindex={interactive ? 0 : undefined}
|
|
47
|
+
data-sinn-spark={interactive ? '' : undefined}
|
|
48
|
+
set:html={renderSparkline(model, { interactive, slot })}
|
|
49
|
+
/>{interactive && <span class="sinn-sr-only" aria-live="polite" data-sinn-chart-live />}
|
|
50
|
+
{/* Top-level, not inside `{interactive && …}`: a <script> nested in an expression may be emitted
|
|
51
|
+
inline instead of bundled, which a strict CSP blocks. Deduped with the chart script; it does
|
|
52
|
+
nothing for sparklines without data-sinn-spark. */}
|
|
53
|
+
<script>
|
|
54
|
+
import './scripts/chart';
|
|
55
|
+
</script>
|
package/src/data/cms.ts
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Editor-managed charts: maps a stored `chart` object (small, hand-entered data such as a page's
|
|
3
|
+
* "by the numbers" section or a report summary) and a `sparkline` object to component input.
|
|
4
|
+
* Charts fed from an API or a database should call defineChart() in code instead.
|
|
5
|
+
*
|
|
6
|
+
* const { chart, frame, errors } = chartFromCms(block.chart);
|
|
7
|
+
* <ChartContainer chart={chart} {...frame}> … </ChartContainer>
|
|
8
|
+
*
|
|
9
|
+
* Editors type one value per line, lined up with the labels; an empty line is a missing value.
|
|
10
|
+
* "1,240", "$9,000" and "91%" are all understood.
|
|
11
|
+
*/
|
|
12
|
+
import type { ValueFormat } from '../primitives/types';
|
|
13
|
+
import { defineChart, type ChartInput, type ChartModel } from './lib/chart';
|
|
14
|
+
import type { SparklineInput } from './lib/sparkline';
|
|
15
|
+
import { safeHref } from '../blocks/lib/contact';
|
|
16
|
+
|
|
17
|
+
export interface CmsChart {
|
|
18
|
+
title?: string | null;
|
|
19
|
+
description?: string | null;
|
|
20
|
+
type?: 'line' | 'area' | 'bar' | null;
|
|
21
|
+
stacked?: boolean | null;
|
|
22
|
+
format?: string | null;
|
|
23
|
+
labels?: string | null;
|
|
24
|
+
series?: Array<{ label?: string | null; values?: string | null } | null> | null;
|
|
25
|
+
yTitle?: string | null;
|
|
26
|
+
sourceLabel?: string | null;
|
|
27
|
+
sourceHref?: string | null;
|
|
28
|
+
height?: 's' | 'm' | 'l' | null;
|
|
29
|
+
tableOpen?: boolean | null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface CmsSparkline {
|
|
33
|
+
label?: string | null;
|
|
34
|
+
values?: string | null;
|
|
35
|
+
labels?: string | null;
|
|
36
|
+
format?: string | null;
|
|
37
|
+
type?: 'line' | 'area' | 'bar' | null;
|
|
38
|
+
reference?: number | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const lines = (s?: string | null) => (s ?? '').replace(/\r\n?/g, '\n').replace(/\n+$/, '').split('\n');
|
|
42
|
+
|
|
43
|
+
function toFormat(f?: string | null): ValueFormat {
|
|
44
|
+
if (f === 'currency-compact') return { kind: 'currency', compact: 'auto' };
|
|
45
|
+
if (f === 'integer' || f === 'number' || f === 'percent' || f === 'currency') return f;
|
|
46
|
+
return 'number';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** "1,240" → 1240, "$9,000" → 9000, "91%" → 0.91, "" → null; anything else is an error. */
|
|
50
|
+
export function parseCmsNumber(raw: string): number | null | undefined {
|
|
51
|
+
const t = raw.trim().replace(/[\s,$€£]/g, '').replace(/[−–]/g, '-');
|
|
52
|
+
if (!t) return null;
|
|
53
|
+
const pct = t.endsWith('%');
|
|
54
|
+
const n = Number(pct ? t.slice(0, -1) : t);
|
|
55
|
+
if (!Number.isFinite(n)) return undefined;
|
|
56
|
+
return pct ? n / 100 : n;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function parseValues(raw: string | null | undefined, format: ValueFormat, name: string, errors: string[]): Array<number | null> {
|
|
60
|
+
const isPercent = (typeof format === 'string' ? format : format.kind) === 'percent';
|
|
61
|
+
return lines(raw).map((line, i) => {
|
|
62
|
+
const v = parseCmsNumber(line);
|
|
63
|
+
if (v === undefined) {
|
|
64
|
+
errors.push(`${name}, line ${i + 1}: "${line.trim()}" isn't a number.`);
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
// In a percent chart "91" means 91%, as editors type it.
|
|
68
|
+
return v !== null && isPercent && !line.includes('%') && Math.abs(v) > 1 ? v / 100 : v;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const slug = (s: string) => s.toLowerCase().normalize('NFKD').replace(/[^\w]+/g, '-').replace(/^-|-$/g, '') || 'chart';
|
|
73
|
+
|
|
74
|
+
/** CMS chart → a chart model and ChartContainer props. Problems come back as plain sentences. */
|
|
75
|
+
export function chartFromCms(data: CmsChart | null | undefined): {
|
|
76
|
+
chart: ChartModel;
|
|
77
|
+
frame: { title: string; description?: string; source?: { label: string; href?: string }; height?: 's' | 'm' | 'l'; table: true | 'open' };
|
|
78
|
+
errors: string[];
|
|
79
|
+
} {
|
|
80
|
+
const errors: string[] = [];
|
|
81
|
+
const title = data?.title?.trim() || 'Chart';
|
|
82
|
+
const format = toFormat(data?.format);
|
|
83
|
+
const x = lines(data?.labels).map((l) => l.trim());
|
|
84
|
+
const series = (data?.series ?? []).filter(Boolean).map((s, i) => {
|
|
85
|
+
const label = s!.label?.trim() || `Series ${i + 1}`;
|
|
86
|
+
const values = parseValues(s!.values, format, label, errors);
|
|
87
|
+
if (values.length !== x.length) errors.push(`${label} has ${values.length} values for ${x.length} labels.`);
|
|
88
|
+
return { label, values: x.map((_, k) => values[k] ?? null) };
|
|
89
|
+
});
|
|
90
|
+
if (!series.length) errors.push('Add at least one series.');
|
|
91
|
+
const input: ChartInput = {
|
|
92
|
+
id: slug(title),
|
|
93
|
+
type: data?.type ?? 'line',
|
|
94
|
+
stacked: Boolean(data?.stacked),
|
|
95
|
+
x,
|
|
96
|
+
yFormat: format,
|
|
97
|
+
yTitle: data?.yTitle?.trim() || undefined,
|
|
98
|
+
// "Site visits" → "site visits" for the summary sentence; leave acronyms ("SEO visits") alone.
|
|
99
|
+
subject: /^[A-Z][a-z]/.test(title) ? title.charAt(0).toLowerCase() + title.slice(1) : title,
|
|
100
|
+
series: series.length ? series : [{ label: title, values: x.map(() => null) }],
|
|
101
|
+
};
|
|
102
|
+
const chart = defineChart(input);
|
|
103
|
+
return {
|
|
104
|
+
chart,
|
|
105
|
+
frame: {
|
|
106
|
+
title,
|
|
107
|
+
description: data?.description?.trim() || undefined,
|
|
108
|
+
source: data?.sourceLabel?.trim() ? { label: data.sourceLabel.trim(), href: safeHref(data.sourceHref) } : undefined,
|
|
109
|
+
height: data?.height ?? undefined,
|
|
110
|
+
table: data?.tableOpen ? 'open' : true,
|
|
111
|
+
},
|
|
112
|
+
errors: [...errors, ...chart.warnings],
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** CMS sparkline → Sparkline props. */
|
|
117
|
+
export function sparklineFromCms(data: CmsSparkline | null | undefined): SparklineInput & { errors: string[] } {
|
|
118
|
+
const errors: string[] = [];
|
|
119
|
+
const format = toFormat(data?.format);
|
|
120
|
+
const labels = data?.labels ? lines(data.labels).map((l) => l.trim()) : undefined;
|
|
121
|
+
return {
|
|
122
|
+
label: data?.label?.trim() || 'Trend',
|
|
123
|
+
values: parseValues(data?.values, format, data?.label || 'Sparkline', errors),
|
|
124
|
+
labels,
|
|
125
|
+
format,
|
|
126
|
+
type: data?.type ?? 'line',
|
|
127
|
+
reference: data?.reference ?? undefined,
|
|
128
|
+
errors,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// @sinnstack/ui/data — charts and sparklines.
|
|
2
|
+
export { default as ChartContainer } from './ChartContainer.astro';
|
|
3
|
+
export { default as ChartAxis } from './ChartAxis.astro';
|
|
4
|
+
export { default as ChartSeries } from './ChartSeries.astro';
|
|
5
|
+
export { default as ChartLegend } from './ChartLegend.astro';
|
|
6
|
+
export { default as ChartTooltip } from './ChartTooltip.astro';
|
|
7
|
+
export { default as ChartTable } from './ChartTable.astro';
|
|
8
|
+
export { default as Sparkline } from './Sparkline.astro';
|
|
9
|
+
export { defineChart, type ChartInput, type ChartModel, type SeriesInput, type SeriesSlot } from './lib/chart';
|
|
10
|
+
export { defineSparkline, type SparklineInput, type SparklineModel } from './lib/sparkline';
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/** Scales and ticks. Pure. */
|
|
2
|
+
|
|
3
|
+
/** A "nice" step (1, 2, 2.5 or 5 × 10ⁿ) that splits `span` into about `count` parts. */
|
|
4
|
+
export function niceStep(span: number, count: number): number {
|
|
5
|
+
if (!(span > 0)) return 1;
|
|
6
|
+
const raw = span / Math.max(1, count);
|
|
7
|
+
const mag = 10 ** Math.floor(Math.log10(raw));
|
|
8
|
+
const norm = raw / mag;
|
|
9
|
+
const nice = norm <= 1 ? 1 : norm <= 2 ? 2 : norm <= 2.5 ? 2.5 : norm <= 5 ? 5 : 10;
|
|
10
|
+
return nice * mag;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const round = (v: number) => Math.round(v * 1e9) / 1e9;
|
|
14
|
+
|
|
15
|
+
/** Expands [min, max] to nice bounds and returns the ticks between them. */
|
|
16
|
+
export function niceDomain(min: number, max: number, count = 5): { min: number; max: number; step: number; ticks: number[] } {
|
|
17
|
+
if (!Number.isFinite(min) || !Number.isFinite(max)) return { min: 0, max: 1, step: 0.25, ticks: [0, 0.25, 0.5, 0.75, 1] };
|
|
18
|
+
if (min === max) {
|
|
19
|
+
const pad = min === 0 ? 1 : Math.abs(min) * 0.5;
|
|
20
|
+
min -= min === 0 ? 0 : pad;
|
|
21
|
+
max += pad;
|
|
22
|
+
}
|
|
23
|
+
let step = niceStep(max - min, count);
|
|
24
|
+
let lo = Math.floor(min / step) * step;
|
|
25
|
+
let hi = Math.ceil(max / step) * step;
|
|
26
|
+
// Too many ticks after rounding outward: take the next nice step.
|
|
27
|
+
if ((hi - lo) / step > count + 2) {
|
|
28
|
+
step = niceStep(max - min, Math.max(2, count - 2));
|
|
29
|
+
lo = Math.floor(min / step) * step;
|
|
30
|
+
hi = Math.ceil(max / step) * step;
|
|
31
|
+
}
|
|
32
|
+
const ticks: number[] = [];
|
|
33
|
+
for (let v = lo; v <= hi + step / 2; v += step) ticks.push(round(v));
|
|
34
|
+
return { min: round(lo), max: round(hi), step, ticks };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Position of `v` from the top of the plot, in percent (SVG y grows downward). */
|
|
38
|
+
export const yPct = (v: number, min: number, max: number) => round((1 - (v - min) / (max - min)) * 100);
|
|
39
|
+
/** Position of `v` from the left, in percent. */
|
|
40
|
+
export const xPct = (v: number, min: number, max: number) => (max === min ? 50 : round(((v - min) / (max - min)) * 100));
|
|
41
|
+
|
|
42
|
+
// ---------------- Time ----------------
|
|
43
|
+
|
|
44
|
+
const DAY = 86_400_000;
|
|
45
|
+
export type TimeUnit = 'day' | 'week' | 'month' | 'quarter' | 'year';
|
|
46
|
+
const STEPS: Array<{ unit: TimeUnit; n: number; approx: number }> = [
|
|
47
|
+
{ unit: 'day', n: 1, approx: DAY },
|
|
48
|
+
{ unit: 'day', n: 2, approx: 2 * DAY },
|
|
49
|
+
{ unit: 'week', n: 1, approx: 7 * DAY },
|
|
50
|
+
{ unit: 'week', n: 2, approx: 14 * DAY },
|
|
51
|
+
{ unit: 'month', n: 1, approx: 30.4 * DAY },
|
|
52
|
+
{ unit: 'quarter', n: 1, approx: 91 * DAY },
|
|
53
|
+
{ unit: 'month', n: 6, approx: 182 * DAY },
|
|
54
|
+
{ unit: 'year', n: 1, approx: 365 * DAY },
|
|
55
|
+
{ unit: 'year', n: 2, approx: 730 * DAY },
|
|
56
|
+
{ unit: 'year', n: 5, approx: 1826 * DAY },
|
|
57
|
+
{ unit: 'year', n: 10, approx: 3652 * DAY },
|
|
58
|
+
];
|
|
59
|
+
|
|
60
|
+
/** Calendar-aligned ticks (UTC) between two times, about `count` of them. */
|
|
61
|
+
export function timeTicks(min: number, max: number, count = 6): { ticks: number[]; unit: TimeUnit } {
|
|
62
|
+
const span = Math.max(DAY, max - min);
|
|
63
|
+
const step = STEPS.find((s) => span / s.approx <= count) ?? STEPS[STEPS.length - 1];
|
|
64
|
+
const d = new Date(min);
|
|
65
|
+
let t: Date;
|
|
66
|
+
if (step.unit === 'day' || step.unit === 'week') t = new Date(Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()));
|
|
67
|
+
else if (step.unit === 'year') t = new Date(Date.UTC(Math.floor(d.getUTCFullYear() / step.n) * step.n, 0, 1));
|
|
68
|
+
else {
|
|
69
|
+
const months = step.unit === 'quarter' ? 3 : step.n;
|
|
70
|
+
t = new Date(Date.UTC(d.getUTCFullYear(), Math.floor(d.getUTCMonth() / months) * months, 1));
|
|
71
|
+
}
|
|
72
|
+
const ticks: number[] = [];
|
|
73
|
+
let guard = 0;
|
|
74
|
+
while (t.getTime() <= max && guard++ < 500) {
|
|
75
|
+
if (t.getTime() >= min) ticks.push(t.getTime());
|
|
76
|
+
if (step.unit === 'day') t = new Date(t.getTime() + step.n * DAY);
|
|
77
|
+
else if (step.unit === 'week') t = new Date(t.getTime() + 7 * step.n * DAY);
|
|
78
|
+
else if (step.unit === 'month') t = new Date(Date.UTC(t.getUTCFullYear(), t.getUTCMonth() + step.n, 1));
|
|
79
|
+
else if (step.unit === 'quarter') t = new Date(Date.UTC(t.getUTCFullYear(), t.getUTCMonth() + 3, 1));
|
|
80
|
+
else t = new Date(Date.UTC(t.getUTCFullYear() + step.n, 0, 1));
|
|
81
|
+
}
|
|
82
|
+
return { ticks, unit: step.unit === 'week' ? 'day' : step.unit };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Finest calendar unit that distinguishes the data's dates (for tooltip/table labels). */
|
|
86
|
+
export function dataGranularity(times: number[]): 'day' | 'month' | 'year' {
|
|
87
|
+
const ds = times.map((t) => new Date(t));
|
|
88
|
+
if (ds.every((d) => d.getUTCMonth() === 0 && d.getUTCDate() === 1)) return 'year';
|
|
89
|
+
if (ds.every((d) => d.getUTCDate() === 1)) return 'month';
|
|
90
|
+
return 'day';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Short tick label: "Jan", "Jan 2026" when the year changes, "Mar 4", "2026". */
|
|
94
|
+
export function timeTickLabel(t: number, unit: TimeUnit, locale: string, previous?: number): string {
|
|
95
|
+
const d = new Date(t);
|
|
96
|
+
const yearChanged = previous === undefined || new Date(previous).getUTCFullYear() !== d.getUTCFullYear();
|
|
97
|
+
const f = (o: Intl.DateTimeFormatOptions) => new Intl.DateTimeFormat(locale, { timeZone: 'UTC', ...o }).format(d);
|
|
98
|
+
if (unit === 'year') return f({ year: 'numeric' });
|
|
99
|
+
if (unit === 'month' || unit === 'quarter') return yearChanged ? f({ month: 'short', year: 'numeric' }) : f({ month: 'short' });
|
|
100
|
+
return yearChanged ? f({ month: 'short', day: 'numeric', year: 'numeric' }) : f({ month: 'short', day: 'numeric' });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Full date label for tooltips and the table, at the data's granularity. */
|
|
104
|
+
export function timeFullLabel(t: number, granularity: 'day' | 'month' | 'year', locale: string): string {
|
|
105
|
+
const o: Intl.DateTimeFormatOptions = granularity === 'year' ? { year: 'numeric' } : granularity === 'month' ? { month: 'long', year: 'numeric' } : { month: 'short', day: 'numeric', year: 'numeric' };
|
|
106
|
+
return new Intl.DateTimeFormat(locale, { timeZone: 'UTC', ...o }).format(new Date(t));
|
|
107
|
+
}
|