ssh echarts mysql_pyecharts绘图保存为图片 适用于ssh无头浏览器运行

网上搜索到的答案是使用chrome driver实现的, 但是本人的程序是运行在centos下的,centos下折腾chrome driver比较蛋疼,所以看了下pyecharts.render的源码,其实这个也支持使用无头phantomjs进行截图的,当然这个不是一般的直接截取屏幕,是通过JS代码把html里面的渲染图像下载下来,清晰度比普通截图要高很多很多。

make_snapshot(snapshot, bar.render(), f"data/{today}_cb.png", driver=driver)

在最后一行传入一个driver既可以了,这个driver使用phantomjs的实例。

import os

from pyecharts.render import make_snapshot

from snapshot_selenium import snapshot

import pandas as pd

from pyecharts import options as opts

from pyecharts.charts import Bar

import sys

from selenium import webdriver

from pyecharts.commons.utils import JsCode

if sys.platform == 'win32':

SELENIUM_PATH = r'C:\OneDrive\Tool\phantomjs-2.1.1-windows\phantomjs-2.1.1-windows\bin\phantomjs.exe'

driver = None

else:

SELENIUM_PATH = './phantomjs'

driver = webdriver.PhantomJS(executable_path=SELENIUM_PATH)

bar = (

Bar()

.add_xaxis(list(result_dict .keys()))

.add_yaxis(f"{today}-可转债价格分布", y_list, category_gap=3)

.add_yaxis(f"{today}-正股价格分布", y_zg_list, category_gap=3)

.set_series_opts(

label_opts=opts.LabelOpts(is_show=True),

axispointer_opts=opts.AxisPointerOpts(is_show=True))

.set_global_opts(

title_opts=opts.TitleOpts(title="可转债价格分布"),

xaxis_opts=opts.AxisOpts(

name="涨跌幅",

is_show=True,

name_rotate=30,

),

graphic_opts=[

opts.GraphicGroup(

graphic_item=opts.GraphicItem(

left="70%",

top="20%",

),

children=[

opts.GraphicText(

graphic_item=opts.GraphicItem(

left="center",

top="middle",

z=100,

),

graphic_textstyle_opts=opts.GraphicTextStyleOpts(

text=JsCode(

f"['涨幅>=0:{bigger}',"

f"'涨幅<0:{smaller}',"

f"'平均涨幅:{avg}%',"

f"'波动方差:{std}',"

f"'',"

f"'最大:{max_name} {max_pct}%',"

f"'最小:{min_name} {min_pct}%',"

"''].join('\\n')"

),

font="14px Microsoft YaHei",

graphic_basicstyle_opts=opts.GraphicBasicStyleOpts(

fill="#333"

)

)

)

]

)

],

)

)

bar.render(os.path.join('data', f"{today}_cb.html"))

make_snapshot(snapshot, bar.render(), f"data/{today}_cb.png", driver=driver)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值