当前pyecharts
版本为1.9.0
概述
globals
模块位于pyecharts
包顶级目录中,文件名为globals.py
。用于定义pyecharts
的“全局变量”。
globals
模块按照Python常用的设计模式,把在全局范围内使用的变量定义在单一模块中的类中。
模块包含11个“私有”类、12个变量,除ToolTipFormatterType
外,其余变量均为对应类的实例。
# 渲染方式
RenderType = _RenderType()
# 允许的生成的文件类型
FileType = _FileType()
# Symbol 样式类型
SymbolType = _SymbolType()
# 图表类型
ChartType = _ChartType
# Tooltip 格式器类型
TooltipFormatterType = _ToolTipFormatterType()
# 主题类型
ThemeType = _ThemeType()
# Geo 图形类型
GeoType = _GeoType()
# BMap 图形全局参数
BMapType = _BMapType
# Notebook 环境类型
NotebookType = _NotebookType()
# 远程资源 Host
OnlineHostType = _OnlineHost()
# Warning 控制
WarningType = _WarningControl()
# 全局环境唯一实例
CurrentConfig = _CurrentConfig()
全局环境实例CurrentConfig
在所有“全局”变量中,最重要的可能是CurrentConfig
,它为pyecharts
图表实例提供了部分全局默认值,注意是部分,这意味着并不是说在导入图表类之前修改某些CurrentConfig
属性就可以影响某些初始值!
先说结论:ONLINE_HOST
和NOTEBOOK_TYPE
支持直接修改初始值,而PAGE_TITLE
则无效。
class _CurrentConfig:
PAGE_TITLE = "Awesome-pyecharts"
ONLINE_HOST = OnlineHostType.DEFAULT_HOST
NOTEBOOK_TYPE = NotebookType.JUPYTER_NOTEBOOK
GLOBAL_ENV = Environment(
keep_trailing_newline=True,
trim_blocks=True,
lstrip_blocks=True,
loader=FileSystemLoader(
os.path.join(
os.path.abspath(os.path.dirname(__file__)), "render", "templates"
)
),
)
CurrentConfig = _CurrentConfig()
案例:验证修改全局变量是否影响图表实例的初始值
代码
from pyecharts.globals import CurrentConfig, NotebookType
CurrentConfig.ONLINE_HOST = "http://127.0.0.1:8000/assets/"
CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB
CurrentConfig.PAGE_TITLE = "test"
from pyecharts.charts import Bar
bar = Bar()
print(CurrentConfig.ONLINE_HOST,CurrentConfig.NOTEBOOK_TYPE,CurrentConfig.PAGE_TITLE)
print(bar.js_host,CurrentConfig.NOTEBOOK_TYPE,bar.page_title)
输出
http://127.0.0.1:8000/assets/ jupyter_lab test
http://127.0.0.1:8000/assets/ jupyter_lab Awesome-pyecharts
原因分析
根据pyecharts\options\global_options.py
和pyecharts\charts\base.py
源码可知,图表类在实例化时,默认配置是从InitOpts
类实例中获取的,page_title
配置项读取的是CurrentConfig
第一次实例化时的CurrentConfig.PAGE_TITLE
值,随后再修改CurrentConfig.PAGE_TITLE
无济于事。
因此,修改页面标题的方法应当直接修改图表实例对象的page_title
属性或者在图表类实例化时在init_opts
参数中修改page_title
属性。
代码
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = Bar(init_opts=opts.InitOpts(page_title="a"))
print(bar.page_title)
bar.page_title="b"
print(bar.page_title)
输出
a
b
相关源码
pyecharts\options\global_options.py
class InitOpts(BasicOpts):
def __init__(
self,
width: str = "900px",
height: str = "500px",
chart_id: Optional[str] = None,
renderer: str = RenderType.CANVAS,
page_title: str = CurrentConfig.PAGE_TITLE,
theme: str = ThemeType.WHITE,
bg_color: Union[str, dict] = None,
js_host: str = "",
animation_opts: Union[AnimationOpts, dict] = AnimationOpts(),
):
self.opts: dict = {
"width": width,
"height": height,
"chart_id": chart_id,
"renderer": renderer,
"page_title": page_title,
"theme": theme,
"bg_color": bg_color,
"js_host": js_host,
"animationOpts": animation_opts,
}
pyechartscharts\base.py
class Base(ChartMixin):
def __init__(self, init_opts: Union[InitOpts, dict] = InitOpts()):
_opts = init_opts
if isinstance(init_opts, InitOpts):
_opts = init_opts.opts
self.width = _opts.get("width", "900px")
self.height = _opts.get("height", "500px")
self.renderer = _opts.get("renderer", RenderType.CANVAS)
self.page_title = _opts.get("page_title", CurrentConfig.PAGE_TITLE)
self.theme = _opts.get("theme", ThemeType.WHITE)
self.chart_id = _opts.get("chart_id") or uuid.uuid4().hexself.options: dict = {}
self.js_host: str = _opts.get("js_host") or CurrentConfig.ONLINE_HOST
self.js_functions: utils.OrderedSet = utils.OrderedSet()
self.js_dependencies: utils.OrderedSet = utils.OrderedSet("echarts")
self.options.update(backgroundColor=_opts.get("bg_color"))
self.options.update(_opts.get("animationOpts", AnimationOpts()).opts)
self._is_geo_chart: bool = False