 |
Creating a complex graph and visualize it
Source code name: "graph_adapter.py"
Programming language: Python
Topic: Data mining/Graphs
DMelt Version 1.8. Last modified: 06/13/2017. License: Pro
https://datamelt.org/code/cache/graph_adapter_4911.py
To run this script using the DMelt IDE,
copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.
# A complex example that build a graph and visualize it using Swing
from jhplot import *
from java.net import *
from javax.swing import *
from java.awt import *
from org.jgrapht.ext import JGraphXAdapter
from com.mxgraph.swing import mxGraphComponent
from com.mxgraph.layout import mxCircleLayout
from org.jgrapht.graph import DefaultListenableGraph
import sys
gh=HGraph().buildDirectedGraph()
g=DefaultListenableGraph(gh)
adapter = JGraphXAdapter(g)
v1,v2,v3,v4 = "v1","v2","v3","v4"
# add vertexes
g.addVertex(v1)
g.addVertex(v2)
g.addVertex(v3)
g.addVertex(v4)
g.addEdge(v1, v2)
g.addEdge(v2, v3)
g.addEdge(v3, v1)
g.addEdge(v4, v3)
# positioning via jgraphx layouts
layout = mxCircleLayout(adapter)
layout.execute(adapter.getDefaultParent())
frame = JFrame()
frame.getContentPane().add(mxGraphComponent(adapter) )
frame.setTitle("JGraphT Adapter")
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
# frame.setSize(Dimension(400, 400))
frame.pack()
frame.setVisible(True)
You see the box below because you did not login.