Exemple #1
0
def drawGraphRelation():
    from pyecharts import Graph

    nodes = [{
        "name": "结点1",
        "symbolSize": 10
    }, {
        "name": "结点2",
        "symbolSize": 20
    }, {
        "name": "结点3",
        "symbolSize": 30
    }, {
        "name": "结点4",
        "symbolSize": 40
    }, {
        "name": "结点5",
        "symbolSize": 50
    }, {
        "name": "结点6",
        "symbolSize": 40
    }, {
        "name": "结点7",
        "symbolSize": 30
    }, {
        "name": "结点8",
        "symbolSize": 20
    }]
    links = []
    for i in nodes:
        for j in nodes:
            links.append({"source": i.get('name'), "target": j.get('name')})
    graph = Graph("关系图-力引导布局示例")
    graph.add("", nodes, links, repulsion=8000)
    graph.render()
Exemple #2
0
def plot_chnx_sample():
    df = pd.read_csv(DATA_URL + '/fanti.edgelist',
                     sep='\t',
                     header=None,
                     names=['ch1', 'ch2'])
    df = df.head(100)
    df.drop_duplicates(keep='first', inplace=True)

    nodes = [{
        "name": ch,
        "symbolSize": 10
    } for ch in set(df['ch1'].unique()) | set(df['ch2'].unique())]
    links = [{"source": value[0], "target": value[1]} for value in df.values]
    graph = Graph("汉字-样例关系图", width=1200, height=600)
    graph.add("",
              nodes,
              links,
              repulsion=80,
              layout=None,
              graph_edge_length=150,
              is_roam=True,
              is_label_show=True,
              is_legend_show=True,
              draggable=False,
              lineStyle='-.',
              graph_edge_symbol=['circle', 'arrow'])
    graph.render()
    context = dict(myechart=graph.render_embed(),
                   host='/static/js',
                   script_list=graph.get_js_dependencies())
    return context
Exemple #3
0
def bulild_html(result, action, disease):
    links = []
    nodes = []
    try:
        for triples in result:
            if 'drug' in triples.keys():
                s = "drug:" + triples['drug']['value'].split('/')[-1]
                p = "action:" + action
                o = "gene:" + triples['gene']['value'].split('/')[-1]
                pp = "action:" + "cure"
                oo = disease
            else:
                s = "disease:" + triples['disease']['value'].split('/')[-1]
                p = "action:" + "relate_to"
                o = "gene:" + triples['gene']['value'].split('/')[-1]
                pp = "action:" + "cause"
                oo = "mutation:" + triples['mutation']['value'].split('/')[-1]
            mm = {"name": s, "symbolSize": 20}
            nn = {"name": o, "symbolSize": 20}
            kk = {"name": oo, "symbolSize": 20}
            if mm not in nodes:
                nodes.append(mm)
            if nn not in nodes:
                nodes.append(nn)
            if kk not in nodes:
                nodes.append(kk)
            links.append({"source": s, "value": p, "target": o})
            links.append({"source": o, "value": pp, "target": oo})
        graph = Graph("基因-疾病-药物")
        graph.add("", nodes, links, graph_repulsion=400)
        graph.render(
            path=r"E:\KG_DEMO\AGAC_KGQA_PART\kgqa\templates\kgqa\render.html")
    except TypeError:
        pass
Exemple #4
0
class Mutdraw():
    def __init__(self):
        self.attr = [
            "ASD", "SCZ", "BPD", "DD", "DEE", "EE", "ID", "OCD", "TS", "CHD",
            "control"
        ]
        self.v1 = [
            173712, 1038, 69, 8336, 288, 580, 1432, 26, 441, 1900, 51093
        ]
        self.attr2 = ["ASD", "SCZ", "BPD", "ID", "control"]
        self.v2 = [486, 66, 25, 81, 121]
        self.attr3 = ["ASD", "SCZ", "BPD", "MDD", "ADHD"]
        self.v3 = [292, 641, 84, 36, 35]
        self.pie1 = Pie("DNM",
                        width=500,
                        height=300,
                        title_pos="center",
                        title_top="bottom")
        self.pie2 = Pie("CNV",
                        width=500,
                        height=300,
                        title_pos="center",
                        title_top="bottom")
        self.pie1.add("",
                      self.attr,
                      self.v1,
                      legend_pos="left",
                      legend_text_size=8,
                      is_toolbox_show=False)
        self.pie2.add("",
                      self.attr2,
                      self.v2,
                      legend_pos="left",
                      legend_text_size=8,
                      is_toolbox_show=False)
        self.nodes = [{"name": "x"}, {"name": "y"}, {"name": "z"}]
        self.links = [{
            "source": "x",
            "target": "y"
        }, {
            "source": "x",
            "target": "z"
        }]
        self.graph = Graph("Graph", "PPI")
        self.graph.add("Pt", self.nodes, self.links)
        """

		self.grid=Grid()
		self.grid.add(self.pie1,grid_left="70%")
		self.grid.add(self.pie2,grid_right="70%")
		"""

    def drawPie(self):
        return self.pie1.render_embed()

    def drawPie2(self):
        return self.pie2.render_embed()

    def drawGraph(self):
        return self.graph.render_embed()
Exemple #5
0
def test_graph_force_layout():
    links = []
    for i in nodes:
        for j in nodes:
            links.append({"source": i.get('name'), "target": j.get('name')})
    graph = Graph("关系图-力引导布局示例")
    graph.add("", nodes, links, repulsion=8000, line_color='#aaa')
    graph.render()
Exemple #6
0
def test_graph_force_layout():
    links = []
    for i in nodes:
        for j in nodes:
            links.append({"source": i.get('name'), "target": j.get('name')})
    graph = Graph("关系图-力引导布局示例")
    graph.add("", nodes, links, repulsion=8000, line_color='#aaa')
    graph.render()
