-
Notifications
You must be signed in to change notification settings - Fork 795
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
Docs: Add another version of the 'Multiline Tooltip' exmaple which uses the standard tooltip #3340
Merged
binste
merged 3 commits into
vega:main
from
binste:add_multiline_tooltip_standard_example
Mar 1, 2024
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
tests/examples_arguments_syntax/multiline_tooltip_standard.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
""" | ||
Multi-Line Tooltip (Standard) | ||
============================= | ||
This example shows how to add a standard tooltip to the same chart | ||
as in :ref:`gallery_multiline_tooltip`. You can find another example | ||
using this approach in the documentation on the :ref:`user-guide-pivot-transform` transformation. | ||
""" | ||
# category: interactive charts | ||
import altair as alt | ||
import pandas as pd | ||
import numpy as np | ||
|
||
np.random.seed(42) | ||
columns = ["A", "B", "C"] | ||
source = pd.DataFrame( | ||
np.cumsum(np.random.randn(100, 3), 0).round(2), | ||
columns=columns, | ||
index=pd.RangeIndex(100, name="x"), | ||
) | ||
source = source.reset_index().melt("x", var_name="category", value_name="y") | ||
|
||
# The basic line | ||
line = ( | ||
alt.Chart(source) | ||
.mark_line(interpolate="basis") | ||
.encode(x="x:Q", y="y:Q", color="category:N") | ||
) | ||
|
||
# Create a selection that chooses the nearest point & selects based on x-value | ||
nearest = alt.selection_point(nearest=True, on="mouseover", fields=["x"], empty=False) | ||
|
||
# Draw points on the line, and highlight based on selection | ||
points = line.mark_point().encode( | ||
opacity=alt.condition(nearest, alt.value(1), alt.value(0)) | ||
) | ||
|
||
# Draw a rule at the location of the selection | ||
rules = ( | ||
alt.Chart(source) | ||
.transform_pivot("category", value="y", groupby=["x"]) | ||
.mark_rule(color="gray") | ||
.encode( | ||
x="x:Q", | ||
opacity=alt.condition(nearest, alt.value(0.3), alt.value(0)), | ||
tooltip=[alt.Tooltip(c, type="quantitative") for c in columns], | ||
) | ||
.add_params(nearest) | ||
) | ||
|
||
|
||
# Put the five layers into a chart and bind the data | ||
alt.layer(line, points, rules).properties(width=600, height=300) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
tests/examples_methods_syntax/multiline_tooltip_standard.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
""" | ||
Multi-Line Tooltip (Standard) | ||
============================= | ||
This example shows how to add a standard tooltip to the same chart | ||
as in :ref:`gallery_multiline_tooltip`. You can find another example | ||
using this approach in the documentation on the :ref:`user-guide-pivot-transform` transformation. | ||
""" | ||
# category: interactive charts | ||
import altair as alt | ||
import pandas as pd | ||
import numpy as np | ||
|
||
np.random.seed(42) | ||
columns = ["A", "B", "C"] | ||
source = pd.DataFrame( | ||
np.cumsum(np.random.randn(100, 3), 0).round(2), | ||
columns=columns, | ||
index=pd.RangeIndex(100, name="x"), | ||
) | ||
source = source.reset_index().melt("x", var_name="category", value_name="y") | ||
|
||
# The basic line | ||
line = ( | ||
alt.Chart(source) | ||
.mark_line(interpolate="basis") | ||
.encode(x="x:Q", y="y:Q", color="category:N") | ||
) | ||
|
||
# Create a selection that chooses the nearest point & selects based on x-value | ||
nearest = alt.selection_point(nearest=True, on="mouseover", fields=["x"], empty=False) | ||
|
||
# Draw points on the line, and highlight based on selection | ||
points = line.mark_point().encode( | ||
opacity=alt.condition(nearest, alt.value(1), alt.value(0)) | ||
) | ||
|
||
# Draw a rule at the location of the selection | ||
rules = ( | ||
alt.Chart(source) | ||
.transform_pivot("category", value="y", groupby=["x"]) | ||
.mark_rule(color="gray") | ||
.encode( | ||
x="x:Q", | ||
opacity=alt.condition(nearest, alt.value(0.3), alt.value(0)), | ||
tooltip=[alt.Tooltip(c, type="quantitative") for c in columns], | ||
) | ||
.add_params(nearest) | ||
) | ||
|
||
|
||
# Put the five layers into a chart and bind the data | ||
alt.layer(line, points, rules).properties(width=600, height=300) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm curious, do you find this more convenient than the approach we use elsewhere in the docs?
I'm not suggesting you change it here, just curious about what you use on a day to day basis.
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.
Good point, they should be the same! I harmonised the code style, mainly adapting the new example to the old one so it's consistent with the approach we use in other places. I also slightly cleaned up the old one.
Personally, I just always use a code formatter (black or ruff). I love that I don't have to think about it and can get nicely formatted code with a hotkey or by saving a file :) The style I used before comes from ruff.