|
|
# 参考文档
|
|
|
# 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")
|