Exemple #7
0
    def mst(self, data=None, dataN=None, dataname="None", choice="prim"):
        page = Page()
        if data == None:
            max_value = 9999999
            row0 = [0, 7, max_value, max_value, max_value, 5]
            row1 = [7, 0, 9, max_value, 3, max_value]
            row2 = [max_value, 9, 0, 6, max_value, max_value]
            row3 = [max_value, max_value, 6, 0, 8, 10]
            row4 = [max_value, 3, max_value, 8, 0, 4]
            row5 = [5, max_value, max_value, 10, 4, 0]
            data = [row0, row1, row2, row3, row4, row5]
            dataN = ["节点1", "节点2", "节点3", "节点4", "节点5", "节点6"]
        #对初始数据可视化
        link = []
        node = []
        n = len(data)
        m = len(data[0])
        for i in range(n):
            for j in range(m):
                if data[i][j] == max_value:
                    continue
                else:
                    link.append({"source": dataN[i], "target": dataN[j]})
            fdata = filter(lambda x: x != max_value, data[i])
            big = reduce(lambda x, y: x + y, fdata)
            node.append({"name": dataN[i], "symbolSize": big})

        tu_graph = Tu_Graph("总关系图")
        tu_graph.add("", node, link)
        page.add(tu_graph)

        graph = tree.Graph(data)

        if choice == "prim":
            res = graph.prim()
        else:
            res = graph.kruskal()
        print(res)

        n1 = len(res)
        m1 = len(res[0])

        def sum2(x, y):
            if type(x) == int:
                return x + y[2]
            else:
                return x[2] + y[2]

        big = reduce(sum2, res)

        link2 = []
        for i in res:
            link2.append({"source": i[0], "target": i[1]})
        tu_graph2 = Tu_Graph("最小生成树图")
        tu_graph2.add("权重和为:" + str(big), node, link2)
        print(link2)
        page.add(tu_graph2)
        sh.save_tu_helper(page, dataname)
Exemple #8
0
def test_graph_circular_layout():
    links = []
    for i in nodes:
        for j in nodes:
            links.append({"source": i.get('name'), "target": j.get('name')})
    graph = Graph("关系图-环形布局示例")
    graph.add("", nodes, links, is_label_show=True, graph_repulsion=8000,
              graph_layout='circular', label_text_color=None)
    graph.render()
Exemple #9
0
def guanxitu():
    '''显示微博转发关系图'''
    with open("./jsonweibo.json", "r", encoding="utf-8") as f:
        j = json.load(f)
    nodes, links, categories, cont, mid, userl = j
    graph = Graph("微博转发关系图", width=1200, height=600)
    graph.add("", nodes, links, categories, label_pos="right", repulsion=50, is_legend_show=False, line_curve=0.2,
              label_text_color=None)
    # graph.show_config()
    graph.render()
Exemple #10
0
def test_graph_official_data():
    import json
    if PY2:
        import codecs
        with codecs.open(os.path.join("..", "json", "weibo.json"), "rb") as f:
            j = json.load(f)
    else:
        with open(os.path.join("..", "json", "weibo.json"),
                  "r", encoding="utf-8") as f:
            j = json.load(f)
    nodes, links, categories, cont, mid, _ = j
    graph = Graph("微博转发关系图", width=1200, height=600)
    graph.add("", nodes, links, categories, label_pos="right",
              graph_repulsion=50, is_legend_show=False,
              line_curve=0.2, label_text_color=None)
    graph.render()
Exemple #11
0
def drawGraphRelation1():
    from pyecharts import Graph

    nodes = [{
        "name": "结点1",
        "symbolSize": 10
    }, {
        "name": "结点2",
        "symbolSize": 20
    }, {
        "name": "结点3",
        "symbolSize": 30
    }, {
        "name": "结点4",
        "symbolSize": 40
    }, {
        "name": "结点5",
        "symbolSize": 50
    }, {
        "name": "结点6",
        "symbolSize": 40
    }, {
        "name": "结点7",
        "symbolSize": 30
    }, {
        "name": "结点8",
        "symbolSize": 20
    }]
    links = []
    for i in nodes:
        for j in nodes:
            links.append({"source": i.get('name'), "target": j.get('name')})
    graph = Graph("关系图-环形布局示例")
    graph.add(
        "",
        nodes,
        links,
        is_label_show=True,
        graph_repulsion=8000,
        graph_layout="circular",
        label_text_color=None,
    )
    graph.render()
Exemple #12
0
 def get(self, request, *args, **kwargs):
     from py2neo import Graph as neo4j
     template_name = 'echart/echart.html'
     echart = Graph("关系图谱")
     graph = neo4j('http://192.168.134.4:7474',
                   username='******',
                   password='******')
     data1 = graph.run(
         "match(n1:Actor{name:'沈腾'})-[r1]->(m1:Movie) return n1.name,type(r1),m1.name;"
     ).to_table()
     data2 = graph.run(
         "match(n1:Actor{name:'沈腾'})-[r1]->(m1:Movie)<-[r2]-(n2:Actor) return n2.name,type(r2),m1.name;"
     ).to_table()
     nodes = [
         {
             "name": "沈腾",
             "symbolSize": 10,
             "category": 1
         },
     ]
     links = []
     for d in data1:
         nodes.append({"name": d[2], "symbolSize": 20, "category": 0})
         links.append({"source": d[0], "target": d[2]})
     for d in data2:
         nodes.append({"name": d[0], "symbolSize": 10, "category": 1})
         links.append({"source": d[0], "target": d[2]})
     categories = ["电影", "演员"]
     # print(nodes)
     # print(links)
     echart.add("测试",
                nodes,
                links,
                categories=categories,
                is_label_show=True,
                is_focusnode=True,
                is_roam=True,
                is_rotatelabel=True,
                graph_repulsion=150)
     context = dict(myechart=echart.render_embed(),
                    host=REMOTE_HOST,
                    script_list=echart.get_js_dependencies())
     return render(request, template_name, context)
Exemple #13
0
def ego_network(author , ego_dic):
    #ego_dic = load_pickle(dic_dir)
    nodes = [{'name':author, 'symbolSize':auscore[author][0]*10**6}]
    links = []
    #print(ego_dic[author].items())
    for item in ego_dic[author].items():
        if item[0] not in auscore.keys():
            nodes.append({'name': item[0], 'symbolSize': 5})
            links.append({"source": author, "target": item[0], 'value': item[1]})
        else:
            nodes.append({'name': item[0],  'symbolSize': auscore[item[0]][0]*10**6})
            links.append({"source": author, "target": item[0], 'value': item[1]})

    graph = Graph("Ego-network of %s"%author, width= 900, height = 800)
    graph.add("", nodes, links,
              is_label_show=True,label_text_color='g',
              repulsion=8000, is_focusnode=True, is_rotatelabel=True, is_roam=True,
              tooltip_formatter=formatter)
    graph.on(events.MOUSE_CLICK, on_click)
    graph.render(path='./Ego-nw-html/'+author+".html")
    return ego_dic[author]
