def draw(self):
     nodes_data = []
     sort_people = sorted(self.people.items(),
                          key=lambda item: item[1],
                          reverse=True)
     for key, value in sort_people:
         nodes_data.append(opts.GraphNode(name=key,
                                          symbol_size=value / 80))  # 节点出现次数
     links_data = []
     for key, key_value in self.relationship.items():
         for key2, value in key_value.items():
             links_data.append(
                 opts.GraphLink(source=key, target=key2,
                                value=value))  # 节点之间权值
     c = Graph(init_opts=opts.InitOpts(width="1200px",
                                       height="800px"))  # 调用类
     c.add(
         "",
         nodes_data,  # 关系图节点数据项列表
         links_data,  # 关系图节点间关系数据项列表
         layout="circular",
         repulsion=800,  # 节点之间的斥力因子
         edge_length=100,  # 边的两个节点之间的距离
         linestyle_opts=opts.LineStyleOpts(curve=0.3),  # 关系边的公用线条样式
         label_opts=opts.LabelOpts(position="right"),
     )
     c.set_global_opts(title_opts=opts.TitleOpts(title="人物图谱"))
     c.render("人物图谱.html")
示例#2
0
def drawGraph(nodes, links, name):
    # 图表初始化配置
    init_opts = opts.InitOpts(page_title=name, height="700px")
    g = Graph(init_opts=init_opts)
    # 标题配置
    title = opts.TitleOpts(title=name, pos_left='center')
    # 图例配置
    legend_opts = opts.LegendOpts(pos_top="5%", pos_left="15%")
    # 工具箱配置
    # 工具箱配置
    toolbox_opts = opts.ToolboxOpts()
    g.set_global_opts(
        title_opts=title,
        legend_opts=legend_opts,
        toolbox_opts=toolbox_opts,
    )

    g.add(
        "",
        nodes,
        links,
        repulsion=8000,
        linestyle_opts=opts.LineStyleOpts(curve=0.2),
    )
    g.render("{0}.html".format(name))
    def print_graph(self, init_options, **kwargs):
        nodes_printable = [opts.GraphNode(**n.dict()) for n in self.nodes]
        edges_printable = [opts.GraphLink(**e.dict()) for e in self.edges]

        graph = Graph(init_options)

        graph.add("", nodes_printable, edges_printable, **kwargs)
        graph.render("./Romania.html")
示例#4
0
def DrawWithPye(links, nodes, name):
    graph = Graph(opts.InitOpts(width=g.w_s, height=g.h_s))
    graph.add("Node Name: ",
              nodes,
              links,
              repulsion=g.repul,
              label_opts=opts.LabelOpts(is_show=g.label),
              is_draggable=g.drag)
    graph.set_global_opts(title_opts=opts.TitleOpts(title=name + g.titleend))
    graph.render()
示例#5
0
def draw_graph():
    nodes_for_draw = []
    links_for_draw = []
    for node in default_graph.nodes:
        nodes_for_draw.append({'name': node.name, "symbolSize": 50})
    for node in default_graph.nodes:
        for child in node.children:
            links_for_draw.append({'source': node.name, 'target': child.name})
    graph = Graph(init_opts=opts.InitOpts(width='1800px', height='1000px'))
    graph.set_global_opts(
        title_opts=opts.TitleOpts(title="MatrixSlow Graph"))
    graph.add("", nodes_for_draw, links_for_draw, layout='force',
              repulsion=8000, edge_symbol=['circle', 'arrow'])

    graph.render('./aaa.html')
示例#6
0
class DrawGraph(object):
    def __init__(self, width="1200px", height="800px"):
        self.graph = Graph(
            init_opts=opts.InitOpts(
                width=width,
                height=height,
                bg_color='rgba(255,255,255)'
            )
        ).set_global_opts(
            # 设置显示工具栏
            toolbox_opts=opts.ToolboxOpts(
                feature=opts.ToolBoxFeatureOpts(
                    save_as_image=opts.ToolBoxFeatureSaveAsImageOpts(
                        name='networks',  # 设置工具栏图片保存的文件名
                    ),
                )
            )
        )
        self.line_width = 1

    def setting(self, title=None, line_width: int = None):
        if title:
            self.graph.set_global_opts(title_opts=opts.TitleOpts(title=title), )
        if line_width:
            self.line_width = line_width
        return self

    def exec_draw(self, nodes, links):
        self.graph.add('',
                       nodes,
                       links,
                       repulsion=1000,
                       is_draggable=True,  # 节点是否可拖拽,只在使用力引导布局的时候有用。
                       layout='force',
                       linestyle_opts=opts.LineStyleOpts(width=self.line_width),
                       )
        return self

    def render(self, path):
        return self.graph.render(path)

    def save_img(self, fp=None, page=None):
        if fp is None:
            fp = 'result.png'

        if page is None:
            fp = 'render.html'
        make_snapshot(snapshot, self.graph.render(page), fp)
