"""
A simple ECharts Parent-Child app that displays a parent plot and a child plot is displayed on (`click`) interactions.
See https://panel.holoviz.org/reference/panes/ECharts.html
Motivated by https://www.linkedin.com/posts/andfanilo_i-had-fun-building-this-small-python-echarts-activity-7325769079401709569-YTBB
"""
import panel as pn
import param
_parent_config = {
"title": {"text": "Parent: Bubble Chart"},
"tooltip": {"trigger": "item"},
"xAxis": {"type": "value"},
"yAxis": {"type": "value"},
"series": [{
"type": "scatter",
"data": [
{"name": "Category A", "value": [10, 20, 40], "symbolSize": 40},
{"name": "Category B", "value": [20, 30, 60], "symbolSize": 60},
{"name": "Category C", "value": [30, 10, 80], "symbolSize": 80},
],
"encode": {"tooltip": [0, 1, 2]}
}]
}
_child_config = {
"Category A": {
"title": {"text": "Child: Details for Category A"},
"xAxis": {
"type": "category",
"data": ["Metric 1", "Metric 2", "Metric 3"]
},
"yAxis": {"type": "value"},
"series": [{
"type": "bar",
"data": [5, 10, 15]
}]
},
"Category B": {
"title": {"text": "Child: Details for Category B"},
"xAxis": {
"type": "category",
"data": ["Metric 1", "Metric 2", "Metric 3"]
},
"yAxis": {"type": "value"},
"series": [{
"type": "bar",
"data": [20, 10, 5]
}]
},
"Category C": {
"title": {"text": "Child: Details for Category C"},
"xAxis": {
"type": "category",
"data": ["Metric 1", "Metric 2", "Metric 3"]
},
"yAxis": {"type": "value"},
"series": [{
"type": "bar",
"data": [10, 30, 10]
}]
}
}
# The selected child to display. '' means no child selected.
selection = pn.rx()
def get_parent_config():
"""
Returns the parent plot.
"""
return _parent_config
def handle_parent_event(event):
"""
Handles the event from the parent plot.
"""
if event.type=="click":
selection.rx.value = event.data["name"]
else:
print(event.data)
def get_child_config(name)->dict:
"""
Returns the child based on the child name
"""
return _child_config.get(name, {})
parent_plot = pn.pane.ECharts(get_parent_config(), sizing_mode="stretch_width", height=400, max_width=1000)
# Other events are not working
# See https://github.com/holoviz/panel/issues/7889
parent_plot.on_event('click', callback=handle_parent_event)
child_plot = pn.pane.ECharts(pn.bind(get_child_config, selection), sizing_mode="stretch_width", height=400, max_width=1000)
pn.FlexBox(
parent_plot,
child_plot,
sizing_mode="stretch_width"
).servable()