Exemple #14
0
 def graph(self):
     V = [{'name': code2name[c]} for c in self.codes]
     E = []
     for i in range(len(self.codes)):
         for j in range(i + 1, len(self.codes)):
             if abs(self.mat[i, j]) >= 0.7:
                 E.append({
                     'source': i,
                     'target': j,
                     'value': round(self.mat[i, j], 3)
                 })
     graph = Graph(width=1000, height=618)
     graph.add("",
               V,
               E,
               graph_layout='force',
               graph_repulsion=1000,
               line_curve=0.2,
               is_label_show=True)
     return graph
     pass
def draw_graph():
    nodes = [{
        "name": "结点1",
        "symbolSize": 10
    }, {
        "name": "结点2",
        "symbolSize": 20
    }, {
        "name": "结点3",
        "symbolSize": 30
    }, {
        "name": "结点4",
        "symbolSize": 40
    }, {
        "name": "结点5",
        "symbolSize": 50
    }, {
        "name": "结点6",
        "symbolSize": 40
    }, {
        "name": "结点7",
        "symbolSize": 30
    }, {
        "name": "结点8",
        "symbolSize": 20
    }]
    links = []
    for i in nodes:
        for j in nodes:
            links.append({"source": i.get('name'), "target": j.get('name')})
    graph = Graph("关系图-环形布局示例")
    graph.add("",
              nodes,
              links,
              is_label_show=True,
              repulsion=8000,
              layout='circular',
              label_text_color=None)
    graph.show_config()
    graph.render()
def echartshow(nodes, links, execlpath, Preservationpath, flogg=False):
    if flogg:
        partstr = '有收付标志'
    else:
        partstr = '无收付标志'

    graph = Graph(os.path.basename(execlpath).split('.')[0] + partstr + "图例",
                  width=1920,
                  height=1080)
    graph.add("",
              nodes,
              links,
              is_label_show=False,
              repulsion=50,
              is_focusnode=True,
              is_roam=True,
              graph_layout='force',
              line_color="rgba(50,50,50,0.7)",
              graph_edge_symbol=['circle', 'arrow'])
    graph.render(
        (str(Preservationpath) + os.path.basename(execlpath).split('.')[0] +
         partstr + '.html'))


# if __name__ == "__main__":
#     execlpath = r'账户交易明细表.xls'
#     execlpath1=r'账户交易明细表副本.xlsx'
#     Preservationpath = r'result/'
#     sourceaccount = '交易账卡号'
#     targetaccount = '对手账号'
#     value = '交易金额'
#     label = '收付标志'
#     #xls
#     execlfileVisual(execlpath, Preservationpath, sourceaccount, targetaccount, value, label)
#     execlfileVisual(execlpath, Preservationpath, sourceaccount, targetaccount, value)
#     # #xlsx
#     execlfileVisual(execlpath1, Preservationpath, sourceaccount, targetaccount, value, label)
#     execlfileVisual(execlpath1, Preservationpath, sourceaccount, targetaccount, value)
Exemple #17
0
    def graph(self, positive_threshold=0.65, nagetive_threshold=-0.65):
        '''
		require: 正确初始化,0<=positive_threshold<=1 and -1<=nagetive_threshold<=0
		function: 根据matrix,绘制echarts.Graph实例并返回
		modified: null
		'''
        graph = Graph('{0}~{1}'.format(self.begin, self.end),
                      width=900,
                      height=700)
        exchanges = ['shfe', 'dce', 'czce']
        nodes = [{
            'name': code2name[x],
            'category': exchanges.index(code2exchange[x])
        } for x in self.commodities]
        edges = []
        n = len(self.commodities)
        for i in range(n):
            for j in range(i + 1, n):
                if self.matrix[i, j] >= positive_threshold or self.matrix[
                        i, j] <= nagetive_threshold:
                    edges.append({
                        'source': i,
                        'target': j,
                        'value': round(self.matrix[i, j], 3)
                    })
        graph.add('',
                  nodes,
                  edges,
                  exchanges,
                  layout="force",
                  repulsion=8000,
                  is_label_show=True,
                  label_text_size=14,
                  line_curve=0.2,
                  label_text_color=None,
                  label_emphasis_textcolor=None,
                  label_emphasis_textsize=14)
        return graph
Exemple #18
0
def get_graph(df):
    """
    根据发现结果df生成关系图

    :param df: df
    :return:
    """
    print('get_graph')
    links = []
    exist_nodes = {}

    for i in df.index:
        source = df.loc[i, 'ip1']
        target = df.loc[i, 'ip2']
        if source not in exist_nodes:
            exist_nodes[source] = []
        if target not in exist_nodes:
            exist_nodes[target] = []
        exist_nodes[source] = int(df.loc[i, 'community_tag1'])
        exist_nodes[target] = int(df.loc[i, 'community_tag2'])
        links.append({'source': source, 'target': target})
    nodes = []
    categories = []

    for node, cat in exist_nodes.items():
        #     # 取众数
        #     cat = Counter(cato_list).most_common(1)[0][0]
        nodes.append({'name': node, 'symbolSize': 10, 'value': cat})
        categories.append(cat)
    categories = LabelEncoder().fit_transform(categories)
    for node, cat in zip(nodes, categories):
        node['category'] = cat
    g = Graph(title="拓扑结构",
              subtitle='IP:{} Links:{}'.format(len(nodes), len(links)),
              width=1600,
              height=1024)
    g.add("", nodes, links, categories=list(categories))
    return g
