pyecharts模块生成网络关系图

一、需求描述

手里有一份主机间远程访问(SSH、RDP)的访问统计数据,包括源地址、目的地址、登录用户名、访问次数,我希望用pyecharts生成主机间的访问关系图。图表就使用Graph(from pyecharts.charts import Graph)

二、数据处理

1、node(被访问的次数表示节点的大小)

data:192.168.90.149,4

def get_nodes():
    with open("nodes.csv","r") as f:
        node = f.read().splitlines()
    print(node)
    return node
nodes = []
for ips in get_nodes():
    print(ips)
    data = ips.split(",")
    print(data)
    ip = data[0]
    size = data[1]
    node = opts.GraphNode(name=ip,symbol_size=size)
    nodes.append(node)

2、links(原打算ip之间的访问次数表示线条粗细,由于连接次数比较多会导致,画面比较诡异,可以做归一化处理,或者统一设定)

data:192.168.1.54,Administrator,192.17.3.83,1


def get_links():
    with open("links.csv","r") as f:
        links = f.read().splitlines()
    return links
links = []
for link in get_links():
    data = link.split(",")
    #print(data)
    des_ip = data[0]
    user_name = data[1]
    sour_ip = data[2]
    width = data[3]
    node = opts.GraphLink(source=des_ip,
                          target=sour_ip,
                          value=user_name,
                          linestyle_opts=opts.LineStyleOpts(width=width))
    links.append(node)

三、生成关联关系图

c = (
    Graph().add("", nodes, links,
                repulsion=4000,
                edge_label=opts.LabelOpts(is_show=True,position="middle",formatter="{c}"),
                edge_symbol=['circle', 'arrow']
         ).set_global_opts(title_opts=opts.TitleOpts(title="Network-GraphLink")).render("Network-GraphLink.html")
)

其中,edge_symbol=['circle', 'arrow']用于设定连接末端箭头

上一篇:php的加载方式和设计模式


下一篇:Markdown使用