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.

clew/wiki/charts.py:190

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
lines 194–194parts = []lines 195–195◇ for version, heading in milestoneslines 196–196i = next((i for i, k in enumerate(columns) if _same(version, k)), None)lines 197–197◇ if i is Nonelines 198–198continuelines 199–204parts.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>" )lines 205–205return parts

Flows

_same

bool clew.wiki.charts._same(str version, str key)

Whether a roadmap version and a version key name the same release.

clew/wiki/charts.py:214

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
lines 216–216return version.lstrip("vV") == key.lstrip("vV")

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.

clew/wiki/charts.py:141

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
lines 143–143keys = [k for k, _c in series]lines 144–144ahead = [v for v, _h in milestones if not any(_same(v, k) for k in keys)]lines 145–145columns = keys + aheadlines 146–146opens = [sum(n for t, n in counts.items() if t not in CLOSED) for _k, counts in series]lines 147–147top = max([*opens, 1])lines 148–148w, h, left, bottom = max(360, 90 * len(columns) + 80), 240, 50, 200lines 149–149step = (w - left - 60) / max(1, len(columns) - 1)lines 150–150xs = [left + step * i for i in range(len(columns))]lines 151–151y = lambda n: bottom - (bottom - 30) * n / toplines 152–159parts = [ 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>', ]lines 160–160parts += _milestone_marks(columns, xs, milestones, bottom)lines 161–161◇ if opens and aheadlines 162–164parts.append( f'<path class="ideal" d="M{xs[0]:.1f} {y(opens[0]):.1f} L{xs[-1]:.1f} {bottom}"/>' )lines 165–165◇ if openslines 166–166points = " ".join(f"{x:.1f},{y(n):.1f}" for x, n in zip(xs, opens, strict=False))lines 167–167parts.append(f'<polyline class="open" points="{points}"/>')lines 168–168◇ for x, n, key in zip(xs, opens, keys, strict=False)lines 169–172parts.append( f'<circle class="pt" cx="{x:.1f}" cy="{y(n):.1f}" r="4"><title>{escape(key)}: ' f"{n} open</title></circle>" )lines 173–173◇ for x, key in zip(xs, columns, strict=True)lines 174–177parts.append( f'<text x="{x:.1f}" y="{bottom + 18}" text-anchor="middle" font-size="12">' f"{escape(key)}</text>" )lines 178–178parts.append("</svg>")lines 179–179return "".join(parts)

Requirements

REQ-DDB-WIKI-013

Flows

donut

str clew.wiki.charts.donut(list[tuple[str, int]] counts, str title)

A donut of labelled counts with its legend.

clew/wiki/charts.py:42

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
lines 44–44total = sum(n for _l, n in counts)lines 45–45r, cx, cy, width = 52, 70, 70, 22lines 46–50parts = [ 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>", ]lines 51–51start = -math.pi / 2lines 52–52◇ for label, n in countslines 53–53◇ if total and nlines 54–54sweep = 2 * math.pi * n / totallines 55–55parts.append(_arc(label, n, (cx, cy, r, width), start, sweep))lines 56–56start += sweeplines 57–57◇ if not totallines 58–61parts.append( f'<circle cx="{cx}" cy="{cy}" r="{r}" fill="none" class="c-absent" ' f'stroke-width="{width}"/>' )lines 62–65parts.append( f'<text x="{cx}" y="{cy + 7}" text-anchor="middle" font-size="22" ' f'font-weight="600">{total}</text>' )lines 66–66◇ for i, (label, n) in enumerate(counts)lines 67–67y = 22 + 24 * ilines 68–71parts.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>' )lines 72–72parts.append("</svg>")lines 73–73return "".join(parts)

Requirements

REQ-DDB-WIKI-012

roadmap_milestones

list[tuple[str, str]] clew.wiki.charts.roadmap_milestones(Path path)

Milestones named by a roadmap's headings.

clew/wiki/charts.py:116

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())

Activity · 9 steps
lines 124–124out: dict[str, str] = {}lines 125–125◇ for line in Path(path).read_text(encoding="utf-8").splitlines()lines 126–126◇ if not line.startswith("#")lines 127–127continuelines 128–128heading = line.lstrip("#").strip()lines 129–129match = _VERSION.match(heading)lines 130–130◇ if matchlines 131–131out.setdefault(match.group(1), heading)lines 132–132return list(out.items())

Requirements

REQ-DDB-WIKI-013

Flows