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)
)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)