question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Showing both x and y value in multi-line tooltip

See original GitHub issue

Hello, thanks for your work on this awesome visualization library.

I am looking to adapt the multi-line tooltip example (https://altair-viz.github.io/gallery/multiline_tooltip.html) to plot molecular spectra. I would like the tooltip to also show the value of the x axis. I’ve added another text mark:

x_text = spectrum.mark_text(align="right", dx=-5, dy=-25).encode(
      text=alt.condition(nearest, "x:Q", alt.value(" "), format="3.4f")
   )

but was wondering: a. Is there a better way to do this? b. How can I have the displayed x-value always appear at the bottom (or top) of the chart without fiddling with dx and dy?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
jakevdpcommented, May 14, 2020

Yes, I’ve been meaning to replace that example because it’s overly verbose and confusing. With the pivot transform in Altair 4+, you can do something a bit more cleanly:

import altair as alt
from vega_datasets import data

source = data.stocks.url

hover = alt.selection_single(
    fields=["date"],
    nearest=True,
    on="mouseover",
    empty="none",
    clear="mouseout"
)

lines = alt.Chart(source).mark_line().encode(
    x='date:T',
    y='price:Q',
    color='symbol:N',
)

points = lines.transform_filter(hover).mark_circle()

tooltips = alt.Chart(source).transform_pivot(
    "symbol", "price", groupby=["date"]
).mark_rule().encode(
    x='date:T',
    opacity=alt.condition(hover, alt.value(0.3), alt.value(0)),
    tooltip=["AAPL:Q", "AMZN:Q", "GOOG:Q", "IBM:Q", "MSFT:Q"]
).add_selection(hover)

lines + points + tooltips

This makes it trivial to add another tooltip field: just add an encoding to the list.

1reaction
jakevdpcommented, May 14, 2020

You’re pivoting x while grouping by x, which constructs an NxN data representation. Here N is 5000, so you’re telling the browser to construct a data object with 25,000,000 elements.

That is… not recommended.

There’s no reason to pivot a value while grouping by that same value. And if all you want is to display a tooltip with x and y, there’s no need to pivot at all. Remove the pivot transform and it should work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multi-Line Tooltip — Altair 4.2.0 documentation
Multi-Line Tooltip . This example shows how you can use selections and layers to create a tooltip-like behavior tied to the x position...
Read more >
How to show values on hover for multiple line graphs - Bokeh
This argument accepts the same values as the HoverTool.tooltips property. If a hover tool is specified in the tools argument, this value ...
Read more >
Line Chart, Multiple Series, with tooltip - Observable
Having multiline tooltips means a better capability for displaying data, ... x = ([x]) => x, // given d in data, returns the...
Read more >
PDF form fields | Add tooltips, data and time, reqired/optional ...
Learn how to use form fields properties to add tooltip, date & time, required or not required, multi-line text, and calculated values in...
Read more >
Configuring plot tools — Bokeh 2.4.3 Documentation
The hover tooltip shows the index of the image, the name of the pattern, the x and y position of the cursor, as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found