pyecharts 树状图可视化

pyecharts 树状图可视化,第1张

pyecharts 树状可视化

import json
import os
import pymysql
import random
from pyecharts import options as opts
from pyecharts.charts import Graph

class mysql_func():
def get_data(db, sql, data=None):
try:
curors = db.cursor()
except:
db.ping(reconnect=True)
curors = db.cursor()
if data == None:
curors.execute(sql)
else:
curors.executemany(sql, data)
db.commit()
data = curors.fetchall()
curors.close()
return data

def get_row_number(db, sql, data=None, type=1):
    try:
        curors = db.cursor()
    except:
        db.ping(reconnect=True)
        curors = db.cursor()
    if data is not None and type == 1:
        curors.executemany(sql, data)
    elif data is None:
        curors.execute(sql)
    else:
        curors.execute(sql, data)
    db.commit()
    data = curors.rowcount
    curors.close()
    # db.close()
    return data

def get_json(db, sql, data=None):
    try:
        curors = db.cursor()
    except:
        db.ping(reconnect=True)
        curors = db.cursor()
    if data == None:
        curors.execute(sql)
    else:
        curors.executemany(sql, data)
    db.commit()
    columns = [col[0] for col in curors.description]

    def dict_fetchall(cursor):
        "Return all rows from a cursor as a dict"
        columns = [col[0] for col in cursor.description]
        return [
            dict(zip(columns, row))
            for row in cursor.fetchall()
        ]

    data = dict_fetchall(curors)
    curors.close()
    return data

db = pymysql.connect(host=’’,user=’’,password=’’,database=’’)

sql = ‘’’’’’
data = mysql_func.get_data(db,sql)
nodelist=[]
linklist=[]
hz=0

for item in data:
if item[2]>2:
xx=item[3]+";"+str(item[2])
hezuoze={‘id’:str(item[1]) , ‘name’:xx , ‘symbo’: item[2], ‘x’: random.randint(-100,200), ‘y’: random.randint(-100,200), ‘value’: item[2], ‘label’: {‘normal’: {‘show’: True}}, ‘cat’: 1}
hzsum+=item[2]
nodelist.append(hezuoze)
linklist.append({“source”:str(item[1]) ,“target”:“1” })
target={‘id’:“1” , ‘name’: ‘Wei, ll’, ‘symbo’: hz, ‘x’: 80, ‘y’: 300, ‘value’:hz , ‘label’: {‘normal’: {‘show’: True}}, ‘cat’: 0}
nodelist.append(target)

base_dir=os.path.dirname(os.path.abspath(file))
filename=os.path.join(base_dir,input(“请输入文件名称:”)+’.html’)

c = (
Graph(init_opts=opts.InitOpts(width=“1000px”, height=“600px”))
.add(
“”,
nodes=nodelist,
links=linklist,
categories= [{ “name”: “” }, { “name”: “”}],#自己输入自己名称
#layout 设置输出图形的样式 circular为圆形,none为默认输出,
# 参考https://echarts.apache.org/examples/zh/editor.html?c=graph-circular-layout
# https://echarts.apache.org/examples/zh/editor.html?c=graph
layout=“circular”,
# layout=“none”,
is_rotate_label=True,
linestyle_opts=opts.LineStyleOpts(color=“source”, curve=0.3),
label_opts=opts.LabelOpts(position=“right”),
)
.set_global_opts(
title_opts=opts.TitleOpts(title=“Graph-魏”),
legend_opts=opts.LegendOpts(orient=“vertical”, pos_left=“2%”, pos_top=“20%”),
)
.render(filename)
)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5653045.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存