第1关 Graph:关系图(一)
from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
from pyecharts.charts 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")})
def graph_chart() -> Graph:
# ********* Begin *********#
graph = (
Graph()
.add("", nodes, links, repulsion=8000)
.set_global_opts(title_opts=opts.TitleOpts(title="Graph-基本示例"))
)
# ********** End **********#
return graph
make_snapshot(snapshot, graph_chart().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, graph_base(nodes, links).render(), "StandardAnswer/task1/standard_answer_1.png")
第2关 Graph:关系图(二)
from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
from pyecharts.charts import Graph
# ********* Begin *********#
nodes_data = [
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),
opts.GraphNode(name="结点6", symbol_size=60),
]
links_data = [
opts.GraphLink(source="结点1", target="结点2", value=2),
opts.GraphLink(source="结点2", target="结点3", value=3),
opts.GraphLink(source="结点3", target="结点4", value=4),
opts.GraphLink(source="结点4", target="结点5", value=5),
opts.GraphLink(source="结点5", target="结点6", value=6),
opts.GraphLink(source="结点6", target="结点1", value=7),
]
# ********** End **********#
def graph_chart() -> Graph:
# ********* Begin *********#
graph = (
Graph()
.add(
"",
nodes_data,
links_data,
repulsion=4000,
edge_label=opts.LabelOpts(
is_show=True, position="middle", formatter="{b} 的数据 {c}"
),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="Graph-GraphNode-GraphLink-WithEdgeLabel")
)
)
# ********** End **********#
return graph
make_snapshot(snapshot, graph_chart().render("Result/render.html"), "StudentAnswer/student_answer.png") # 输出图片
make_snapshot(snapshot, graph_with_options().render(), "StandardAnswer/task2/standard_answer_2.png")
第3关 Graph:关系图(三)
from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
from pyecharts.charts import Graph
import json
with open("les-miserables.json", "r", encoding="utf-8") as f:
j = json.load(f)
nodes = j["nodes"]
links = j["links"]
categories = j["categories"]
def graph_les_miserables(nodes, links, categories) -> Graph:
# ********* Begin *********#
graph = (
Graph(init_opts=opts.InitOpts(width="1000px", height="600px"))
.add(
"",
nodes=nodes,
links=links,
categories=categories,
layout="circular",
is_rotate_label=True,
linestyle_opts=opts.LineStyleOpts(color="source", curve=0.3),
label_opts=opts.LabelOpts(position="right"),
)
.set_global_opts(
title_opts=opts.TitleOpts(title="Graph-Les Miserables"),
legend_opts=opts.LegendOpts(orient="vertical", pos_left="2%", pos_top="20%"),
)
)
# ********** End **********#
return graph
make_snapshot(snapshot, graph_les_miserables(nodes, links, categories).render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, graph_les_miserables(nodes, links, categories).render(), "StandardAnswer/task3/standard_answer_3.png")
第4关 Graph:关系图(四)
from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot
from pyecharts.charts import Graph
import json
# ********* Begin *********#
# File: weibo.json
# variables: nodes, links, categories
# ********** End **********#
def graph_weibo() -> Graph:
# ********* Begin *********#
with open("les-miserables.json", "r", encoding="utf-8") as f:
j = json.load(f)
nodes = j["nodes"]
links = j["links"]
categories = j["categories"]
with open("weibo.json", "r", encoding="utf-8") as f:
j = json.load(f)
nodes, links, categories, cont, mid, userl = j
graph = (
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-微博转发关系图"),
)
)
# ********** End **********#
return graph
make_snapshot(snapshot, graph_weibo().render("Result/render.html"), 'StudentAnswer/student_answer.png') # 输出图片
make_snapshot(snapshot, graph_weibo().render(), "StandardAnswer/task4/standard_answer_4.png")