Skip to content

Commit bb49bc5

Browse files
authored
fix: Responsive support for echart (#273)
1 parent a9324e7 commit bb49bc5

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

solara/components/echarts.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class EchartsWidget(ipyvuetify.VuetifyTemplate):
1010
template_file = (__file__, "echarts.vue")
1111

1212
attributes = traitlets.Dict(default_value=None, allow_none=True).tag(sync=True)
13-
13+
responsive = traitlets.Bool(False).tag(sync=True)
1414
maps = traitlets.Any({}).tag(sync=True)
1515
option = traitlets.Any({}).tag(sync=True)
1616
on_click = traitlets.Callable(None, allow_none=True)
@@ -49,7 +49,8 @@ def FigureEcharts(
4949
on_mouseover: Callable[[Any], Any] = None,
5050
on_mouseout: Callable[[Any], Any] = None,
5151
maps: dict = {},
52-
attributes={"style": "height: 400px"},
52+
attributes={"style": "height: 400px;"},
53+
responsive: bool = False,
5354
):
5455
"""Create a Echarts figure.
5556
@@ -68,6 +69,7 @@ def FigureEcharts(
6869
* on_mouseout: Callable, a function that will be called when the user moves the mouse out of a certain component.
6970
* maps: dict, a dictionary of maps to be used in the figure.
7071
* attributes: dict, a dictionary of attributes to be passed to the container (like style, class).
72+
* responsive: bool, whether the chart should resize when the container changes size.
7173
7274
7375
"""
@@ -80,4 +82,5 @@ def FigureEcharts(
8082
on_mouseout=on_mouseout,
8183
on_mouseout_enabled=on_mouseout is not None,
8284
attributes=attributes,
85+
responsive=responsive,
8386
)

solara/components/echarts.vue

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<template>
2-
<div>
3-
<div ref="echarts" class="solara-echarts" v-bind="attributes"></div>
4-
</div>
2+
<div ref="echarts" class="solara-echarts" v-bind="attributes"></div>
53
</template>
64
<script>
75
module.exports = {
@@ -14,6 +12,22 @@ module.exports = {
1412
this.echarts = echarts;
1513
this.create();
1614
})();
15+
if(this.responsive){
16+
this.resizeObserver = new ResizeObserver(entries => {
17+
for (let entry of entries) {
18+
if (entry.target === this.$refs.echarts) {
19+
this.handleContainerResize();
20+
}
21+
}
22+
});
23+
this.resizeObserver.observe(this.$refs.echarts);
24+
};
25+
},
26+
beforeDestroy() {
27+
if (this.resizeObserver) {
28+
this.resizeObserver.unobserve(this.$refs.echarts);
29+
this.resizeObserver.disconnect();
30+
}
1731
},
1832
watch: {
1933
option() {
@@ -66,6 +80,11 @@ module.exports = {
6680
if (this.on_mouseout_enabled) this.on_mouseout(eventData);
6781
});
6882
},
83+
handleContainerResize() {
84+
if (this.chart) {
85+
this.chart.resize();
86+
}
87+
},
6988
import(deps) {
7089
return this.loadRequire().then(() => {
7190
if (window.jupyterVue) {

solara/website/pages/documentation/components/viz/echarts.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ def Page():
6363
with solara.ToggleButtonsSingle("bars", on_value=set_option):
6464
solara.Button("bars")
6565
solara.Button("pie")
66-
solara.FigureEcharts(option=options[option], on_click=set_click_data, on_mouseover=set_mouseover_data, on_mouseout=set_mouseout_data)
66+
solara.FigureEcharts(
67+
option=options[option], on_click=set_click_data, on_mouseover=set_mouseover_data, on_mouseout=set_mouseout_data, responsive=True
68+
)
6769
with solara.Card("Event data"):
6870
solara.Markdown(f"**Click data**: {click_data}")
6971
solara.Markdown(f"**Mouseover data**: {mouseover_data}")

0 commit comments

Comments
 (0)