deckkit 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.
- deckkit/__init__.py +12 -0
- deckkit/_bundled_fonts.py +54 -0
- deckkit/_diagnostics.py +48 -0
- deckkit/_font_assets/SOURCES.txt +9 -0
- deckkit/_font_assets/SourceCodePro-Bold.ttf +0 -0
- deckkit/_font_assets/SourceCodePro-BoldIt.ttf +0 -0
- deckkit/_font_assets/SourceCodePro-It.ttf +0 -0
- deckkit/_font_assets/SourceCodePro-LICENSE.txt +93 -0
- deckkit/_font_assets/SourceCodePro-Regular.ttf +0 -0
- deckkit/_font_assets/SourceSans3-Bold.ttf +0 -0
- deckkit/_font_assets/SourceSans3-BoldIt.ttf +0 -0
- deckkit/_font_assets/SourceSans3-It.ttf +0 -0
- deckkit/_font_assets/SourceSans3-LICENSE.txt +93 -0
- deckkit/_font_assets/SourceSans3-Regular.ttf +0 -0
- deckkit/_variable_evaluation.py +82 -0
- deckkit/animation.py +211 -0
- deckkit/annotations.py +161 -0
- deckkit/backends/__init__.py +1 -0
- deckkit/backends/_font_embedding.py +41 -0
- deckkit/backends/_pptx_fonts.py +230 -0
- deckkit/backends/_pptx_shadows.py +241 -0
- deckkit/backends/_pptx_tables.py +251 -0
- deckkit/backends/pptx.py +1225 -0
- deckkit/backends/pptx_animation.py +175 -0
- deckkit/backends/svg.py +527 -0
- deckkit/canvas/__init__.py +40 -0
- deckkit/canvas/_geometry.py +49 -0
- deckkit/canvas/clipping.py +336 -0
- deckkit/canvas/context.py +34 -0
- deckkit/canvas/elements.py +306 -0
- deckkit/canvas/path.py +250 -0
- deckkit/canvas/scene.py +74 -0
- deckkit/canvas/style.py +31 -0
- deckkit/charts/__init__.py +59 -0
- deckkit/charts/axes.py +249 -0
- deckkit/charts/chart.py +244 -0
- deckkit/charts/content.py +132 -0
- deckkit/charts/context.py +45 -0
- deckkit/charts/interval.py +166 -0
- deckkit/charts/legends.py +82 -0
- deckkit/charts/marks.py +410 -0
- deckkit/charts/scales.py +205 -0
- deckkit/citations.py +200 -0
- deckkit/compiler.py +410 -0
- deckkit/context.py +72 -0
- deckkit/deck.py +168 -0
- deckkit/drawables/__init__.py +42 -0
- deckkit/drawables/bounds.py +17 -0
- deckkit/drawables/connector.py +22 -0
- deckkit/drawables/drawable.py +38 -0
- deckkit/drawables/ellipse.py +21 -0
- deckkit/drawables/group.py +10 -0
- deckkit/drawables/image.py +14 -0
- deckkit/drawables/line.py +18 -0
- deckkit/drawables/path.py +78 -0
- deckkit/drawables/rectangle.py +24 -0
- deckkit/drawables/rich_text.py +176 -0
- deckkit/drawables/table.py +42 -0
- deckkit/drawables/text.py +29 -0
- deckkit/effects.py +84 -0
- deckkit/expanded_views.py +561 -0
- deckkit/fonts.py +329 -0
- deckkit/geometry.py +48 -0
- deckkit/identity.py +72 -0
- deckkit/layout.py +398 -0
- deckkit/layouts/__init__.py +20 -0
- deckkit/layouts/_content.py +27 -0
- deckkit/layouts/annotations.py +217 -0
- deckkit/layouts/containers.py +202 -0
- deckkit/layouts/grid.py +299 -0
- deckkit/layouts/modifiers.py +200 -0
- deckkit/layouts/primitives.py +213 -0
- deckkit/layouts/table.py +139 -0
- deckkit/measurement.py +420 -0
- deckkit/paint.py +73 -0
- deckkit/py.typed +0 -0
- deckkit/reconcile.py +119 -0
- deckkit/semantic.py +47 -0
- deckkit/theme.py +364 -0
- deckkit/themes/__init__.py +91 -0
- deckkit/themes/clarity.py +98 -0
- deckkit/tokens.py +226 -0
- deckkit/units.py +73 -0
- deckkit/variables.py +212 -0
- deckkit/view_expansion.py +185 -0
- deckkit/views/__init__.py +100 -0
- deckkit/views/_code_source.py +42 -0
- deckkit/views/_drawing.py +52 -0
- deckkit/views/canvas.py +27 -0
- deckkit/views/code.py +481 -0
- deckkit/views/code_evolution.py +284 -0
- deckkit/views/composite_view.py +19 -0
- deckkit/views/divider.py +38 -0
- deckkit/views/ellipse.py +38 -0
- deckkit/views/grid.py +156 -0
- deckkit/views/hstack.py +43 -0
- deckkit/views/image.py +43 -0
- deckkit/views/layout_view.py +30 -0
- deckkit/views/line.py +31 -0
- deckkit/views/modifier_views.py +281 -0
- deckkit/views/path.py +36 -0
- deckkit/views/primitive_view.py +16 -0
- deckkit/views/pygments_highlighter.py +67 -0
- deckkit/views/rectangle.py +61 -0
- deckkit/views/section_number.py +21 -0
- deckkit/views/semantic/__init__.py +51 -0
- deckkit/views/semantic/before_after.py +109 -0
- deckkit/views/semantic/bullet_graph.py +216 -0
- deckkit/views/semantic/card.py +129 -0
- deckkit/views/semantic/code_block.py +41 -0
- deckkit/views/semantic/diff.py +642 -0
- deckkit/views/semantic/estimate_interval.py +137 -0
- deckkit/views/semantic/figure.py +55 -0
- deckkit/views/semantic/item_list.py +171 -0
- deckkit/views/semantic/metric.py +74 -0
- deckkit/views/semantic/pill.py +69 -0
- deckkit/views/semantic/pipeline.py +94 -0
- deckkit/views/semantic/pipeline_stage.py +59 -0
- deckkit/views/semantic/semantic_view.py +76 -0
- deckkit/views/semantic/sparkline.py +164 -0
- deckkit/views/semantic/table.py +439 -0
- deckkit/views/semantic/terminal.py +361 -0
- deckkit/views/semantic/timeline.py +158 -0
- deckkit/views/slide.py +37 -0
- deckkit/views/slide_number.py +20 -0
- deckkit/views/slides/__init__.py +16 -0
- deckkit/views/slides/section_slide.py +89 -0
- deckkit/views/slides/slide_margins.py +32 -0
- deckkit/views/slides/slide_with_margins.py +22 -0
- deckkit/views/slides/title_and_content.py +49 -0
- deckkit/views/slides/title_and_content_slide.py +55 -0
- deckkit/views/spacer.py +45 -0
- deckkit/views/stack.py +36 -0
- deckkit/views/text.py +142 -0
- deckkit/views/view.py +229 -0
- deckkit/views/vstack.py +31 -0
- deckkit/views/zstack.py +27 -0
- deckkit-0.1.0.dist-info/METADATA +123 -0
- deckkit-0.1.0.dist-info/RECORD +144 -0
- deckkit-0.1.0.dist-info/WHEEL +5 -0
- deckkit-0.1.0.dist-info/licenses/LICENSE +21 -0
- deckkit-0.1.0.dist-info/licenses/src/deckkit/_font_assets/SourceCodePro-LICENSE.txt +93 -0
- deckkit-0.1.0.dist-info/licenses/src/deckkit/_font_assets/SourceSans3-LICENSE.txt +93 -0
- deckkit-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""Declarative chart content and layout-independent chart specifications."""
|
|
2
|
+
|
|
3
|
+
from .axes import Axis
|
|
4
|
+
from .chart import Chart, ChartInsets, ColorScaleSpec, PositionScaleSpec
|
|
5
|
+
from .content import (
|
|
6
|
+
ChartContent,
|
|
7
|
+
ChartOverflow,
|
|
8
|
+
CompositeChartContent,
|
|
9
|
+
DomainContribution,
|
|
10
|
+
Value,
|
|
11
|
+
chart_content,
|
|
12
|
+
value,
|
|
13
|
+
)
|
|
14
|
+
from .context import ChartContext, ResolvedPositionScale
|
|
15
|
+
from .interval import IntervalMark
|
|
16
|
+
from .legends import Legend
|
|
17
|
+
from .marks import BarMark, LineMark, PointMark
|
|
18
|
+
from .scales import (
|
|
19
|
+
BandScale,
|
|
20
|
+
BandScaleSpec,
|
|
21
|
+
LinearScale,
|
|
22
|
+
LinearScaleSpec,
|
|
23
|
+
OrdinalScale,
|
|
24
|
+
OrdinalScaleSpec,
|
|
25
|
+
nice_domain,
|
|
26
|
+
nice_step,
|
|
27
|
+
nice_ticks,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
"Axis",
|
|
32
|
+
"BandScale",
|
|
33
|
+
"BandScaleSpec",
|
|
34
|
+
"BarMark",
|
|
35
|
+
"Chart",
|
|
36
|
+
"ChartContent",
|
|
37
|
+
"ChartContext",
|
|
38
|
+
"ChartInsets",
|
|
39
|
+
"ChartOverflow",
|
|
40
|
+
"ColorScaleSpec",
|
|
41
|
+
"CompositeChartContent",
|
|
42
|
+
"DomainContribution",
|
|
43
|
+
"IntervalMark",
|
|
44
|
+
"Legend",
|
|
45
|
+
"LineMark",
|
|
46
|
+
"LinearScale",
|
|
47
|
+
"LinearScaleSpec",
|
|
48
|
+
"OrdinalScale",
|
|
49
|
+
"OrdinalScaleSpec",
|
|
50
|
+
"PointMark",
|
|
51
|
+
"PositionScaleSpec",
|
|
52
|
+
"ResolvedPositionScale",
|
|
53
|
+
"Value",
|
|
54
|
+
"chart_content",
|
|
55
|
+
"nice_domain",
|
|
56
|
+
"nice_step",
|
|
57
|
+
"nice_ticks",
|
|
58
|
+
"value",
|
|
59
|
+
]
|
deckkit/charts/axes.py
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable, Hashable
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from math import ceil, isfinite
|
|
6
|
+
from typing import Literal
|
|
7
|
+
|
|
8
|
+
from ..canvas import CanvasContext, CanvasElement, CanvasText, Line, Stroke
|
|
9
|
+
from ..drawables import Bounds
|
|
10
|
+
from ..geometry import Point
|
|
11
|
+
from ..theme import (
|
|
12
|
+
ChartAxisColor,
|
|
13
|
+
ChartAxisTitleTextStyle,
|
|
14
|
+
ChartGridColor,
|
|
15
|
+
ChartTickTextStyle,
|
|
16
|
+
)
|
|
17
|
+
from ..units import Length, pt
|
|
18
|
+
from .context import ResolvedPositionScale
|
|
19
|
+
from .scales import BandScale, LinearScale, nice_ticks
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _format_tick(value: object) -> str:
|
|
23
|
+
return f"{value:g}" if isinstance(value, (int, float)) else str(value)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True)
|
|
27
|
+
class Axis:
|
|
28
|
+
title: str | None = None
|
|
29
|
+
tick_count: int = 5
|
|
30
|
+
formatter: Callable[[object], str] = _format_tick
|
|
31
|
+
grid: bool = True
|
|
32
|
+
label_policy: Literal["automatic", "all"] = "automatic"
|
|
33
|
+
label_rotation: float = 0.0
|
|
34
|
+
|
|
35
|
+
def __post_init__(self) -> None:
|
|
36
|
+
if self.tick_count < 2:
|
|
37
|
+
raise ValueError("Axis tick count must be at least two")
|
|
38
|
+
if not callable(self.formatter):
|
|
39
|
+
raise TypeError("Axis formatter must be callable")
|
|
40
|
+
if self.label_policy not in ("automatic", "all"):
|
|
41
|
+
raise ValueError("Axis label policy must be automatic or all")
|
|
42
|
+
if not isfinite(self.label_rotation):
|
|
43
|
+
raise ValueError("Axis label rotation must be finite")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
DEFAULT_AXIS = Axis()
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def required_axis_space(
|
|
50
|
+
canvas: CanvasContext,
|
|
51
|
+
x_scale: ResolvedPositionScale,
|
|
52
|
+
y_scale: ResolvedPositionScale,
|
|
53
|
+
x_axis: Axis | None,
|
|
54
|
+
y_axis: Axis | None,
|
|
55
|
+
) -> tuple[Length, Length]:
|
|
56
|
+
bottom = pt(0)
|
|
57
|
+
left = pt(0)
|
|
58
|
+
if x_axis is not None:
|
|
59
|
+
labels = tuple(label for _, label in _tick_items(canvas, x_scale, x_axis, "x"))
|
|
60
|
+
bottom = max(
|
|
61
|
+
(
|
|
62
|
+
_measure_tick(canvas, label, x_axis.label_rotation).height
|
|
63
|
+
for label in labels
|
|
64
|
+
),
|
|
65
|
+
default=pt(0),
|
|
66
|
+
)
|
|
67
|
+
bottom += pt(8)
|
|
68
|
+
if x_axis.title:
|
|
69
|
+
bottom += _measure_title(canvas, x_axis.title).height + pt(4)
|
|
70
|
+
if y_axis is not None:
|
|
71
|
+
labels = tuple(label for _, label in _tick_items(canvas, y_scale, y_axis, "y"))
|
|
72
|
+
left = max(
|
|
73
|
+
(
|
|
74
|
+
_measure_tick(canvas, label, y_axis.label_rotation).width
|
|
75
|
+
for label in labels
|
|
76
|
+
),
|
|
77
|
+
default=pt(0),
|
|
78
|
+
)
|
|
79
|
+
left += pt(8)
|
|
80
|
+
if y_axis.title:
|
|
81
|
+
left += _measure_title(canvas, y_axis.title).height + pt(4)
|
|
82
|
+
return bottom, left
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def lower_axes(
|
|
86
|
+
canvas: CanvasContext,
|
|
87
|
+
plot: Bounds,
|
|
88
|
+
x_scale: ResolvedPositionScale,
|
|
89
|
+
y_scale: ResolvedPositionScale,
|
|
90
|
+
x_axis: Axis | None,
|
|
91
|
+
y_axis: Axis | None,
|
|
92
|
+
) -> tuple[CanvasElement, ...]:
|
|
93
|
+
result: list[CanvasElement] = []
|
|
94
|
+
axis_stroke = Stroke(ChartAxisColor, pt(1))
|
|
95
|
+
grid_stroke = Stroke(ChartGridColor, pt(0.5))
|
|
96
|
+
if x_axis is not None:
|
|
97
|
+
baseline = plot.y + plot.height
|
|
98
|
+
result.append(
|
|
99
|
+
Line(
|
|
100
|
+
Point(plot.x, baseline),
|
|
101
|
+
Point(plot.x + plot.width, baseline),
|
|
102
|
+
axis_stroke,
|
|
103
|
+
)
|
|
104
|
+
)
|
|
105
|
+
for value, label in _tick_items(canvas, x_scale, x_axis, "x"):
|
|
106
|
+
x = _position(x_scale, value)
|
|
107
|
+
if x_axis.grid and plot.x < x < plot.x + plot.width:
|
|
108
|
+
result.append(Line(Point(x, plot.y), Point(x, baseline), grid_stroke))
|
|
109
|
+
result.append(
|
|
110
|
+
Line(Point(x, baseline), Point(x, baseline + pt(4)), axis_stroke)
|
|
111
|
+
)
|
|
112
|
+
result.append(
|
|
113
|
+
CanvasText(
|
|
114
|
+
label,
|
|
115
|
+
Point(x, baseline + pt(6)),
|
|
116
|
+
typography=ChartTickTextStyle,
|
|
117
|
+
color=ChartAxisColor,
|
|
118
|
+
horizontal_alignment="center",
|
|
119
|
+
rotation=x_axis.label_rotation,
|
|
120
|
+
)
|
|
121
|
+
)
|
|
122
|
+
if x_axis.title:
|
|
123
|
+
tick_height = max(
|
|
124
|
+
(
|
|
125
|
+
_measure_tick(canvas, label).height
|
|
126
|
+
for _, label in _tick_items(canvas, x_scale, x_axis, "x")
|
|
127
|
+
),
|
|
128
|
+
default=pt(0),
|
|
129
|
+
)
|
|
130
|
+
result.append(
|
|
131
|
+
CanvasText(
|
|
132
|
+
x_axis.title,
|
|
133
|
+
Point(
|
|
134
|
+
plot.x + plot.width / 2,
|
|
135
|
+
baseline + pt(8) + tick_height + pt(4),
|
|
136
|
+
),
|
|
137
|
+
typography=ChartAxisTitleTextStyle,
|
|
138
|
+
color=ChartAxisColor,
|
|
139
|
+
horizontal_alignment="center",
|
|
140
|
+
)
|
|
141
|
+
)
|
|
142
|
+
if y_axis is not None:
|
|
143
|
+
result.append(
|
|
144
|
+
Line(
|
|
145
|
+
Point(plot.x, plot.y),
|
|
146
|
+
Point(plot.x, plot.y + plot.height),
|
|
147
|
+
axis_stroke,
|
|
148
|
+
)
|
|
149
|
+
)
|
|
150
|
+
for value, label in _tick_items(canvas, y_scale, y_axis, "y"):
|
|
151
|
+
y = _position(y_scale, value)
|
|
152
|
+
if y_axis.grid and plot.y < y < plot.y + plot.height:
|
|
153
|
+
result.append(
|
|
154
|
+
Line(Point(plot.x, y), Point(plot.x + plot.width, y), grid_stroke)
|
|
155
|
+
)
|
|
156
|
+
result.append(Line(Point(plot.x - pt(4), y), Point(plot.x, y), axis_stroke))
|
|
157
|
+
result.append(
|
|
158
|
+
CanvasText(
|
|
159
|
+
label,
|
|
160
|
+
Point(plot.x - pt(6), y),
|
|
161
|
+
typography=ChartTickTextStyle,
|
|
162
|
+
color=ChartAxisColor,
|
|
163
|
+
horizontal_alignment="right",
|
|
164
|
+
vertical_alignment="center",
|
|
165
|
+
rotation=y_axis.label_rotation,
|
|
166
|
+
)
|
|
167
|
+
)
|
|
168
|
+
if y_axis.title:
|
|
169
|
+
label_width = max(
|
|
170
|
+
(
|
|
171
|
+
_measure_tick(canvas, label).width
|
|
172
|
+
for _, label in _tick_items(canvas, y_scale, y_axis, "y")
|
|
173
|
+
),
|
|
174
|
+
default=pt(0),
|
|
175
|
+
)
|
|
176
|
+
result.append(
|
|
177
|
+
CanvasText(
|
|
178
|
+
y_axis.title,
|
|
179
|
+
Point(
|
|
180
|
+
plot.x
|
|
181
|
+
- label_width
|
|
182
|
+
- pt(12)
|
|
183
|
+
- _measure_title(canvas, y_axis.title).height / 2,
|
|
184
|
+
plot.y + plot.height / 2,
|
|
185
|
+
),
|
|
186
|
+
typography=ChartAxisTitleTextStyle,
|
|
187
|
+
color=ChartAxisColor,
|
|
188
|
+
horizontal_alignment="center",
|
|
189
|
+
vertical_alignment="center",
|
|
190
|
+
rotation=-90,
|
|
191
|
+
)
|
|
192
|
+
)
|
|
193
|
+
return tuple(result)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _ticks(scale: ResolvedPositionScale, axis: Axis) -> tuple[float | Hashable, ...]:
|
|
197
|
+
if isinstance(scale, BandScale):
|
|
198
|
+
return scale.domain
|
|
199
|
+
return nice_ticks(scale.domain, axis.tick_count)
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _tick_labels(scale: ResolvedPositionScale, axis: Axis) -> tuple[str, ...]:
|
|
203
|
+
return tuple(axis.formatter(value) for value in _ticks(scale, axis))
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _tick_items(
|
|
207
|
+
canvas: CanvasContext,
|
|
208
|
+
scale: ResolvedPositionScale,
|
|
209
|
+
axis: Axis,
|
|
210
|
+
channel: Literal["x", "y"],
|
|
211
|
+
) -> tuple[tuple[float | Hashable, str], ...]:
|
|
212
|
+
items = tuple(zip(_ticks(scale, axis), _tick_labels(scale, axis)))
|
|
213
|
+
if not isinstance(scale, BandScale) or axis.label_policy == "all" or not items:
|
|
214
|
+
return items
|
|
215
|
+
extent = max(
|
|
216
|
+
(
|
|
217
|
+
_measure_tick(canvas, label, axis.label_rotation).width
|
|
218
|
+
if channel == "x"
|
|
219
|
+
else _measure_tick(canvas, label, axis.label_rotation).height
|
|
220
|
+
for _, label in items
|
|
221
|
+
),
|
|
222
|
+
default=pt(0),
|
|
223
|
+
)
|
|
224
|
+
if scale.step <= pt(0):
|
|
225
|
+
return items
|
|
226
|
+
stride = max(1, ceil((extent + pt(6)).points / scale.step.points))
|
|
227
|
+
return items[::stride]
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def _position(scale: ResolvedPositionScale, value: float | Hashable) -> Length:
|
|
231
|
+
if isinstance(scale, LinearScale):
|
|
232
|
+
assert isinstance(value, (int, float))
|
|
233
|
+
return scale(float(value))
|
|
234
|
+
return scale.center(value)
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def _measure_tick(canvas: CanvasContext, text: str, rotation: float = 0.0) -> Bounds:
|
|
238
|
+
return CanvasText(
|
|
239
|
+
text,
|
|
240
|
+
Point(pt(0), pt(0)),
|
|
241
|
+
typography=ChartTickTextStyle,
|
|
242
|
+
rotation=rotation,
|
|
243
|
+
).measured_anchor_bounds(canvas)
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def _measure_title(canvas: CanvasContext, text: str) -> Bounds:
|
|
247
|
+
return CanvasText(
|
|
248
|
+
text, Point(pt(0), pt(0)), typography=ChartAxisTitleTextStyle
|
|
249
|
+
).measured_anchor_bounds(canvas)
|
deckkit/charts/chart.py
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Hashable
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from typing import Union, cast
|
|
6
|
+
|
|
7
|
+
from ..canvas import CanvasContext, CanvasElement, CanvasScene, CanvasSize
|
|
8
|
+
from ..canvas._geometry import translate_bounds
|
|
9
|
+
from ..canvas.clipping import clip_drawable
|
|
10
|
+
from ..context import Context
|
|
11
|
+
from ..drawables import Bounds, Drawable, DrawableMetadata
|
|
12
|
+
from ..geometry import Point
|
|
13
|
+
from ..theme import AccentColor, Color, ColorStyle, MutedColor, PrimaryColor
|
|
14
|
+
from ..units import Length, pt, require_length
|
|
15
|
+
from ..views.canvas import Canvas
|
|
16
|
+
from ..views.composite_view import CompositeView
|
|
17
|
+
from ..views.view import View
|
|
18
|
+
from .axes import DEFAULT_AXIS, Axis, lower_axes, required_axis_space
|
|
19
|
+
from .content import ChartContent, ChartOverflow, DomainContribution, chart_content
|
|
20
|
+
from .context import ChartContext, ResolvedPositionScale
|
|
21
|
+
from .legends import DEFAULT_LEGEND, Legend, lower_legend, required_legend_width
|
|
22
|
+
from .scales import BandScaleSpec, LinearScaleSpec, OrdinalScale, OrdinalScaleSpec
|
|
23
|
+
|
|
24
|
+
PositionScaleSpec = Union[LinearScaleSpec, BandScaleSpec[Hashable]]
|
|
25
|
+
ColorScaleSpec = OrdinalScaleSpec[Hashable, ColorStyle]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(frozen=True)
|
|
29
|
+
class ChartInsets:
|
|
30
|
+
top: Length = pt(12)
|
|
31
|
+
right: Length = pt(12)
|
|
32
|
+
bottom: Length = pt(12)
|
|
33
|
+
left: Length = pt(12)
|
|
34
|
+
|
|
35
|
+
def __post_init__(self) -> None:
|
|
36
|
+
for value in (self.top, self.right, self.bottom, self.left):
|
|
37
|
+
require_length(value)
|
|
38
|
+
if value < pt(0):
|
|
39
|
+
raise ValueError("Chart insets must be non-negative")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@dataclass(frozen=True, init=False)
|
|
43
|
+
class Chart(CompositeView):
|
|
44
|
+
content: ChartContent
|
|
45
|
+
x_scale: PositionScaleSpec | None
|
|
46
|
+
y_scale: PositionScaleSpec | None
|
|
47
|
+
color_scale: ColorScaleSpec | None
|
|
48
|
+
plot_insets: ChartInsets
|
|
49
|
+
x_axis: Axis | None
|
|
50
|
+
y_axis: Axis | None
|
|
51
|
+
legend: Legend | None
|
|
52
|
+
|
|
53
|
+
def __init__(
|
|
54
|
+
self,
|
|
55
|
+
*content: ChartContent,
|
|
56
|
+
x_scale: PositionScaleSpec | None = None,
|
|
57
|
+
y_scale: PositionScaleSpec | None = None,
|
|
58
|
+
color_scale: ColorScaleSpec | None = None,
|
|
59
|
+
plot_insets: ChartInsets | None = None,
|
|
60
|
+
x_axis: Axis | None = DEFAULT_AXIS,
|
|
61
|
+
y_axis: Axis | None = DEFAULT_AXIS,
|
|
62
|
+
legend: Legend | None = DEFAULT_LEGEND,
|
|
63
|
+
) -> None:
|
|
64
|
+
if not content:
|
|
65
|
+
raise ValueError("Chart requires at least one ChartContent value")
|
|
66
|
+
resolved_insets = ChartInsets() if plot_insets is None else plot_insets
|
|
67
|
+
if not isinstance(resolved_insets, ChartInsets):
|
|
68
|
+
raise TypeError("Chart.plot_insets must be ChartInsets")
|
|
69
|
+
object.__setattr__(self, "content", chart_content(content))
|
|
70
|
+
object.__setattr__(self, "x_scale", x_scale)
|
|
71
|
+
object.__setattr__(self, "y_scale", y_scale)
|
|
72
|
+
object.__setattr__(self, "color_scale", color_scale)
|
|
73
|
+
object.__setattr__(self, "plot_insets", resolved_insets)
|
|
74
|
+
object.__setattr__(self, "x_axis", x_axis)
|
|
75
|
+
object.__setattr__(self, "y_axis", y_axis)
|
|
76
|
+
object.__setattr__(self, "legend", legend)
|
|
77
|
+
|
|
78
|
+
def body(self, context: Context) -> View:
|
|
79
|
+
return Canvas(self._render)
|
|
80
|
+
|
|
81
|
+
def _render(self, canvas: CanvasContext, size: CanvasSize) -> CanvasScene:
|
|
82
|
+
contributions = tuple(self.content.domain_contributions())
|
|
83
|
+
overflow = self.content.overflow()
|
|
84
|
+
resolved_color = _resolve_color_scale(contributions, self.color_scale)
|
|
85
|
+
color_title = next(
|
|
86
|
+
(
|
|
87
|
+
contribution.label
|
|
88
|
+
for contribution in contributions
|
|
89
|
+
if contribution.channel == "color" and contribution.label
|
|
90
|
+
),
|
|
91
|
+
None,
|
|
92
|
+
)
|
|
93
|
+
legend_width = required_legend_width(
|
|
94
|
+
canvas, resolved_color, self.legend, color_title
|
|
95
|
+
)
|
|
96
|
+
insets_with_legend = ChartInsets(
|
|
97
|
+
self.plot_insets.top,
|
|
98
|
+
self.plot_insets.right + legend_width,
|
|
99
|
+
self.plot_insets.bottom,
|
|
100
|
+
self.plot_insets.left,
|
|
101
|
+
)
|
|
102
|
+
provisional_plot = _plot_bounds(size, insets_with_legend)
|
|
103
|
+
provisional_x = _resolve_scale(
|
|
104
|
+
"x", contributions, self.x_scale, provisional_plot, overflow
|
|
105
|
+
)
|
|
106
|
+
provisional_y = _resolve_scale(
|
|
107
|
+
"y", contributions, self.y_scale, provisional_plot, overflow
|
|
108
|
+
)
|
|
109
|
+
bottom, left = required_axis_space(
|
|
110
|
+
canvas, provisional_x, provisional_y, self.x_axis, self.y_axis
|
|
111
|
+
)
|
|
112
|
+
plot_bounds = _plot_bounds(
|
|
113
|
+
size,
|
|
114
|
+
ChartInsets(
|
|
115
|
+
insets_with_legend.top,
|
|
116
|
+
insets_with_legend.right,
|
|
117
|
+
insets_with_legend.bottom + bottom,
|
|
118
|
+
insets_with_legend.left + left,
|
|
119
|
+
),
|
|
120
|
+
)
|
|
121
|
+
chart_context = ChartContext(
|
|
122
|
+
canvas,
|
|
123
|
+
plot_bounds,
|
|
124
|
+
_resolve_scale("x", contributions, self.x_scale, plot_bounds, overflow),
|
|
125
|
+
_resolve_scale("y", contributions, self.y_scale, plot_bounds, overflow),
|
|
126
|
+
resolved_color,
|
|
127
|
+
)
|
|
128
|
+
elements = tuple(self.content.lower(chart_context))
|
|
129
|
+
if not all(isinstance(element, CanvasElement) for element in elements):
|
|
130
|
+
raise TypeError("Chart content must lower to CanvasElement values")
|
|
131
|
+
clipped = tuple(
|
|
132
|
+
_ClippedChartElement(element, plot_bounds) for element in elements
|
|
133
|
+
)
|
|
134
|
+
axes = lower_axes(
|
|
135
|
+
canvas,
|
|
136
|
+
plot_bounds,
|
|
137
|
+
chart_context.x_scale,
|
|
138
|
+
chart_context.y_scale,
|
|
139
|
+
self.x_axis,
|
|
140
|
+
self.y_axis,
|
|
141
|
+
)
|
|
142
|
+
legend_elements = lower_legend(
|
|
143
|
+
canvas, plot_bounds, resolved_color, self.legend, color_title
|
|
144
|
+
)
|
|
145
|
+
return CanvasScene(*axes, *clipped, *legend_elements)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
@dataclass(frozen=True)
|
|
149
|
+
class _ClippedChartElement(CanvasElement):
|
|
150
|
+
element: CanvasElement
|
|
151
|
+
clip: Bounds
|
|
152
|
+
key: str | None = field(init=False)
|
|
153
|
+
|
|
154
|
+
def __post_init__(self) -> None:
|
|
155
|
+
object.__setattr__(self, "key", self.element.key)
|
|
156
|
+
|
|
157
|
+
@property
|
|
158
|
+
def anchor_bounds(self) -> Bounds:
|
|
159
|
+
return self.element.anchor_bounds
|
|
160
|
+
|
|
161
|
+
def make_drawable(
|
|
162
|
+
self,
|
|
163
|
+
identity: str,
|
|
164
|
+
origin: Point,
|
|
165
|
+
context: CanvasContext,
|
|
166
|
+
metadata: DrawableMetadata,
|
|
167
|
+
) -> Drawable:
|
|
168
|
+
drawable = self.element.make_drawable(identity, origin, context, metadata)
|
|
169
|
+
return clip_drawable(drawable, translate_bounds(self.clip, origin))
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def _plot_bounds(size: CanvasSize, insets: ChartInsets) -> Bounds:
|
|
173
|
+
return Bounds(
|
|
174
|
+
insets.left,
|
|
175
|
+
insets.top,
|
|
176
|
+
max(pt(0), size.width - insets.left - insets.right),
|
|
177
|
+
max(pt(0), size.height - insets.top - insets.bottom),
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def _resolve_scale(
|
|
182
|
+
channel: str,
|
|
183
|
+
contributions: tuple[DomainContribution, ...],
|
|
184
|
+
specification: PositionScaleSpec | None,
|
|
185
|
+
plot: Bounds,
|
|
186
|
+
overflow: ChartOverflow,
|
|
187
|
+
) -> ResolvedPositionScale:
|
|
188
|
+
selected = tuple(item for item in contributions if item.channel == channel)
|
|
189
|
+
kinds = {item.kind for item in selected}
|
|
190
|
+
if len(kinds) > 1:
|
|
191
|
+
raise ValueError(
|
|
192
|
+
f"Chart {channel}-axis mixes quantitative and categorical data"
|
|
193
|
+
)
|
|
194
|
+
kind = next(iter(kinds), "quantitative")
|
|
195
|
+
if specification is not None:
|
|
196
|
+
expected = (
|
|
197
|
+
"quantitative"
|
|
198
|
+
if isinstance(specification, LinearScaleSpec)
|
|
199
|
+
else "categorical"
|
|
200
|
+
)
|
|
201
|
+
if selected and kind != expected:
|
|
202
|
+
raise TypeError(f"Chart {channel}-scale does not match its data")
|
|
203
|
+
scale_spec = specification
|
|
204
|
+
elif kind == "categorical":
|
|
205
|
+
scale_spec = BandScaleSpec[Hashable]()
|
|
206
|
+
else:
|
|
207
|
+
scale_spec = LinearScaleSpec(
|
|
208
|
+
include_zero=any(item.include_zero for item in selected)
|
|
209
|
+
)
|
|
210
|
+
start = (
|
|
211
|
+
plot.x + overflow.left
|
|
212
|
+
if channel == "x"
|
|
213
|
+
else plot.y + plot.height - overflow.bottom
|
|
214
|
+
)
|
|
215
|
+
end = (
|
|
216
|
+
plot.x + plot.width - overflow.right
|
|
217
|
+
if channel == "x"
|
|
218
|
+
else plot.y + overflow.top
|
|
219
|
+
)
|
|
220
|
+
values = tuple(value for item in selected for value in item.values)
|
|
221
|
+
if isinstance(scale_spec, LinearScaleSpec):
|
|
222
|
+
numeric = tuple(float(cast(float, value)) for value in values)
|
|
223
|
+
return scale_spec.resolve(numeric, (start, end))
|
|
224
|
+
return scale_spec.resolve(values, (start, end))
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def _resolve_color_scale(
|
|
228
|
+
contributions: tuple[DomainContribution, ...],
|
|
229
|
+
specification: ColorScaleSpec | None,
|
|
230
|
+
) -> OrdinalScale[Hashable, ColorStyle] | None:
|
|
231
|
+
selected = tuple(item for item in contributions if item.channel == "color")
|
|
232
|
+
if not selected:
|
|
233
|
+
return None
|
|
234
|
+
values = tuple(value for item in selected for value in item.values)
|
|
235
|
+
scale_spec = specification or OrdinalScaleSpec(
|
|
236
|
+
(
|
|
237
|
+
AccentColor,
|
|
238
|
+
PrimaryColor,
|
|
239
|
+
MutedColor,
|
|
240
|
+
Color("#D97706"),
|
|
241
|
+
Color("#7C3AED"),
|
|
242
|
+
)
|
|
243
|
+
)
|
|
244
|
+
return scale_spec.resolve(values)
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from collections.abc import Callable, Hashable, Iterable, Sequence
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from typing import Generic, Literal, TypeVar
|
|
7
|
+
|
|
8
|
+
from ..canvas import CanvasElement
|
|
9
|
+
from ..units import Length, pt, require_length
|
|
10
|
+
from .context import ChartContext
|
|
11
|
+
|
|
12
|
+
Datum = TypeVar("Datum")
|
|
13
|
+
ValueType_co = TypeVar("ValueType_co", covariant=True)
|
|
14
|
+
Channel = Literal["x", "y", "color"]
|
|
15
|
+
DomainKind = Literal["quantitative", "categorical"]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True)
|
|
19
|
+
class Value(Generic[Datum, ValueType_co]):
|
|
20
|
+
label: str
|
|
21
|
+
accessor: Callable[[Datum], ValueType_co]
|
|
22
|
+
|
|
23
|
+
def __post_init__(self) -> None:
|
|
24
|
+
if not self.label:
|
|
25
|
+
raise ValueError("Chart value labels must not be empty")
|
|
26
|
+
if not callable(self.accessor):
|
|
27
|
+
raise TypeError("Chart value accessors must be callable")
|
|
28
|
+
|
|
29
|
+
def __call__(self, datum: Datum) -> ValueType_co:
|
|
30
|
+
return self.accessor(datum)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def value(
|
|
34
|
+
label: str, accessor: Callable[[Datum], ValueType_co]
|
|
35
|
+
) -> Value[Datum, ValueType_co]:
|
|
36
|
+
return Value(label, accessor)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass(frozen=True)
|
|
40
|
+
class DomainContribution:
|
|
41
|
+
channel: Channel
|
|
42
|
+
kind: DomainKind
|
|
43
|
+
values: tuple[float | Hashable, ...]
|
|
44
|
+
include_zero: bool = False
|
|
45
|
+
label: str | None = None
|
|
46
|
+
|
|
47
|
+
@classmethod
|
|
48
|
+
def quantitative(
|
|
49
|
+
cls,
|
|
50
|
+
channel: Channel,
|
|
51
|
+
values: Iterable[float],
|
|
52
|
+
*,
|
|
53
|
+
include_zero: bool = False,
|
|
54
|
+
label: str | None = None,
|
|
55
|
+
) -> DomainContribution:
|
|
56
|
+
return cls(channel, "quantitative", tuple(values), include_zero, label)
|
|
57
|
+
|
|
58
|
+
@classmethod
|
|
59
|
+
def categorical(
|
|
60
|
+
cls, channel: Channel, values: Iterable[Hashable], *, label: str | None = None
|
|
61
|
+
) -> DomainContribution:
|
|
62
|
+
return cls(channel, "categorical", tuple(values), label=label)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@dataclass(frozen=True)
|
|
66
|
+
class ChartOverflow:
|
|
67
|
+
top: Length = pt(0)
|
|
68
|
+
right: Length = pt(0)
|
|
69
|
+
bottom: Length = pt(0)
|
|
70
|
+
left: Length = pt(0)
|
|
71
|
+
|
|
72
|
+
def __post_init__(self) -> None:
|
|
73
|
+
for value in (self.top, self.right, self.bottom, self.left):
|
|
74
|
+
require_length(value)
|
|
75
|
+
if value < pt(0):
|
|
76
|
+
raise ValueError("Chart overflow must be non-negative")
|
|
77
|
+
|
|
78
|
+
@classmethod
|
|
79
|
+
def maximum(cls, values: Iterable[ChartOverflow]) -> ChartOverflow:
|
|
80
|
+
items = tuple(values)
|
|
81
|
+
if not items:
|
|
82
|
+
return cls()
|
|
83
|
+
return cls(
|
|
84
|
+
max(item.top for item in items),
|
|
85
|
+
max(item.right for item in items),
|
|
86
|
+
max(item.bottom for item in items),
|
|
87
|
+
max(item.left for item in items),
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class ChartContent(ABC):
|
|
92
|
+
"""Declarative chart content that contributes domains and lowers to Canvas."""
|
|
93
|
+
|
|
94
|
+
@abstractmethod
|
|
95
|
+
def domain_contributions(self) -> Iterable[DomainContribution]:
|
|
96
|
+
raise NotImplementedError
|
|
97
|
+
|
|
98
|
+
def overflow(self) -> ChartOverflow:
|
|
99
|
+
return ChartOverflow()
|
|
100
|
+
|
|
101
|
+
@abstractmethod
|
|
102
|
+
def lower(self, context: ChartContext) -> Iterable[CanvasElement]:
|
|
103
|
+
raise NotImplementedError
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
@dataclass(frozen=True, init=False)
|
|
107
|
+
class CompositeChartContent(ChartContent):
|
|
108
|
+
children: tuple[ChartContent, ...]
|
|
109
|
+
|
|
110
|
+
def __init__(self, *children: ChartContent) -> None:
|
|
111
|
+
if not all(isinstance(child, ChartContent) for child in children):
|
|
112
|
+
raise TypeError("Composite chart children must be ChartContent values")
|
|
113
|
+
object.__setattr__(self, "children", tuple(children))
|
|
114
|
+
|
|
115
|
+
def domain_contributions(self) -> Iterable[DomainContribution]:
|
|
116
|
+
return (
|
|
117
|
+
contribution
|
|
118
|
+
for child in self.children
|
|
119
|
+
for contribution in child.domain_contributions()
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
def lower(self, context: ChartContext) -> Iterable[CanvasElement]:
|
|
123
|
+
return (element for child in self.children for element in child.lower(context))
|
|
124
|
+
|
|
125
|
+
def overflow(self) -> ChartOverflow:
|
|
126
|
+
return ChartOverflow.maximum(child.overflow() for child in self.children)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def chart_content(children: Sequence[ChartContent]) -> ChartContent:
|
|
130
|
+
if len(children) == 1:
|
|
131
|
+
return children[0]
|
|
132
|
+
return CompositeChartContent(*children)
|