-
-
Notifications
You must be signed in to change notification settings - Fork 477
/
echarts.py
93 lines (71 loc) · 2.88 KB
/
echarts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from __future__ import annotations
import json
import sys
from typing import ClassVar, Mapping
import param
from pyviz_comms import JupyterComm
from ..util import lazy_load
from .base import PaneBase
class ECharts(PaneBase):
"""
ECharts panes allow rendering echarts.js dictionaries and pyecharts plots.
Reference: https://panel.holoviz.org/reference/panes/ECharts.html
:Example:
>>> pn.extension('echarts')
>>> ECharts(some_echart_dict_or_pyecharts_object, height=480, width=640)
"""
object = param.Parameter(default=None, doc="""
The Echarts object being wrapped. Can be an Echarts dictionary or a pyecharts chart""")
renderer = param.ObjectSelector(default="canvas", objects=["canvas", "svg"], doc="""
Whether to render as HTML canvas or SVG""")
theme = param.ObjectSelector(default="default", objects=["default", "light", "dark"], doc="""
Theme to apply to plots.""")
priority = None
_rename: ClassVar[Mapping[str, str | None]] = {"object": "data"}
_rerender_params = []
_updates = True
@classmethod
def applies(cls, obj, **params):
if isinstance(obj, dict):
return 0
elif "pyecharts." in repr(obj.__class__):
return 0.8
return None
@classmethod
def _get_dimensions(cls, props):
if json is None:
return
responsive = props.get('data', {}).get('responsive')
if responsive:
props['sizing_mode'] = 'stretch_both'
else:
props['sizing_mode'] = 'fixed'
def _get_model(self, doc, root=None, parent=None, comm=None):
ECharts = lazy_load('panel.models.echarts', 'ECharts', isinstance(comm, JupyterComm), root)
props = self._get_echart_dict(self.object)
props.update(self._process_param_change(self._init_params()))
self._get_dimensions(props)
model = ECharts(**props)
if root is None:
root = model
self._models[root.ref['id']] = (model, parent)
return model
def _process_param_change(self, msg):
msg = super()._process_param_change(msg)
if 'data' in msg:
msg.update(self._get_echart_dict(msg['data']))
return msg
def _get_echart_dict(self, object):
if isinstance(object, dict):
return {'data': dict(object)}
elif "pyecharts" in sys.modules:
import pyecharts # pylint: disable=import-outside-toplevel,import-error
if isinstance(object, pyecharts.charts.chart.Chart):
w, h = object.width, object.height
params = {'data': json.loads(object.dump_options())}
if not self.height and h:
params['height'] = int(h.replace('px', ''))
if not self.width and w:
params['width'] = int(w.replace('px', ''))
return params
return {}