"""
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]
}]
}
}
class PanelChildEcharts(pn.viewable.Viewer):
"""
A Panel component that displays a parent-child ECharts interaction.
"""
selection = param.String(default="", doc="The selected child to display. '' means no child selected.")
@param.depends()
def get_parent_config(self)->dict:
"""
Returns the parent plot.
"""
return _parent_config
def _handle_parent_event(self, event):
"""
Handles the event from the parent plot.
"""
if event.type=="click":
self.selection = event.data["name"]
else:
print(event.data)
@param.depends('selection')
def get_child_config(self)->dict:
"""
Returns the child based on the child name
"""
return _child_config.get(self.selection, {})
def __panel__(self):
"""
Returns the Panel layout.
"""
parent_plot = pn.pane.ECharts(self.get_parent_config(), sizing_mode="stretch_width", height=400, max_width=1000)
child_plot = pn.pane.ECharts(self.get_child_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=self._handle_parent_event)
return pn.FlexBox(
parent_plot,
child_plot,
sizing_mode="stretch_width"
)
if pn.state.served:
pn.extension('echarts')
PanelChildEcharts().servable()