|
2 | 2 |
|
3 | 3 | from typing import TYPE_CHECKING, Callable, Dict, Union |
4 | 4 |
|
| 5 | +from polars.dependencies import altair as alt |
| 6 | + |
5 | 7 | if TYPE_CHECKING: |
6 | 8 | import sys |
7 | 9 |
|
8 | | - import altair as alt |
9 | 10 | from altair.typing import ChannelColor as Color |
10 | 11 | from altair.typing import ChannelOrder as Order |
11 | 12 | from altair.typing import ChannelSize as Size |
|
25 | 26 | else: |
26 | 27 | from typing_extensions import Unpack |
27 | 28 |
|
28 | | - Encodings: TypeAlias = Dict[ |
29 | | - str, |
30 | | - Union[X, Y, Color, Order, Size, Tooltip], |
31 | | - ] |
| 29 | + Encoding: TypeAlias = Union[X, Y, Color, Order, Size, Tooltip] |
| 30 | + Encodings: TypeAlias = Dict[str, Encoding] |
| 31 | + |
| 32 | + |
| 33 | +def _maybe_extract_shorthand(encoding: Encoding) -> Encoding: |
| 34 | + if isinstance(encoding, alt.SchemaBase): |
| 35 | + # e.g. for `alt.X('x:Q', axis=alt.Axis(labelAngle=30))`, return `'x:Q'` |
| 36 | + return getattr(encoding, "shorthand", encoding) |
| 37 | + return encoding |
32 | 38 |
|
33 | 39 |
|
34 | 40 | def _add_tooltip(encodings: Encodings, /, **kwargs: Unpack[EncodeKwds]) -> None: |
35 | 41 | if "tooltip" not in kwargs: |
36 | | - encodings["tooltip"] = [*encodings.values(), *kwargs.values()] # type: ignore[assignment] |
| 42 | + encodings["tooltip"] = [ |
| 43 | + *[_maybe_extract_shorthand(x) for x in encodings.values()], |
| 44 | + *[_maybe_extract_shorthand(x) for x in kwargs.values()], # type: ignore[arg-type] |
| 45 | + ] # type: ignore[assignment] |
37 | 46 |
|
38 | 47 |
|
39 | 48 | class DataFramePlot: |
40 | 49 | """DataFrame.plot namespace.""" |
41 | 50 |
|
42 | 51 | def __init__(self, df: DataFrame) -> None: |
43 | | - import altair as alt |
44 | | - |
45 | 52 | self._chart = alt.Chart(df) |
46 | 53 |
|
47 | 54 | def bar( |
|
0 commit comments