You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.5 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 参考文档 
#  https://www.jb51.net/article/235161.htm
# pip install pyecharts
# pip install snapshot_selenium
from unittest import mock
def get_chrome_driver():
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
return webdriver.Chrome(options=options)
from snapshot_selenium import snapshot as driver
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot
from snapshot_selenium import snapshot # 使用 snapshot-selenium 渲染图片
from selenium.webdriver.chrome.options import Options
c = (
Bar()
# 添加X轴数据
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
# 添加Y轴数据,系列的名称
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90]) # 此处数据应该从PG中通过SQL进行获取然后再转换为数组形式,封装一个通用的PgUtil.py
.add_yaxis("商家B", [8, 15, 60, 20, 25, 30])
# 添加标题
.set_global_opts(title_opts=opts.TitleOpts(title="主标题: 双十一销量", subtitle="副标题:服饰类"))
)
# 导出图片
with mock.patch('snapshot_selenium.snapshot.get_chrome_driver', get_chrome_driver):
# 需要安装 snapshot-selenium 或者 snapshot-phantomjs
make_snapshot(driver, c.render(), "bar.png")