• 02 pycharts 结果生成为 html 、图片(示例代码+效果图)


    目录

    1、生成HTML

    2 、生成图片

            make_snapshot详解


    1、生成HTML

    # render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
    # 也可以传入路径参数,如 bar.render("mycharts.html")

    1. from pyecharts.charts import Bar
    2. from pyecharts import options as opts
    3. bar = Bar() #单独调用
    4. bar.add_xaxis(["语文", "数学", "英语", "生物", "物理", "化学", "地理"])
    5. bar.add_yaxis("10月", [114, 95, 107, 81, 85, 87, 85])
    6. bar.add_yaxis("11月", [106, 116, 125, 91, 88, 86, 87])
    7. bar.add_yaxis("12月", [97, 134, 137, 89, 95, 93, 89])
    8. bar.set_global_opts(title_opts=opts.TitleOpts(title="学生月考成绩", subtitle="马冬梅"))
    9. bar.render()

    2、 生成图片

    # 如果运行完当前目录图片没有出来,可以右键项目从磁盘重新加载 Reload from Disc
    # 运行报错

    WebDriverException( selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home

    解决方法可以参考链接:https://blog.csdn.net/c_lanxiaofang/article/details/126001670

    1. from snapshot_selenium import snapshot as driver
    2. from pyecharts import options as opts
    3. from pyecharts.charts import Bar
    4. from pyecharts.render import make_snapshot
    5. def bar_chart() -> Bar:
    6. c = (
    7. Bar()
    8. .add_xaxis(["语文", "数学", "英语", "生物", "物理", "化学", "地理"])
    9. .add_yaxis("10月", [114, 95, 107, 81, 85, 87, 85])
    10. .add_yaxis("11月", [106, 116, 125, 91, 88, 86, 87])
    11. .add_yaxis("12月", [97, 134, 137, 89, 95, 93, 89])
    12. .reversal_axis() # 翻转 XY 轴数据
    13. .set_series_opts(label_opts=opts.LabelOpts(position="right")) # 将标签放置在图形右边
    14. .set_global_opts(title_opts=opts.TitleOpts(title="学生月考成绩", subtitle="马冬梅")) #title 主标题 subtitle副标题
    15. )
    16. return c
    17. # 需要安装 snapshot-selenium 或者 snapshot-phantomjs
    18. make_snapshot(driver, bar_chart().render(), "学生月考成绩-马冬梅.png") # 生成图片到当前文件夹下

    make_snapshot详解

    PNG_FORMAT = "png"
    JPG_FORMAT = "jpeg"
    GIF_FORMAT = "gif"
    PDF_FORMAT = "pdf"
    SVG_FORMAT = "svg"
    EPS_FORMAT = "eps"
    B64_FORMAT = "base64"
    
    
    def make_snapshot(
        engine: Any,
        file_name: str,
        output_name: str,
        delay: float = 2,
        pixel_ratio: int = 2,
        is_remove_html: bool = False,
        **kwargs,
    ):
        logger.info("Generating file ...")
        file_type = output_name.split(".")[-1]
    
        content = engine.make_snapshot(
            html_path=file_name,
            file_type=file_type,
            delay=delay,
            pixel_ratio=pixel_ratio,
            **kwargs,
        )
        if file_type in [SVG_FORMAT, B64_FORMAT]:
            save_as_text(content, output_name)
        else:
            # pdf, gif, png, jpeg
            content_array = content.split(",")
            if len(content_array) != 2:
                raise OSError(content_array)
    
            image_data = decode_base64(content_array[1])
    
            if file_type in [PDF_FORMAT, GIF_FORMAT, EPS_FORMAT]:
                save_as(image_data, output_name, file_type)
            elif file_type in [PNG_FORMAT, JPG_FORMAT]:
                save_as_png(image_data, output_name)
            else:
                raise TypeError(f"Not supported file type '{file_type}'")
    
        if "/" not in output_name:
            output_name = os.path.join(os.getcwd(), output_name)
    
        if is_remove_html and not file_name.startswith("http"):
            os.unlink(file_name)
        logger.info(f"File saved in {output_name}")
  • 相关阅读:
    确定Mac\Linux系统的架构类型是 x86-64(amd64),还是 arm64 架构
    01 STL概论与版本介绍
    清览题库--C语言程序设计第五版编程题解析(2)
    使用guacamole进行WEB远程桌面连接
    【面试:并发篇20:多线程:多把锁问题】
    django计算机毕业设计基于安卓Android/微信小程序的移动电商平台系统APP-商品购物商城app
    基于 Socket 网络编程
    【java计算机毕设】 留守儿童爱心捐赠管理系统 springboot vue html mysql 送文档ppt
    一个注解@Recover搞定丑陋的循环重试代码
    ES6 入门教程 24 Module 的语法 24.1 概述 & 24.2 严格模式
  • 原文地址:https://blog.csdn.net/c_lanxiaofang/article/details/126022015