A rose window as a radar chart: drawing quiz results in plain SVG
I recently built a small personality quiz about the seven heavenly virtues: 35 statements, five per virtue, and a score out of 100 for each one at the end.
The site is plain Astro with no backend, and I wanted the result to look like something from a cathedral rather than a dashboard. So instead of a radar chart library, the result is drawn as a seven-light rose window, where each pane of glass fills from the centre outward in proportion to its score.
Here are the three pieces that made it work, with the real code.
Scoring with reversed statements
Some statements describe the virtue ("When I'm praised, I mention the people who helped") and some describe the sin against it. Sin statements are reverse-scored, so agreeing with everything doesn't inflate the result.
Each virtue has n answers of 1 to 5, which gives a range of n to 5n, mapped onto 0-100:
function score(questions: Question[], answers: number[]): number[] {
const sums = new Map(virtues.map((v) => [v.id, 0]));
const counts = new Map(virtues.map((v) => [v.id, 0]));
questions.forEach((q, i) => {
const raw = answers[i] ?? 3;
const val = q.reverse ? 6 - raw : raw;
sums.set(q.virtue, (sums.get(q.virtue) ?? 0) + val);
counts.set(q.virtue, (counts.get(q.virtue) ?? 0) + 1);
});
return virtues.map((v) => {
const n = counts.get(v.id) || 1;
return Math.round((((sums.get(v.id) ?? 3 * n) - n) / (4 * n)) * 100);
});
}
Because it divides by the count per virtue, the same function also scores a 14-question quick version (two statements per virtue) without any changes.
Shareable results with no database
There's no server to store results, so the seven scores go into the URL as two-digit pairs:
// ?r=72558190436067
function encode(scores: number[]) {
return scores.map((s) => String(Math.min(99, s)).padStart(2, '0')).join('');
}
function decode(r: string | null): number[] | null {
if (!r || !/^\d{14}$/.test(r)) return null;
return Array.from({ length: 7 }, (_, i) => Number(r.slice(i * 2, i * 2 + 2)));
}
A perfect 100 is stored as 99 to keep every pair two digits. When the page loads with a valid ?r=, it renders the result directly and switches the wording from "your" to "their", so a shared link reads as a friend's result instead of yours. Answers never leave the browser.
The rose window
The window is a string of SVG built in a few lines of trigonometry. Each light is a pointed lancet drawn once, pointing straight up, and then rotated into place:
const N = 7;
const R_IN = 33; // where the lancets start
const R_SPRING = 66; // where the pointed arch springs
const R_TIP = 88; // apex of the arch
const HALF_ANGLE = (22 * Math.PI) / 180;
const lancet = (() => {
const t = Math.tan(HALF_ANGLE);
const xi = R_IN * t;
const xs = R_SPRING * t;
const arc = 30;
return `M${-xi} ${-R_IN}L${-xs} ${-R_SPRING}`
+ `A${arc} ${arc} 0 0 1 0 ${-R_TIP}`
+ `A${arc} ${arc} 0 0 1 ${xs} ${-R_SPRING}`
+ `L${xi} ${-R_IN}A${R_IN} ${R_IN} 0 0 0 ${-xi} ${-R_IN}Z`;
})();
Two circular arcs meeting at the tip give the Gothic point. To turn the window into a chart, each pane is first filled with its colour at 17% opacity (unlit glass), then a full-strength circle centred on the boss is clipped to the lancet's shape:
const reach = R_IN + (R_TIP - R_IN) * Math.max(score, 0.03);
Inside the rotated <g> for light i:
<clipPath id="c${i}"><use href="#lancet"/></clipPath><use href="#lancet" fill="${colour}" fill-opacity=".17"/><g clip-path="url(#c${i})"><circle r="${reach}" fill="${colour}"/></g>
A score of 0.4 lights the inner 40% of the pane; the rest stays dim. Because the lit edge is part of a circle around the centre, all seven fills line up like rings on a radar chart. With the virtue names written around the rim, it reads as one without looking like one.
Because it's just a string dropped into innerHTML, there's no chart library to ship, and leaving out scores draws the same window with every light fully lit.
Bonus: one canvas gotcha
The share cards are 1080 × 1920 PNGs drawn on a canvas, with the top virtue's painting as the background. The obvious move is to await img.decode() before drawing, but on large images decode() can stay pending indefinitely in some browsers, and the card never appears. Waiting on the plain load event avoids it:
const img = new Image();
img.onload = () => resolve(img);
img.onerror = () => reject(new Error(`could not load ${id}`));
img.src = `/art/card/${id}.png`;
If you want to see the window lit up with your own scores, the quiz is live at 7virtuestest.org. It takes about five minutes, and the result link it gives you is just those fourteen digits.
Comments
No comments yet. Start the discussion.