Exemple #19
0
 def draw_network(self, layout="force"):
     """将网络可视化输出"""
     if self.scale > 10000:  # 对于太大的网络,可视化意义不大且效率极低,故只对小型网络进行可视化
         print("网络规模太大(" + str(self.scale) + "),不支持可视化!请抽取更小的子网络进行可视化")
         return
     g = self.network
     partition = community.best_partition(g)
     if layout.lower() != 'force' and layout.lower() != 'circular':
         raise ValueError("没有这种布局!布局请选择force或circular")
     # 获取节点名称映射
     names = nx.get_node_attributes(g, 'name')
     nodes = [{
         'name':
         names[n],
         'symbolSize':
         math.log2(nx.degree(g, n, weight=self.weight_type) + 1),
         'category':
         partition[n]
     } for n in g.nodes()]
     links = [{
         'source': names[e[0]],
         'target': names[e[1]],
         'value': e[2][self.weight_type]
     } for e in g.edges]
     graph = Graph(self.net_type, width=1200, height=750)
     graph.add(
         "",
         nodes,
         links,
         categories=list(set(partition.values())),
         label_pos="right",
         graph_repulsion=50,
         graph_layout=layout,
         is_legend_show=False,
         line_curve=0.2,
         label_text_color=None,
     )
     graph.render(self.net_type + '.html')
Exemple #20
0
 def visualize(self,rangee,com_name):
     #可视化为名为render的html文件
     nodes=[]
     for n in self.SnapGraph.Nodes():
         if n.GetId() in rangee:
             size=np.sqrt(n.GetDeg())
             if n.GetId() in self.topnlist:
                 size=20
             nodes.append({"name":n.GetId(),
                           "symbolSize":size,
                           "category":self.ComStructDict[com_name][n.GetId()]})
             
     links=[]
     for n in self.SnapGraph.Nodes():
         if n.GetId() in rangee:
             for j in n.GetOutEdges():
                 if j in rangee:
                     links.append({"source": n.GetId(), "target": j})
     
     graph = Graph("try")
     graph.add("", nodes, links,categories=self.categories[com_name], repulsion=50,line_curve=0.2,
               graph_edge_symbol= ['circle', 'arrow'])
     graph.render()
Exemple #21
0
def graph_show(edge_list):
    nodes = []
    links = []
    nodes_list = []
    w_min = sys.float_info.max
    w_max = 0
    for edge in edge_list:
        u, v, w = edge.getLeft(), edge.getRight(), edge.getWeight()

        if w < w_min:
            w_min = w
        if w > w_max:
            w_max = w

        if u not in nodes_list:
            nodes_list.append(u)
            nodes.append({"name": str(u), "symbolSize": 10})

        if v not in nodes_list:
            nodes_list.append(v)
            nodes.append({"name": str(v), "symbolSize": 10})

        links.append({"source": str(u), "target": str(v), "value": w})

    print("min w:", w_min)
    print("max w:", w_max)
    graph = Graph("Minimum Spanning Tree")
    graph.add("",
              nodes,
              links,
              graph_layout='force',
              graph_edge_length=[w_min, w_max],
              graph_gravity=0,
              graph_repulsion=0,
              is_label_show=True)
    graph.show_config()
    graph.render()
Exemple #22
0
def gen_graph(csv_file):
    dc = read_network(csv_file)
    max_degree = max(dc.values())
    nodes = [node for node in dc.keys() if dc[node] != 1]
    links = []
    with open(csv_file, 'r') as f:
        f_csv = csv.reader(f)
        for row in f_csv:
            source, target = row
            if source not in nodes or target not in nodes:
                continue
            else:
                link = {'source': source, 'target': target}
                links.append(link)
    nodes = [{
        'name': node,
        'symbolSize': math.log10(dc[node]) / math.log10(max_degree) * 20,
        'value': dc[node]
    } for node in nodes]

    graph = Graph(title='微博传播网络图', width=1080, height=800)

    graph.add('', nodes, links, is_label_show=True, line_curve=0.2)
    graph.render()
def draw_relation_2():
    sql = "select distinct(fid),`name`,relation_gen from renren_user_info where relation_gen < 2"
    nodes, categories = db_pool.execute_query_sql(sql, fetch_user_data)

    r_sql = """select r.fid,r.tid from renren.renren_relation r where tid in (%s)"""
    s = ''
    for node in nodes:
        s += "'" + node['fid'] + "',"

    links = db_pool.execute_query_sql(r_sql % s[0:len(s) - 1],
                                      fetch_relation_data)
    graph = Graph("人人好友关系", width=1200, height=600)
    graph.add(
        "",
        nodes,
        links,
        categories,
        label_pos="right",
        graph_repulsion=50,
        is_legend_show=False,
        line_curve=0.2,
        label_text_color=None,
    )
    graph.render()
def draw_map(AS):
    graph = Graph("AS Links Map")

    nodes = []

    maxDegree = 0

    for key in AS:
        if len(AS[key]) >= maxDegree:
            maxDegree = len(AS[key])

    for key in AS:
        singleNode = {"name": "AS" + key, "symbolSize": (len(AS[key]) + 1) / (maxDegree + 1) * 5}
        nodes.append(singleNode)

    links = []

    for i in nodes:
        key = i.get('name')[2:]
        for j in AS[key]:
            links.append({"source": i.get('name'), "target": "AS" + j})

    graph.add("AS Links Map", nodes, links, layout="cycle")
    graph.render()
Exemple #25
0
def test_graph():

    # graph_0
    nodes = [{"name": "结点1", "symbolSize": 10},
             {"name": "结点2", "symbolSize": 20},
             {"name": "结点3", "symbolSize": 30},
             {"name": "结点4", "symbolSize": 40},
             {"name": "结点5", "symbolSize": 50},
             {"name": "结点6", "symbolSize": 40},
             {"name": "结点7", "symbolSize": 30},
             {"name": "结点8", "symbolSize": 20}]
    links = []
    for i in nodes:
        for j in nodes:
            links.append({"source": i.get('name'), "target": j.get('name')})
    graph = Graph("关系图-力引导布局示例")
    graph.add("", nodes, links, repulsion=8000)
    graph.show_config()
    graph.render()

    # graph_1
    graph = Graph("关系图-环形布局示例")
    graph.add("", nodes, links, is_label_show=True,
              graph_repulsion=8000, graph_layout='circular', label_text_color=None)
    graph.show_config()
    graph.render()

    # graph_2
    import json
    if PY2:
        import codecs
        with codecs.open(os.path.join("..", "json", "weibo.json"), "rb") as f:
            j = json.load(f)
    else:
        with open(os.path.join("..", "json", "weibo.json"), "r", encoding="utf-8") as f:
            j = json.load(f)
    nodes, links, categories, cont, mid, userl = j
    graph = Graph("微博转发关系图", width=1200, height=600)
    graph.add("", nodes, links, categories, label_pos="right", graph_repulsion=50,
              is_legend_show=False, line_curve=0.2, label_text_color=None)
    graph.show_config()
    graph.render()
