1

I am creating a web app with charts. If one hovers (and clicks) on a chart, I want to display more information about that element below (i.e. outside of) that chart. Currently, I am using the following to highlight a specific bar in the chart.

single = alt.selection_single(on="click"

c = alt.Chart(df).mark_bar.encode(
    ...
).add_selection(single)

Can I further use the information which bar is being clicked on?

V W
  • 21
  • 2
  • Have you looked at [this](https://stackoverflow.com/questions/64565901/how-to-retrieve-attributes-from-selected-datum-in-altair)? – LazyClown Oct 13 '22 at 13:22
  • Thanks! I have seen it, but didn't quite understand the mechanics. – V W Oct 13 '22 at 13:28
  • The streamlit [package](https://github.com/domoritz/streamlit-vega-lite) package looks straightforward. If you are looking for Altair specific solution without streamlit, I am not sure if that is possible at the moment. Let's see if someone else can show us how to do it. – LazyClown Oct 13 '22 at 13:52
  • Yes, thanks a lot. So this is exactly what I need. But I somehow can't install it. I ran pip but Streamlit can't find it `No module named 'streamlit_vega_lite'`. Do I assume correctly that I have to run the `Dev Setup` part? – V W Oct 14 '22 at 07:48
  • I installed it successfully on Python 3.10 with pip version 22.2.2. Check if the pip is up to date. Otherwise, I think the module creator could help you better regarding the installation issue. – LazyClown Oct 14 '22 at 08:10
  • Installation worked. I implemented the example in my streamlit code but it doesn't write anything. If I select something in the chart, the page just refreshes and a new random histogram appears. Any ideas? – V W Oct 14 '22 at 08:53

1 Answers1

1

If you are created the second chart with Altair, then you can use the information about which bar is clicked in that second chart. Example here Altair choropleth map, color highlight based on line chart selection

Altair cannot send the information of which bar has been clicked on outside of Altair. If you want to do that, you need to use a package that is able to retrieve this info from Altair/VegaLite on its own. Currently, the only robust solution for this is via Panel, you can read more in this issue https://github.com/altair-viz/altair/issues/1153

joelostblom
  • 43,590
  • 17
  • 150
  • 159