The other day I wanted to present a graphic presentation of Matlab’s position in the computer programming world. Naturally, my first attempt, being an addict of infographics (IG), was to search for an IG about computer programs. One of my favorite IG sites is CoolInfographics. Unfortunately, it’s not technology-oriented. I am not aware of technology-related IG sites, although Online Schools has a technology section with some cool IGs (including one about the evolution of computer programming that I found interesting). Unfortunately again, none of the IGs I found online discuss Matlab.
So I resorted to preparing my own graphic presentation. I wanted the graphics to appear special – not an ordinary Matlab or Excel charts. For the fun of it, I wanted to prepare this report in Matlab (call me a Matlab addict if you will…).
Luckily, Java has some extremely cool open-source charting and reporting tools, that can easily be integrated in Matlab.
An extremely powerful and widely-used Java-based charting library is JFreeChart, which includes classes for displaying charts, graphs and gauges in Java panels. JFreeChart solves Matlab’s limitation that plot axes cannot be added to Java containers. JFreeChart is free open-source. Used by over 40,000 Java developers worldwide (as well as some Matlab developers), it is in constant development and improvement.
Let us now integrate a JFreeChart pie-chart within a Matlab figure, as a means of illustrating how to integrate third-party Swing-derived components into Matlab.
First, download the latest JFreeChart version. Next, unzip the downloaded file into some new folder. Now, edit classpath.txt (or use javaaddpath) to load jfreechart-1.0.13.jar and jcommon-1.0.16.jar (which are located in the /lib/ sub-folder) to Matlab’s Java classpath (replace the version numbers as appropriate):
javaaddpath C:/Yair/Utils/JFreeChart/lib/jcommon-1.0.16.jar javaaddpath C:/Yair/Utils/JFreeChart/lib/jfreechart-1.0.13.jar
Within the Matlab code, load the data into an object that implements the org.jfree.data.Dataset interface. There are separate such objects for each specific chart type. For example, in order to display a pie-chart we would use org.jfree.data.general.DefaultPieDataset:
% Prepare a data-set with some unrealistic numbers... dataset = org.jfree.data.general.DefaultPieDataset; dataset.setValue(java.lang.String('C'), 4); dataset.setValue(java.lang.String('C++'), 7); dataset.setValue(java.lang.String('Matlab'), 52); dataset.setValue(java.lang.String('Java'), 23); dataset.setValue(java.lang.String('Other'), 14); % Now prepare an org.jfree.chart.JFreeChart object % Arguments = title string, data set, display legend flag, display tooltips flag, generate URLs flag chart3D = org.jfree.chart.ChartFactory.createPieChart3D('Programming languages', dataset, true, true, false); % Update some chart properties plot3D = chart3D.getPlot; % an org.jfree.chart.plot.PiePlot3D obj plot3D.setForegroundAlpha(0.7); % set transparency level % Finally, place the chart in a Swing-compliant panel and display using javacomponent jPanel = org.jfree.chart.ChartPanel(chart3D); [jp,hp] = javacomponent(jPanel,[20,20,300,300],gcf);
…and similarly for a 2D exploding pie-chart (no need to recreate the panel – simply point it to the new chart using jPanel.setChart and the entire figure is automatically redrawn):
chart2D = org.jfree.chart.ChartFactory.createPieChart('Programming languages', dataset, true, true, false); plot2D = chart2D.getPlot; % an org.jfree.chart.plot.PiePlot obj plot2D.setExplodePercent(0,0.6); % 1st value, 60% outward plot2D.setExplodePercent(3,0.30); % 4th value, 30% outward jPanel.setChart(chart2D);
JFreeChart has some limitations compared to Matlab plots, but it can do things that are extremely difficult to achieve in Matlab, as shown in the following screenshots:
Just in case you were wondering – these charts and gauges can easily be made interactive and animated. Adding them within a Matlab figure panel provides a wonderful GUI interaction experience.
Bottom line: I started out looking for a simple infographic for work, and ended up wasting hours playing around with JFreeeChart… Another day’s work down the drain.
Have you used JFreeChart (or any other 3rd-party charting library) in your code? If you have (or if you find some interesting Matlab-related infographic), please tell us about it in a comment.
- Plotly graphs Plotly charts can be created and customized in Matlab. ...
- Pinning annotations to graphs Annotation object can be programmatically set at, and pinned-to, plot axes data points. ...
- Specialized Matlab plots The new MathWorks Plot Gallery provides access to some plotting examples on the File Exchange. Numerous others are available, extending the customizability of Matlab graphics. ...
- JGraph and BDE This article shows how to display graph-theory diagrams in Matlab using several different Java libraries...
- Matlab-Java interface using a static control The switchyard function design pattern can be very useful when setting Matlab callbacks to Java GUI controls. This article explains why and how....
- Editable combo-box Matlab's popup menu (combo-box) control is quite limited in its abilities. This article explains how we can work around these limitations. ...