Exemple #26
0
def test_graph():

    # graph_0
    nodes = [{"name": "结点1", "symbolSize": 10},
             {"name": "结点2", "symbolSize": 20},
             {"name": "结点3", "symbolSize": 30},
             {"name": "结点4", "symbolSize": 40},
             {"name": "结点5", "symbolSize": 50},
             {"name": "结点6", "symbolSize": 40},
             {"name": "结点7", "symbolSize": 30},
             {"name": "结点8", "symbolSize": 20}]
    links = []
    for i in nodes:
        for j in nodes:
            links.append({"source": i.get('name'), "target": j.get('name')})
    graph = Graph("关系图-力引导布局示例")
    graph.add("", nodes, links, repulsion=8000)
    graph.show_config()
    graph.render()

    # graph_1
    graph = Graph("关系图-环形布局示例")
    graph.add("", nodes, links, is_label_show=True, repulsion=8000, layout='circular', label_text_color=None)
    graph.show_config()
    graph.render()

    # graph_2
    import json
    if PY2:
        import codecs
        with codecs.open(os.path.join("..", "json", "weibo.json"), "rb") as f:
            j = json.load(f)
    else:
        with open(os.path.join("..", "json", "weibo.json"), "r", encoding="utf-8") as f:
            j = json.load(f)
    nodes, links, categories, cont, mid, userl = j
    graph = Graph("微博转发关系图", width=1200, height=600)
    graph.add("", nodes, links, categories, label_pos="right", repulsion=50, is_legend_show=False, line_curve=0.2,
              label_text_color=None)
    graph.show_config()
    graph.render()
Exemple #27
0
    },
    {
        "name": "结点6",
        "symbolSize": 20
    },
    {
        "name": "结点7",
        "symbolSize": 20
    },
    {
        "name": "结点8",
        "symbolSize": 10
    },
]

links = []
for i in nodes:
    for j in nodes:
        links.append({"source": i.get("name"), "target": j.get("name")})
graph = Graph("关系图-力引导布局示例")  #width=1200,height=600)
graph.add("",
          nodes,
          links,
          is_label_show=True,
          repulsion=8000,
          graph_layout="circular",
          label_pos="right")
graph.render()

data = [[i, j, random.randint(0, 50)] for i in range(24) for j in range(7)]
print(data)
# coding=utf8
"""
本示例使用 networkx 库构建复杂、更人性化的的关系图
安装 pip install networkx
参考 https://networkx.github.io/
"""

from __future__ import unicode_literals

import networkx as nx
from networkx.readwrite import json_graph
from pyecharts import Graph

