#关系可视化
from pyecharts import options as opts
from pyecharts.charts import Graph
from pyecharts.globals import ThemeTypeimport webbrowser
#---------------------------------------#主要设置
#InitOpts:初始化配置项(在图形创建开始时即可设置)
init_opts=opts.InitOpts(width="100%", #图宽height="900px", #图高renderer="canvas", #渲染模式 svg 或 canvas,即 RenderType.CANVAS 或 RenderType.SVGpage_title="Pyecharts Graph关系图", #网页标题theme=ThemeType.DARK, #主题风格可选:WHITE,LIGHT,DARK,CHALK,ESSOS,INFOGRAPHIC,MACARONS,PURPLE_PASSION,ROMA,ROMANTIC,SHINE,VINTAGE,WALDEN,WESTEROS,WONDERLAND#bg_color="#333333", #背景颜色js_host="" #js主服务位置 留空则默认官方远程主服务)#label_opts:节点显示文字样式设置
#formatter结合rich可以设置丰富的文本样式 类似CSS
label_opts=opts.LabelOpts(color="#008080",distance=0,font_size=16,font_weight="bold",formatter="{a}\n{x| {b} }\n{t| {c} }", #{a}, {b},{c}...,分别表示系列名,数据名,数据值等rich={"x":{"color":"pink","backgroundColor":"#008080","borderRadius":8,"fontSize":20},"t":{"color":"yellow"}}, #像CSS类似设置) #ToolboxOpts:工具栏配置(可实现图片保存等功能)
toolbox_opts=opts.ToolboxOpts(is_show=True, #是否显示工具栏orient="vertical", #工具栏工具摆放方向pos_left="right") #工具栏左边位置#TooltipOpts:提示文字显示设置
tooltip_opts=opts.TooltipOpts(formatter="{a}<br/>{b}<br/>{c}",background_color="rgba(255,255,255,0.3)",border_color="yellow",border_width=2,)#★★★★★
#通过opts.ItemStyleOpts,设置node节点样式(颜色、大小、透明度)
itemstyle_opts=opts.ItemStyleOpts(color="orange", #节点颜色border_color="red", #节点边线颜色border_width=1, #节点边线宽度opacity=0.9, #节点透明度)#节点间连接线样式设置
linestyle_opts=opts.LineStyleOpts(is_show=True,width=1,opacity=0.6,curve=0.3,type_="solid", color="red", )
#---------------------------------------
#生成大量节点
nodes=[]
for i in range(20):#node={"name":"节点"+str(i),"symbolsize":20}node=opts.GraphNode(name="节点"+str(i), #节点名称 ★ 不能有重名!value="节点值"+str(i), #节点值symbol_size=10+i, #节点大小symbol="image://cat_brown.svg", #★★★★★ 节点样式可选:'circle', 'rect', 'roundRect', 'triangle', 'diamond', 'pin', 'arrow', 'none', 'image://url'label_opts=label_opts,)nodes.append(node)#连接所有节点
links=[]
for i in nodes:for j in nodes: source_node_name=i.get("name")target_node_name=j.get("name") links.append({"source":source_node_name,"target":target_node_name,"lineStyle":linestyle_opts})#生成图像并渲染输出
g=(Graph(init_opts).add("系列节点", nodes, links, repulsion=8000,itemstyle_opts=itemstyle_opts,is_draggable=True).set_global_opts(title_opts=opts.TitleOpts(title="Pyecharts Graph 示例"),toolbox_opts=toolbox_opts,tooltip_opts=tooltip_opts).render("graph.html")
)#---------------------------------------#---------------------------------------from flask import Flask,g,request,render_template,Response,stream_with_context
import threading
import time#---------------------------------------
app=Flask(__name__,static_url_path='',template_folder='',static_folder='')@app.route('/')
def index():return render_template("graph.html")#---------------------------------------def run_app():print("应用启动")if __name__ == '__main__':app.run("0.0.0.0")def open_browser():time.sleep(1)print("打开浏览器")webbrowser.open("http://localhost:5000")def main():#主要程序# 创建线程thread1 = threading.Thread(target=open_browser)thread2 = threading.Thread(target=run_app)# 启动线程thread1.start()thread2.start()if __name__ == '__main__':main()