from pyecharts import options as opts
from pyecharts.charts import Graph
from pyecharts.commons.utils import JsCode
nodes_data = [
opts.GraphNode(name="行贿人", value=['a', '行贿人'], symbol_size=60, symbol="roundRect", category=0,
label_opts=opts.LabelOpts(font_size=20,
position="inside",
color="yellow")
),
opts.GraphNode(name="受贿人", value=['b', '受贿人'], symbol_size=60, symbol="roundRect", category=1,#roundRect,
label_opts=opts.LabelOpts(font_size=20,
position="inside",
color="yellow")),
opts.GraphNode(name="张三", value=['c', '张三'], symbol_size=30),
opts.GraphNode(name="李四", value=['f', '李四'], symbol_size=20),
opts.GraphNode(name="王五", value=['g', '王五'], symbol_size=20),
]
links_data = [
{"source": "行贿人", "target": "张三", "value": ['转账:3批\n\t金额:5000元', '行贿人--->张三 :2000元', '行贿人--->张三 :1500元', '行贿人--->张三 :1500元'],
"draggable": "true"},
{"source": "行贿人", "target": "李四", "value": ['转账:3批\n\t金额:3000元', '行贿人--->李四 :1000元', '行贿人--->李四 :500元', '行贿人--->李四 :1500元'],
"draggable": "true"},
{"source": "行贿人", "target": "受贿人", "value": ['转账:1批\n\t金额:500元', '行贿人--->受贿人 :500元'],
"draggable": "true"},
{"source": "张三", "target": "受贿人", "value": ['转账:2批\n\t金额:2000元', '张三--->受贿人 :200元', '张三--->受贿人 :1800元'],
"draggable": "true"},
{"source": "李四", "target": "受贿人", "value": ['转账:2批\n\t金额:4000元', '李四--->受贿人 :2000元', '李四--->受贿人 :2000元'],
"draggable": "true"},
{"source": "李四", "target": "王五", "value": ['转账:1批\n\t金额:1000元', '李四--->王五 :1000元'],
"draggable": "true"},
{"source": "王五", "target": "受贿人", "value": ['转账:1批\n\t金额:1000元', '王五--->受贿人 :1000元'],
"draggable": "true"},
]
edge_label = opts.LabelOpts(
is_show=True,
position="middle",
color="blue",
formatter=JsCode(
"""
function(params){
return params.value[0]
}
"""
)
)
tooltip_opts = opts.TooltipOpts(is_show=True, trigger_on='mousemove|click', axis_pointer_type='cross', formatter=JsCode(
"""function(params){
var str='';
for(var i=0;i<params.value.length;i++){
if(i!=0){
str+=params.value[i]+'<br/>'
}
}
return str
/*if(params.value[0].indexOf('转账:')==0){
return '明细:'+params.value[1]+'<br/>'+params.value[2]+'<br/>'+params.value[3];
}else{
return '学号:'+params.value[0]+'<br/>'+'姓名:'+params.value[1]
}*/
}
"""
))
categories = [
{
"name": "行贿人:0000",
"itemStyle": {
"color": '#0094f7',
"borderColor": "#5182ab",
"borderWidth": 5,
}
},
{
"name": "受贿人:4444",
"itemStyle": {
"color": 'green',
"borderColor": "#5182ab",
"borderWidth": 5
}
},
]
# 节点间连接线样式设置
linestyle_opts = opts.LineStyleOpts(
width=1,
opacity=0.9,
curve=0.2,
type_="dashed", # "solid",
)
graph = Graph()
# label_opts=None------显示标签文本
# graph.add("", nodes, links, repulsion=8000,is_label_show=True)#is_label_show=True表示节点名字为一直显示
graph.add("", nodes_data, links_data, repulsion=8000, edge_symbol=['circle', 'arrow'],
is_draggable=True, edge_label=edge_label, linestyle_opts=linestyle_opts,tooltip_opts=tooltip_opts,categories=categories)
graph.set_global_opts(title_opts=opts.TitleOpts(title="资金流向图"), )
# graph.show_config()#表示将生成的文件打印出来,我们只要结果的HTML不需要知道代码
graph.render(r'./zijinliuxiang.html') # 生成html文件并保存到当前路劲下。
图一:
图二:
图三: