Exemple #1
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 #2
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 #3
0
def plot_chnx(ch1=None, ch2=None, check_code=None):
    # df = pd.read_csv(DATA_URL + '/chinese_characters.csv')
    df = pd.read_csv(DATA_URL + '/fanti.edgelist',
                     sep='\t',
                     header=None,
                     names=['ch1', 'ch2'])
    df.drop_duplicates(keep='first', inplace=True)

    if check_code in [1, 5]:
        df = df.loc[(df['ch1'] == ch1) | (df['ch2'] == ch1)]
        nodes = [{
            "name": ch,
            "symbolSize": 10,
            "itemStyle": {
                "normal": {
                    "color": 'red'
                }
            }
        } for ch in set(df['ch1'].unique()) | set(df['ch2'].unique())]
        links = [{
            "source": value[0],
            "target": value[1],
            "lineStyle": {
                "normal": {
                    "color": 'green'
                }
            }
        } for value in df.values]
        graph = Graph("汉字-关系图", width=1200, height=600)
        graph.add("",
                  nodes,
                  links,
                  repulsion=80,
                  layout="force",
                  graph_edge_length=150,
                  is_roam=True,
                  is_label_show=True,
                  is_legend_show=True,
                  draggable=True,
                  lineStyle='-.',
                  graph_edge_symbol=['circle', 'arrow'])
        graph.render()
    elif check_code == 4:
        df = df.loc[(df['ch1'] == ch1) | (df['ch2'] == ch1) |
                    (df['ch1'] == ch2) | (df['ch2'] == ch2)]
        df1 = df.loc[((df['ch1'] == ch1) & (df['ch2'] != ch2)) |
                     ((df['ch2'] == ch1) & (df['ch1'] != ch2))]
        df2 = df.loc[((df['ch1'] == ch2) & (df['ch2'] != ch1)) |
                     ((df['ch2'] == ch2) & (df['ch1'] != ch1))]
        df3 = df.loc[((df['ch1'] == ch1) & (df['ch2'] == ch2)) |
                     ((df['ch2'] == ch1) & (df['ch1'] == ch2))]

        nodes = [{
            "name": ch,
            "symbolSize": 10,
            "itemStyle": {
                "normal": {
                    "color": 'red'
                }
            }
        } for ch in set(df1['ch1'].unique()) | set(df1['ch2'].unique())]
        nodes += [{
            "name": ch,
            "symbolSize": 10,
            "itemStyle": {
                "normal": {
                    "color": 'blue'
                }
            }
        } for ch in set(df2['ch1'].unique()) | set(df2['ch2'].unique())]

        links = [{
            "source": value[0],
            "target": value[1],
            "lineStyle": {
                "normal": {
                    "color": 'PaleTurquoise'
                }
            }
        } for value in df1.values]
        links += [{
            "source": value[0],
            "target": value[1],
            "lineStyle": {
                "normal": {
                    "color": 'gray'
                }
            }
        } for value in df2.values]
        links += [{
            "source": value[0],
            "target": value[1],
            "lineStyle": {
                "normal": {
                    "color": 'Teal'
                }
            }
        } for value in df3.values]

        graph = Graph("汉字-关系图", width=1200, height=600)
        graph.add("",
                  nodes,
                  links,
                  repulsion=80,
                  layout="force",
                  graph_edge_length=150,
                  is_roam=True,
                  is_label_show=True,
                  is_legend_show=True,
                  draggable=True,
                  lineStyle='-.',
                  graph_edge_symbol=['circle', 'arrow'])
        graph.render()

    elif check_code == 6:
        df = df.loc[(df['ch1'] == ch2) | (df['ch2'] == ch2)]
        nodes = [{
            "name": ch,
            "symbolSize": 10,
            "itemStyle": {
                "normal": {
                    "color": 'red'
                }
            }
        } for ch in set(df['ch1'].unique()) | set(df['ch2'].unique())]
        links = [{
            "source": value[0],
            "target": value[1],
            "lineStyle": {
                "normal": {
                    "color": 'green'
                }
            }
        } for value in df.values]
        graph = Graph("汉字-关系图", width=1200, height=600)
        graph.add("",
                  nodes,
                  links,
                  repulsion=80,
                  layout="force",
                  graph_edge_length=150,
                  is_roam=True,
                  is_label_show=True,
                  is_legend_show=True,
                  draggable=True,
                  lineStyle='-.',
                  graph_edge_symbol=['circle', 'arrow'])
        graph.render()

    context = dict(myechart=graph.render_embed(),
                   host='/static/js',
                   script_list=graph.get_js_dependencies())
    context.update(network_info(ch1, ch2))
    return context