github-statistics 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- github_statistics/__init__.py +14 -0
- github_statistics/__main__.py +10 -0
- github_statistics/_version.py +24 -0
- github_statistics/cli/__init__.py +9 -0
- github_statistics/cli/app.py +104 -0
- github_statistics/cli/cache.py +159 -0
- github_statistics/cli/collect.py +219 -0
- github_statistics/cli/common.py +128 -0
- github_statistics/cli/config.py +80 -0
- github_statistics/cli/doctor.py +214 -0
- github_statistics/cli/export.py +111 -0
- github_statistics/cli/options.py +175 -0
- github_statistics/cli/render.py +126 -0
- github_statistics/cli/run.py +137 -0
- github_statistics/cli/scope.py +96 -0
- github_statistics/cli/site.py +150 -0
- github_statistics/client/__init__.py +40 -0
- github_statistics/client/accounting.py +99 -0
- github_statistics/client/budget.py +125 -0
- github_statistics/client/cooldown.py +113 -0
- github_statistics/client/errors.py +60 -0
- github_statistics/client/graphql.py +281 -0
- github_statistics/client/paging.py +92 -0
- github_statistics/client/policy.py +243 -0
- github_statistics/client/transport.py +437 -0
- github_statistics/collect/__init__.py +44 -0
- github_statistics/collect/common.py +69 -0
- github_statistics/collect/enrich_items.py +423 -0
- github_statistics/collect/items.py +238 -0
- github_statistics/collect/jobs.py +194 -0
- github_statistics/collect/listed.py +42 -0
- github_statistics/collect/members.py +70 -0
- github_statistics/collect/reader.py +124 -0
- github_statistics/collect/run.py +354 -0
- github_statistics/collect/runs.py +436 -0
- github_statistics/collect/slices.py +55 -0
- github_statistics/collect/steps.py +154 -0
- github_statistics/collect/sweep.py +70 -0
- github_statistics/config/__init__.py +37 -0
- github_statistics/config/loader.py +430 -0
- github_statistics/config/models.py +156 -0
- github_statistics/config/schema.py +245 -0
- github_statistics/export/__init__.py +11 -0
- github_statistics/export/csv.py +93 -0
- github_statistics/export/documents.py +189 -0
- github_statistics/export/json.py +33 -0
- github_statistics/py.typed +2 -0
- github_statistics/render/__init__.py +44 -0
- github_statistics/render/bars3d.py +420 -0
- github_statistics/render/charts.py +214 -0
- github_statistics/render/demo.py +133 -0
- github_statistics/render/images.py +31 -0
- github_statistics/render/layout.py +341 -0
- github_statistics/render/model.py +260 -0
- github_statistics/render/palette.py +41 -0
- github_statistics/scope/__init__.py +17 -0
- github_statistics/scope/apply.py +95 -0
- github_statistics/scope/resolver.py +228 -0
- github_statistics/series/__init__.py +22 -0
- github_statistics/series/actors.py +128 -0
- github_statistics/series/coverage.py +86 -0
- github_statistics/series/daily.py +229 -0
- github_statistics/series/leaderboard.py +221 -0
- github_statistics/series/repositories.py +154 -0
- github_statistics/site/__init__.py +9 -0
- github_statistics/site/assets/site.css +281 -0
- github_statistics/site/build.py +392 -0
- github_statistics/site/templates/base.html.j2 +25 -0
- github_statistics/site/templates/index.html.j2 +30 -0
- github_statistics/site/templates/macros.html.j2 +5 -0
- github_statistics/site/templates/site.js.j2 +66 -0
- github_statistics/site/templates/target.html.j2 +95 -0
- github_statistics/store/__init__.py +40 -0
- github_statistics/store/activity.py +311 -0
- github_statistics/store/aggregates.py +294 -0
- github_statistics/store/base.py +163 -0
- github_statistics/store/codec.py +263 -0
- github_statistics/store/models.py +136 -0
- github_statistics/store/paths.py +36 -0
- github_statistics/store/schema.py +169 -0
- github_statistics/store/sqlite.py +299 -0
- github_statistics-0.1.0.dist-info/METADATA +308 -0
- github_statistics-0.1.0.dist-info/RECORD +86 -0
- github_statistics-0.1.0.dist-info/WHEEL +4 -0
- github_statistics-0.1.0.dist-info/entry_points.txt +2 -0
- github_statistics-0.1.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# SPDX-FileCopyrightText: 2026 The Linux Foundation
|
|
3
|
+
"""Synthetic chart specifications for the ``demo`` command.
|
|
4
|
+
|
|
5
|
+
The data imitates a busy organisation: weekly seasonality, a slow
|
|
6
|
+
upward drift, two bulk-merge spikes, four days with no data and one
|
|
7
|
+
with partial data. It needs no token and no store.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import random
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
from datetime import date, timedelta
|
|
15
|
+
|
|
16
|
+
from .model import ChartSpec, Coverage, Series
|
|
17
|
+
from .palette import series_colour
|
|
18
|
+
|
|
19
|
+
# Monday first: weekends are quiet.
|
|
20
|
+
_WEEKLY = (1.0, 1.08, 1.05, 1.0, 0.86, 0.3, 0.22)
|
|
21
|
+
_GAP_DAYS = 4
|
|
22
|
+
_PARTIAL_DAYS = 1
|
|
23
|
+
_SPIKES = 2
|
|
24
|
+
_SPIKE_FACTOR = 6.0
|
|
25
|
+
MIN_DAYS = _SPIKES + _GAP_DAYS + _PARTIAL_DAYS
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(frozen=True)
|
|
29
|
+
class _Line:
|
|
30
|
+
"""One generated series: its key, legend text and mean daily count."""
|
|
31
|
+
|
|
32
|
+
key: str
|
|
33
|
+
label: str
|
|
34
|
+
level: float
|
|
35
|
+
spikes: bool
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass(frozen=True)
|
|
39
|
+
class _Metric:
|
|
40
|
+
"""How one demo chart's two series are generated."""
|
|
41
|
+
|
|
42
|
+
name: str
|
|
43
|
+
title: str
|
|
44
|
+
first: _Line
|
|
45
|
+
second: _Line
|
|
46
|
+
back_on_tie: str
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
_METRICS = (
|
|
50
|
+
_Metric(
|
|
51
|
+
"pull-requests",
|
|
52
|
+
"Pull requests opened and closed",
|
|
53
|
+
_Line("opened", "Opened", 24.0, spikes=False),
|
|
54
|
+
_Line("closed", "Closed", 22.0, spikes=True),
|
|
55
|
+
"closed",
|
|
56
|
+
),
|
|
57
|
+
_Metric(
|
|
58
|
+
"issues",
|
|
59
|
+
"Issues opened and closed",
|
|
60
|
+
_Line("opened", "Opened", 9.0, spikes=False),
|
|
61
|
+
_Line("closed", "Closed", 8.0, spikes=True),
|
|
62
|
+
"closed",
|
|
63
|
+
),
|
|
64
|
+
_Metric(
|
|
65
|
+
"workflows",
|
|
66
|
+
"Workflow runs and jobs",
|
|
67
|
+
_Line("runs", "Runs", 110.0, spikes=True),
|
|
68
|
+
_Line("jobs", "Jobs", 330.0, spikes=True),
|
|
69
|
+
"jobs",
|
|
70
|
+
),
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _series(
|
|
75
|
+
rng: random.Random, line: _Line, days: tuple[date, ...], spikes: set[int]
|
|
76
|
+
) -> Series:
|
|
77
|
+
"""Generate one series with weekly seasonality, a slow drift and optional spikes."""
|
|
78
|
+
count = len(days)
|
|
79
|
+
values = []
|
|
80
|
+
for index, day in enumerate(days):
|
|
81
|
+
mean = (
|
|
82
|
+
line.level
|
|
83
|
+
* _WEEKLY[day.weekday()]
|
|
84
|
+
* (0.8 + 0.45 * index / max(count - 1, 1))
|
|
85
|
+
)
|
|
86
|
+
if line.spikes and index in spikes:
|
|
87
|
+
mean *= _SPIKE_FACTOR
|
|
88
|
+
values.append(max(0, round(rng.gauss(mean, mean**0.5))))
|
|
89
|
+
return Series(
|
|
90
|
+
key=line.key,
|
|
91
|
+
label=line.label,
|
|
92
|
+
colour=series_colour(line.key),
|
|
93
|
+
values=tuple(values),
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def demo_specs(days: int, seed: int, until: date) -> list[tuple[str, ChartSpec]]:
|
|
98
|
+
"""Return ``(name, spec)`` for each demo chart over ``days`` days ending on ``until``.
|
|
99
|
+
|
|
100
|
+
The same ``days``, ``seed`` and ``until`` always give the same data.
|
|
101
|
+
"""
|
|
102
|
+
if days < MIN_DAYS:
|
|
103
|
+
raise ValueError(f"the demo needs at least {MIN_DAYS} days")
|
|
104
|
+
rng = random.Random(seed)
|
|
105
|
+
window = tuple(until - timedelta(days=offset) for offset in range(days - 1, -1, -1))
|
|
106
|
+
picked = rng.sample(range(days), MIN_DAYS)
|
|
107
|
+
spikes = set(picked[:_SPIKES])
|
|
108
|
+
gaps = set(picked[_SPIKES : _SPIKES + _GAP_DAYS])
|
|
109
|
+
partial = set(picked[_SPIKES + _GAP_DAYS :])
|
|
110
|
+
coverage = tuple(
|
|
111
|
+
Coverage.NONE
|
|
112
|
+
if index in gaps
|
|
113
|
+
else Coverage.PARTIAL
|
|
114
|
+
if index in partial
|
|
115
|
+
else Coverage.COMPLETE
|
|
116
|
+
for index in range(days)
|
|
117
|
+
)
|
|
118
|
+
subtitle = f"demo-org · synthetic data, seed {seed} · {days} days to {until.isoformat()} · all actors"
|
|
119
|
+
footer = f"Synthetic demo data · {_GAP_DAYS} days without data, {_PARTIAL_DAYS} partial · github-statistics demo"
|
|
120
|
+
specs = []
|
|
121
|
+
for metric in _METRICS:
|
|
122
|
+
spec = ChartSpec(
|
|
123
|
+
title=metric.title,
|
|
124
|
+
subtitle=subtitle,
|
|
125
|
+
footer=footer,
|
|
126
|
+
days=window,
|
|
127
|
+
first=_series(rng, metric.first, window, spikes),
|
|
128
|
+
second=_series(rng, metric.second, window, spikes),
|
|
129
|
+
coverage=coverage,
|
|
130
|
+
back_on_tie=metric.back_on_tie,
|
|
131
|
+
)
|
|
132
|
+
specs.append((metric.name, spec))
|
|
133
|
+
return specs
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# SPDX-FileCopyrightText: 2026 The Linux Foundation
|
|
3
|
+
"""Downscaled copies of a rendered master for inline use."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from collections.abc import Sequence
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from PIL import Image
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def write_derivatives(master: Path, widths: Sequence[int]) -> list[Path]:
|
|
14
|
+
"""Write a Lanczos downscale of ``master`` per width, as ``<stem>-<width>.png``.
|
|
15
|
+
|
|
16
|
+
Each copy keeps the master's aspect ratio. A width that is not
|
|
17
|
+
positive or exceeds the master's raises :class:`ValueError`.
|
|
18
|
+
"""
|
|
19
|
+
written: list[Path] = []
|
|
20
|
+
with Image.open(master) as image:
|
|
21
|
+
source = image.convert("RGB")
|
|
22
|
+
for width in widths:
|
|
23
|
+
if not 0 < width <= source.width:
|
|
24
|
+
raise ValueError(f"width {width} is outside 1..{source.width}")
|
|
25
|
+
height = max(1, round(source.height * width / source.width))
|
|
26
|
+
target = master.with_name(f"{master.stem}-{width}.png")
|
|
27
|
+
source.resize((width, height), Image.Resampling.LANCZOS).save(
|
|
28
|
+
target, optimize=True
|
|
29
|
+
)
|
|
30
|
+
written.append(target)
|
|
31
|
+
return written
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
# SPDX-FileCopyrightText: 2026 The Linux Foundation
|
|
3
|
+
"""Chart layout: row assignment, ticks, gap spans, month labels and geometry.
|
|
4
|
+
|
|
5
|
+
Nothing here draws. :func:`plan_chart` turns a :class:`ChartSpec` into
|
|
6
|
+
a :class:`ChartPlan` that tests can check without inspecting pixels,
|
|
7
|
+
and :class:`Geometry` maps that plan to design-pixel positions.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import math
|
|
13
|
+
from collections.abc import Sequence
|
|
14
|
+
from dataclasses import dataclass
|
|
15
|
+
from datetime import date
|
|
16
|
+
|
|
17
|
+
from .model import (
|
|
18
|
+
Bar,
|
|
19
|
+
Box,
|
|
20
|
+
ChartPlan,
|
|
21
|
+
ChartSpec,
|
|
22
|
+
Coverage,
|
|
23
|
+
GapSpan,
|
|
24
|
+
MonthLabel,
|
|
25
|
+
Peak,
|
|
26
|
+
PeakLabel,
|
|
27
|
+
Style,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
MONTHS = (
|
|
31
|
+
"Jan",
|
|
32
|
+
"Feb",
|
|
33
|
+
"Mar",
|
|
34
|
+
"Apr",
|
|
35
|
+
"May",
|
|
36
|
+
"Jun",
|
|
37
|
+
"Jul",
|
|
38
|
+
"Aug",
|
|
39
|
+
"Sep",
|
|
40
|
+
"Oct",
|
|
41
|
+
"Nov",
|
|
42
|
+
"Dec",
|
|
43
|
+
)
|
|
44
|
+
_NICE_STEPS = (1.0, 2.0, 2.5, 5.0)
|
|
45
|
+
_MONTH_STRIDES = (1, 2, 3, 4, 6, 12)
|
|
46
|
+
# Average advance of a DejaVu Sans glyph, as a fraction of the font size.
|
|
47
|
+
GLYPH_WIDTH = 0.62
|
|
48
|
+
|
|
49
|
+
# A legend swatch, and its label's offset from the swatch's front-bottom-left corner.
|
|
50
|
+
SWATCH_WIDTH = 32.0
|
|
51
|
+
SWATCH_HEIGHT = 40.0
|
|
52
|
+
SWATCH_DX = 16.0
|
|
53
|
+
SWATCH_DY = 10.0
|
|
54
|
+
LEGEND_TEXT_X = 72.0
|
|
55
|
+
LEGEND_TEXT_Y = 20.0
|
|
56
|
+
# Text height, glyph bottom to top, as a fraction of the font size.
|
|
57
|
+
LINE_HEIGHT = 1.2
|
|
58
|
+
# Vertical distance between stacked peak labels, as a fraction of the font size.
|
|
59
|
+
LABEL_STEP = 1.6
|
|
60
|
+
|
|
61
|
+
NO_DATA_LABEL = "no data"
|
|
62
|
+
PARTIAL_DATA_LABEL = "partial data"
|
|
63
|
+
|
|
64
|
+
Point = tuple[float, float]
|
|
65
|
+
Polygon = list[Point]
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@dataclass(frozen=True)
|
|
69
|
+
class Geometry:
|
|
70
|
+
"""Pixel positions derived from a style and a day count."""
|
|
71
|
+
|
|
72
|
+
left: float
|
|
73
|
+
right: float
|
|
74
|
+
bottom: float
|
|
75
|
+
top: float
|
|
76
|
+
pitch: float
|
|
77
|
+
width: float
|
|
78
|
+
dx: float
|
|
79
|
+
dy: float
|
|
80
|
+
floor_dx: float
|
|
81
|
+
floor_dy: float
|
|
82
|
+
|
|
83
|
+
@classmethod
|
|
84
|
+
def build(cls, style: Style, days: int) -> Geometry:
|
|
85
|
+
"""Lay out the plot area for ``days`` bars per row."""
|
|
86
|
+
left = style.margin_left
|
|
87
|
+
right = style.canvas_width - style.margin_right
|
|
88
|
+
pitch = (right - left) / days
|
|
89
|
+
depth_pitch = min(pitch, style.max_depth_pitch)
|
|
90
|
+
dx = style.depth_x * depth_pitch
|
|
91
|
+
dy = style.depth_y * depth_pitch
|
|
92
|
+
return cls(
|
|
93
|
+
left=left,
|
|
94
|
+
right=right,
|
|
95
|
+
bottom=style.margin_bottom,
|
|
96
|
+
top=style.canvas_height - style.margin_top,
|
|
97
|
+
pitch=pitch,
|
|
98
|
+
width=style.bar_width * pitch,
|
|
99
|
+
dx=dx,
|
|
100
|
+
dy=dy,
|
|
101
|
+
floor_dx=style.floor_depth * dx,
|
|
102
|
+
floor_dy=style.floor_depth * dy,
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
def scale(self, y_max: int) -> float:
|
|
106
|
+
"""Pixels per unit of value, mapping ``y_max`` to the top of the wall."""
|
|
107
|
+
return (self.top - self.bottom - self.floor_dy) / y_max
|
|
108
|
+
|
|
109
|
+
def bar_origin(self, day: int, row: int) -> Point:
|
|
110
|
+
"""The front-bottom-left corner of the bar of ``day`` in ``row``."""
|
|
111
|
+
return (
|
|
112
|
+
self.left + day * self.pitch + row * self.dx,
|
|
113
|
+
self.bottom + row * self.dy,
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def nice_ticks(peak: int, target: int = 6) -> tuple[int, ...]:
|
|
118
|
+
"""Round tick values from zero to at least ``peak``, about ``target`` intervals apart.
|
|
119
|
+
|
|
120
|
+
Steps come from 1, 2, 2.5 and 5 times a power of ten; 2.5 is used
|
|
121
|
+
only where it yields whole numbers, since every value is a count.
|
|
122
|
+
"""
|
|
123
|
+
peak = max(peak, 1)
|
|
124
|
+
raw = peak / max(target, 1)
|
|
125
|
+
exponent = 0
|
|
126
|
+
while True:
|
|
127
|
+
for multiple in _NICE_STEPS:
|
|
128
|
+
step = multiple * 10**exponent
|
|
129
|
+
if step >= raw and step.is_integer():
|
|
130
|
+
top = math.ceil(peak / step) * int(step)
|
|
131
|
+
return tuple(range(0, top + 1, int(step)))
|
|
132
|
+
exponent += 1
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _assign(spec: ChartSpec) -> tuple[tuple[Bar, ...], tuple[Bar, ...]]:
|
|
136
|
+
"""Put the larger value of each day in the back row and the smaller in front."""
|
|
137
|
+
back: list[Bar] = []
|
|
138
|
+
front: list[Bar] = []
|
|
139
|
+
first, second = spec.first, spec.second
|
|
140
|
+
for day, (a, b) in enumerate(zip(first.values, second.values, strict=True)):
|
|
141
|
+
if a > b or (a == b and spec.back_on_tie == first.key):
|
|
142
|
+
back_series, front_series = first, second
|
|
143
|
+
else:
|
|
144
|
+
back_series, front_series = second, first
|
|
145
|
+
back.append(Bar(day, back_series.key, back_series.values[day]))
|
|
146
|
+
front.append(Bar(day, front_series.key, front_series.values[day]))
|
|
147
|
+
return tuple(back), tuple(front)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _gap_spans(coverage: Sequence[Coverage]) -> tuple[GapSpan, ...]:
|
|
151
|
+
"""Group consecutive gap days that share a coverage into spans."""
|
|
152
|
+
spans: list[GapSpan] = []
|
|
153
|
+
for day, state in enumerate(coverage):
|
|
154
|
+
if state is Coverage.COMPLETE:
|
|
155
|
+
continue
|
|
156
|
+
if spans and spans[-1].stop == day and spans[-1].coverage is state:
|
|
157
|
+
spans[-1] = GapSpan(spans[-1].start, day + 1, state)
|
|
158
|
+
else:
|
|
159
|
+
spans.append(GapSpan(day, day + 1, state))
|
|
160
|
+
return tuple(spans)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _months(days: Sequence[date]) -> list[tuple[int, int, date]]:
|
|
164
|
+
"""Return ``(start, stop, first_day)`` for each calendar month in ``days``."""
|
|
165
|
+
months: list[tuple[int, int, date]] = []
|
|
166
|
+
start = 0
|
|
167
|
+
for index in range(1, len(days) + 1):
|
|
168
|
+
if index == len(days) or days[index].month != days[start].month:
|
|
169
|
+
months.append((start, index, days[start]))
|
|
170
|
+
start = index
|
|
171
|
+
return months
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _month_labels(
|
|
175
|
+
days: Sequence[date], style: Style, pitch: float
|
|
176
|
+
) -> tuple[MonthLabel, ...]:
|
|
177
|
+
"""Centre a ``Mon YYYY`` label in each month, thinning them where they would collide."""
|
|
178
|
+
months = _months(days)
|
|
179
|
+
font_px = style.month_pt * style.reference_dpi / 72
|
|
180
|
+
label_px = len("Mmm YYYY") * GLYPH_WIDTH * font_px
|
|
181
|
+
clearance = label_px * 1.15
|
|
182
|
+
stride = next((s for s in _MONTH_STRIDES if 30 * pitch * s >= clearance), 12)
|
|
183
|
+
labels: list[MonthLabel] = []
|
|
184
|
+
for start, stop, first in months:
|
|
185
|
+
if (first.year * 12 + first.month - 1) % stride:
|
|
186
|
+
continue
|
|
187
|
+
centre = (start + stop) / 2
|
|
188
|
+
if (stop - start) * pitch < label_px and len(months) > 1:
|
|
189
|
+
continue
|
|
190
|
+
if labels and (centre - labels[-1].centre) * pitch < clearance:
|
|
191
|
+
continue
|
|
192
|
+
labels.append(MonthLabel(centre, f"{MONTHS[first.month - 1]} {first.year}"))
|
|
193
|
+
return tuple(labels)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _peaks(spec: ChartSpec, back: Sequence[Bar]) -> tuple[Peak, ...]:
|
|
197
|
+
"""Find each series' highest covered day, earliest on a tie."""
|
|
198
|
+
peaks: list[Peak] = []
|
|
199
|
+
for series in (spec.first, spec.second):
|
|
200
|
+
best: tuple[int, int] | None = None
|
|
201
|
+
for day, value in enumerate(series.values):
|
|
202
|
+
if spec.coverage[day] is not Coverage.COMPLETE or value <= 0:
|
|
203
|
+
continue
|
|
204
|
+
if best is None or value > best[1]:
|
|
205
|
+
best = (day, value)
|
|
206
|
+
if best is not None:
|
|
207
|
+
row = 1 if back[best[0]].series == series.key else 0
|
|
208
|
+
peaks.append(Peak(series.key, best[0], best[1], row))
|
|
209
|
+
return tuple(peaks)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def plan_chart(spec: ChartSpec, style: Style = Style()) -> ChartPlan:
|
|
213
|
+
"""Compute rows, ticks, gap spans, month labels, peaks and legend for ``spec``."""
|
|
214
|
+
back, front = _assign(spec)
|
|
215
|
+
covered = [bar.value for bar in back if spec.coverage[bar.day] is Coverage.COMPLETE]
|
|
216
|
+
ticks = nice_ticks(max(covered, default=0), style.target_ticks)
|
|
217
|
+
gaps = _gap_spans(spec.coverage)
|
|
218
|
+
geometry = Geometry.build(style, len(spec.days))
|
|
219
|
+
legend = [spec.first.label, spec.second.label]
|
|
220
|
+
kinds = {span.coverage for span in gaps}
|
|
221
|
+
if Coverage.NONE in kinds:
|
|
222
|
+
legend.append(NO_DATA_LABEL)
|
|
223
|
+
if Coverage.PARTIAL in kinds:
|
|
224
|
+
legend.append(PARTIAL_DATA_LABEL)
|
|
225
|
+
return ChartPlan(
|
|
226
|
+
back=back,
|
|
227
|
+
front=front,
|
|
228
|
+
ticks=ticks,
|
|
229
|
+
gaps=gaps,
|
|
230
|
+
month_starts=tuple(start for start, _, _ in _months(spec.days)[1:]),
|
|
231
|
+
month_labels=_month_labels(spec.days, style, geometry.pitch),
|
|
232
|
+
peaks=_peaks(spec, back),
|
|
233
|
+
legend=tuple(legend),
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def faces(
|
|
238
|
+
x0: float, y0: float, height: float, width: float, dx: float, dy: float
|
|
239
|
+
) -> tuple[Polygon, Polygon, Polygon]:
|
|
240
|
+
"""Return the right side, top and front polygons of one bar, in drawing order."""
|
|
241
|
+
front = [(x0, y0), (x0 + width, y0), (x0 + width, y0 + height), (x0, y0 + height)]
|
|
242
|
+
top = [
|
|
243
|
+
(x0, y0 + height),
|
|
244
|
+
(x0 + width, y0 + height),
|
|
245
|
+
(x0 + width + dx, y0 + height + dy),
|
|
246
|
+
(x0 + dx, y0 + height + dy),
|
|
247
|
+
]
|
|
248
|
+
side = [
|
|
249
|
+
(x0 + width, y0),
|
|
250
|
+
(x0 + width + dx, y0 + dy),
|
|
251
|
+
(x0 + width + dx, y0 + height + dy),
|
|
252
|
+
(x0 + width, y0 + height),
|
|
253
|
+
]
|
|
254
|
+
return side, top, front
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def _font_px(points: float, style: Style) -> float:
|
|
258
|
+
"""The size in design pixels of a font of ``points``."""
|
|
259
|
+
return points * style.reference_dpi / 72
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def legend_box(legend: Sequence[str], style: Style) -> Box:
|
|
263
|
+
"""Bound the legend's swatches and labels, one entry per item of ``legend``."""
|
|
264
|
+
font_px = _font_px(style.legend_pt, style)
|
|
265
|
+
text_px = max((len(label) for label in legend), default=0) * GLYPH_WIDTH * font_px
|
|
266
|
+
half_line = font_px * LINE_HEIGHT / 2
|
|
267
|
+
bottom = style.legend_y - (len(legend) - 1) * style.legend_step
|
|
268
|
+
return Box(
|
|
269
|
+
style.legend_x,
|
|
270
|
+
min(bottom, bottom + LEGEND_TEXT_Y - half_line),
|
|
271
|
+
style.legend_x + max(SWATCH_WIDTH + SWATCH_DX, LEGEND_TEXT_X + text_px),
|
|
272
|
+
style.legend_y + max(SWATCH_HEIGHT + SWATCH_DY, LEGEND_TEXT_Y + half_line),
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def _bars_top(
|
|
277
|
+
spec: ChartSpec, plan: ChartPlan, geo: Geometry, x0: float, x1: float
|
|
278
|
+
) -> float:
|
|
279
|
+
"""The highest point of any drawn bar reaching into ``x0`` to ``x1``."""
|
|
280
|
+
scale = geo.scale(plan.y_max)
|
|
281
|
+
top = geo.bottom
|
|
282
|
+
for row, bars in ((1, plan.back), (0, plan.front)):
|
|
283
|
+
for bar in bars:
|
|
284
|
+
if bar.value <= 0 or spec.coverage[bar.day] is not Coverage.COMPLETE:
|
|
285
|
+
continue
|
|
286
|
+
left, base = geo.bar_origin(bar.day, row)
|
|
287
|
+
if left < x1 and x0 < left + geo.width + geo.dx:
|
|
288
|
+
top = max(top, base + bar.value * scale + geo.dy)
|
|
289
|
+
return top
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def place_peak_labels(
|
|
293
|
+
spec: ChartSpec, plan: ChartPlan, style: Style, legend: Box
|
|
294
|
+
) -> tuple[PeakLabel, ...]:
|
|
295
|
+
"""Place each series' peak label above its bar, clear of the legend and each other.
|
|
296
|
+
|
|
297
|
+
A label stands above every bar beneath its width, so it is not
|
|
298
|
+
printed over the bar behind its peak or over taller neighbours;
|
|
299
|
+
labels on nearby days stack. A label that would reach into the
|
|
300
|
+
legend moves left of it, raised to clear any bar beneath its new
|
|
301
|
+
position.
|
|
302
|
+
"""
|
|
303
|
+
geo = Geometry.build(style, len(spec.days))
|
|
304
|
+
scale = geo.scale(plan.y_max)
|
|
305
|
+
font_px = _font_px(style.annotation_pt, style)
|
|
306
|
+
height = font_px * LINE_HEIGHT
|
|
307
|
+
labels: list[PeakLabel] = []
|
|
308
|
+
|
|
309
|
+
def stack(x: float, y: float, half: float) -> float:
|
|
310
|
+
for other in labels:
|
|
311
|
+
other_half = (other.box.x1 - other.box.x0) / 2
|
|
312
|
+
if (
|
|
313
|
+
abs(other.x - x) < half + other_half + font_px
|
|
314
|
+
and abs(other.y - y) < font_px * LABEL_STEP
|
|
315
|
+
):
|
|
316
|
+
y = other.y + font_px * LABEL_STEP
|
|
317
|
+
return y
|
|
318
|
+
|
|
319
|
+
for peak in plan.peaks:
|
|
320
|
+
x0, y0 = geo.bar_origin(peak.day, 1)
|
|
321
|
+
x = x0 + (geo.width + geo.dx) / 2
|
|
322
|
+
y = y0 + plan.back[peak.day].value * scale + geo.dy + style.annotation_gap
|
|
323
|
+
day = spec.days[peak.day]
|
|
324
|
+
text = f"{peak.value:,} · {day.day} {MONTHS[day.month - 1]}"
|
|
325
|
+
half = len(text) * GLYPH_WIDTH * font_px / 2
|
|
326
|
+
x = min(max(x, geo.left + half), geo.right + geo.floor_dx - half)
|
|
327
|
+
y = max(
|
|
328
|
+
y, _bars_top(spec, plan, geo, x - half, x + half) + style.annotation_gap
|
|
329
|
+
)
|
|
330
|
+
y = stack(x, y, half)
|
|
331
|
+
if Box(x - half, y, x + half, y + height).overlaps(legend):
|
|
332
|
+
x = max(legend.x0 - font_px / 2 - half, geo.left + half)
|
|
333
|
+
y = max(
|
|
334
|
+
y,
|
|
335
|
+
_bars_top(spec, plan, geo, x - half, x + half) + style.annotation_gap,
|
|
336
|
+
)
|
|
337
|
+
y = stack(x, y, half)
|
|
338
|
+
labels.append(
|
|
339
|
+
PeakLabel(peak.series, text, x, y, Box(x - half, y, x + half, y + height))
|
|
340
|
+
)
|
|
341
|
+
return tuple(labels)
|