Last week, when I presented JFreeChart, I intended to follow it with other articles dedicated to JGraph, JFreeReport and other useful open-source packages that can be used from within Matlab. However, blog follower Scott Koch was quick to post a comment about JGraph and even took the time to add a short code snippet illustrating JGraph’s use. So, I will honor Scott by re-posting his example here.
JGraph is an open-source Java library that helps visualize graph-theory objects and connectors. Note that in today’s article we discuss graphs in the sense of connected object trees, not graphs in the sense of x-vs.-y data charts.
To use JGraph, download the JGraphx zip file, then extract the zip file. You’ll get a jgraphx/ subfolder containing separate sub-folders with docs, examples, source-code and a lib/ sub-folder with a jgraphx.jar file. To use JGraph in Matlab, load jgraphx.jar into Matlab’s Java classpath (there is no need to use the static classpath in this case, although you can of course do so if you like):
We can now run Scott Koch’s example of using JGraph in Matlab:
% Make the graph object graph = com.mxgraph.view.mxGraph; % Get the parent cell parent = graph.getDefaultParent(); % Group update graph.getModel().beginUpdate(); % Add some child cells v1 = graph.insertVertex(parent, '', 'Hello', 240, 150, 80, 30); v2 = graph.insertVertex(parent, '', 'World', 20, 20, 80, 30); graph.insertEdge(parent, '', 'Edge', v1, v2); graph.getModel().endUpdate(); % Get scrollpane graphComponent = com.mxgraph.swing.mxGraphComponent(graph); % Make a figure and stick the component on it f = figure('units','pixels'); pos = get(f,'position'); mypanel = javax.swing.JPanel(java.awt.BorderLayout); mypanel.add(graphComponent); [obj, hcontainer] = javacomponent(mypanel, [0,0,pos(3:4)], f);
And some more complex JGraph examples, taken from the “official” samples gallery:
JGraphT is another open-source Java-based library. JGraphT is a non-GUI library that provides underlying graph-theory objects and algorithms. These can be used in conjunction with the JGraph visualization library.
The Matlab File Exchange has a submission that wraps JGraphT within Matlab. Apparently, this Matlab utility is a wrapper for the non-GUI JGraphT, but I see no reason not to use JGraph’s GUI visualization capabilities, since JGraphT has an extremely simple JGraph adapter.
BDE (Block Diagram Editor?) is an internal Matlab Java class library that has been included in Matlab releases for many years, to this day. Judging from the size of this library file (%matlabroot%/java/jar/bde.jar), MathWorks continues to develop BDE, which grows from one release to another.
BDE can be used to present and edit block diagrams. The diagrams themselves are stored in XML files in a BDE/ subfolder beneath the user’s prefdir folder. However, I do not know how these diagrams can be used. Here’s a simple usage example:
bde = com.mathworks.bde.clients.BDEDesktop; % fails in R2009b+ diagram = com.mathworks.bde.diagram.Diagram; client = com.mathworks.bde.clients.DiagramViewDTClient('YMA',diagram); bde.addClient(client,'YMA');
Note: In Matlab release R2009b, BDE apparently had a major redesign, and the above code no longer works.
I would be happy if anyone could enlighten me regarding the possible usage of BDE, or how it can be ran in one of the latest releases. Please leave a comment below.