g = nx.Graph()
g.add_node('G1', name='Gateway 1')
g.add_node('N2', name='Node 2')
g.add_node('N3', name='Node 3')
g.add_edge('G1', 'N2')
g.add_edge('G1', 'N3')
g_data = json_graph.node_link_data(g)
eg = Graph('设备最新拓扑图')
eg.add('Devices', nodes=g_data['nodes'], links=g_data['links'])
eg.render()
Exemple #29
0
class CapitalFlow(object):
    '''
	资金流动类
	描述中国期货市场上交易者资金的流动
	目前的版本仅支持一个交易日内一个交易者的市场级资金流动
	
	全局变量:
	tradeday_list	list<datetime.date>		数据涉及时间段内所有交易日列表
	'''
    def __init__(self, account, day):
        '''
		参数:
		account		string		交易者的账号
		day			date		交易日
		功能:
		初始化
		'''
        # 检查参数的数据类型
        assert type(account) is types.StringType
        assert type(day) is datetime.date

        # 检查日期的范围
        assert datetime.date(2014, 1, 1) <= day <= datetime.date(2016, 12, 31)

        # 初始化实例属性account
        self.account = account

        # 初始化实例属性account
        self.day = day

        # 链接数据库
        conn = MySQLdb.connect(**db_config)
        cursor = conn.cursor()

        # 求上一个交易日的日期
        sql = 'select lastday from last_day where day=%s'
        cursor.execute(sql, (self.day, ))
        self.lastday = cursor.fetchone()[0]

        # 入金、出金
        sql = 'select in_money,out_money from zijin where capital_account_new=%s and tradedate=%s'
        cursor.execute(sql, (self.account, self.day))
        self.in_money, self.out_money = cursor.fetchone()

        # 交易所列表
        self.exchange_list = ['sfe', 'dce', 'czce', 'cffex']

        # 今日结算后的保证金、上日结算后的保证金、持仓收益、平仓收益、手续费
        today_margin_list = []
        lastday_margin_list = []
        drop_profit_list = []
        hold_profit_list = []
        commission_list = []

        for exchange in self.exchange_list:
            sql = 'select sum(margin),sum(hold_profit_d) from chicang where capital_account_new=%s and tradedate=%s and seat_code=%s'
            cursor.execute(sql, (self.account, self.day, exchange))
            for row in cursor.fetchall():
                today_margin_list.append(row[0] if row[0] else 0)
                hold_profit_list.append(row[1] if row[1] else 0)

            sql = 'select sum(margin) from chicang where capital_account_new=%s and tradedate=%s and seat_code=%s'
            cursor.execute(sql, (self.account, self.lastday, exchange))
            for row in cursor.fetchall():
                lastday_margin_list.append(row[0] if row[0] else 0)

            sql = 'select sum(drop_profit_d) from pingcang where capital_account_new=%s and tradedate=%s and seat_code=%s'
            cursor.execute(sql, (self.account, self.day, exchange))
            for row in cursor.fetchall():
                drop_profit_list.append(row[0] if row[0] else 0)

            sql = 'select sum(commission) from chengjiao where capital_account_new=%s and tradedate=%s and seat_code=%s'
            cursor.execute(sql, (self.account, self.day, exchange))
            for row in cursor.fetchall():
                commission_list.append(row[0] if row[0] else 0)

        assert len(today_margin_list) == len(lastday_margin_list) == len(
            drop_profit_list) == len(hold_profit_list) == len(
                commission_list) == len(self.exchange_list)

        self.today_margin_array = np.array(today_margin_list, dtype='double')
        self.lastday_margin_array = np.array(lastday_margin_list,
                                             dtype='double')
        self.drop_profit_array = np.array(drop_profit_list, dtype='double')
        self.hold_profit_array = np.array(hold_profit_list, dtype='double')
        self.commission_array = np.array(commission_list, dtype='double')

        # 关闭数据库
        cursor.close()
        conn.close()

        # 计算总收益
        self.profit_array = self.hold_profit_array + self.drop_profit_array

        # 保证金追加
        self.exchange_margin_array = self.today_margin_array - self.lastday_margin_array

        # 资金流入流出(正表示流出,负表示流入)
        self.flow_array = self.profit_array - self.exchange_margin_array - self.commission_array
        n = len(self.flow_array)

        # 拷贝flow_array,把后面对其的修改变为对flow的修改
        flows = copy.deepcopy(self.flow_array)

        # 初始化转移矩阵
        self.cf = np.zeros((n + 1, n + 1))

        # 计算总流出量和总流入量
        total_out = sum(filter(lambda x: x > 0, self.flow_array))
        total_in = abs(sum(filter(lambda x: x < 0, self.flow_array)))
        # 当流出大于流入时,多出的部分流入fund,流量按照比例分配
        if total_out > total_in:
            delta = (total_out - total_in) / total_out
            for i in range(n):
                if flows[i] > 0:
                    tmp = flows[i] * delta
                    flows[i] -= tmp
                    self.cf[i, n] = tmp
        elif total_out < total_in:
            delta = (total_in - total_out) / total_in
            for i in range(n):
                if flows[i] < 0:
                    tmp = abs(flows[i]) * delta
                    flows[i] += tmp
                    self.cf[n, i] = tmp
        out = []
        _in = []
        total = 0
        for i in range(n):
            if flows[i] > 0:
                out.append(i)
                total += flows[i]
            elif flows[i] < 0:
                _in.append(i)
        for i in _in:
            for j in out:
                tmp = flows[j] / total * abs(flows[i])
                self.cf[j, i] = tmp
        # 可视化
        self.__graph()

    def __graph(self):
        '''
		将转移矩阵cf绘制为有向图G,使得G的邻接矩阵为cf
		结果存于实例属性G中
		'''
        # 初始化有向图
        self.G = Graph('账户:{0}\t日期:{1}'.format(self.account,
                                               self.day.isoformat()),
                       width=600,
                       height=370)
        # 节点
        V = [{
            'name': self.exchange_list[i],
            'value': round(self.flow_array[i]),
            'symbolSize': 32
        } for i in range(4)]
        V.append({'name': 'fund', 'symbolSize': 32})
        # 边
        E = []
        for i in range(5):
            for j in range(5):
                if self.cf[i, j] > 0:
                    E.append({
                        'source': i,
                        'target': j,
                        'value': round(self.cf[i, j]),
                        'lineStyle': {
                            'normal': {
                                'width': min(self.cf[i, j] / 2000 + 0.8, 10)
                            }
                        }
                    })
        self.G.add('',
                   V,
                   E,
                   graph_repulsion=1000,
                   is_label_show=True,
                   graph_edge_symbol=[None, 'arrow'],
                   graph_edge_symbolsize=30,
                   label_text_size=18,
                   label_emphasis_textsize=18,
                   label_emphasis_textcolor='#000',
                   line_color='#00F')
