-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(python): Add tooltip by default to charts #18625
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,15 +6,12 @@ | |
import sys | ||
|
||
import altair as alt | ||
from altair.typing import ( | ||
ChannelColor, | ||
ChannelOrder, | ||
ChannelSize, | ||
ChannelTooltip, | ||
ChannelX, | ||
ChannelY, | ||
EncodeKwds, | ||
) | ||
from altair.typing import ChannelColor as Color | ||
from altair.typing import ChannelOrder as Order | ||
from altair.typing import ChannelSize as Size | ||
from altair.typing import ChannelX as X | ||
from altair.typing import ChannelY as Y | ||
from altair.typing import EncodeKwds | ||
|
||
from polars import DataFrame | ||
|
||
|
@@ -29,12 +26,15 @@ | |
|
||
Encodings: TypeAlias = Dict[ | ||
str, | ||
Union[ | ||
ChannelX, ChannelY, ChannelColor, ChannelOrder, ChannelSize, ChannelTooltip | ||
], | ||
Union[X, Y, Color, Order, Size], | ||
] | ||
|
||
|
||
def _add_tooltip(chart: alt.Chart) -> alt.Chart: | ||
chart.mark = {"type": chart.mark, "tooltip": True} | ||
return chart | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are marks guaranteed to be strings here? It seems like they are by default in polars, but I'm flagging just in case you want to do something like this to check that the mark is not a dict (which you might already have seen). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @joelostblom I'm pretty sure @MarcoGorelli has been peeking at that PR 😉 Maybe I'm being overly cautious here, but I'm not sure we've tested this thoroughly enough for it to be included in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks both! I've reworked this to create There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good to me too |
||
|
||
|
||
class DataFramePlot: | ||
"""DataFrame.plot namespace.""" | ||
|
||
|
@@ -45,10 +45,9 @@ def __init__(self, df: DataFrame) -> None: | |
|
||
def bar( | ||
self, | ||
x: ChannelX | None = None, | ||
y: ChannelY | None = None, | ||
color: ChannelColor | None = None, | ||
tooltip: ChannelTooltip | None = None, | ||
x: X | None = None, | ||
y: Y | None = None, | ||
color: Color | None = None, | ||
/, | ||
**kwargs: Unpack[EncodeKwds], | ||
) -> alt.Chart: | ||
|
@@ -77,8 +76,6 @@ def bar( | |
Column with y-coordinates of bars. | ||
color | ||
Column to color bars by. | ||
tooltip | ||
Columns to show values of when hovering over bars with pointer. | ||
**kwargs | ||
Additional keyword arguments passed to Altair. | ||
Comment on lines
-80
to
81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note that this is a backwards-compatible change, as anyone passing |
||
|
||
|
@@ -102,17 +99,16 @@ def bar( | |
encodings["y"] = y | ||
if color is not None: | ||
encodings["color"] = color | ||
if tooltip is not None: | ||
encodings["tooltip"] = tooltip | ||
return self._chart.mark_bar().encode(**encodings, **kwargs).interactive() | ||
return _add_tooltip( | ||
self._chart.mark_bar().encode(**encodings, **kwargs).interactive() | ||
) | ||
|
||
def line( | ||
self, | ||
x: ChannelX | None = None, | ||
y: ChannelY | None = None, | ||
color: ChannelColor | None = None, | ||
order: ChannelOrder | None = None, | ||
tooltip: ChannelTooltip | None = None, | ||
x: X | None = None, | ||
y: Y | None = None, | ||
color: Color | None = None, | ||
order: Order | None = None, | ||
/, | ||
**kwargs: Unpack[EncodeKwds], | ||
) -> alt.Chart: | ||
|
@@ -142,8 +138,6 @@ def line( | |
Column to color lines by. | ||
order | ||
Column to use for order of data points in lines. | ||
tooltip | ||
Columns to show values of when hovering over lines with pointer. | ||
**kwargs | ||
Additional keyword arguments passed to Altair. | ||
|
||
|
@@ -168,17 +162,16 @@ def line( | |
encodings["color"] = color | ||
if order is not None: | ||
encodings["order"] = order | ||
if tooltip is not None: | ||
encodings["tooltip"] = tooltip | ||
return self._chart.mark_line().encode(**encodings, **kwargs).interactive() | ||
return _add_tooltip( | ||
self._chart.mark_line().encode(**encodings, **kwargs).interactive() | ||
) | ||
|
||
def point( | ||
self, | ||
x: ChannelX | None = None, | ||
y: ChannelY | None = None, | ||
color: ChannelColor | None = None, | ||
size: ChannelSize | None = None, | ||
tooltip: ChannelTooltip | None = None, | ||
x: X | None = None, | ||
y: Y | None = None, | ||
color: Color | None = None, | ||
size: Size | None = None, | ||
/, | ||
**kwargs: Unpack[EncodeKwds], | ||
) -> alt.Chart: | ||
|
@@ -209,8 +202,6 @@ def point( | |
Column to color points by. | ||
size | ||
Column which determines points' sizes. | ||
tooltip | ||
Columns to show values of when hovering over points with pointer. | ||
**kwargs | ||
Additional keyword arguments passed to Altair. | ||
|
||
|
@@ -234,9 +225,7 @@ def point( | |
encodings["color"] = color | ||
if size is not None: | ||
encodings["size"] = size | ||
if tooltip is not None: | ||
encodings["tooltip"] = tooltip | ||
return ( | ||
return _add_tooltip( | ||
self._chart.mark_point() | ||
.encode( | ||
**encodings, | ||
|
@@ -253,4 +242,4 @@ def __getattr__(self, attr: str) -> Callable[..., alt.Chart]: | |
if method is None: | ||
msg = "Altair has no method 'mark_{attr}'" | ||
raise AttributeError(msg) | ||
return lambda **kwargs: method().encode(**kwargs).interactive() | ||
return lambda **kwargs: _add_tooltip(method().encode(**kwargs).interactive()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just linking the context for this change #18609 (comment)
cc'ing @mattijn as this is a possible solution for vega/altair#3582 (comment)