Esempio n. 1
0
def test_graph_draggable_and_symbol_size(fake_writer):
    nodes = [
        {
            "name": "结点1",
            "symbolSize": 10
        },
        {
            "name": "结点2",
            "symbolSize": 20
        },
        {
            "name": "结点3",
            "symbolSize": 30
        },
        {
            "name": "结点4",
            "symbolSize": 40
        },
    ]
    links = []
    for i in nodes:
        for j in nodes:
            links.append({"source": i.get("name"), "target": j.get("name")})
    c = Graph().add("",
                    nodes,
                    links,
                    repulsion=8000,
                    is_draggable=True,
                    symbol_size=50)
    c.render()
    _, content = fake_writer.call_args[0]
    assert_in("draggable", content)
    assert_in("symbolSize", content)
Esempio n. 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))
Esempio n. 3
0
def test_graph_base(fake_writer):
    nodes = [
        {
            "name": "结点1",
            "symbolSize": 10
        },
        {
            "name": "结点2",
            "symbolSize": 20
        },
        {
            "name": "结点3",
            "symbolSize": 30
        },
        {
            "name": "结点4",
            "symbolSize": 40
        },
    ]
    links = []
    for i in nodes:
        for j in nodes:
            links.append({"source": i.get("name"), "target": j.get("name")})
    c = Graph().add("", nodes, links, repulsion=8000)
    c.render()
    _, content = fake_writer.call_args[0]
    assert_equal(c.theme, "white")
    assert_equal(c.renderer, "canvas")
 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")
Esempio n. 5
0
def test_graph_edge_label_opts(fake_writer):
    nodes = [
        {
            "name": "结点1",
            "symbolSize": 10
        },
        {
            "name": "结点2",
            "symbolSize": 20
        },
        {
            "name": "结点3",
            "symbolSize": 30
        },
        {
            "name": "结点4",
            "symbolSize": 40
        },
    ]
    links = []
    for i in nodes:
        for j in nodes:
            links.append({"source": i.get("name"), "target": j.get("name")})
    c = Graph().add("",
                    nodes,
                    links,
                    repulsion=4000,
                    edge_label=opts.LabelOpts(is_show=True))
    c.render()
    _, content = fake_writer.call_args[0]
    assert_in("edgeLabel", content)
    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")
Esempio n. 7
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()
Esempio n. 8
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')
Esempio n. 9
0
def test_graph_base():
    nodes = [
        {"name": "结点1", "symbolSize": 10},
        {"name": "结点2", "symbolSize": 20},
        {"name": "结点3", "symbolSize": 30},
        {"name": "结点4", "symbolSize": 40},
    ]
    links = []
    for i in nodes:
        for j in nodes:
            links.append({"source": i.get("name"), "target": j.get("name")})
    c = Graph().add("", nodes, links, repulsion=8000)
    assert c.theme == "white"
    assert c.renderer == "canvas"
    c.render("render.html")
Esempio n. 10
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)
Esempio n. 11
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")
Esempio n. 12
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)
Esempio n. 13
0
            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)