#!/usr/bin/env/python3
# -*- coding:utf-8 -*-
'''
Author:leo
Date&Time:2021/4/30 14:05
Project:python
FileName:20210501_pyecharts图表库.py
Desc:
各种图表(柱状图)
参考地址:https://pyecharts.org/#/zh-cn/quickstart
'''
from pyecharts.charts import Bar
from pyecharts import options as opts
import time, random
class Pyecharts(object):
def __init__(self):
self.logTime = time.strftime('%Y%m%d_%H%M%S', time.localtime(time.time()))
self.file_path = r"D:\Mytest\Svnbucket\python\demo\script\test_files\\"
# 生成柱状图
def test_create1(self):
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(self.file_path + f"pyecharts_{self.logTime}.html")
# 生成柱状图
def test_create2(self):
from pyecharts.charts import Bar
from pyecharts import options as opts
# 内置主题类型可查看 pyecharts.globals.ThemeType
from pyecharts.globals import ThemeType
bar = (
Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT))
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
.add_yaxis("商家B", [15, 6, 45, 20, 35, 66])
.set_global_opts(title_opts=opts.TitleOpts(title="主标题", subtitle="副标题"))
)
bar.render(self.file_path + f"pyecharts_{self.logTime}.html")
def test_create3(self):
from pyecharts.charts import Bar
from pyecharts.render import make_snapshot
# 使用 snapshot-selenium 渲染图片
from snapshot_selenium import snapshot
bar = (
Bar()
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
)
make_snapshot(snapshot, bar.render(self.file_path + f"pyecharts_{self.logTime}.html"), "bar.png")
if __name__ == '__main__':
test = Pyecharts()
# test.test_create1()
# test.test_create2()
test.test_create3()