pyecharts生成图片

 

 

复制代码
# -*- coding: utf-8 -*-
from pyecharts import options as opts
from pyecharts.charts import Pie
import pymysql
from snapshot_phantomjs import snapshot
from pyecharts.render import make_snapshot
import datetime

l_mysql_server="192.168.1.14"
l_user_name="root"
l_password= "123456"
l_db_name="db_admin"
l_port = 3306


sqltext = "select table_schema as dbname,round(sum(data_length+index_length)/1024/1024/1024,2) as 'dbsize' " \
          "from information_schema.tables " \
          "where table_schema not in ('db_admin','sys','information_schema','mysql','performance_schema') " \
          "group by table_schema order by 2 desc limit 10"



def gente_dbsize_image(image_name,title) :
    db = pymysql.connect(host=l_mysql_server, user=l_user_name, password=l_password, database=l_db_name,port=l_port)
    cursor = db.cursor()
    pie_chart = Pie(init_opts=opts.InitOpts(bg_color="#ffffff"))
    data = {"dbname":[], "dbsize":[]}
    l_title = title
    try:
        cursor.execute(sqltext)
        results = cursor.fetchall()
        for row in results:
            data["dbname"].append(row[0])
            data["dbsize"].append(row[1])
        pie_chart.add("", [list(z) for z in zip(data["dbname"], data["dbsize"])])
        pie_chart.set_global_opts(title_opts=opts.TitleOpts(title = l_title,
                                  pos_left="40%",
                                  pos_bottom = "4%",
                                  ##pos_left = 'bottom',
                                  title_textstyle_opts=opts.TextStyleOpts(font_family="Microsoft YaHei",font_size=20)),
                                  legend_opts = opts.LegendOpts(type_="scroll", pos_left="1%", orient='vertical', pos_top="20%",is_show=False),)
        pie_chart.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}",font_size=12))
        ##pie_chart.render("pie_chart02.html")
        make_snapshot(snapshot, pie_chart.render(), "image/" + image_name,output="jpeg")
    except Exception as e:
        print(e)
    db.close()
    return 0

if __name__ == '__main__':
    now_time = datetime.datetime.now()
    yes_time = now_time + datetime.timedelta(days=-1)
    today = datetime.datetime.now().weekday() ##0 表示周一
    image_name = "1_rds_yimiao.jpeg"
    title = "my实例(GB)"
    if today == 2:
        gente_dbsize_image(image_name,title)
复制代码

 

posted @   slnngk  阅读(53)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2023-05-29 markdown日常使用
2019-05-29 DELL R740/720 使用U盘安装Centos7
点击右上角即可分享
微信分享提示