我正在使用Django,在我看来,我生成了一个各方之间的关系图.问题出在以下方面:
nx.set_node_attributes(G, fill_coloring, name="background")
但不幸的是,这并不管用.我在文档中找不到如何正确设置它.我想要实现的是基于与其相关的主数据来指导每个 node 的着色/格式化.
nx.set_node_attributes(G, border_coloring, name="color")
有了上面的设置,我可以设置边框 colored颜色 ,但不能设置内部 colored颜色 .我忽略了什么?
理想的情况是稍后通过向线条添加信息等方式,但现在我想用特定的 colored颜色 为 node 本身上色.目前,它只为边框上色.我忽略/误解了什么?
def index(request):
filename = f'{datetime.now():%Y-%m-%d_%H-%M-%S}.png'
save_path = 'static/app/master/routing/'
completeName = os.path.join(save_path, filename)
print(completeName)
G = nx.DiGraph()
routings = Customer_Routing.objects.all()
end_customers = []
border_coloring = {}
fill_coloring = {}
for e in routings:
G.add_edge(e.customer_start.name, e.customer_end.name)
if e.customer_end.end_customer == True:
try:
print(e.customer_end.customer_class.name)
if e.customer_end.customer_class.name == "DIY":
border_coloring[e.customer_end.name] = "#DAC85A"
fill_coloring[e.customer_end.name] = "#FEF19C"
except AttributeError:
print(e.customer_end.name + " has no class.")
### Define the customer coloring
nx.set_node_attributes(G, border_coloring, name="color")
nx.set_node_attributes(G, fill_coloring, name="background")
A = to_agraph(G)
A.draw(completeName, prog='dot')
template = loader.get_template('index.html')
context = {
'img': completeName,
'routing': routings,
}
return HttpResponse(template.render(context, request))
编辑: 在发布了这篇文章后不久,我找到了答案.Graphviz需要将样式设置为"已填充".
下面解决了这个问题:
nx.set_node_attributes(G, {e.customer_end.name: "filled"}, name="style")