Files
YunNanProject/Test/TestEcharts.py
2025-09-10 15:03:48 +08:00

40 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 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")]
),
)
)
# 获取图表的选项配置,使用 dump_options_with_quotes 方法获取可以直接序列化的选项
options_str = c.dump_options_with_quotes()
# 输出选项字符串
print(options_str)
# 可选:将选项保存到文件
with open("bar_chart_config.json", "w", encoding="utf-8") as f:
f.write(options_str)
# 如果需要同时生成 HTML 文件,取消下面这行的注释
# c.render("bar_markline_custom.html")