charts.py¶
clew/wiki/charts.py
Inline-SVG charts for the wiki: donuts of counts, and a burndown across versions.
_milestone_marks
list[str] clew.wiki.charts._milestone_marks(list[str] columns, list[float] xs, list[tuple[str, str]] milestones, int bottom)
Vertical marks for the milestones.
Source · 18 lines
def _milestone_marks(
columns: list[str], xs: list[float], milestones: list[tuple[str, str]], bottom: int
) -> list[str]:
"""@brief Milestone lines. @return Parts."""
parts = []
for version, heading in milestones:
i = next((i for i, k in enumerate(columns) if _same(version, k)), None)
if i is None:
continue
parts.append(
f'<line class="milestone" x1="{xs[i]:.1f}" y1="24" x2="{xs[i]:.1f}" y2="{bottom}">'
f"<title>{escape(heading)}</title></line>"
f'<text class="ms-label" x="{xs[i] + 4:.1f}" y="22" font-size="11">'
f"{escape(version)}</text>"
)
return parts
Activity · 7 steps
Flows
_same
bool clew.wiki.charts._same(str version, str key)
Whether a roadmap version and a version key name the same release.
Source · 3 lines
def _same(version: str, key: str) -> bool:
"""@brief Compare. @return Equal?"""
return version.lstrip("vV") == key.lstrip("vV")1 more lines, not shown
Activity · 1 steps
Flows
burndown
str clew.wiki.charts.burndown(list[tuple[str, dict[str, int]]] series, list[tuple[str, str]] milestones)
The burndown of open requirements across versions.
Source · 41 lines
def burndown(series: list[tuple[str, dict[str, int]]], milestones: list[tuple[str, str]]) -> str:
"""@brief Draw the burndown. @return SVG."""
keys = [k for k, _c in series]
ahead = [v for v, _h in milestones if not any(_same(v, k) for k in keys)]
columns = keys + ahead
opens = [sum(n for t, n in counts.items() if t not in CLOSED) for _k, counts in series]
top = max([*opens, 1])
w, h, left, bottom = max(360, 90 * len(columns) + 80), 240, 50, 200
step = (w - left - 60) / max(1, len(columns) - 1)
xs = [left + step * i for i in range(len(columns))]
y = lambda n: bottom - (bottom - 30) * n / top # noqa: E731
parts = [
f'<svg class="burndown" xmlns="http://www.w3.org/2000/svg" width="{w:.0f}" height="{h}" '
'role="img" aria-label="Open requirements by version"><title>Open requirements by '
"version</title>",
f'<path class="axis" d="M{left} 20 V{bottom} H{w - 20}"/>',
f'<text x="{left - 8}" y="{y(top) + 4:.1f}" text-anchor="end" font-size="11">{top}</text>',
f'<text x="{left - 8}" y="{bottom + 4}" text-anchor="end" font-size="11">0</text>',
]
parts += _milestone_marks(columns, xs, milestones, bottom)
if opens and ahead:
parts.append(
f'<path class="ideal" d="M{xs[0]:.1f} {y(opens[0]):.1f} L{xs[-1]:.1f} {bottom}"/>'
)
if opens:
points = " ".join(f"{x:.1f},{y(n):.1f}" for x, n in zip(xs, opens, strict=False))
parts.append(f'<polyline class="open" points="{points}"/>')
for x, n, key in zip(xs, opens, keys, strict=False):
parts.append(
f'<circle class="pt" cx="{x:.1f}" cy="{y(n):.1f}" r="4"><title>{escape(key)}: '
f"{n} open</title></circle>"
)
for x, key in zip(xs, columns, strict=True):
parts.append(
f'<text x="{x:.1f}" y="{bottom + 18}" text-anchor="middle" font-size="12">'
f"{escape(key)}</text>"
)
parts.append("</svg>")
return "".join(parts)
Activity · 22 steps
Requirements
Flows
donut
str clew.wiki.charts.donut(list[tuple[str, int]] counts, str title)
A donut of labelled counts with its legend.
Source · 34 lines
def donut(counts: list[tuple[str, int]], title: str) -> str:
"""@brief Draw a donut. @return SVG."""
total = sum(n for _l, n in counts)
r, cx, cy, width = 52, 70, 70, 22
parts = [
f'<svg class="donut" xmlns="http://www.w3.org/2000/svg" width="300" '
f'height="{max(150, 24 * len(counts) + 20)}" role="img" aria-label="{escape(title)}">',
f"<title>{escape(title)}</title>",
]
start = -math.pi / 2
for label, n in counts:
if total and n:
sweep = 2 * math.pi * n / total
parts.append(_arc(label, n, (cx, cy, r, width), start, sweep))
start += sweep
if not total:
parts.append(
f'<circle cx="{cx}" cy="{cy}" r="{r}" fill="none" class="c-absent" '
f'stroke-width="{width}"/>'
)
parts.append(
f'<text x="{cx}" y="{cy + 7}" text-anchor="middle" font-size="22" '
f'font-weight="600">{total}</text>'
)
for i, (label, n) in enumerate(counts):
y = 22 + 24 * i
parts.append(
f'<rect x="160" y="{y - 11}" width="14" height="14" rx="3" class="c-{escape(label)}"/>'
f'<text x="182" y="{y}" font-size="13">{escape(label)} {n}</text>'
)
parts.append("</svg>")
return "".join(parts)
Activity · 17 steps
Requirements
roadmap_milestones
list[tuple[str, str]] clew.wiki.charts.roadmap_milestones(Path path)
Milestones named by a roadmap's headings.
Source · 19 lines
def roadmap_milestones(path: Path) -> list[tuple[str, str]]:
"""ONLY A HEADING THAT LEADS WITH A VERSION is a milestone ("## v0.9 — later", "# Roadmap to
v0.8.0"); a version mentioned in passing names something else. Prose and tables are not read,
so a roadmap's item lists can change shape without moving the chart.
@brief Read milestones.
@return Milestones.
"""
out: dict[str, str] = {}
for line in Path(path).read_text(encoding="utf-8").splitlines():
if not line.startswith("#"):
continue
heading = line.lstrip("#").strip()
match = _VERSION.match(heading)
if match:
out.setdefault(match.group(1), heading)
return list(out.items())