-
Notifications
You must be signed in to change notification settings - Fork 929
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
Add tooltip option to Altair chart #2082
Conversation
for more information, see https://pre-commit.ci
Performance benchmarks:
|
def detect_type(key): | ||
key_type = type(all_agent_data[0][key]) | ||
tooltip_type = "" | ||
if key_type == int: |
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.
For checking type, it is recommended with isinstance
, see https://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python:
first_by_key = all_agent_data[0][key]
if isinstance(first_by_key, int):
...
elif isinstance(first_by_key, ...):
...
@@ -34,12 +35,39 @@ def portray(g): | |||
all_agent_data.append(agent_data) | |||
return all_agent_data | |||
|
|||
def detect_type(key): |
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.
The naming is clearer with detect_tooltip_type
.
encoding_dict = { | ||
# no x-axis label | ||
"x": alt.X("x", axis=None, type="ordinal"), | ||
# no y-axis label | ||
"y": alt.Y("y", axis=None, type="ordinal"), | ||
"tooltip": [ | ||
alt.Tooltip(key, type=tooltip_types[key]) | ||
for key in all_agent_data[0].keys() |
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.
The [key for key in all_agent_data[0].keys() if key not in invalid_tooltips]
here is a repetition from L57-59. They can precomputed once so that there is no repetition.
There are Ruff errors that can't be autofixed in the pre-commit.ci. |
I started by changing type checks using
The altair proper function doesn't implement date object detection on strings, so the user has to specify the date object in the agent_portrayal function. E.g:
|
alt.Tooltip( | ||
key, type=alt.utils.infer_vegalite_type([all_agent_data[0][key]]) | ||
) | ||
for key in all_agent_data[0] |
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.
One last simplification, you can do
[
alt.Tooltip(key, type=alst.utils.infer_vegalite_type([value])
for key, value in all_agent_data[0].items()
if key not in invalid_tooltips
]
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.
Okay done. Yeah I agree with you.
One last simplification, you can do
I see, that simplifies the implementation a lot. |
Merged, thank you @FoFFolo. |
I added in the altair.py experimental script the option to see a tooltip on each agent in the canvas when hovering on them. The logic is to see every option described by the agent_portrayal function, except for the x, y, color and size properties.
When defining the type of each tooltip, I didn't manage to find an optimal algorithm to check if a string can be considered a date object, in order to change its type from "nominal" to "temporal", without using an external library called dateutil, which contains the function parser.parse() that returns a boolean if the string is a right date format.
Here is an example:
In the agent_portrayal function, I set that every agent will have a tooltip to see their unique_id.
If we run this script using solara:
If I click on "View Source" in the altair chart, the content is:
What's new is the tooltip array element and the unique_id value on each agent.