2025-09-10 15:01:32 +08:00
|
|
|
|
# pip install pyecharts
|
|
|
|
|
import pyecharts
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
#print(pyecharts.__version__)
|
|
|
|
|
from pyecharts import options as opts
|
|
|
|
|
from pyecharts.charts import Bar
|
|
|
|
|
from pyecharts.faker import Faker
|
|
|
|
|
from pyecharts.globals import CurrentConfig
|
|
|
|
|
|
|
|
|
|
# 配置使用自定义的 ECharts 路径
|
|
|
|
|
CurrentConfig.ONLINE_HOST = "https://gcore.jsdelivr.net/npm/echarts@6.0.0/dist/"
|
|
|
|
|
|
|
|
|
|
c = (
|
|
|
|
|
Bar()
|
|
|
|
|
.add_xaxis(Faker.choose())
|
|
|
|
|
.add_yaxis("商家A", Faker.values())
|
|
|
|
|
.add_yaxis("商家B", Faker.values())
|
|
|
|
|
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-MarkLine(自定义)"))
|
|
|
|
|
.set_series_opts(
|
|
|
|
|
label_opts=opts.LabelOpts(is_show=False),
|
|
|
|
|
markline_opts=opts.MarkLineOpts(
|
|
|
|
|
data=[opts.MarkLineItem(y=50, name="yAxis=50")]
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2025-09-10 15:03:48 +08:00
|
|
|
|
# 获取图表的选项配置,使用 dump_options_with_quotes 方法获取可以直接序列化的选项
|
|
|
|
|
options_str = c.dump_options_with_quotes()
|
2025-09-10 15:01:32 +08:00
|
|
|
|
|
2025-09-10 15:03:48 +08:00
|
|
|
|
# 输出选项字符串
|
|
|
|
|
print(options_str)
|
2025-09-10 15:01:32 +08:00
|
|
|
|
|
2025-09-10 15:03:48 +08:00
|
|
|
|
# 可选:将选项保存到文件
|
2025-09-10 15:01:32 +08:00
|
|
|
|
with open("bar_chart_config.json", "w", encoding="utf-8") as f:
|
2025-09-10 15:03:48 +08:00
|
|
|
|
f.write(options_str)
|
|
|
|
|
|
|
|
|
|
# 如果需要同时生成 HTML 文件,取消下面这行的注释
|
|
|
|
|
# c.render("bar_markline_custom.html")
|