JFreeChart graphs and gauges

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.

JFreeChart

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);

JFreeChart 3D pie chart

JFreeChart 3D pie chart

…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 2D pie chart

JFreeChart 2D pie chart

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:

JFreeChart Gantt Charts

JFreeChart Dial Gauges

JFreeChart Custom Gauges

JFreeChart Ring Charts

Few of the many JFreeChart charts and gauges

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.

Related posts:

  1. Plotly graphs Plotly charts can be created and customized in Matlab. ...
  2. Plotly graphs in IPython Notebook Plotly graphs can be embedded in an IPython Notebook directly from Matlab. ...
  3. 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. ...
  4. Pinning annotations to graphs Annotation object can be programmatically set at, and pinned-to, plot axes data points. ...
  5. JGraph and BDE This article shows how to display graph-theory diagrams in Matlab using several different Java libraries...
  6. 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....

Categories: GUI, Java, Low risk of breaking in future versions

Tags: , ,

Bookmark and SharePrint Print

17 Responses to JFreeChart graphs and gauges

  1. Scott Koch says:

    Hi Yair, interesting post.

    I’ve played with jgraph (http://www.jgraph.com/jgraph.html). It’s more of a graphing (flow diagram) tool originally meant to extend jtree. I thought it might be useful in creating a simple “visual programing” interface akin to Simulink. It works almost as advertised except for dragging vertices around. For some reason, no matter how hard I try, I can’t seem to get that behavior enabled when using Matlab (it works fine in pure java). Maybe it has something to do with DnD but it still eludes me.

    Anyhow, for anyone interested, here’s some code:

    javaaddpath('jgraphx.jar')
     
    %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);
  2. Hi Yair, this is the best blog ever! (Second is http://www.advancedmcode.org/)

    Before I read this post I didn’ t use any other graphic component. But now I’ll use JFreeChart and its capabilities for cool charting. I’m deeply impressed of the “easy” way how multiple axis charts can be produced and designed by JFreeChart.

    Here is some code, oriented to the original demo of JFreeChart.
    Possible result can be seen here: http://wwwpub.zih.tu-dresden.de/~s9034647/multipleAxis.png

    function multiple_axis_demo
    % TimeSeriesDemo alá JFreeGraph-Demo
    % see best blog ever for more information: http://undocumentedmatlab.com/blog/jfreechart-graphs-and-gauges/
    %
    % need to download and install first: JFreeChart 1.0.13 and JFreeCommon 1.0.16
    % (http://www.jfree.org/jfreechart/download.html)
     
    %% settings:
    % change path to your local JFreeChart-Insatllation first
    javaaddpath C:/Users/sk/Documents/MATLAB/jfreechart-1.0.13/lib/jcommon-1.0.16.jar
    javaaddpath C:/Users/sk/Documents/MATLAB/jfreechart-1.0.13/lib/jfreechart-1.0.13.jar
     
    %% Start
    dataset1 = createDataset('1. TSeries', 100, org.jfree.data.time.Minute, 200 );
     
    % generate chart and edit chart settings
    chart = org.jfree.chart.ChartFactory.createTimeSeriesChart('Multiple Axis Demo 1', 'Time of Day', 'Primary Range Axis', dataset1, true, true, false);
    background_color = chart.getBackgroundPaint;
    chart.setBackgroundPaint(background_color.white); 
     
    % plot object of chart editing
    plot_obj = chart.getXYPlot();
    plot_obj.setOrientation(org.jfree.chart.plot.PlotOrientation.VERTICAL);
    plot_obj.setBackgroundPaint(background_color.lightGray);
    plot_obj.setDomainGridlinePaint(background_color.white);
    plot_obj.setRangeGridlinePaint(background_color.white);
    %axis_spacer = plot_obj.getAxisOffset;
    plot_obj.setAxisOffset(org.jfree.ui.RectangleInsets(5,5,5,5));
    plot_obj.getRangeAxis().setFixedDimension(15.0);
    Standard_renderer = org.jfree.chart.renderer.xy.XYLineAndShapeRenderer(true, false);
    Standard_renderer.setSeriesPaint(0, background_color.black);
    renderer          = plot_obj.getRenderer;
    % renderer          = plot_obj.getRenderer;
    renderer.setPaint(background_color.black);
    %plot_obj.setRenderer(0, Standard_renderer);
     
    %% AXIS 2 
    axis2 = org.jfree.chart.axis.NumberAxis('Range Axis 2');
    axis2.setAutoRangeIncludesZero(false);
    axis2.setLabelPaint(java.awt.Color(255/255,0,0));
    axis2.setTickLabelPaint(background_color.red);
    plot_obj.setRangeAxis(1, axis2);
    plot_obj.setRangeAxisLocation(1, org.jfree.chart.axis.AxisLocation.BOTTOM_OR_LEFT);
    % create new Dataset
    dataset2 = createDataset('2. TSeries', 1000, org.jfree.data.time.Minute, 170 );
    plot_obj.setDataset(1, dataset2); 
    plot_obj.mapDatasetToRangeAxis(1,1);
    renderer2 = org.jfree.chart.renderer.xy.XYLineAndShapeRenderer(true, false);
    renderer2.setSeriesPaint(0, background_color.red);
    plot_obj.setRenderer(1, renderer2);
     
    %% AXIS 3 
    axis3 = org.jfree.chart.axis.NumberAxis('Range Axis 3');
    axis3.setLabelPaint(java.awt.Color(0,0,255/255));
    axis3.setTickLabelPaint(background_color.blue);
    plot_obj.setRangeAxis(2, axis3);
    % create new Dataset
    dataset3 = createDataset('3. TSeries', 10000, org.jfree.data.time.Minute, 170 );
    plot_obj.setDataset(2, dataset3);
    plot_obj.mapDatasetToRangeAxis(2,2);
    renderer3 = org.jfree.chart.renderer.xy.XYLineAndShapeRenderer(true, false);
    renderer3.setSeriesPaint(0, background_color.blue);
    plot_obj.setRenderer(2, renderer3);
     
    %% AXIS 4
    axis4 = org.jfree.chart.axis.NumberAxis('Range Axis 4');
    axis4.setLabelPaint(java.awt.Color(0,255/255,0));
    axis4.setTickLabelPaint(background_color.green);
    plot_obj.setRangeAxis(3, axis4);
    % create new Dataset
    dataset4 = createDataset('4. TSeries', 25, org.jfree.data.time.Minute, 200 );
    plot_obj.setDataset(3, dataset4);
    plot_obj.mapDatasetToRangeAxis(3,3);
    renderer4 = org.jfree.chart.renderer.xy.XYLineAndShapeRenderer(true, true);   % Line and Marker
    renderer4.setSeriesPaint(0, background_color.green);
    plot_obj.setRenderer(3, renderer4);
     
    %% Show graph
    jPanel2 = org.jfree.chart.ChartPanel(chart);
    figure;
    [jp,hp] = javacomponent(jPanel2,[20,20,500,500],gcf);
     
    % Function for TimeSeries-Data Generation
    function dataset = createDataset(datasetname, value, startperiod, anzahl_werte )
    series = org.jfree.data.time.TimeSeries(java.lang.String(datasetname));  % create TimeSeries
    for i =0:1:anzahl_werte
        series.add(startperiod, value);
        startperiod = startperiod.next();
        value = value * (1 + (rand(1) - 0.495) / 10.0);
    end;
     
    dataset_timeseries = org.jfree.data.time.TimeSeriesCollection(series);   % dataset generation
    dataset_timeseries.removeAllSeries
    dataset_timeseries.addSeries(series);
    dataset = dataset_timeseries;

    Multi-axis JFreeChart graph

    Addendum Feb 14, 2011: The code above was uploaded to the Matlab File Exchange today (here), along with a few other JFreeChart-based plotting utilities (here).

  3. Yair also asked for Matlab-related infographics, as well as other 3.rd party used java charting libraries. Because it is a pretty cool feature I’ll show you an interactive html-document produced by Matlab (examples can be found here: http://wwwpub.zih.tu-dresden.de/~s9034647/peaksurface.html).
    I use some Java-based stuff for presentations of interactive 3D data produced by Matlab. The software, which allows that is called Javaview (http://www.javaview.de).
    The way it works is, that the 3D-data of the Matlab figure can be exported to the *.stl file format (http://en.wikipedia.org/wiki/STL_(file_format)). This file can be opened by javaview, then you can edit the graph the way you like and export it as an html-document. An alternative way is to use symbolic math toolbox to generate javaview file (jvd, jvx) and generate from within Matlab the html document.
    For further information you may have a look on my MCFE: http://www.mathworks.com/matlabcentral/fileexchange/authors/92825

  4. Jan Vidar says:

    Hi,

    It would be interesting to see your “graphic presentation of Matlab’s position in the computer programming world” – Matlab at 52 % ? That sounds like a lot?

    Jan

    • @Jan – the numbers are fake of course. I was just using it to illustrate the point of using JFreeChart.

      And before anyone else jumps – yes, I know that Matlab has a built-in pie chart. Again, I was just using it to illustrate JFreeChart. Believe me when I say that it has many features that are not available in regular Matlab graphs.

  5. toto says:

    hi, thank you for this tutorial. nice tutorial about matlab ..

  6. Pingback: Specialized Matlab plots | Undocumented Matlab

  7. Reza says:

    Wow
    This library is great and amazing….
    but I try Sven Körner function, when I put the mouse cursor on line plot for only one plot just data will be display under the cursor, and the others nothing happend…
    How can I get data for other line plot by cursor?

    Thanks so much…

  8. Reza says:

    Hi dear Yair
    in the Sven posted file in this page I tried to change the time period to my sapmles that belongs to long ago I couldn’t.
    in the command lines
    series.add(startperiod, value);
    startperiod = startperiod.next();
    the program takes current system time and plot data.
    How can I set my data on the plot according to my sample time that takes in past?

  9. Mathew says:

    Hi Yair,
    I bought your book.I should say, its well documented in revealing the hidden powers of MATLAB. I am tried to add Jfreecharts.
    But not yet succesful. Can you please help me with yur advise. Attached is the o/p from command window.
    Thanks in advance

    >> javaaddpath C:\Users\c_mathewj\Desktop\Matlab_java\jfreechart-1.0.15\jfreechart-1.0.15\lib\jcommon-1.0.18
    Warning: Invalid file or directory
    ‘C:\Users\c_mathewj\Desktop\Matlab_java\jfreechart-1.0.15\jfreechart-1.0.15\lib\jcommon-1.0.18′.
    > In javaclasspath>local_validate_dynamic_path at 276
    In javaclasspath>local_javapath at 184
    In javaclasspath at 119
    In javaaddpath at 69
    >> javaaddpath C:\Users\c_mathewj\Desktop\Matlab_java\jfreechart-1.0.15\jfreechart-1.0.15\lib\jfreechart-1.0.15
    Warning: Invalid file or directory
    ‘C:\Users\c_mathewj\Desktop\Matlab_java\jfreechart-1.0.15\jfreechart-1.0.15\lib\jfreechart-1.0.15′.
    > In javaclasspath>local_validate_dynamic_path at 276
    In javaclasspath>local_javapath at 184
    In javaclasspath at 119
    In javaaddpath at 69
    >> javaaddpath C:\Users\c_mathewj\Desktop\Matlab_java\jfreechart-1.0.15\jfreechart-1.0.15\lib\

    >> dataset = org.jfree.data.general.DefaultPieDataset;
    Undefined variable “org” or function “org.jfree.data.general.DefaultPieDataset”.

    • Read the output – It tells you right there and there “Warning: Invalid file or directory”

    • Matthew says:

      The warning came when i tried to include the directory with the file name. Please check the last two commands. Still it shows undefined variable org

    • you did not follow the instructions in the article. you need to javaaddpath the jar files directly, not their folder

    • Matthew says:

      Hi Yair,
      Thanks for the guidance. I am totally new to java on MATLAB. The file extension was missing on the add path command.
      It works now.

      Regards
      Matthew

  10. Pingback: Plotly graphs | Undocumented Matlab

Leave a Reply

Your email address will not be published. Required fields are marked *

*

<pre lang="matlab">
a = magic(3);
sum(a)
</pre>