Exemple #30
0
def relation(node=None):
    # data=pd.read_csv('/Users/fxm/经语料/经语料/成果/图/词典-繁体.csv',encoding='utf8')
    # cds= pd.read_csv('/Users/fxm/经语料/经语料/成果/人名朝代表格.csv',encoding='utf8')
    # cds.columns=['A','B','C','D']
    # data.columns=['A','B','C']
    # nodes,links = [],[]
    # for l in data[data['B']!='地名'].values:
    #     if str(l[2])!='nan':
    #         x=re.search('[^\u4e00-\u9fa5]([\u4e00-\u9fa5]*)(之女|之二子|之子|最後之一子|弟名|之太子|之夫人|妻之名|女之名|子之名|之長者子|二子之一)[^\u4e00-\u9fa5]','。'+l[2])
    #         if x:
    #             nodes.append({"name": str(l[0]), "symbolSize": 10, "category": 1,'value':'人物'})
    #             nodes.append({"name":x.groups()[0], "symbolSize": 10, "category": 1,'value':'人物'})
    #             links.append({"source":l[0],"target":x.groups()[0],"value":x.groups()[1]})
    #             print(x.groups()[1])
    #         x = re.search('[^\u4e00-\u9fa5]([\u4e00-\u9fa5]{2,8})人[^\u4e00-\u9fa5]', '。' + l[2])
    #         if x:
    #             nodes.append({"name": str(l[0]), "symbolSize": 10, "category": 1,'value':'人物'})
    #             if x.groups()[0].find('之夫人')==-1:
    #                 nodes.append({"name": x.groups()[0], "symbolSize": 10, "category": 2})
    #                 links.append({"source":l[0],"target":x.groups()[0],"value":'故乡'})
    #         x = re.search('[^\u4e00-\u9fa5]初住([\u4e00-\u9fa5]{2,8})[^\u4e00-\u9fa5]', '。' + l[2])
    #         if x:
    #             nodes.append({"name": str(l[0]), "symbolSize": 10, "category": 1,'value':'人物'})
    #             nodes.append({"name": x.groups()[0], "symbolSize": 10, "category": 2})
    #             links.append({"source": l[0], "target": x.groups()[0], "value": '住所'})
    #         x = re.search('[^\u4e00-\u9fa5]後住([\u4e00-\u9fa5]{2,8})[^\u4e00-\u9fa5]', '。' + l[2])
    #         if x:
    #             nodes.append({"name": str(l[0]), "symbolSize": 10, "category": 1,'value':'人物'})
    #             nodes.append({"name": x.groups()[0], "symbolSize": 10, "category": 2})
    #             links.append({"source": l[0], "target": x.groups()[0], "value": '住所'})
    #         else:
    #             x = re.search('[^\u4e00-\u9fa5][住居]([\u4e00-\u9fa5]{2,8})[^\u4e00-\u9fa5]', '。' + l[2])
    #             if x:
    #                 nodes.append({"name": str(l[0]), "symbolSize": 10, "category": 1,'value':'人物'})
    #                 nodes.append({"name": x.groups()[0], "symbolSize": 10, "category": 2})
    #                 links.append({"source": l[0], "target": x.groups()[0], "value": '住所'})
    #         x = re.search('[^\u4e00-\u9fa5]([\u4e00-\u9fa5]{2,8})之一[^\u4e00-\u9fa5]', '。' + l[2])
    #         if x:
    #             nodes.append({"name": str(l[0]), "symbolSize": 10, "category": 1,'value':'人物'})
    #             nodes.append({"name": x.groups()[0], "symbolSize": 10, "category": 0})
    #             links.append({"source": l[0], "target": x.groups()[0], "value": '属于'})
    #
    # nodes1,links1=[],[]
    # names1,nodes2=[],[]
    # for d in nodes:
    #     if d["name"] not in names1:
    #         names1.append(d["name"])
    #         nodes1.append(d)
    # for d in nodes1:
    #     resss=cds[cds['C']==d['name']]
    #     if not resss.empty:
    #         if {"name": str(resss.values[0][0]), "symbolSize": 10, "category": 3,'value':'朝代'} not in nodes2:
    #             nodes2.append({"name": str(resss.values[0][0]), "symbolSize": 10, "category": 3,'value':'朝代'})
    #         links.append({"source": d["name"], "target": resss.values[0][0], "value": '朝代地区'})
    # nodes1+=nodes2
    # names1,nodes3=[],[]
    # for d in nodes1:
    #     if d["name"] not in names1:
    #         names1.append(d["name"])
    #         nodes3.append(d)
    # for d in links:
    #     if d not in links1:
    #         links1.append(d)
    # with open('../data/relation.josn','w',encoding='utf8') as f:
    #     json.dump((nodes3, links1),f)
    with open('/Users/fxm/PycharmProjects/gra_fo/data/relation.json',
              'r',
              encoding='utf8') as f:
        nodes3, links1 = json.load(f)
    if node and node != '':
        df_nodes = json_normalize(nodes3)
        df_links = json_normalize(links1)
        nodes3, links1 = [], []
        bur = [node]
        burall = bur.copy()
        nn = bur.pop(0)
        if df_nodes[df_nodes['name'] == nn].empty:
            page = Page()
            return page
        if str(nn) != 'nan':
            # print(nn)
            l = df_nodes[df_nodes['name'] == nn][['category', 'value']].values
            nodes3.append({
                "name": nn,
                "symbolSize": 20,
                "category": int(l[0][0]),
                'value': str(l[0][1])
            })
            for x in df_links[df_links['source'] == nn][[
                    'source', 'target', 'value'
            ]].values:
                if x[1] not in burall:
                    bur.append(x[1])
                    burall.append(x[1])
                if {
                        "source": x[0],
                        "target": x[1],
                        "value": x[2]
                } not in links1:
                    links1.append({
                        "source": nn,
                        "target": x[1],
                        "value": x[2]
                    })
            for x in df_links[df_links['target'] == nn][[
                    'source', 'target', 'value'
            ]].values:
                if x[0] not in burall:
                    bur.append(x[0])
                    burall.append(x[0])
                if {
                        "source": x[0],
                        "target": x[1],
                        "value": x[2]
                } not in links1:
                    links1.append({
                        "source": x[0],
                        "target": x[1],
                        "value": x[2]
                    })
        while bur:
            nn = bur.pop(0)
            if str(nn) != 'nan':
                # print(nn)
                l = df_nodes[df_nodes['name'] == nn][['category',
                                                      'value']].values
                nodes3.append({
                    "name": nn,
                    "symbolSize": 10,
                    "category": int(l[0][0]),
                    'value': str(l[0][1])
                })
                for x in df_links[df_links['source'] == nn][[
                        'source', 'target', 'value'
                ]].values:
                    if x[1] not in burall:
                        bur.append(x[1])
                        burall.append(x[1])
                    if {
                            "source": x[0],
                            "target": x[1],
                            "value": x[2]
                    } not in links1:
                        links1.append({
                            "source": nn,
                            "target": x[1],
                            "value": x[2]
                        })
                for x in df_links[df_links['target'] == nn][[
                        'source', 'target', 'value'
                ]].values:
                    if x[0] not in burall:
                        bur.append(x[0])
                        burall.append(x[0])
                    if {
                            "source": x[0],
                            "target": x[1],
                            "value": x[2]
                    } not in links1:
                        links1.append({
                            "source": x[0],
                            "target": x[1],
                            "value": x[2]
                        })

    # print(nodes1)
    # nodes2=[{'name': '1', 'symbolSize': 100, 'category': 1},{'name': '2', 'symbolSize': 100, 'category': 0}]
    # links1=[{"source":'1', "target": '2', "value": 1}]
    page = Page()
    categories = [0, 1, 2, 3]
    graph = Graph("", width=1200, height=600)
    # graph.use_theme('chalk')
    graph.add(
        "",
        nodes3,
        links1,
        categories,
        is_focusnode=True,
        is_roam=True,
        is_rotatelabel=False,
        label_pos="right",
        repulsion=100,
        line_curve=0,
        legend_text_size=10,
    )
    page.add(graph)
    return page
