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
deckkit/charts/marks.py
ADDED
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable, Hashable, Iterable
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from itertools import groupby
|
|
6
|
+
from typing import Generic, Literal, TypeVar, Union, cast
|
|
7
|
+
|
|
8
|
+
from ..canvas import (
|
|
9
|
+
CanvasElement,
|
|
10
|
+
CanvasText,
|
|
11
|
+
Ellipse,
|
|
12
|
+
LineTo,
|
|
13
|
+
MoveTo,
|
|
14
|
+
Path,
|
|
15
|
+
Rect,
|
|
16
|
+
Stroke,
|
|
17
|
+
)
|
|
18
|
+
from ..drawables import Bounds
|
|
19
|
+
from ..geometry import Point
|
|
20
|
+
from ..theme import AccentColor, ChartDataLabelTextStyle, ColorStyle, PrimaryColor
|
|
21
|
+
from ..units import Length, pt, require_length
|
|
22
|
+
from .content import ChartContent, ChartOverflow, DomainContribution, DomainKind, Value
|
|
23
|
+
from .context import ChartContext
|
|
24
|
+
|
|
25
|
+
Datum = TypeVar("Datum")
|
|
26
|
+
PositionValue = Union[float, Hashable]
|
|
27
|
+
StyleValue = Union[ColorStyle, Callable[[Datum], ColorStyle]]
|
|
28
|
+
RadiusValue = Union[Length, Callable[[Datum], Length]]
|
|
29
|
+
_DEFAULT_LINE_STROKE = Stroke(AccentColor, pt(1.5), line_cap="round", line_join="round")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass(frozen=True)
|
|
33
|
+
class PointMark(ChartContent, Generic[Datum]):
|
|
34
|
+
data: tuple[Datum, ...]
|
|
35
|
+
x: Value[Datum, PositionValue]
|
|
36
|
+
y: Value[Datum, PositionValue]
|
|
37
|
+
color: Value[Datum, Hashable] | None = None
|
|
38
|
+
fill: StyleValue[Datum] = AccentColor
|
|
39
|
+
radius: RadiusValue[Datum] = pt(3)
|
|
40
|
+
key: Callable[[Datum], str | int] | None = None
|
|
41
|
+
series: str | None = None
|
|
42
|
+
x_kind: DomainKind | None = None
|
|
43
|
+
y_kind: DomainKind | None = None
|
|
44
|
+
|
|
45
|
+
def __init__(
|
|
46
|
+
self,
|
|
47
|
+
data: Iterable[Datum],
|
|
48
|
+
*,
|
|
49
|
+
x: Value[Datum, PositionValue],
|
|
50
|
+
y: Value[Datum, PositionValue],
|
|
51
|
+
color: Value[Datum, Hashable] | None = None,
|
|
52
|
+
fill: StyleValue[Datum] = AccentColor,
|
|
53
|
+
radius: RadiusValue[Datum] = pt(3),
|
|
54
|
+
key: Callable[[Datum], str | int] | None = None,
|
|
55
|
+
series: str | None = None,
|
|
56
|
+
x_kind: DomainKind | None = None,
|
|
57
|
+
y_kind: DomainKind | None = None,
|
|
58
|
+
) -> None:
|
|
59
|
+
object.__setattr__(self, "data", tuple(data))
|
|
60
|
+
object.__setattr__(self, "x", x)
|
|
61
|
+
object.__setattr__(self, "y", y)
|
|
62
|
+
object.__setattr__(self, "color", color)
|
|
63
|
+
object.__setattr__(self, "fill", fill)
|
|
64
|
+
object.__setattr__(self, "radius", radius)
|
|
65
|
+
object.__setattr__(self, "key", key)
|
|
66
|
+
object.__setattr__(self, "series", series or y.label)
|
|
67
|
+
object.__setattr__(self, "x_kind", x_kind)
|
|
68
|
+
object.__setattr__(self, "y_kind", y_kind)
|
|
69
|
+
if not callable(x) or not callable(y):
|
|
70
|
+
raise TypeError("PointMark x and y values must be callable")
|
|
71
|
+
if key is not None and not callable(key):
|
|
72
|
+
raise TypeError("PointMark key must be callable")
|
|
73
|
+
if not callable(radius):
|
|
74
|
+
require_length(radius)
|
|
75
|
+
if radius <= pt(0):
|
|
76
|
+
raise ValueError("PointMark radius must be positive")
|
|
77
|
+
|
|
78
|
+
def domain_contributions(self) -> tuple[DomainContribution, ...]:
|
|
79
|
+
x_values = tuple(self.x(datum) for datum in self.data)
|
|
80
|
+
y_values = tuple(self.y(datum) for datum in self.data)
|
|
81
|
+
position = (
|
|
82
|
+
_contribution("x", x_values, self.x_kind),
|
|
83
|
+
_contribution("y", y_values, self.y_kind),
|
|
84
|
+
)
|
|
85
|
+
if self.color is None:
|
|
86
|
+
return position
|
|
87
|
+
return (
|
|
88
|
+
*position,
|
|
89
|
+
DomainContribution.categorical(
|
|
90
|
+
"color",
|
|
91
|
+
(self.color(datum) for datum in self.data),
|
|
92
|
+
label=self.color.label,
|
|
93
|
+
),
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
def overflow(self) -> ChartOverflow:
|
|
97
|
+
radii = tuple(
|
|
98
|
+
self.radius(datum) if callable(self.radius) else self.radius
|
|
99
|
+
for datum in self.data
|
|
100
|
+
)
|
|
101
|
+
radius = max(radii, default=pt(0))
|
|
102
|
+
return ChartOverflow(radius, radius, radius, radius)
|
|
103
|
+
|
|
104
|
+
def lower(self, context: ChartContext) -> Iterable[CanvasElement]:
|
|
105
|
+
result: list[CanvasElement] = []
|
|
106
|
+
for datum in self.data:
|
|
107
|
+
radius = self.radius(datum) if callable(self.radius) else self.radius
|
|
108
|
+
require_length(radius)
|
|
109
|
+
if radius <= pt(0):
|
|
110
|
+
raise ValueError("PointMark radius must be positive")
|
|
111
|
+
fill = (
|
|
112
|
+
context.color(self.color(datum))
|
|
113
|
+
if self.color is not None
|
|
114
|
+
else self.fill(datum)
|
|
115
|
+
if callable(self.fill)
|
|
116
|
+
else self.fill
|
|
117
|
+
)
|
|
118
|
+
center_x = context.center("x", self.x(datum))
|
|
119
|
+
center_y = context.center("y", self.y(datum))
|
|
120
|
+
result.append(
|
|
121
|
+
Ellipse(
|
|
122
|
+
Bounds(
|
|
123
|
+
center_x - radius,
|
|
124
|
+
center_y - radius,
|
|
125
|
+
radius * 2,
|
|
126
|
+
radius * 2,
|
|
127
|
+
),
|
|
128
|
+
fill=fill,
|
|
129
|
+
key=_datum_key(self.series or self.y.label, self.key(datum))
|
|
130
|
+
if self.key
|
|
131
|
+
else None,
|
|
132
|
+
)
|
|
133
|
+
)
|
|
134
|
+
return tuple(result)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
@dataclass(frozen=True)
|
|
138
|
+
class LineMark(ChartContent, Generic[Datum]):
|
|
139
|
+
"""Connect observations in input order, breaking the line at missing y values."""
|
|
140
|
+
|
|
141
|
+
data: tuple[Datum, ...]
|
|
142
|
+
x: Value[Datum, PositionValue]
|
|
143
|
+
y: Value[Datum, float | None]
|
|
144
|
+
stroke: Stroke
|
|
145
|
+
point_radius: Length
|
|
146
|
+
key: Callable[[Datum], str | int] | None
|
|
147
|
+
series: str | None
|
|
148
|
+
x_kind: DomainKind | None
|
|
149
|
+
|
|
150
|
+
def __init__(
|
|
151
|
+
self,
|
|
152
|
+
data: Iterable[Datum],
|
|
153
|
+
*,
|
|
154
|
+
x: Value[Datum, PositionValue],
|
|
155
|
+
y: Value[Datum, float | None],
|
|
156
|
+
stroke: Stroke = _DEFAULT_LINE_STROKE,
|
|
157
|
+
point_radius: Length = pt(2),
|
|
158
|
+
key: Callable[[Datum], str | int] | None = None,
|
|
159
|
+
series: str | None = None,
|
|
160
|
+
x_kind: DomainKind | None = None,
|
|
161
|
+
) -> None:
|
|
162
|
+
if not callable(x) or not callable(y):
|
|
163
|
+
raise TypeError("LineMark x and y values must be callable")
|
|
164
|
+
if not isinstance(stroke, Stroke):
|
|
165
|
+
raise TypeError("LineMark.stroke must be a Stroke")
|
|
166
|
+
require_length(point_radius)
|
|
167
|
+
if point_radius < pt(0):
|
|
168
|
+
raise ValueError("LineMark point radius must be nonnegative")
|
|
169
|
+
if key is not None and not callable(key):
|
|
170
|
+
raise TypeError("LineMark key must be callable")
|
|
171
|
+
object.__setattr__(self, "data", tuple(data))
|
|
172
|
+
object.__setattr__(self, "x", x)
|
|
173
|
+
object.__setattr__(self, "y", y)
|
|
174
|
+
object.__setattr__(self, "stroke", stroke)
|
|
175
|
+
object.__setattr__(self, "point_radius", point_radius)
|
|
176
|
+
object.__setattr__(self, "key", key)
|
|
177
|
+
object.__setattr__(self, "series", series)
|
|
178
|
+
object.__setattr__(self, "x_kind", x_kind)
|
|
179
|
+
|
|
180
|
+
def domain_contributions(self) -> tuple[DomainContribution, ...]:
|
|
181
|
+
return (
|
|
182
|
+
_contribution(
|
|
183
|
+
"x", tuple(self.x(datum) for datum in self.data), self.x_kind
|
|
184
|
+
),
|
|
185
|
+
_contribution(
|
|
186
|
+
"y",
|
|
187
|
+
tuple(
|
|
188
|
+
observation
|
|
189
|
+
for datum in self.data
|
|
190
|
+
if (observation := self.y(datum)) is not None
|
|
191
|
+
),
|
|
192
|
+
"quantitative",
|
|
193
|
+
),
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
def overflow(self) -> ChartOverflow:
|
|
197
|
+
inset = max(self.stroke.width / 2, self.point_radius)
|
|
198
|
+
return ChartOverflow(inset, inset, inset, inset)
|
|
199
|
+
|
|
200
|
+
def lower(self, context: ChartContext) -> Iterable[CanvasElement]:
|
|
201
|
+
inset = self.overflow().left
|
|
202
|
+
plot = context.plot_bounds
|
|
203
|
+
if min(plot.width, plot.height) <= inset * 2:
|
|
204
|
+
return
|
|
205
|
+
observations = ((i, datum, self.y(datum)) for i, datum in enumerate(self.data))
|
|
206
|
+
for present, group in groupby(observations, lambda item: item[2] is not None):
|
|
207
|
+
if not present:
|
|
208
|
+
continue
|
|
209
|
+
run = tuple(group)
|
|
210
|
+
index, first_datum, _ = run[0]
|
|
211
|
+
points = tuple(
|
|
212
|
+
Point(
|
|
213
|
+
context.center("x", self.x(datum)), context.position("y", ordinate)
|
|
214
|
+
)
|
|
215
|
+
for _, datum, ordinate in run
|
|
216
|
+
if ordinate is not None
|
|
217
|
+
)
|
|
218
|
+
cluster_key = None
|
|
219
|
+
if self.key is not None or self.series is not None:
|
|
220
|
+
anchor = self.key(first_datum) if self.key is not None else index
|
|
221
|
+
cluster_key = f"series:{self.series or self.y.label}/run:{anchor}"
|
|
222
|
+
if len(points) > 1:
|
|
223
|
+
yield Path(
|
|
224
|
+
(MoveTo(points[0]), *(LineTo(point) for point in points[1:])),
|
|
225
|
+
stroke=self.stroke,
|
|
226
|
+
key=cluster_key,
|
|
227
|
+
)
|
|
228
|
+
elif self.point_radius > pt(0):
|
|
229
|
+
point = points[0]
|
|
230
|
+
radius = self.point_radius
|
|
231
|
+
yield Ellipse(
|
|
232
|
+
Bounds(point.x - radius, point.y - radius, radius * 2, radius * 2),
|
|
233
|
+
fill=self.stroke.style,
|
|
234
|
+
fill_opacity=self.stroke.opacity,
|
|
235
|
+
key=cluster_key,
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
@dataclass(frozen=True)
|
|
240
|
+
class BarMark(ChartContent, Generic[Datum]):
|
|
241
|
+
data: tuple[Datum, ...]
|
|
242
|
+
x: Value[Datum, PositionValue]
|
|
243
|
+
y: Value[Datum, PositionValue]
|
|
244
|
+
color: Value[Datum, Hashable] | None
|
|
245
|
+
orientation: Literal["vertical", "horizontal"]
|
|
246
|
+
baseline: float
|
|
247
|
+
fill: StyleValue[Datum]
|
|
248
|
+
key: Callable[[Datum], str | int] | None
|
|
249
|
+
series: str
|
|
250
|
+
label: Callable[[Datum], str] | None
|
|
251
|
+
|
|
252
|
+
def __init__(
|
|
253
|
+
self,
|
|
254
|
+
data: Iterable[Datum],
|
|
255
|
+
*,
|
|
256
|
+
x: Value[Datum, PositionValue],
|
|
257
|
+
y: Value[Datum, PositionValue],
|
|
258
|
+
color: Value[Datum, Hashable] | None = None,
|
|
259
|
+
orientation: Literal["vertical", "horizontal"] = "vertical",
|
|
260
|
+
baseline: float = 0,
|
|
261
|
+
fill: StyleValue[Datum] = AccentColor,
|
|
262
|
+
key: Callable[[Datum], str | int] | None = None,
|
|
263
|
+
series: str | None = None,
|
|
264
|
+
label: Callable[[Datum], str] | None = None,
|
|
265
|
+
) -> None:
|
|
266
|
+
if orientation not in ("vertical", "horizontal"):
|
|
267
|
+
raise ValueError("BarMark orientation must be vertical or horizontal")
|
|
268
|
+
object.__setattr__(self, "data", tuple(data))
|
|
269
|
+
object.__setattr__(self, "x", x)
|
|
270
|
+
object.__setattr__(self, "y", y)
|
|
271
|
+
object.__setattr__(self, "color", color)
|
|
272
|
+
object.__setattr__(self, "orientation", orientation)
|
|
273
|
+
object.__setattr__(self, "baseline", float(baseline))
|
|
274
|
+
object.__setattr__(self, "fill", fill)
|
|
275
|
+
object.__setattr__(self, "key", key)
|
|
276
|
+
object.__setattr__(self, "label", label)
|
|
277
|
+
object.__setattr__(
|
|
278
|
+
self,
|
|
279
|
+
"series",
|
|
280
|
+
series or (y.label if orientation == "vertical" else x.label),
|
|
281
|
+
)
|
|
282
|
+
if key is not None and not callable(key):
|
|
283
|
+
raise TypeError("BarMark key must be callable")
|
|
284
|
+
if label is not None and not callable(label):
|
|
285
|
+
raise TypeError("BarMark label must be callable")
|
|
286
|
+
|
|
287
|
+
def domain_contributions(self) -> tuple[DomainContribution, ...]:
|
|
288
|
+
x_values = tuple(self.x(datum) for datum in self.data)
|
|
289
|
+
y_values = tuple(self.y(datum) for datum in self.data)
|
|
290
|
+
if self.orientation == "vertical":
|
|
291
|
+
position = (
|
|
292
|
+
_contribution("x", x_values, "categorical"),
|
|
293
|
+
_quantitative_with_baseline("y", y_values, self.baseline),
|
|
294
|
+
)
|
|
295
|
+
else:
|
|
296
|
+
position = (
|
|
297
|
+
_quantitative_with_baseline("x", x_values, self.baseline),
|
|
298
|
+
_contribution("y", y_values, "categorical"),
|
|
299
|
+
)
|
|
300
|
+
if self.color is None:
|
|
301
|
+
return position
|
|
302
|
+
return (
|
|
303
|
+
*position,
|
|
304
|
+
DomainContribution.categorical(
|
|
305
|
+
"color",
|
|
306
|
+
(self.color(datum) for datum in self.data),
|
|
307
|
+
label=self.color.label,
|
|
308
|
+
),
|
|
309
|
+
)
|
|
310
|
+
|
|
311
|
+
def lower(self, context: ChartContext) -> Iterable[CanvasElement]:
|
|
312
|
+
result: list[CanvasElement] = []
|
|
313
|
+
for datum in self.data:
|
|
314
|
+
fill = (
|
|
315
|
+
context.color(self.color(datum))
|
|
316
|
+
if self.color is not None
|
|
317
|
+
else self.fill(datum)
|
|
318
|
+
if callable(self.fill)
|
|
319
|
+
else self.fill
|
|
320
|
+
)
|
|
321
|
+
if self.orientation == "vertical":
|
|
322
|
+
x = context.position("x", self.x(datum))
|
|
323
|
+
width = context.bandwidth("x")
|
|
324
|
+
value = context.position("y", self.y(datum))
|
|
325
|
+
baseline = context.position("y", self.baseline)
|
|
326
|
+
bounds = Bounds(
|
|
327
|
+
x, min(value, baseline), width, _distance(value, baseline)
|
|
328
|
+
)
|
|
329
|
+
else:
|
|
330
|
+
y = context.position("y", self.y(datum))
|
|
331
|
+
height = context.bandwidth("y")
|
|
332
|
+
value = context.position("x", self.x(datum))
|
|
333
|
+
baseline = context.position("x", self.baseline)
|
|
334
|
+
bounds = Bounds(
|
|
335
|
+
min(value, baseline), y, _distance(value, baseline), height
|
|
336
|
+
)
|
|
337
|
+
result.append(
|
|
338
|
+
Rect(
|
|
339
|
+
bounds,
|
|
340
|
+
fill=fill,
|
|
341
|
+
key=_datum_key(self.series, self.key(datum)) if self.key else None,
|
|
342
|
+
)
|
|
343
|
+
)
|
|
344
|
+
if self.label is not None:
|
|
345
|
+
if self.orientation == "vertical":
|
|
346
|
+
position = Point(x + width / 2, min(value, baseline) - pt(4))
|
|
347
|
+
horizontal_alignment: Literal["left", "center", "right"] = "center"
|
|
348
|
+
vertical_alignment: Literal["top", "center", "bottom"] = "bottom"
|
|
349
|
+
else:
|
|
350
|
+
position = Point(max(value, baseline) + pt(4), y + height / 2)
|
|
351
|
+
horizontal_alignment = "left"
|
|
352
|
+
vertical_alignment = "center"
|
|
353
|
+
result.append(
|
|
354
|
+
CanvasText(
|
|
355
|
+
self.label(datum),
|
|
356
|
+
position,
|
|
357
|
+
typography=ChartDataLabelTextStyle,
|
|
358
|
+
color=PrimaryColor,
|
|
359
|
+
horizontal_alignment=horizontal_alignment,
|
|
360
|
+
vertical_alignment=vertical_alignment,
|
|
361
|
+
)
|
|
362
|
+
)
|
|
363
|
+
return tuple(result)
|
|
364
|
+
|
|
365
|
+
def overflow(self) -> ChartOverflow:
|
|
366
|
+
if self.label is None:
|
|
367
|
+
return ChartOverflow()
|
|
368
|
+
return (
|
|
369
|
+
ChartOverflow(top=pt(18))
|
|
370
|
+
if self.orientation == "vertical"
|
|
371
|
+
else ChartOverflow(right=pt(48))
|
|
372
|
+
)
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
def _contribution(
|
|
376
|
+
channel: Literal["x", "y"],
|
|
377
|
+
values: tuple[PositionValue, ...],
|
|
378
|
+
kind: DomainKind | None,
|
|
379
|
+
) -> DomainContribution:
|
|
380
|
+
resolved_kind = kind or (
|
|
381
|
+
"quantitative"
|
|
382
|
+
if all(isinstance(item, (int, float)) for item in values)
|
|
383
|
+
else "categorical"
|
|
384
|
+
)
|
|
385
|
+
if resolved_kind == "quantitative":
|
|
386
|
+
if not all(isinstance(item, (int, float)) for item in values):
|
|
387
|
+
raise TypeError(f"Quantitative chart {channel} values must be numeric")
|
|
388
|
+
return DomainContribution.quantitative(
|
|
389
|
+
channel, (float(cast(float, item)) for item in values)
|
|
390
|
+
)
|
|
391
|
+
return DomainContribution.categorical(channel, values)
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
def _datum_key(series: str, key: str | int) -> str:
|
|
395
|
+
return f"series:{series}/datum:{key}"
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
def _quantitative_with_baseline(
|
|
399
|
+
channel: Literal["x", "y"], values: tuple[PositionValue, ...], baseline: float
|
|
400
|
+
) -> DomainContribution:
|
|
401
|
+
contribution = _contribution(channel, values, "quantitative")
|
|
402
|
+
return DomainContribution.quantitative(
|
|
403
|
+
channel,
|
|
404
|
+
(*(float(cast(float, value)) for value in contribution.values), baseline),
|
|
405
|
+
include_zero=baseline == 0,
|
|
406
|
+
)
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
def _distance(first: Length, second: Length) -> Length:
|
|
410
|
+
return first - second if first >= second else second - first
|
deckkit/charts/scales.py
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Hashable, Iterable
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from math import ceil, floor, isfinite, log10
|
|
6
|
+
from typing import Generic, TypeVar
|
|
7
|
+
|
|
8
|
+
from ..units import Length, pt, require_length
|
|
9
|
+
|
|
10
|
+
Category = TypeVar("Category", bound=Hashable)
|
|
11
|
+
Output = TypeVar("Output")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _number(value: float) -> float:
|
|
15
|
+
result = float(value)
|
|
16
|
+
if not isfinite(result):
|
|
17
|
+
raise ValueError("Scale values must be finite")
|
|
18
|
+
return result
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass(frozen=True)
|
|
22
|
+
class LinearScaleSpec:
|
|
23
|
+
domain: tuple[float, float] | None = None
|
|
24
|
+
include_zero: bool = False
|
|
25
|
+
nice: bool = True
|
|
26
|
+
tick_count: int = 5
|
|
27
|
+
|
|
28
|
+
def __post_init__(self) -> None:
|
|
29
|
+
if self.tick_count < 2:
|
|
30
|
+
raise ValueError("Linear scale tick count must be at least two")
|
|
31
|
+
|
|
32
|
+
def infer_domain(self, values: Iterable[float]) -> tuple[float, float]:
|
|
33
|
+
inferred = tuple(_number(value) for value in values)
|
|
34
|
+
if self.domain is not None:
|
|
35
|
+
lower, upper = map(_number, self.domain)
|
|
36
|
+
elif inferred:
|
|
37
|
+
lower, upper = min(inferred), max(inferred)
|
|
38
|
+
else:
|
|
39
|
+
lower, upper = 0.0, 1.0
|
|
40
|
+
if lower > upper:
|
|
41
|
+
raise ValueError("Linear scale domain must be ordered")
|
|
42
|
+
if self.include_zero:
|
|
43
|
+
lower, upper = min(lower, 0.0), max(upper, 0.0)
|
|
44
|
+
if lower == upper:
|
|
45
|
+
padding = abs(lower) * 0.05 or 0.5
|
|
46
|
+
lower, upper = lower - padding, upper + padding
|
|
47
|
+
if self.nice and self.domain is None:
|
|
48
|
+
return nice_domain((lower, upper), self.tick_count)
|
|
49
|
+
return lower, upper
|
|
50
|
+
|
|
51
|
+
def resolve(
|
|
52
|
+
self,
|
|
53
|
+
values: Iterable[float],
|
|
54
|
+
output: tuple[Length, Length],
|
|
55
|
+
) -> LinearScale:
|
|
56
|
+
return LinearScale(self.infer_domain(values), output)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@dataclass(frozen=True)
|
|
60
|
+
class LinearScale:
|
|
61
|
+
domain: tuple[float, float]
|
|
62
|
+
output: tuple[Length, Length]
|
|
63
|
+
|
|
64
|
+
def __post_init__(self) -> None:
|
|
65
|
+
require_length(self.output[0])
|
|
66
|
+
require_length(self.output[1])
|
|
67
|
+
|
|
68
|
+
def __call__(self, value: float) -> Length:
|
|
69
|
+
lower, upper = self.domain
|
|
70
|
+
amount = (_number(value) - lower) / (upper - lower)
|
|
71
|
+
return self.output[0] + (self.output[1] - self.output[0]) * amount
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@dataclass(frozen=True)
|
|
75
|
+
class BandScaleSpec(Generic[Category]):
|
|
76
|
+
domain: tuple[Category, ...] | None = None
|
|
77
|
+
padding_inner: float = 0.1
|
|
78
|
+
padding_outer: float = 0.05
|
|
79
|
+
|
|
80
|
+
def __post_init__(self) -> None:
|
|
81
|
+
if not 0 <= self.padding_inner < 1:
|
|
82
|
+
raise ValueError("Band scale inner padding must be between zero and one")
|
|
83
|
+
if self.padding_outer < 0:
|
|
84
|
+
raise ValueError("Band scale outer padding must be non-negative")
|
|
85
|
+
|
|
86
|
+
def infer_domain(self, values: Iterable[Category]) -> tuple[Category, ...]:
|
|
87
|
+
source = self.domain if self.domain is not None else tuple(values)
|
|
88
|
+
return tuple(dict.fromkeys(source))
|
|
89
|
+
|
|
90
|
+
def resolve(
|
|
91
|
+
self, values: Iterable[Category], output: tuple[Length, Length]
|
|
92
|
+
) -> BandScale[Category]:
|
|
93
|
+
return BandScale(
|
|
94
|
+
self.infer_domain(values),
|
|
95
|
+
output,
|
|
96
|
+
self.padding_inner,
|
|
97
|
+
self.padding_outer,
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@dataclass(frozen=True)
|
|
102
|
+
class BandScale(Generic[Category]):
|
|
103
|
+
domain: tuple[Category, ...]
|
|
104
|
+
output: tuple[Length, Length]
|
|
105
|
+
padding_inner: float
|
|
106
|
+
padding_outer: float
|
|
107
|
+
|
|
108
|
+
@property
|
|
109
|
+
def step(self) -> Length:
|
|
110
|
+
return -self._signed_step if self._signed_step < pt(0) else self._signed_step
|
|
111
|
+
|
|
112
|
+
@property
|
|
113
|
+
def _signed_step(self) -> Length:
|
|
114
|
+
if not self.domain:
|
|
115
|
+
return pt(0)
|
|
116
|
+
extent = self.output[1] - self.output[0]
|
|
117
|
+
denominator = len(self.domain) - self.padding_inner + 2 * self.padding_outer
|
|
118
|
+
return extent / denominator
|
|
119
|
+
|
|
120
|
+
@property
|
|
121
|
+
def bandwidth(self) -> Length:
|
|
122
|
+
return self.step * (1 - self.padding_inner)
|
|
123
|
+
|
|
124
|
+
def __call__(self, value: Category) -> Length:
|
|
125
|
+
try:
|
|
126
|
+
index = self.domain.index(value)
|
|
127
|
+
except ValueError as error:
|
|
128
|
+
raise KeyError(value) from error
|
|
129
|
+
offset = self.padding_outer + index
|
|
130
|
+
if self._signed_step < pt(0):
|
|
131
|
+
offset += 1 - self.padding_inner
|
|
132
|
+
return self.output[0] + self._signed_step * offset
|
|
133
|
+
|
|
134
|
+
def center(self, value: Category) -> Length:
|
|
135
|
+
return self(value) + self.bandwidth / 2
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
@dataclass(frozen=True)
|
|
139
|
+
class OrdinalScaleSpec(Generic[Category, Output]):
|
|
140
|
+
output: tuple[Output, ...]
|
|
141
|
+
domain: tuple[Category, ...] | None = None
|
|
142
|
+
|
|
143
|
+
def __post_init__(self) -> None:
|
|
144
|
+
if not self.output:
|
|
145
|
+
raise ValueError("Ordinal scale requires at least one output")
|
|
146
|
+
|
|
147
|
+
def resolve(self, values: Iterable[Category]) -> OrdinalScale[Category, Output]:
|
|
148
|
+
source = self.domain if self.domain is not None else tuple(values)
|
|
149
|
+
return OrdinalScale(tuple(dict.fromkeys(source)), self.output)
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
@dataclass(frozen=True)
|
|
153
|
+
class OrdinalScale(Generic[Category, Output]):
|
|
154
|
+
domain: tuple[Category, ...]
|
|
155
|
+
output: tuple[Output, ...]
|
|
156
|
+
|
|
157
|
+
def __call__(self, value: Category) -> Output:
|
|
158
|
+
try:
|
|
159
|
+
index = self.domain.index(value)
|
|
160
|
+
except ValueError as error:
|
|
161
|
+
raise KeyError(value) from error
|
|
162
|
+
return self.output[index % len(self.output)]
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def nice_domain(
|
|
166
|
+
domain: tuple[float, float], tick_count: int = 5
|
|
167
|
+
) -> tuple[float, float]:
|
|
168
|
+
lower, upper = domain
|
|
169
|
+
step = nice_step(upper - lower, tick_count)
|
|
170
|
+
return (
|
|
171
|
+
_clean_number(floor(lower / step + 1e-10) * step),
|
|
172
|
+
_clean_number(ceil(upper / step - 1e-10) * step),
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def nice_ticks(domain: tuple[float, float], tick_count: int = 5) -> tuple[float, ...]:
|
|
177
|
+
lower, upper = domain
|
|
178
|
+
step = nice_step(upper - lower, tick_count)
|
|
179
|
+
first = ceil(lower / step) * step
|
|
180
|
+
last = floor(upper / step) * step
|
|
181
|
+
count = max(0, round((last - first) / step) + 1)
|
|
182
|
+
return tuple(_clean_number(first + index * step) for index in range(count))
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def nice_step(span: float, tick_count: int) -> float:
|
|
186
|
+
if not isfinite(span) or span <= 0:
|
|
187
|
+
raise ValueError("Tick span must be positive and finite")
|
|
188
|
+
if tick_count < 2:
|
|
189
|
+
raise ValueError("Tick count must be at least two")
|
|
190
|
+
rough = span / (tick_count - 1)
|
|
191
|
+
magnitude = 10.0 ** floor(log10(rough))
|
|
192
|
+
normalized = rough / magnitude
|
|
193
|
+
if normalized >= 50**0.5:
|
|
194
|
+
factor = 10.0
|
|
195
|
+
elif normalized >= 10**0.5:
|
|
196
|
+
factor = 5.0
|
|
197
|
+
elif normalized >= 2**0.5:
|
|
198
|
+
factor = 2.0
|
|
199
|
+
else:
|
|
200
|
+
factor = 1.0
|
|
201
|
+
return factor * magnitude
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _clean_number(value: float) -> float:
|
|
205
|
+
return 0.0 if abs(value) < 1e-12 else float(f"{value:.12g}")
|