示例#7
0
def Graph_with_edge_options(nodes_data, links_data):
    Graph_tuopu = Graph()
    ## 配置大小
    Graph_tuopu.__init__(
        init_opts=opts.InitOpts(width="1100px", height="1500px"))
    Graph_tuopu.add(
        "",
        nodes_data,
        links_data,
        repulsion=4000,
        # edge_label=opts.LabelOpts(
        #     is_show=True, position="middle", formatter="{b} 的数据 {c}"
        # ),
        itemstyle_opts=opts.ItemStyleOpts(color="gray"))
    Graph_tuopu.set_global_opts(title_opts=opts.TitleOpts(
        title="topology_edges_node.json"))
    Graph_tuopu.render("graph_with_edge_options.html")
    webbrowser.open("../../Game/suning2020/graph_with_edge_options.html")
    print("OK")
示例#8
0
def echarts_from_nx(nx: nx.DiGraph, output_path, title):
    """
    对networkx图使用echarts进行可视化(仅供调试时使用,正式的可视化不在这)
    :param nx: networkx图实例
    :param output_path: 输出的HTML文件目录
    :param title: 图表的标题
    :return: 无
    """

    nodes = []
    for node in nx.nodes:
        nodes.append({'name': node, 'symbolSize': 40})

    links = []
    for u, v in nx.edges:
        links.append({'source': u, 'target': v})

    graph = Graph(global_options.InitOpts(width='1920px', height='1080px'))
    graph.add(title,
              nodes,
              links,
              repulsion=8000,
              edge_symbol=['circle', 'arrow'])
    graph.render(output_path)
示例#9
0
    node_in_graph.append(opts.GraphNode(
            name=one_line_list[0], 
            value=int(one_line_list[1]), 
            symbol_size=int(one_line_list[1])/20,  # 手动调整节点的尺寸
            category=int(one_line_list[2])))  # 类别,例如categories[2]=='蜀'
#print('-'*20)  # 测试点
link_in_graph = []
for one_line in link_line_list:
    one_line = one_line.strip('\n')
    one_line_list = one_line.split(',')
    #print(one_line_list)  # 测试点
    link_in_graph.append(opts.GraphLink(
            source=one_line_list[0], 
            target=one_line_list[1], 
            value=int(one_line_list[2])))


##--- 第3步:画图
c = Graph(init_opts=opts.InitOpts(page_title='关系图-仿生人会梦见电子羊吗?',width='1400px',height='700px'))
c.add("", 
      node_in_graph, 
      link_in_graph, 
      edge_length=[10,400], 
      repulsion=100,
      categories=categories, 
      is_draggable = True,
#      linestyle_opts=opts.LineStyleOpts(curve=0.2),  # 增加连线弧度
      layout="force",  # "force"-力引导布局,"circular"-环形布局
      )
c.set_global_opts(title_opts=opts.TitleOpts(title="关系图-仿生人会梦见电子羊吗?"))
c.render(out_file_name)
示例#10
0
    "symbolSize": 5
}, {
    "name": "N6",
    "symbolSize": 4
}, {
    "name": "N7",
    "symbolSize": 3
}]
_node_names = [i.get("name") for i in nodes]
links = [{
    "source": i,
    "target": j,
    "value": 10
} for i, j in itl.product(_node_names, _node_names)]
graph = Graph()
graph.add("", nodes, links)
graph.render_notebook()

# %% [markdown]
# ### KLine -- K线图

with open("data/candlestick.json", encoding="utf8") as j:
    ori_data = json.load(j)
    data = {
        "data": [rec[1:] for rec in ori_data],
        "times": [rec[0] for rec in ori_data],
        "vols": [int(rec[5]) for rec in ori_data],
        "macds": [rec[7] for rec in ori_data],
        "difs": [rec[8] for rec in ori_data],
        "deas": [rec[9] for rec in ori_data]
    }
示例#11
0
    one_line = one_line.strip('\n')
    one_line_list = one_line.split(',')
    #print(one_line_list)  # 测试点
    node_in_graph.append(
        opts.GraphNode(name=one_line_list[0],
                       value=int(one_line_list[1]),
                       symbol_size=int(one_line_list[1]) / 20))  # 手动调整节点的尺寸
#print('-'*20)  # 测试点
link_in_graph = []
for one_line in link_line_list:
    one_line = one_line.strip('\n')
    one_line_list = one_line.split(',')
    print(one_line_list)  # 测试点
    link_in_graph.append(
        opts.GraphLink(source=one_line_list[0],
                       target=one_line_list[1],
                       value=int(one_line_list[2])))

##--- 画图
c = Graph()
c.add(
    "",
    node_in_graph,
    link_in_graph,
    edge_length=[10, 50],
    repulsion=5000,
    layout="force",  # "force"-力引导布局,"circular"-环形布局
)
c.set_global_opts(title_opts=opts.TitleOpts(title="关系图-红楼梦人物"))
c.render(out_file_name)