Files
sorsfonalak/services/frontend/src/components/ChartWheel.tsx
T

241 lines
8.9 KiB
TypeScript
Raw Normal View History

import { ASPECTS, POINTS, SIGNS, dms, orbText, pointName, signName } from "@/lib/astro";
import type { ChartResult, Position } from "@/lib/types";
// Radii of the rings (viewBox is SIZE × SIZE, centre C).
const SIZE = 680;
const C = SIZE / 2;
const R_LABEL = 322; // ASC / MC labels
const R_OUT = 300; // outer edge of the zodiac ring
const R_ZOD = 254; // inner edge of the zodiac ring
const R_GLYPH = 222; // planet glyphs
const R_DEG = 188; // planet degrees
const R_HOUSE = 172; // outer edge of the house-number ring
const R_INNER = 146; // aspect circle
// Points drawn on the wheel; the asteroids and the south node are only in the tables.
const WHEEL_POINTS = [
"sun",
"moon",
"mercury",
"venus",
"mars",
"jupiter",
"saturn",
"uranus",
"neptune",
"pluto",
"north_node",
"lilith",
"chiron",
];
const SIGN_ORDER = Object.keys(SIGNS);
const SIGN_ELEMENT = ["fire", "earth", "air", "water"];
const ANGLE_LABELS: [string, string][] = [
["ascendant", "ASC"],
["midheaven", "MC"],
["descendant", "DSC"],
["imum_coeli", "IC"],
];
/** Screen point of an ecliptic longitude: the ascendant (or 0° Aries) is on the left. */
function xy(lon: number, r: number, start: number): [number, number] {
const a = ((180 + lon - start) * Math.PI) / 180;
return [C + r * Math.cos(a), C - r * Math.sin(a)];
}
const f = (n: number) => n.toFixed(2);
/** Pushes glyphs apart so that neighbours are at least `min` degrees from each other. */
function spread(lons: number[], min: number): number[] {
const n = lons.length;
const order = lons.map((_, i) => i).sort((a, b) => lons[a] - lons[b]);
const pos = order.map((i) => lons[i]);
for (let iter = 0; iter < 200 && n > 1; iter++) {
let moved = false;
for (let i = 0; i < n; i++) {
const j = (i + 1) % n;
const gap = pos[j] - pos[i] + (j === 0 ? 360 : 0);
if (gap < min - 0.01) {
const d = (min - gap) / 2;
pos[i] -= d;
pos[j] += d;
moved = true;
}
}
if (!moved) break;
}
const out = new Array<number>(n);
order.forEach((orig, k) => (out[orig] = pos[k]));
return out;
}
function arcPath(r1: number, r2: number, from: number, to: number, start: number): string {
const [x1, y1] = xy(from, r2, start);
const [x2, y2] = xy(to, r2, start);
const [x3, y3] = xy(to, r1, start);
const [x4, y4] = xy(from, r1, start);
// Increasing longitude runs counter-clockwise on screen (sweep flag 0).
return `M${f(x1)} ${f(y1)} A${r2} ${r2} 0 0 0 ${f(x2)} ${f(y2)} L${f(x3)} ${f(y3)} A${r1} ${r1} 0 0 1 ${f(x4)} ${f(y4)} Z`;
}
/** The natal chart as a wheel: zodiac, houses, planets and aspect lines. */
export function ChartWheel({ chart, timeKnown }: { chart: ChartResult; timeKnown: boolean }) {
const angle = (key: string): Position | null => {
const a = chart.angles?.[key];
return typeof a === "object" && a !== null ? a : null;
};
const asc = timeKnown ? angle("ascendant") : null;
const start = asc?.longitude ?? 0;
const cusps = timeKnown ? (chart.houses?.cusps ?? []) : [];
const points = WHEEL_POINTS.map((id) => chart.points.find((p) => p.id === id)).filter((p) => p !== undefined);
const placed = spread(
points.map((p) => p.longitude),
9,
);
// Longitudes for the aspect line ends (planets and, with a known time, the angles).
const ends = new Map<string, number>(points.map((p) => [p.id, p.longitude]));
if (timeKnown) {
for (const [key] of ANGLE_LABELS) {
const a = angle(key);
if (a) ends.set(key, a.longitude);
}
}
const aspects = (chart.aspects ?? []).filter(
(a) => a.aspect !== "conjunction" && ends.has(a.point1) && ends.has(a.point2),
);
const description = points
.map((p) => `${pointName(p.id)} ${signName(p.sign)} ${dms(p.degree_in_sign_dms)}`)
.join(", ");
return (
<svg
className="chart-wheel"
viewBox={`0 0 ${SIZE} ${SIZE}`}
role="img"
aria-labelledby="wheel-title wheel-desc"
>
<title id="wheel-title">Születési képlet</title>
<desc id="wheel-desc">
{timeKnown && asc ? `Aszcendens ${signName(asc.sign)}. ` : "Születési idő nélkül, házak nélkül. "}
{description}
</desc>
{/* Zodiac ring */}
<circle cx={C} cy={C} r={R_OUT} className="w-ring" />
{SIGN_ORDER.map((sign, i) => {
const [gx, gy] = xy(i * 30 + 15, (R_OUT + R_ZOD) / 2, start);
return (
<g key={sign}>
<path d={arcPath(R_ZOD, R_OUT, i * 30, i * 30 + 30, start)} className={`w-sign w-${SIGN_ELEMENT[i % 4]}`} />
<text x={f(gx)} y={f(gy)} className="w-sign-glyph" dominantBaseline="central" textAnchor="middle">
<title>{SIGNS[sign].hu}</title>
{SIGNS[sign].glyph}
</text>
</g>
);
})}
{Array.from({ length: 360 }, (_, d) => {
const len = d % 10 === 0 ? 9 : d % 5 === 0 ? 6 : 3;
const [x1, y1] = xy(d, R_ZOD, start);
const [x2, y2] = xy(d, R_ZOD + len, start);
return <line key={d} x1={f(x1)} y1={f(y1)} x2={f(x2)} y2={f(y2)} className="w-tick" />;
})}
<circle cx={C} cy={C} r={R_ZOD} className="w-ring" />
{/* Houses */}
{cusps.length === 12 && (
<g>
<circle cx={C} cy={C} r={R_HOUSE} className="w-ring w-faint" />
{cusps.map((c, i) => {
const isAngle = i % 3 === 0;
const [x1, y1] = xy(c.longitude, R_INNER, start);
const [x2, y2] = xy(c.longitude, isAngle ? R_OUT : R_ZOD, start);
const next = cusps[(i + 1) % 12].longitude;
const mid = c.longitude + (((next - c.longitude + 360) % 360) / 2);
const [nx, ny] = xy(mid, (R_HOUSE + R_INNER) / 2, start);
return (
<g key={c.house}>
<line x1={f(x1)} y1={f(y1)} x2={f(x2)} y2={f(y2)} className={isAngle ? "w-axis" : "w-cusp"} />
<text x={f(nx)} y={f(ny)} className="w-house-num" dominantBaseline="central" textAnchor="middle">
{c.house}
</text>
</g>
);
})}
{ANGLE_LABELS.map(([key, label]) => {
const a = angle(key);
if (!a) return null;
const [x, y] = xy(a.longitude, R_LABEL, start);
return (
<text key={key} x={f(x)} y={f(y)} className="w-axis-label" dominantBaseline="central" textAnchor="middle">
<title>
{pointName(key)}: {dms(a.degree_in_sign_dms)} {signName(a.sign)}
</title>
{label}
</text>
);
})}
</g>
)}
{/* Aspects */}
<circle cx={C} cy={C} r={R_INNER} className="w-ring w-inner" />
{aspects.map((a) => {
const [x1, y1] = xy(ends.get(a.point1)!, R_INNER, start);
const [x2, y2] = xy(ends.get(a.point2)!, R_INNER, start);
const kind = ASPECTS[a.aspect]?.kind ?? "neutral";
const strength = a.max_orb > 0 ? 1 - Math.min(a.orb / a.max_orb, 1) : 0.5;
return (
<line
key={`${a.point1}-${a.point2}-${a.aspect}`}
x1={f(x1)}
y1={f(y1)}
x2={f(x2)}
y2={f(y2)}
className={`w-aspect w-${kind}`}
strokeOpacity={(0.3 + 0.7 * strength).toFixed(2)}
strokeWidth={(0.8 + 1.4 * strength).toFixed(2)}
>
<title>
{pointName(a.point1)} {ASPECTS[a.aspect]?.hu ?? a.aspect} {pointName(a.point2)} (orbis{" "}
{orbText(a.orb)})
</title>
</line>
);
})}
{/* Planets */}
{points.map((p, i) => {
const [t1x, t1y] = xy(p.longitude, R_ZOD, start);
const [t2x, t2y] = xy(p.longitude, R_ZOD - 8, start);
const [lx, ly] = xy(placed[i], R_GLYPH + 14, start);
const [gx, gy] = xy(placed[i], R_GLYPH, start);
const [dx, dy] = xy(placed[i], R_DEG, start);
const [ex, ey] = xy(p.longitude, R_INNER, start);
const deg = Number.parseInt(p.degree_in_sign_dms, 10);
return (
<g key={p.id} className="w-point">
<title>
{pointName(p.id)}: {dms(p.degree_in_sign_dms)} {signName(p.sign)}
{p.retrograde ? ", retrográd" : ""}
{timeKnown && p.house ? `, ${p.house}. ház` : ""}
</title>
<line x1={f(t1x)} y1={f(t1y)} x2={f(t2x)} y2={f(t2y)} className="w-point-tick" />
<line x1={f(t2x)} y1={f(t2y)} x2={f(lx)} y2={f(ly)} className="w-leader" />
<text x={f(gx)} y={f(gy)} className="w-point-glyph" dominantBaseline="central" textAnchor="middle">
{POINTS[p.id]?.glyph ?? p.id}
</text>
<text x={f(dx)} y={f(dy)} className="w-point-deg" dominantBaseline="central" textAnchor="middle">
{deg}°{p.retrograde ? "℞" : ""}
</text>
<circle cx={f(ex)} cy={f(ey)} r={2.2} className="w-end" />
</g>
);
})}
</svg>
);
}