Adding label to an edge of a graph in nodebox opne

2019-06-27 03:57发布

I am trying to add a label to each edge in my Graph, below:

enter image description here

Basically the above with labels for each edge at the center:

enter image description here

I've tried to add a label when I add an edge to each graph, like so (for the graph g):

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

After some research, I found that such labeling was possible under Nodebox 1, which only works for Mac, there seems to be no suitable alternative for Nodebox-OpenGL from the documentation. The error I receive:

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'

You can reproduce the problem:

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)

So, the question remains, how can one add a label to a graph's edge in Nodebox-OpenGL?

1条回答
你好瞎i
2楼-- · 2019-06-27 04:21

As you can see in the source there is no argument labelfor add_edge. (search for class Edge(object):)

The best way i can see is to create your own MyEdge Class derived from the official Edge Class which adds a Text (the label) using

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

or

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

in the draw() Method.

EDIT Mind the add_edge Methods docstring:

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)
    """
查看更多
登录 后发表回答