pyecharts/example/graph_example.py
2019-05-11 15:34:40 +08:00

119 lines
3.5 KiB
Python

import json
import os
from example.commons import Collector
from pyecharts import options as opts
from pyecharts.charts import Graph, Page
C = Collector()
@C.funcs
def graph_base() -> 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")})
c = (
Graph()
.add("", nodes, links, repulsion=8000)
.set_global_opts(title_opts=opts.TitleOpts(title="Graph-基本示例"))
)
return c
@C.funcs
def graph_with_opts() -> Graph:
nodes = [
opts.GraphNode(name="结点1", symbol_size=10),
opts.GraphNode(name="结点2", symbol_size=20),
opts.GraphNode(name="结点3", symbol_size=30),
opts.GraphNode(name="结点4", symbol_size=40),
opts.GraphNode(name="结点5", symbol_size=50),
]
links = [
opts.GraphLink(source="结点1", target="结点2"),
opts.GraphLink(source="结点2", target="结点3"),
opts.GraphLink(source="结点3", target="结点4"),
opts.GraphLink(source="结点4", target="结点5"),
opts.GraphLink(source="结点5", target="结点1"),
]
c = (
Graph()
.add("", nodes, links, repulsion=4000)
.set_global_opts(title_opts=opts.TitleOpts(title="Graph-GraphNode-GraphLink"))
)
return c
@C.funcs
def graph_weibo() -> Graph:
with open(os.path.join("fixtures", "weibo.json"), "r", encoding="utf-8") as f:
j = json.load(f)
nodes, links, categories, cont, mid, userl = j
c = (
Graph()
.add(
"",
nodes,
links,
categories,
repulsion=50,
linestyle_opts=opts.LineStyleOpts(curve=0.2),
label_opts=opts.LabelOpts(is_show=False),
)
.set_global_opts(
legend_opts=opts.LegendOpts(is_show=False),
title_opts=opts.TitleOpts(title="Graph-微博转发关系图"),
)
)
return c
@C.funcs
def graph_npm_dependencies() -> Graph:
with open(os.path.join("fixtures", "npmdepgraph.json"), "r", encoding="utf-8") as f:
j = json.load(f)
nodes = [
{
"x": node["x"],
"y": node["y"],
"id": node["id"],
"name": node["label"],
"symbolSize": node["size"],
"itemStyle": {"normal": {"color": node["color"]}},
}
for node in j["nodes"]
]
edges = [
{"source": edge["sourceID"], "target": edge["targetID"]} for edge in j["edges"]
]
c = (
Graph(init_opts=opts.InitOpts(width="1000px", height="600px"))
.add(
"",
nodes=nodes,
links=edges,
layout="none",
label_opts=opts.LabelOpts(is_show=False),
linestyle_opts=opts.LineStyleOpts(width=0.5, curve=0.3, opacity=0.7),
)
.set_global_opts(title_opts=opts.TitleOpts(title="Graph-NPM Dependencies"))
)
return c
Page().add(*[fn() for fn, _ in C.charts]).render()