Exemple #31
0
def class_re(request):
    name = request.session.get('nn')
    with open("/Users/fxm/PycharmProjects/gra_fo/data/人物共现.json",
              "r",
              encoding='utf-8') as f1:
        Data = json.load(f1)
    jing = ''
    juan = ''
    for i in Data:
        if i["name"] == name:
            jing = i["经号"]
            juan = i["卷号"]
            lei = i["类别"]
            Data.remove(i)

    nodes, links = [{"name": name, "symbolSize": 10, "category": 0}], []
    nodes1, links1 = [{"name": name, "symbolSize": 10, "category": 0}], []
    nodes2, links2 = [{"name": name, "symbolSize": 10, "category": 0}], []
    nodes3, links3 = [{"name": name, "symbolSize": 10, "category": 0}], []
    categories = [0, 1, 2]

    for i in Data:
        if i["经号"] == jing:
            node = {}
            link = {}
            node["name"] = i["name"]
            node["symbolSize"] = 5
            node["category"] = 1
            link["source"] = name
            link["target"] = i["name"]
            link["value"] = i["经号"]
            if i["卷号"] == juan:
                node["symbolSize"] = 10
                node["category"] = 0
                link["value"] = i["卷号"]
            nodes.append(node)
            links.append(link)
            if i["类别"] == "王":
                # or"龍王"or"王子"
                nodes1.append(node)
                links1.append(link)
            elif i["类别"] == "佛" or i["类别"] == "如來":
                # "佛"or
                nodes2.append(node)
                links2.append(link)
            else:
                nodes3.append(node)
                links3.append(link)

    a = len(nodes) - 1
    b = len(nodes1) - 1
    c = len(nodes2) - 1
    d = len(nodes3) - 1
    print("共发现与%s同经或同卷共现人物%s个,其中王类%s个,佛类%s个,其他类%s个" % (name, a, b, c, d))
    # 生成图谱,同卷共现的颜色相同
    page = Page()
    graph = Graph(
        "经卷共现图谱",
        width=1200,
        height=600,
    )
    # graph.use_theme('chalk')
    graph.add(
        "",
        nodes,
        links,
        categories,
        is_focusnode=True,
        is_roam=True,
        is_rotatelabel=False,
        label_pos="right",
        repulsion=100,
        line_curve=0,
        legend_text_size=10,
    )
    page.add(graph)
    graph1 = Graph(
        "王类",
        width=1200,
        height=600,
    )
    # graph1.use_theme('chalk')
    graph1.add(
        "",
        nodes1,
        links1,
        categories,
        is_focusnode=True,
        is_roam=True,
        is_rotatelabel=False,
        label_pos="right",
        repulsion=100,
        line_curve=0,
        legend_text_size=10,
    )
    page.add(graph1)
    graph2 = Graph(
        "佛类",
        width=1200,
        height=600,
    )
    # graph2.use_theme('chalk')
    graph2.add(
        "",
        nodes2,
        links2,
        categories,
        is_focusnode=True,
        is_roam=True,
        is_rotatelabel=False,
        label_pos="right",
        repulsion=100,
        line_curve=0,
        legend_text_size=10,
    )
    page.add(graph2)
    graph3 = Graph(
        "其他",
        width=1200,
        height=600,
    )
    # graph3.use_theme('chalk')
    graph3.add(
        "",
        nodes3,
        links3,
        categories,
        is_focusnode=True,
        is_roam=True,
        is_rotatelabel=False,
        label_pos="right",
        repulsion=100,
        line_curve=0,
        legend_text_size=10,
    )
    page.add(graph3)
    x_axis = ["王类", "佛类", "其他"]
    y_axis = [a, b, c]
    bar = Bar("分类柱状图", width=1200, height=600)
    # bar.use_theme('chalk')
    bar.add("", x_axis, y_axis)
    page.add(bar)
    page.render(
        path="/Users/fxm/PycharmProjects/gra_fo/templates/class_re.html")
    return render(request, "index.html", {'inner': 'class_re'})
Exemple #32
0
def gongxian(request):
    # 将excel转为列表形式 [{"name":name,"jingmu":[xx经,xx经,]}, , ,]
    name = request.session.get('nn')
    data = xlrd.open_workbook(
        "/Users/fxm/PycharmProjects/gra_fo/data/短篇分词回收人名与经目.xlsx")
    table = data.sheets()[0]
    nrows = table.nrows
    list = []
    for i in range(1, nrows):
        newdata = {}
        content = table.row_values(i)
        newdata["name"] = content[0]
        newdata["经目"] = []
        for j in range(2, 40):
            if content[j] is "":
                break
            newdata["经目"].append(content[j])
        list.append(newdata)

    list1 = []  # 存放一级相关节点
    list2 = []  # 存放二级相关节点

    #构造节点及连线
    nodes = [{"name": name, "symbolSize": 15, "category": 0}]
    links = []
    categories = [0, 1, 2]
    # 取与输入人物同经共现的一级人物节点
    for i in list:
        if i["name"] == name:
            list.remove(i)
            for j in list:
                for x in i["经目"]:
                    for y in j["经目"]:
                        if x == y and j not in list1:
                            node = {}
                            node["name"] = j["name"]
                            node["symbolSize"] = 10
                            node["category"] = 1
                            nodes.append(node)
                            link = {}
                            link["source"] = i["name"]
                            link["target"] = j["name"]
                            link["value"] = x
                            links.append(link)
                            list.remove(j)
                            list1.append(j)

    # 对一级共现人物节点取同经共现人物
    for i in list1:
        for j in list:
            for x in i["经目"]:
                for y in j["经目"]:
                    if x == y and {
                            "name": j["name"],
                            "symbolSize": 5,
                            "category": 2
                    } not in nodes:
                        node = {}
                        node["name"] = j["name"]
                        node["symbolSize"] = 5
                        node["category"] = 2
                        nodes.append(node)
                        link = {}
                        link["source"] = i["name"]
                        link["target"] = j["name"]
                        link["value"] = x
                        links.append(link)
                        list.remove(j)
                        list2.append(j)
    a = len(list1)
    b = len(list2)
    # 生成关系图
    page = Page()
    graph = Graph("二级共现人物", width=1200, height=600)
    # graph.use_theme('chalk')
    graph.add(
        "",
        nodes,
        links,
        categories,
        is_focusnode=True,
        is_roam=True,
        is_rotatelabel=False,
        label_pos="right",
        repulsion=100,
        line_curve=0,
        legend_text_size=10,
    )
    page.add(graph)
    # bar图
    x_axis = ["一级共现", "二级共现"]
    y_axis = [a, b]
    bar = Bar("同经共现", width=1200, height=600)
    # bar.use_theme('chalk')
    bar.add(
        "",
        x_axis,
        y_axis,
        bar_category_gap='50%',
    )
    page.add(bar)
    page.render(
        path="/Users/fxm/PycharmProjects/gra_fo/templates/render_all.html")
    return render(request, "index.html", {'inner': 'gongxian'})