最近基于pyecharts + phantomjs + snapshot_phantomjs 进行图表的绘制与保存

关于渲染图片的方式, 请参考官网:
https://pyecharts.org/#/zh-cn/render_images
上面的图片, 也是个人基于官网整理, 可参考.
那回到图片的大小设置问题, 使用make_snapshot渲染为图片后, 发现每张图片大约7~8M, 太大
修改之前:
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
make_snapshot(snapshot, 其他参数, pixel_ratio=2)
修改后:
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
make_snapshot(snapshot, 其他参数, pixel_ratio=1)
解决方法: 设置pixel_ratio参数, 比如, 对于 snapshot_phantomjs 来说, 默认的pixel_ratio=2, 我将其设置为pixel_ratio=1, 最终的图片大小才1.5M.
更新:
其实 snapshot_phantomjs 等工具包, 仅仅是调用了下 phantomjs 程序, 该程序渲染html后, 使用的是echarts本身的导出接口:
调用链:

mychart.getDataURL({
type: file_type,
pixelRatio: pixel_ratio, # 实际影响的地方. echarts本身默认值为1, 到 snapshot_phantomjs 默认2...
excludeComponents: ['toolbox']
});
echarts 导出接口细节参考: https://echarts.apache.org/zh/api.html#echartsInstance.getDataURL

本文探讨了使用PyEcharts结合Phantomjs和snapshot_phantomjs进行图表渲染时,如何通过调整pixel_ratio参数来减小图片文件大小。从默认的pixel_ratio=2改为pixel_ratio=1,显著降低了图片体积。
732

被折叠的 条评论
为什么被折叠?



