forked from Tadrisino/MathApp_frontend
Diagrams: hide answer-giving counts in quizzes (HideCounts); bar/pie slices take the colour named by their label
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { ChartSpec } from './types';
|
||||
import { COLORS, GRID, INK, Label, SERIES, SOFT, bidi, fa, mathText, niceStep, ticksBetween } from './svg';
|
||||
import { COLORS, GRID, INK, Label, SOFT, bidi, fa, mathText, niceStep, seriesColors, ticksBetween } from './svg';
|
||||
|
||||
const W = 330;
|
||||
|
||||
@@ -20,6 +20,7 @@ function Bars({ kind, labels, values, unit }: { kind: 'bar' | 'linechart'; label
|
||||
const Y = (v: number) => top + ((y1 - v) / (y1 - y0)) * plotH;
|
||||
const slot = plotW / labels.length;
|
||||
const X = (i: number) => left + slot * (i + 0.5);
|
||||
const barColors = seriesColors(labels, values.length);
|
||||
const barW = Math.min(38, slot * 0.62);
|
||||
const long = labels.some((l) => l.length > 6);
|
||||
|
||||
@@ -34,7 +35,7 @@ function Bars({ kind, labels, values, unit }: { kind: 'bar' | 'linechart'; label
|
||||
{unit && <text x={left - 4} y={12} fontSize={9.5} fill={SOFT} textAnchor="end" {...bidi(mathText(unit))}>{mathText(unit)}</text>}
|
||||
{kind === 'bar'
|
||||
? values.map((v, i) => {
|
||||
const c = COLORS[SERIES[i % SERIES.length]];
|
||||
const c = barColors[i];
|
||||
return (
|
||||
<g key={i}>
|
||||
<rect x={X(i) - barW / 2} y={Math.min(Y(v), Y(0))} width={barW} height={Math.abs(Y(v) - Y(0))} rx={4} fill={c} />
|
||||
@@ -64,12 +65,13 @@ function Bars({ kind, labels, values, unit }: { kind: 'bar' | 'linechart'; label
|
||||
function Pie({ labels, values }: { labels: string[]; values: number[] }) {
|
||||
const total = values.reduce((a, v) => a + Math.max(0, v), 0);
|
||||
if (total <= 0) return null;
|
||||
const sliceColors = seriesColors(labels, values.length);
|
||||
const cx = 105, cy = 105, r = 88;
|
||||
let a = -Math.PI / 2;
|
||||
const slices = values.map((v, i) => {
|
||||
const frac = Math.max(0, v) / total, a0 = a, a1 = a + frac * 2 * Math.PI;
|
||||
a = a1;
|
||||
return { i, frac, a0, a1, c: COLORS[SERIES[i % SERIES.length]] };
|
||||
return { i, frac, a0, a1, c: sliceColors[i] };
|
||||
});
|
||||
const H = Math.max(215, 24 + labels.length * 22);
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user