使用 options 配置项,在 pyecharts 中,一切皆 Options。
from pyecharts.charts import Bar
from pyecharts import options as opts
# V1 版本开始支持链式调用
# 你所看到的格式其实是 `black` 格式化以后的效果
# 可以执行 `pip install black` 下载使用
bar = (
Bar()
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
.set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
# 或者直接使用字典参数
# .set_global_opts(title_opts={"text": "主标题", "subtext": "副标题"})
)
bar.render()
class TitleOpts(BasicOpts):
def __init__(
self,
title: Optional[str] = None,
title_link: Optional[str] = None,
title_target: Optional[str] = None,
subtitle: Optional[str] = None,
subtitle_link: Optional[str] = None,
subtitle_target: Optional[str] = None,
pos_left: Optional[str] = None,
pos_right: Optional[str] = None,
pos_top: Optional[str] = None,
pos_bottom: Optional[str] = None,
title_textstyle_opts: Union[TextStyleOpts, dict, None] = None,
subtitle_textstyle_opts: Union[TextStyleOpts, dict, None] = None,
):
self.opts: Sequence = [
{
"text": title,
"link": title_link,
"target": title_target,
"subtext": subtitle,
"sublink": subtitle_link,
"subtarget": subtitle_target,
"left": pos_left,
"right": pos_right,
"top": pos_top,
"bottom": pos_bottom,
"textStyle": title_textstyle_opts,
"subtextStyle": subtitle_textstyle_opts,
}
]