astro-component-kit 0.3.1 → 0.4.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/README.md +33 -14
- package/package.json +2 -2
- package/src/components/index.ts +16 -202
- package/src/components/lib/buttons/NeonBorderButton.astro +162 -0
- package/src/components/lib/buttons/RippleButton.astro +199 -0
- package/src/components/lib/buttons/SlideRevealButton.astro +159 -0
- package/src/components/lib/buttons/index.ts +4 -0
- package/src/components/lib/cards/GithubRepoCard.astro +176 -0
- package/src/components/lib/cards/PricingCompareMiniCard.astro +196 -0
- package/src/components/lib/cards/StatsTrendCard.astro +149 -0
- package/src/components/lib/cards/index.ts +3 -0
- package/src/components/lib/charts/ActivityRings.astro +105 -0
- package/src/components/lib/charts/BoxPlotChart.astro +109 -0
- package/src/components/lib/charts/BubbleChart.astro +101 -0
- package/src/components/lib/charts/BulletChart.astro +160 -0
- package/src/components/lib/charts/BumpChart.astro +92 -0
- package/src/components/lib/charts/CandlestickChart.astro +105 -0
- package/src/components/lib/charts/ChartFrame.astro +373 -0
- package/src/components/lib/charts/DivergingBarChart.astro +98 -0
- package/src/components/lib/charts/DumbbellChart.astro +92 -0
- package/src/components/lib/charts/ForecastLineChart.astro +125 -0
- package/src/components/lib/charts/FunnelChart.astro +122 -0
- package/src/components/lib/charts/GaugeChart.astro +111 -0
- package/src/components/lib/charts/GoalProgressChart.astro +97 -0
- package/src/components/lib/charts/GroupedBarChart.astro +125 -0
- package/src/components/lib/charts/HeatmapChart.astro +131 -0
- package/src/components/lib/charts/HistogramChart.astro +111 -0
- package/src/components/lib/charts/HorizontalBarChart.astro +155 -0
- package/src/components/lib/charts/KpiStatTile.astro +116 -0
- package/src/components/lib/charts/KpiTrendGrid.astro +60 -0
- package/src/components/lib/charts/LineChart.astro +160 -0
- package/src/components/lib/charts/LinearProgressGauge.astro +136 -0
- package/src/components/lib/charts/LollipopChart.astro +100 -0
- package/src/components/lib/charts/MeterGauge.astro +122 -0
- package/src/components/lib/charts/MetricDeltaTable.astro +125 -0
- package/src/components/lib/charts/PieChart.astro +135 -0
- package/src/components/lib/charts/PolarAreaChart.astro +85 -0
- package/src/components/lib/charts/PopulationPyramid.astro +122 -0
- package/src/components/lib/charts/PunchCardChart.astro +89 -0
- package/src/components/lib/charts/RadarChart.astro +94 -0
- package/src/components/lib/charts/RadarMetricChart.astro +168 -0
- package/src/components/lib/charts/RadialBarChart.astro +124 -0
- package/src/components/lib/charts/RangeAreaChart.astro +113 -0
- package/src/components/lib/charts/RetentionCohortTable.astro +126 -0
- package/src/components/lib/charts/SankeyChart.astro +128 -0
- package/src/components/lib/charts/ScatterPlot.astro +139 -0
- package/src/components/lib/charts/SegmentedBarChart.astro +119 -0
- package/src/components/lib/charts/SlopeChart.astro +105 -0
- package/src/components/lib/charts/SparklineChart.astro +87 -0
- package/src/components/lib/charts/StackedAreaChart.astro +130 -0
- package/src/components/lib/charts/StackedBarChart.astro +136 -0
- package/src/components/lib/charts/SunburstChart.astro +103 -0
- package/src/components/lib/charts/TreemapChart.astro +94 -0
- package/src/components/lib/charts/WaffleChart.astro +121 -0
- package/src/components/lib/charts/WaterfallChart.astro +118 -0
- package/src/components/lib/charts/chart-tooltip.ts +75 -0
- package/src/components/lib/charts/chart-utils.ts +271 -0
- package/src/components/lib/charts/index.ts +46 -0
- package/src/components/lib/data/ActivityHeatmapGrid.astro +157 -0
- package/src/components/lib/data/MetricComparisonRow.astro +109 -0
- package/src/components/lib/data/StatusTimelineFeed.astro +168 -0
- package/src/components/lib/data/index.ts +3 -0
- package/src/components/lib/feedback/ProgressRingCounter.astro +118 -0
- package/src/components/lib/feedback/ToastNotificationGroup.astro +173 -0
- package/src/components/lib/feedback/index.ts +2 -0
- package/src/components/lib/forms/GlassInlineToggleGroup.astro +161 -0
- package/src/components/lib/forms/PasswordStrengthInput.astro +242 -0
- package/src/components/lib/forms/PhoneCountryInput.astro +145 -0
- package/src/components/lib/forms/TagsAutocompleteInput.astro +249 -0
- package/src/components/lib/forms/index.ts +5 -0
- package/src/components/lib/layout/DiagonalSectionDivider.astro +69 -0
- package/src/components/lib/layout/HolyGrailGrid.astro +125 -0
- package/src/components/lib/layout/index.ts +3 -1
- package/src/components/lib/navigation/QuickJumpDock.astro +125 -0
- package/src/components/lib/navigation/StepBreadcrumbBar.astro +151 -0
- package/src/components/lib/navigation/SubNavbarPills.astro +102 -0
- package/src/components/lib/navigation/index.ts +3 -0
- package/src/components/lib/overlays/CookiePreferencesDrawer.astro +226 -0
- package/src/components/lib/overlays/FloatingFeedbackWidget.astro +203 -0
- package/src/components/lib/overlays/ImageCompareSlider.astro +172 -0
- package/src/components/lib/overlays/index.ts +3 -0
- package/src/components/lib/sections/BlogGridSection.astro +131 -0
- package/src/components/lib/sections/CareersOpenings.astro +139 -0
- package/src/components/lib/sections/ChangelogSection.astro +144 -0
- package/src/components/lib/sections/ContactSplitMap.astro +139 -0
- package/src/components/lib/sections/CtaNewsletterSection.astro +163 -0
- package/src/components/lib/sections/CtaSplitImage.astro +147 -0
- package/src/components/lib/sections/FaqTwoColumn.astro +119 -0
- package/src/components/lib/sections/FeatureAlternatingRows.astro +190 -0
- package/src/components/lib/sections/FeatureIconGrid.astro +155 -0
- package/src/components/lib/sections/FeatureTabsShowcase.astro +180 -0
- package/src/components/lib/sections/FooterMegaSection.astro +155 -0
- package/src/components/lib/sections/HeroDashboardPreview.astro +180 -0
- package/src/components/lib/sections/HeroMinimalCentered.astro +175 -0
- package/src/components/lib/sections/HeroShowcaseSplit.astro +304 -0
- package/src/components/lib/sections/HeroTerminal.astro +251 -0
- package/src/components/lib/sections/HeroWaitlist.astro +229 -0
- package/src/components/lib/sections/HowItWorksSteps.astro +178 -0
- package/src/components/lib/sections/IntegrationsGrid.astro +167 -0
- package/src/components/lib/sections/LogoCloudSection.astro +96 -0
- package/src/components/lib/sections/LogoWallGrid.astro +91 -0
- package/src/components/lib/sections/PricingSinglePlan.astro +157 -0
- package/src/components/lib/sections/PricingToggleCards.astro +171 -0
- package/src/components/lib/sections/StatsCounterBand.astro +122 -0
- package/src/components/lib/sections/StatsDashboardSection.astro +83 -0
- package/src/components/lib/sections/TestimonialCarousel.astro +165 -0
- package/src/components/lib/sections/TestimonialFeatured.astro +120 -0
- package/src/components/lib/sections/TestimonialGridSection.astro +188 -0
- package/src/components/lib/sections/index.ts +29 -0
- package/src/components/lib/typography/AnimatedCounterText.astro +102 -0
- package/src/components/lib/typography/CurvedPathHeading.astro +86 -0
- package/src/components/lib/typography/GradientTextMarquee.astro +105 -0
- package/src/components/lib/typography/HackerDecodeText.astro +85 -0
- package/src/components/lib/typography/index.ts +4 -0
- package/src/styles/global.css +18 -1
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* BulletChart — Compact KPI-vs-target bars with qualitative bands
|
|
4
|
+
* (poor / ok / good), a measure bar and a target tick. Stack several for a grid.
|
|
5
|
+
*
|
|
6
|
+
* @param {Array<{label: string, value: number, target: number, ranges: [number, number, number], unit?: string}>} metrics
|
|
7
|
+
* - Each metric with its qualitative band thresholds (ascending).
|
|
8
|
+
* @param {string} color - Measure bar color.
|
|
9
|
+
* @param {string} variant - Card style (glass|solid|ghost).
|
|
10
|
+
* @param {string} size - Density (sm|md|lg).
|
|
11
|
+
*/
|
|
12
|
+
import ChartFrame from './ChartFrame.astro';
|
|
13
|
+
|
|
14
|
+
interface Metric {
|
|
15
|
+
label: string;
|
|
16
|
+
value: number;
|
|
17
|
+
target: number;
|
|
18
|
+
ranges: [number, number, number];
|
|
19
|
+
unit?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface Props {
|
|
23
|
+
title?: string;
|
|
24
|
+
subtitle?: string;
|
|
25
|
+
metrics?: Metric[];
|
|
26
|
+
color?: string;
|
|
27
|
+
variant?: 'glass' | 'solid' | 'ghost';
|
|
28
|
+
size?: 'sm' | 'md' | 'lg';
|
|
29
|
+
className?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const {
|
|
33
|
+
title = 'Quarterly targets',
|
|
34
|
+
subtitle = 'Measure vs target with performance bands',
|
|
35
|
+
metrics = [
|
|
36
|
+
{ label: 'Revenue ($k)', value: 270, target: 250, ranges: [150, 225, 300] },
|
|
37
|
+
{ label: 'Profit', value: 23, target: 26, ranges: [20, 25, 30], unit: '%' },
|
|
38
|
+
{ label: 'New customers', value: 1650, target: 2100, ranges: [1400, 2000, 2500] },
|
|
39
|
+
{ label: 'NPS', value: 54, target: 50, ranges: [30, 50, 70] },
|
|
40
|
+
],
|
|
41
|
+
color = 'var(--c-text-1, #fff)',
|
|
42
|
+
variant = 'glass',
|
|
43
|
+
size = 'md',
|
|
44
|
+
className = '',
|
|
45
|
+
} = Astro.props;
|
|
46
|
+
|
|
47
|
+
const pct = (v: number, max: number) => `${Math.min(100, (v / max) * 100)}%`;
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
<ChartFrame
|
|
51
|
+
title={title}
|
|
52
|
+
subtitle={subtitle}
|
|
53
|
+
variant={variant}
|
|
54
|
+
size={size}
|
|
55
|
+
className={className}
|
|
56
|
+
table={{ headers: ['Metric', 'Value', 'Target', 'Status'], rows: metrics.map((mt) => [mt.label, `${mt.value}${mt.unit ?? ''}`, `${mt.target}${mt.unit ?? ''}`, mt.value >= mt.target ? 'On target' : 'Below target']) }}
|
|
57
|
+
>
|
|
58
|
+
<div class="bc">
|
|
59
|
+
{metrics.map((mt) => {
|
|
60
|
+
const max = mt.ranges[2];
|
|
61
|
+
const met = mt.value >= mt.target;
|
|
62
|
+
return (
|
|
63
|
+
<div
|
|
64
|
+
class="bc__row cf-mark"
|
|
65
|
+
tabindex="0"
|
|
66
|
+
role="meter"
|
|
67
|
+
aria-valuemin={0}
|
|
68
|
+
aria-valuemax={max}
|
|
69
|
+
aria-valuenow={mt.value}
|
|
70
|
+
aria-label={`${mt.label}: ${mt.value}${mt.unit ?? ''} of target ${mt.target}${mt.unit ?? ''}`}
|
|
71
|
+
data-tip-title={mt.label}
|
|
72
|
+
data-tip={`Value: ${mt.value}${mt.unit ?? ''}|Target: ${mt.target}${mt.unit ?? ''}|${met ? 'var(--chart-pos)::On target' : 'var(--chart-neg)::Below target'}`}
|
|
73
|
+
>
|
|
74
|
+
<div class="bc__meta">
|
|
75
|
+
<span class="bc__label">{mt.label}</span>
|
|
76
|
+
<span class="bc__value">
|
|
77
|
+
{mt.value}{mt.unit ?? ''}
|
|
78
|
+
<span class={`bc__status ${met ? 'bc__status--ok' : 'bc__status--bad'}`}>
|
|
79
|
+
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" aria-hidden="true">
|
|
80
|
+
{met ? <path d="M20 6 9 17l-5-5" /> : <><circle cx="12" cy="12" r="9" /><path d="M12 8v4M12 16h.01" /></>}
|
|
81
|
+
</svg>
|
|
82
|
+
{met ? 'On target' : 'Below'}
|
|
83
|
+
</span>
|
|
84
|
+
</span>
|
|
85
|
+
</div>
|
|
86
|
+
<div class="bc__track" aria-hidden="true">
|
|
87
|
+
<span class="bc__band" style={`width:${pct(mt.ranges[2], max)}; opacity:0.07`}></span>
|
|
88
|
+
<span class="bc__band" style={`width:${pct(mt.ranges[1], max)}; opacity:0.11`}></span>
|
|
89
|
+
<span class="bc__band" style={`width:${pct(mt.ranges[0], max)}; opacity:0.16`}></span>
|
|
90
|
+
<span class="bc__measure" style={`width:${pct(mt.value, max)}; background:${color}`}></span>
|
|
91
|
+
<span class="bc__target" style={`left:${pct(mt.target, max)}`}></span>
|
|
92
|
+
</div>
|
|
93
|
+
</div>
|
|
94
|
+
);
|
|
95
|
+
})}
|
|
96
|
+
</div>
|
|
97
|
+
</ChartFrame>
|
|
98
|
+
|
|
99
|
+
<style>
|
|
100
|
+
.bc { display: grid; gap: 1rem; }
|
|
101
|
+
.bc__row {
|
|
102
|
+
display: grid;
|
|
103
|
+
gap: 0.45rem;
|
|
104
|
+
padding: 0.35rem;
|
|
105
|
+
border-radius: 8px;
|
|
106
|
+
}
|
|
107
|
+
.bc__row:focus-visible { outline: 2px solid var(--c-primary-light, #d4a017); }
|
|
108
|
+
.bc__meta { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; }
|
|
109
|
+
.bc__label { font-size: 0.8125rem; color: var(--c-text-2, #a3a3a3); }
|
|
110
|
+
.bc__value {
|
|
111
|
+
display: inline-flex;
|
|
112
|
+
align-items: center;
|
|
113
|
+
gap: 0.6rem;
|
|
114
|
+
font-weight: 650;
|
|
115
|
+
color: var(--c-text-1, #fff);
|
|
116
|
+
font-variant-numeric: tabular-nums;
|
|
117
|
+
}
|
|
118
|
+
.bc__status {
|
|
119
|
+
display: inline-flex;
|
|
120
|
+
align-items: center;
|
|
121
|
+
gap: 0.25rem;
|
|
122
|
+
font-size: 0.6875rem;
|
|
123
|
+
font-weight: 600;
|
|
124
|
+
color: var(--c-text-2, #a3a3a3);
|
|
125
|
+
white-space: nowrap;
|
|
126
|
+
}
|
|
127
|
+
.bc__status--ok svg { color: var(--chart-pos, #34c38f); }
|
|
128
|
+
.bc__status--bad svg { color: var(--chart-neg, #f06a6a); }
|
|
129
|
+
.bc__track {
|
|
130
|
+
position: relative;
|
|
131
|
+
height: 22px;
|
|
132
|
+
border-radius: 4px;
|
|
133
|
+
overflow: hidden;
|
|
134
|
+
}
|
|
135
|
+
.bc__band {
|
|
136
|
+
position: absolute;
|
|
137
|
+
inset: 0 auto 0 0;
|
|
138
|
+
background: #fff;
|
|
139
|
+
}
|
|
140
|
+
.bc__measure {
|
|
141
|
+
position: absolute;
|
|
142
|
+
left: 0;
|
|
143
|
+
top: 7px;
|
|
144
|
+
height: 8px;
|
|
145
|
+
border-radius: 0 3px 3px 0;
|
|
146
|
+
transform-origin: left;
|
|
147
|
+
animation: bc-grow 0.8s cubic-bezier(0.4, 0, 0.2, 1) both;
|
|
148
|
+
}
|
|
149
|
+
.bc__target {
|
|
150
|
+
position: absolute;
|
|
151
|
+
top: 3px;
|
|
152
|
+
bottom: 3px;
|
|
153
|
+
width: 3px;
|
|
154
|
+
margin-left: -1.5px;
|
|
155
|
+
border-radius: 2px;
|
|
156
|
+
background: var(--chart-4, #c98500);
|
|
157
|
+
}
|
|
158
|
+
@keyframes bc-grow { from { transform: scaleX(0); } }
|
|
159
|
+
@media (prefers-reduced-motion: reduce) { .bc__measure { animation: none; } }
|
|
160
|
+
</style>
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* BumpChart — Rank-over-time chart. Each line tracks one entity's position
|
|
4
|
+
* (1 = top) across periods, with labels at both ends.
|
|
5
|
+
*
|
|
6
|
+
* @param {string[]} periods - X-axis labels.
|
|
7
|
+
* @param {Array<{name: string, ranks: number[]}>} series - Rank per period (1-based), max 8.
|
|
8
|
+
* @param {string[]} colors - Optional per-series colors.
|
|
9
|
+
* @param {string} variant - Card style (glass|solid|ghost).
|
|
10
|
+
* @param {string} size - Density (sm|md|lg).
|
|
11
|
+
*/
|
|
12
|
+
import ChartFrame from './ChartFrame.astro';
|
|
13
|
+
import { colorAt, smoothPath, type Point } from './chart-utils';
|
|
14
|
+
|
|
15
|
+
interface Props {
|
|
16
|
+
title?: string;
|
|
17
|
+
subtitle?: string;
|
|
18
|
+
periods?: string[];
|
|
19
|
+
series?: Array<{ name: string; ranks: number[] }>;
|
|
20
|
+
colors?: string[];
|
|
21
|
+
variant?: 'glass' | 'solid' | 'ghost';
|
|
22
|
+
size?: 'sm' | 'md' | 'lg';
|
|
23
|
+
className?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const {
|
|
27
|
+
title = 'Framework popularity rank',
|
|
28
|
+
subtitle = 'Developer survey position by year',
|
|
29
|
+
periods = ['2021', '2022', '2023', '2024', '2025', '2026'],
|
|
30
|
+
series = [
|
|
31
|
+
{ name: 'Astro', ranks: [5, 4, 3, 2, 1, 1] },
|
|
32
|
+
{ name: 'Next', ranks: [1, 1, 1, 1, 2, 2] },
|
|
33
|
+
{ name: 'Svelte', ranks: [3, 2, 2, 3, 3, 4] },
|
|
34
|
+
{ name: 'Nuxt', ranks: [2, 3, 4, 4, 5, 3] },
|
|
35
|
+
{ name: 'Remix', ranks: [4, 5, 5, 5, 4, 5] },
|
|
36
|
+
],
|
|
37
|
+
colors,
|
|
38
|
+
variant = 'glass',
|
|
39
|
+
size = 'md',
|
|
40
|
+
className = '',
|
|
41
|
+
} = Astro.props;
|
|
42
|
+
|
|
43
|
+
const W = 640;
|
|
44
|
+
const maxRank = Math.max(...series.flatMap((s) => s.ranks));
|
|
45
|
+
const rowH = 44;
|
|
46
|
+
const H = maxRank * rowH + 36;
|
|
47
|
+
const m = { t: 30, l: 80, r: 80 };
|
|
48
|
+
const n = periods.length;
|
|
49
|
+
const x = (i: number) => m.l + (i / Math.max(1, n - 1)) * (W - m.l - m.r);
|
|
50
|
+
const y = (r: number) => m.t + (r - 1) * rowH + 8;
|
|
51
|
+
const lines = series.slice(0, 8).map((s, si) => ({
|
|
52
|
+
...s,
|
|
53
|
+
color: colorAt(si, colors),
|
|
54
|
+
pts: s.ranks.map((r, i): Point => [x(i), y(r)]),
|
|
55
|
+
}));
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
<ChartFrame
|
|
59
|
+
title={title}
|
|
60
|
+
subtitle={subtitle}
|
|
61
|
+
variant={variant}
|
|
62
|
+
size={size}
|
|
63
|
+
className={className}
|
|
64
|
+
legend={lines.map((l) => ({ label: l.name, color: l.color }))}
|
|
65
|
+
table={{ headers: ['Name', ...periods], rows: series.map((s) => [s.name, ...s.ranks.map((r) => `#${r}`)]) }}
|
|
66
|
+
>
|
|
67
|
+
<svg viewBox={`0 0 ${W} ${H}`} role="img" aria-label={`${title}: bump chart of ranks over ${n} periods`}>
|
|
68
|
+
{periods.map((p, i) => (
|
|
69
|
+
<g>
|
|
70
|
+
<line class="cf-grid" x1={x(i)} x2={x(i)} y1={m.t - 6} y2={H - 10} />
|
|
71
|
+
<text class="cf-label" x={x(i)} y={14} text-anchor="middle">{p}</text>
|
|
72
|
+
</g>
|
|
73
|
+
))}
|
|
74
|
+
{lines.map((l) => (
|
|
75
|
+
<g class="cf-mark cf-dim" tabindex="0" role="img"
|
|
76
|
+
aria-label={`${l.name}: ${l.ranks.map((r, i) => `${periods[i]} #${r}`).join(', ')}`}
|
|
77
|
+
data-tip-title={l.name}
|
|
78
|
+
data-tip={l.ranks.map((r, i) => `${periods[i]}: #${r}`).join('|')}>
|
|
79
|
+
<path d={smoothPath(l.pts)} fill="none" stroke={l.color} stroke-width="3" stroke-linecap="round" />
|
|
80
|
+
<path d={smoothPath(l.pts)} fill="none" stroke="transparent" stroke-width="16" />
|
|
81
|
+
{l.pts.map(([px, py], i) => (
|
|
82
|
+
<g>
|
|
83
|
+
<circle cx={px} cy={py} r="11" fill={l.color} stroke="var(--chart-surface, #0d0d0f)" stroke-width="2" />
|
|
84
|
+
<text x={px} y={py + 4} text-anchor="middle" font-size="10" font-weight="700" fill="#fff">{l.ranks[i]}</text>
|
|
85
|
+
</g>
|
|
86
|
+
))}
|
|
87
|
+
<text class="cf-label cf-label--strong" x={m.l - 18} y={l.pts[0][1] + 4} text-anchor="end">{l.name}</text>
|
|
88
|
+
<text class="cf-label cf-label--strong" x={W - m.r + 18} y={l.pts[n - 1][1] + 4}>{l.name}</text>
|
|
89
|
+
</g>
|
|
90
|
+
))}
|
|
91
|
+
</svg>
|
|
92
|
+
</ChartFrame>
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* CandlestickChart — OHLC price chart. Bodies span open→close, wicks span
|
|
4
|
+
* low→high. Up candles are hollow, down candles filled, so direction is
|
|
5
|
+
* readable without color.
|
|
6
|
+
*
|
|
7
|
+
* @param {Array<{label: string, open: number, high: number, low: number, close: number}>} candles - OHLC data.
|
|
8
|
+
* @param {string} prefix - Price prefix.
|
|
9
|
+
* @param {number} height - SVG height in viewBox units (width is 640).
|
|
10
|
+
* @param {string} variant - Card style (glass|solid|ghost).
|
|
11
|
+
* @param {string} size - Density (sm|md|lg).
|
|
12
|
+
*/
|
|
13
|
+
import ChartFrame from './ChartFrame.astro';
|
|
14
|
+
import { scaleLinear, niceTicks, POSITIVE, NEGATIVE } from './chart-utils';
|
|
15
|
+
|
|
16
|
+
interface Candle { label: string; open: number; high: number; low: number; close: number }
|
|
17
|
+
interface Props {
|
|
18
|
+
title?: string;
|
|
19
|
+
subtitle?: string;
|
|
20
|
+
candles?: Candle[];
|
|
21
|
+
prefix?: string;
|
|
22
|
+
height?: number;
|
|
23
|
+
variant?: 'glass' | 'solid' | 'ghost';
|
|
24
|
+
size?: 'sm' | 'md' | 'lg';
|
|
25
|
+
className?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const demo: Candle[] = (() => {
|
|
29
|
+
let p = 182;
|
|
30
|
+
return Array.from({ length: 22 }, (_, i) => {
|
|
31
|
+
const r1 = Math.sin(i * 4.7) * 0.5 + Math.sin(i * 1.3) * 0.5;
|
|
32
|
+
const r2 = Math.abs(Math.sin(i * 9.1));
|
|
33
|
+
const open = p;
|
|
34
|
+
const close = Math.round((p + r1 * 6 + 0.6) * 100) / 100;
|
|
35
|
+
const high = Math.round((Math.max(open, close) + r2 * 3 + 0.4) * 100) / 100;
|
|
36
|
+
const low = Math.round((Math.min(open, close) - (1 - r2) * 3 - 0.4) * 100) / 100;
|
|
37
|
+
p = close;
|
|
38
|
+
return { label: `Oct ${i + 6}`, open, high, low, close };
|
|
39
|
+
});
|
|
40
|
+
})();
|
|
41
|
+
|
|
42
|
+
const {
|
|
43
|
+
title = 'ACME / USD',
|
|
44
|
+
subtitle = 'Daily candles',
|
|
45
|
+
candles = demo,
|
|
46
|
+
prefix = '$',
|
|
47
|
+
height = 300,
|
|
48
|
+
variant = 'glass',
|
|
49
|
+
size = 'md',
|
|
50
|
+
className = '',
|
|
51
|
+
} = Astro.props;
|
|
52
|
+
|
|
53
|
+
const W = 640;
|
|
54
|
+
const H = height;
|
|
55
|
+
const m = { t: 12, r: 52, b: 28, l: 12 };
|
|
56
|
+
const ticks = niceTicks(Math.min(...candles.map((c) => c.low)), Math.max(...candles.map((c) => c.high)), 5);
|
|
57
|
+
const y = scaleLinear(ticks[0], ticks[ticks.length - 1], H - m.b, m.t);
|
|
58
|
+
const band = (W - m.l - m.r) / candles.length;
|
|
59
|
+
const bw = Math.max(3, band * 0.6);
|
|
60
|
+
const f = (v: number) => `${prefix}${v.toFixed(2)}`;
|
|
61
|
+
const last = candles[candles.length - 1];
|
|
62
|
+
const first = candles[0];
|
|
63
|
+
const change = ((last.close - first.open) / first.open) * 100;
|
|
64
|
+
const labelEvery = Math.ceil(candles.length / 6);
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
<ChartFrame
|
|
68
|
+
title={title}
|
|
69
|
+
subtitle={subtitle}
|
|
70
|
+
value={f(last.close)}
|
|
71
|
+
delta={`${change >= 0 ? '+' : ''}${change.toFixed(2)}%`}
|
|
72
|
+
deltaPositive={change >= 0}
|
|
73
|
+
variant={variant}
|
|
74
|
+
size={size}
|
|
75
|
+
className={className}
|
|
76
|
+
legend={[{ label: 'Up (hollow)', color: POSITIVE }, { label: 'Down (filled)', color: NEGATIVE }]}
|
|
77
|
+
table={{ headers: ['Date', 'Open', 'High', 'Low', 'Close'], rows: candles.map((c) => [c.label, f(c.open), f(c.high), f(c.low), f(c.close)]) }}
|
|
78
|
+
>
|
|
79
|
+
<svg viewBox={`0 0 ${W} ${H}`} role="img" aria-label={`${title}: candlestick chart, ${candles.length} periods, ${change.toFixed(1)}% change`}>
|
|
80
|
+
{ticks.map((t) => (
|
|
81
|
+
<g>
|
|
82
|
+
<line class="cf-grid" x1={m.l} x2={W - m.r} y1={y(t)} y2={y(t)} />
|
|
83
|
+
<text class="cf-label" x={W - m.r + 8} y={y(t) + 4}>{f(t)}</text>
|
|
84
|
+
</g>
|
|
85
|
+
))}
|
|
86
|
+
{candles.map((c, i) => {
|
|
87
|
+
const cx = m.l + i * band + band / 2;
|
|
88
|
+
const up = c.close >= c.open;
|
|
89
|
+
const col = up ? POSITIVE : NEGATIVE;
|
|
90
|
+
const top = y(Math.max(c.open, c.close));
|
|
91
|
+
const h = Math.max(1.5, Math.abs(y(c.open) - y(c.close)));
|
|
92
|
+
return (
|
|
93
|
+
<g class="cf-mark cf-dim" tabindex="0" role="img"
|
|
94
|
+
aria-label={`${c.label}: open ${f(c.open)}, high ${f(c.high)}, low ${f(c.low)}, close ${f(c.close)}`}
|
|
95
|
+
data-tip-title={c.label} data-tip={`${col}::${up ? 'Up' : 'Down'} ${(((c.close - c.open) / c.open) * 100).toFixed(2)}%|O ${f(c.open)} · H ${f(c.high)}|L ${f(c.low)} · C ${f(c.close)}`}>
|
|
96
|
+
<rect x={cx - band / 2} y={m.t} width={band} height={H - m.t - m.b} fill="transparent" />
|
|
97
|
+
<line x1={cx} x2={cx} y1={y(c.high)} y2={y(c.low)} stroke={col} stroke-width="1.5" />
|
|
98
|
+
<rect x={cx - bw / 2} y={top} width={bw} height={h} rx="1.5" fill={up ? 'var(--chart-surface, #0d0d0f)' : col} stroke={col} stroke-width="1.5" />
|
|
99
|
+
{i % labelEvery === 0 && <text class="cf-label" x={cx} y={H - 8} text-anchor="middle">{c.label}</text>}
|
|
100
|
+
</g>
|
|
101
|
+
);
|
|
102
|
+
})}
|
|
103
|
+
<line x1={m.l} x2={W - m.r} y1={y(last.close)} y2={y(last.close)} stroke="var(--c-text-1, #fff)" stroke-opacity="0.3" stroke-dasharray="3 4" aria-hidden="true" />
|
|
104
|
+
</svg>
|
|
105
|
+
</ChartFrame>
|