Skip to content

Instantly share code, notes, and snippets.

@tmori3y2
Last active May 27, 2017 13:00
Show Gist options
  • Save tmori3y2/c8d4787dd023a666b0899d603e4268d0 to your computer and use it in GitHub Desktop.
Save tmori3y2/c8d4787dd023a666b0899d603e4268d0 to your computer and use it in GitHub Desktop.
graphviz node with labeled by subgraph.
# http://tmori3y2.hatenablog.com/entry/2017/05/14/133003
# import Digraph from graphviz pakage.
# http://graphviz.readthedocs.io/en/latest/manual.html
from graphviz import Digraph
# graphviz node with labeled by subgraph.
# http://d.hatena.ne.jp/simply-k/20100727/1280224098
# http://nekowarau.seesaa.net/article/424645749.html
class XLabeledNode(object):
# class global properties.
shape = 'ellipse'
style = 'solid, filled'
height = '1'
width = '0.5'
@classmethod
def change_styles(cls, *, style='solid, filled', height='1', width='0.5'):
cls.style = style
cls.height = height
cls.width = width
# constructor
def __init__(self,
identifier, # node name and subgraph name without prefix 'cluster_'.
xlabel, # subgraph label (external label of node).
*,
xlabelloc = 'b', # overwrites subgraph label location.
xlabeljust = 'c', # overwrites subgraph label justification.
xbgcolor = 'white', # overwrites subgraph bgcolor.
height = '1', # overwrites node height.
width = '0.5', # overwrites node width.
fixsize = False, # overwrites
color = 'black', # overwrites node color.
fillcolor = 'white', # overwrites node fillcolor.
label = ''): # overwirutes node label.
if fixsize == True:
height = self.height
width = self.width
self.xnode = Digraph('cluster_' + identifier)
self.xnode.attr('graph', label=xlabel,
labelloc=xlabelloc, labeljust=xlabeljust,
penwidth='0', bgcolor=xbgcolor)
self.xnode.attr('node', shape=self.shape, style=self.style,
height=height, width=width,
color=color, fillcolor=fillcolor)
self.xnode.node(identifier, label)
self.identifier = identifier
class FixSizeCircleNode(XLabeledNode):
# class global properties.
shape = 'circle'
style = 'solid, filled'
height = '1'
width = '1'
@classmethod
def change_circlestyles(cls, *, style='solid, filled', size='1'):
cls.style = style
cls.height = size
cls.width = size
# constructor
def __init__(self,
identifier, # node name and subgraph name without prefix 'cluster_'.
xlabel, # subgraph label (external label of node).
*,
xlabelloc = 'b', # overwrites subgraph label location.
xlabeljust = 'c', # overwrites subgraph label justification.
xbgcolor = 'white', # overwrites subgraph bgcolor.
color = 'black', # overwrites node color.
fillcolor = 'white', # overwrites node fillcolor.
label = ''): # overwirutes node label.
super().__init__(identifier, xlabel,
xlabelloc=xlabelloc, xlabeljust=xlabeljust, xbgcolor=xbgcolor,
fixsize=True, color=color, fillcolor=fillcolor, label=label)
class FixSizeBarNode(XLabeledNode):
# class global properties.
shape = 'box'
height = '1'
width = '0.1'
# constructor
def __init__(self,
identifier, # node name and subgraph name without prefix 'cluster_'.
xlabel, # subgraph label (external label of node).
*,
xlabelloc = 'b', # overwrites subgraph label location.
xlabeljust = 'c', # overwrites subgraph label justification.
xbgcolor = 'white', # overwrites subgraph bgcolor.
color = 'black', # overwrites node color.
fillcolor = 'white', # overwrites node fillcolor.
label = ''): # overwirutes node label.
super().__init__(identifier, xlabel,
xlabelloc=xlabelloc, xlabeljust=xlabeljust, xbgcolor=xbgcolor,
fixsize=True, color=color, fillcolor=fillcolor, label=label)
if __name__ == '__main__':
fig = Digraph('XNode Test', filename='xLabelNode.dot', format='png', graph_attr = {'rankdir': 'LR'})
FixSizeCircleNode.change_circlestyles(size='2') # overwrites global node style.
c0 = FixSizeCircleNode('c0', 'Circle Node 0', xlabelloc='t')
b0 = FixSizeBarNode('b0', 'Bar Node 0')
c1 = FixSizeCircleNode('c1', 'Circle Node 1')
fig.subgraph(c0.xnode)
fig.subgraph(b0.xnode)
fig.subgraph(c1.xnode)
fig.edge(c0.identifier, b0.identifier)
fig.edge(b0.identifier, c1.identifier)
print(fig)
#fig.save()
fig.render(view=True)
#fig.view()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment