| """Tiny dependency-free, theme-aware, self-contained SVG figure builders for the logbook. |
| Each returns a complete <div> with inline <svg> (a few KB), safe to embed in a figure cell.""" |
|
|
| PALETTE = ["#2f6df6", "#d9480f", "#2b8a3e", "#8a5cf6", "#e8590c", "#0c8599"] |
|
|
|
|
| def _axes(w, h, pad): |
| return (f'<line x1="{pad}" y1="{h-pad}" x2="{w-10}" y2="{h-pad}" stroke="currentColor" opacity="0.5"/>' |
| f'<line x1="{pad}" y1="10" x2="{pad}" y2="{h-pad}" stroke="currentColor" opacity="0.5"/>') |
|
|
|
|
| def line_chart(xs, ys, title, xlabel, ylabel, ymax=100, hline=None, hline_label=None, |
| w=720, h=420, pad=54, color="#d9480f"): |
| xmin, xmax = min(xs), max(xs) |
|
|
| def sx(x): |
| return pad + (x - xmin) / (xmax - xmin + 1e-9) * (w - pad - 20) |
|
|
| def sy(y): |
| return (h - pad) - (y / ymax) * (h - pad - 20) |
| pts = " ".join(f"{sx(x):.1f},{sy(y):.1f}" for x, y in zip(xs, ys)) |
| dots = "".join(f'<circle cx="{sx(x):.1f}" cy="{sy(y):.1f}" r="4" fill="{color}"/>' for x, y in zip(xs, ys)) |
| grid = "" |
| for gy in range(0, ymax + 1, max(1, ymax // 5)): |
| yy = sy(gy) |
| grid += (f'<line x1="{pad}" y1="{yy:.1f}" x2="{w-10}" y2="{yy:.1f}" stroke="currentColor" opacity="0.12"/>' |
| f'<text x="{pad-8}" y="{yy+4:.1f}" text-anchor="end" font-size="11" fill="currentColor" opacity="0.7">{gy}</text>') |
| xticks = "" |
| for x in xs: |
| xticks += f'<text x="{sx(x):.1f}" y="{h-pad+16}" text-anchor="middle" font-size="11" fill="currentColor" opacity="0.7">{x}</text>' |
| hl = "" |
| if hline is not None: |
| yy = sy(hline) |
| hl = (f'<line x1="{pad}" y1="{yy:.1f}" x2="{w-10}" y2="{yy:.1f}" stroke="#888" stroke-dasharray="5,4"/>' |
| f'<text x="{w-14}" y="{yy-5:.1f}" text-anchor="end" font-size="11" fill="currentColor" opacity="0.8">{hline_label or ""}</text>') |
| return f'''<div style="max-width:100%;overflow-x:auto"> |
| <svg viewBox="0 0 {w} {h}" width="100%" style="font-family:system-ui,sans-serif;color:inherit"> |
| <text x="{w/2}" y="20" text-anchor="middle" font-size="14" font-weight="600" fill="currentColor">{title}</text> |
| {grid}{_axes(w,h,pad)}{hl} |
| <polyline points="{pts}" fill="none" stroke="{color}" stroke-width="3"/>{dots}{xticks} |
| <text x="{w/2}" y="{h-6}" text-anchor="middle" font-size="12" fill="currentColor" opacity="0.8">{xlabel}</text> |
| <text x="16" y="{h/2}" text-anchor="middle" font-size="12" fill="currentColor" opacity="0.8" transform="rotate(-90 16 {h/2})">{ylabel}</text> |
| </svg></div>''' |
|
|
|
|
| def grouped_bars(categories, series, title, ylabel, ymax=100, hline=None, hline_label=None, |
| w=760, h=440, pad=56): |
| """series: list of (name, [values aligned to categories]).""" |
| n_cat = len(categories) |
| n_ser = len(series) |
| plot_w = w - pad - 20 |
| group_w = plot_w / n_cat |
| bar_w = group_w / (n_ser + 0.5) |
|
|
| def sy(y): |
| return (h - pad) - (y / ymax) * (h - pad - 40) |
| grid = "" |
| for gy in range(0, ymax + 1, max(1, ymax // 5)): |
| yy = sy(gy) |
| grid += (f'<line x1="{pad}" y1="{yy:.1f}" x2="{w-10}" y2="{yy:.1f}" stroke="currentColor" opacity="0.12"/>' |
| f'<text x="{pad-8}" y="{yy+4:.1f}" text-anchor="end" font-size="11" fill="currentColor" opacity="0.7">{gy}</text>') |
| bars = "" |
| labels = "" |
| for ci, cat in enumerate(categories): |
| gx = pad + ci * group_w |
| for si, (sname, vals) in enumerate(series): |
| v = vals[ci] |
| if v is None: |
| continue |
| bx = gx + 6 + si * bar_w |
| by = sy(v) |
| bars += (f'<rect x="{bx:.1f}" y="{by:.1f}" width="{bar_w*0.9:.1f}" height="{(h-pad)-by:.1f}" ' |
| f'fill="{PALETTE[si%len(PALETTE)]}" rx="2"><title>{sname}: {v}</title></rect>') |
| labels += f'<text x="{gx+group_w/2:.1f}" y="{h-pad+16}" text-anchor="middle" font-size="10.5" fill="currentColor" opacity="0.75">{cat}</text>' |
| hl = "" |
| if hline is not None: |
| yy = sy(hline) |
| hl = (f'<line x1="{pad}" y1="{yy:.1f}" x2="{w-10}" y2="{yy:.1f}" stroke="#888" stroke-dasharray="5,4"/>' |
| f'<text x="{w-14}" y="{yy-5:.1f}" text-anchor="end" font-size="11" fill="currentColor" opacity="0.85">{hline_label or ""}</text>') |
| legend = "" |
| lx = pad |
| for si, (sname, _) in enumerate(series): |
| legend += (f'<rect x="{lx}" y="{h-20}" width="12" height="12" fill="{PALETTE[si%len(PALETTE)]}" rx="2"/>' |
| f'<text x="{lx+16}" y="{h-10}" font-size="11" fill="currentColor">{sname}</text>') |
| lx += 30 + len(sname) * 7.2 |
| return f'''<div style="max-width:100%;overflow-x:auto"> |
| <svg viewBox="0 0 {w} {h}" width="100%" style="font-family:system-ui,sans-serif;color:inherit"> |
| <text x="{w/2}" y="20" text-anchor="middle" font-size="14" font-weight="600" fill="currentColor">{title}</text> |
| {grid}{_axes(w,h,pad)}{hl}{bars}{labels}{legend} |
| <text x="16" y="{h/2}" text-anchor="middle" font-size="12" fill="currentColor" opacity="0.8" transform="rotate(-90 16 {h/2})">{ylabel}</text> |
| </svg></div>''' |
|
|
|
|
| def wrap(inner, title="figure"): |
| return f'<!doctype html><meta charset="utf-8"><title>{title}</title><body style="margin:0;padding:8px">{inner}</body>' |
|
|