【问题标题】:Adding label to an edge of a graph in nodebox opnegl在nodebox opengl中将标签添加到图的边缘
【发布时间】:2013-12-06 22:00:39
【问题描述】:

我正在尝试为图表中的每条边添加一个标签,如下所示:

基本上上面的每个边缘都有标签:

我尝试在为每个图形添加边时添加标签,如下所示(对于图形g):

g.add_edge(... label=edge.distance ...)

经过一番研究,我发现在Nodebox 1, which only works for Mac 下可以进行这样的标记,从文档中似乎没有适合Nodebox-OpenGL 的替代方案。我收到的错误:

Traceback (most recent call last):
  File "C:\foo\bar\baz\Imager.py", line 29, in <module>
    g.add_edge(edge.fr, edge.to, length=edge.distance, weight=2, stroke=color(1.0, 0.2, 0.0), label="cheese")
  File "C:\Python27\lib\site-packages\nodebox\graphics\physics.py", line 1254, in add_edge
    e2 = e2(n1, n2, *args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'label'

您可以重现该问题:

from nodebox.graphics import *
from nodebox.graphics.physics import Node, Edge, Graph

# Create a graph with randomly connected nodes.
# Nodes and edges can be styled with fill, stroke, strokewidth parameters.
# Each node displays its id as a text label, stored as a Text object in Node.text.
# To hide the node label, set the text parameter to None.
g = Graph()
# Random nodes.
for i in range(50):
    g.add_node(id=str(i+1), 
        radius = 5,
        stroke = color(0), 
          text = color(0))
# Random edges.
for i in range(75):
    node1 = choice(g.nodes)
    node2 = choice(g.nodes)
    g.add_edge(node1, node2, 
        length = 1.0, 
        weight = random(), 
        stroke = color(0),
        label = "Placeholder")    #!!!!!!!!!!!!! ADDING THE label HERE

# Two handy tricks to prettify the layout:
# 1) Nodes with a higher weight (i.e. incoming traffic) appear bigger.
for node in g.nodes:
    node.radius = node.radius + node.radius*node.weight
# 2) Nodes with only one connection ("leaf" nodes) have a shorter connection.
for node in g.nodes:
    if len(node.edges) == 1:
        node.edges[0].length *= 0.1

g.prune(depth=0)          # Remove orphaned nodes with no connections.
g.distance         = 10   # Overall spacing between nodes.
g.layout.force     = 0.01 # Strength of the attractive & repulsive force.
g.layout.repulsion = 15   # Repulsion radius.

dragged = None
def draw(canvas):

    canvas.clear()
    background(1)
    translate(250, 250)

    # With directed=True, edges have an arrowhead indicating the direction of the connection.
    # With weighted=True, Node.centrality is indicated by a shadow under high-traffic nodes.
    # With weighted=0.0-1.0, indicates nodes whose centrality > the given threshold.
    # This requires some extra calculations.
    g.draw(weighted=0.5, directed=True)
    g.update(iterations=10)

    # Make it interactive!
    # When the mouse is pressed, remember on which node.
    # Drag this node around when the mouse is moved.
    dx = canvas.mouse.x - 250 # Undo translate().
    dy = canvas.mouse.y - 250
    global dragged
    if canvas.mouse.pressed and not dragged:
        dragged = g.node_at(dx, dy)
    if not canvas.mouse.pressed:
        dragged = None
    if dragged:
        dragged.x = dx
        dragged.y = dy

canvas.size = 500, 500
canvas.run(draw)

那么,问题仍然存在,如何在 Nodebox-OpenGL 中为图的边缘添加标签?

【问题讨论】:

  • cbg @games,你介意发布完整的回溯吗??
  • @KDawG 你好。已更新,但回溯对您没有任何帮助。错误很明显。

标签: python nodebox


【解决方案1】:

正如您在 source 中看到的那样,label 没有参数 add_edge。 (搜索class Edge(object):

我能看到的最好的方法是创建自己的 MyEdge 类,该类派生自官方 Edge 类,该类使用添加文本(label

txt = Text(str, x=0, y=0, width=None, height=None)

textpath(string, x=0, y=0, fontname=None, fontsize=None, fontweight=None)

在 draw() 方法中。

编辑 注意add_edge 方法文档字符串:

def add_edge(self, id1, id2, *args, **kwargs):
    """ Appends a new Edge to the graph.
        An optional base parameter can be used to pass a subclass of Edge:
        Graph.add_edge("cold", "winter", base=IsPropertyOf)
    """

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多