我正在使用Django,在我的视图中生成各方之间关系的图表。问题出现在以下部分:
nx.set_node_attributes(G, fill_coloring, name="background")
但是不幸的是,这并不起作用。我无法在文档中找到如何正确设置它的方法。我的目标是根据与其相关的主数据来直接对每个节点进行着色/格式化。
nx.set_node_attributes(G, border_coloring, name="color")
通过上述方法,我能够设置边框颜色,但无法设置内部颜色。我忽略了什么?
理想情况下,稍后通过添加信息到线条等来实现,但目前我想以特定颜色着色节点本身。目前,它只能着色边框。我忽略了什么/误解了什么?
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需要将样式设置为“filled”。
以下解决了这个问题:
nx.set_node_attributes(G, {e.customer_end.name: "filled"}, name="style")