I have a world map hvplot image, and when I hover it I can observe the geographical coordinates being displayed. I would like to connect a click event on this map to a line chart below that can display the historical available data for this geographical coordinate. But I do not know how to connect such a click event from the hvplot chart. I was not able to find an example doing this in the documentation, and therefore I was wondering if anyone had a solution or a pointer where to look. Thank you!
Below is what I have so far:
import panel as pn
import os
import glob
import xarray as xr
import datetime
import pandas as pd
import hvplot.xarray
ds = xr.open_mfdataset(file_glob, engine="h5netcdf")
date_options = pd.to_datetime(ds.time.data)
date_dict = {d.strftime("%Y-%m-%d"): d.timestamp() for d in date_options}
min_date = ds.time.min().dt
max_date = ds.time.max().dt
t_slider = pn.widgets.DiscreteSlider(
name="Time Selection",
value = pd.Timestamp(datetime.date(min_date.year.item(), min_date.month.item(), min_date.day.item())).timestamp(),
options=date_dict
)
# create a world map
def get_world_map(unix_time):
"""Plot world map from dataset."""
return ds.sel(time=datetime.datetime.fromtimestamp(unix_time), method="nearest").sla.hvplot.image().opts(bgcolor="gray", cmap="rainbow", height=650,width=1300,data_aspect=1)
# define the layout of the app
world_map_row = pn.Row(
pn.Column(title, t_slider),
get_world_map(t_slider.value)
)
# watch for updates from slider to trigger world map update
def update_world_map_from_t_slider(event):
"""Update the world map from slider."""
world_map_row[1].object = get_world_map(t_slider.value)
# lineplot for timeseries for coordinates
# def update_timeseries_from_click(event):
def get_timeseries():
"""Retrieve timeseries from dataset based on geographical coordinates."""
return ds.sel(latitude=ds.latitude.median(), longitude=ds.longitude.median(), method="nearest").sla.hvplot.line()
timeseries_row = pn.Row(
get_timeseries()
)
t_slider.param.watch(update_world_map_from_t_slider, "value")
world_map_row.servable(title="App")
timeseries_row.servable()