**- Undocumented Matlab - https://undocumentedmatlab.com -**

JFreeChart graphs and gauges

Posted By Yair Altman On January 17, 2011 | __32 Comments__

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 ^{[3]}. Unfortunately, it’s not technology-oriented. I am not aware of technology-related IG sites, although Online Schools ^{[4]} has a technology section with some cool IGs (including one about the evolution of computer programming ^{[5]} 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 ^{[6]}, 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 ^{[7]}), 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 ^{[8]} 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 ^{[9]}.

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

32 Comments (Open | Close)

Article printed from Undocumented Matlab: **https://undocumentedmatlab.com**

URL to article: **https://undocumentedmatlab.com/blog/jfreechart-graphs-and-gauges**

URLs in this post:

[1] Image: **http://undocumentedmatlab.com/feed/**

[2] **email feed**: **http://undocumentedmatlab.com/subscribe_email.html**

[3] CoolInfographics: **http://www.coolinfographics.com/**

[4] Online Schools: **http://www.onlineschools.org/**

[5] evolution of computer programming: **http://www.onlineschools.org/blog/programming/**

[6] JFreeChart: **http://www.jfree.org**

[7] some Matlab developers: **https://www.mathworks.com/matlabcentral/newsreader/search_results?search_string=jfreechart&dur=all**

[8] download: **http://sourceforge.net/projects/jfreechart/files/**

[9] comment: **https://undocumentedmatlab.com/blog/jfreechart-graphs-and-gauges/#respond**

[10] Adding a context-menu to a uitree : **https://undocumentedmatlab.com/blog/adding-context-menu-to-uitree**

[11] Uicontrol callbacks : **https://undocumentedmatlab.com/blog/uicontrol-callbacks**

[12] Customizing Matlab labels : **https://undocumentedmatlab.com/blog/customizing-matlab-labels**

[13] Customizing editboxes : **https://undocumentedmatlab.com/blog/customizing-editboxes**

[14] Hyperlink text labels : **https://undocumentedmatlab.com/blog/hyperlink-text-labels**

[15] Smart listbox & editbox scrollbars : **https://undocumentedmatlab.com/blog/smart-listbox-editbox-scrollbars**

[16] : **http://www.jgraph.com/jgraph.html**

[17] : **http://www.advancedmcode.org/**

[18] : **http://wwwpub.zih.tu-dresden.de/~s9034647/multipleAxis.png**

[19] : **http://www.mathworks.com/matlabcentral/fileexchange/30405**

[20] : **http://www.mathworks.com/matlabcentral/fileexchange/authors/92825**

[21] : **http://wwwpub.zih.tu-dresden.de/~s9034647/peaksurface.html**

[22] : **http://www.javaview.de**

[23] : **http://en.wikipedia.org/wiki/STL_(file_format)**

[24] : **http://amazon.com/Undocumented-Secrets-MATLAB-Java-Programming-Altman/product-reviews/1439869030/ref=cm_cr_dp_see_all_summary**

[25] : **http://amazon.com/Accelerating-MATLAB-Performance-speed-programs/product-reviews/1482211297/ref=cm_cr_dp_see_all_summary**

Click here to print.

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.

32 Comments To "JFreeChart graphs and gauges"

#1 CommentByScott KochOn January 17, 2011 @ 4:27 pmHi Yair, interesting post.

I’ve played with jgraph (

^{[16]}). 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:

#2 CommentBySven KörnerOn January 21, 2011 @ 7:55 amHi Yair, this is the best blog ever! (Second is

^{[17]})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:

^{[18]}The code above was uploaded to the Matlab File Exchange today (Addendum Feb 14, 2011:^{[19]}), along with a few other JFreeChart-based plotting utilities (^{[20]}).#3 CommentBySven KörnerOn January 21, 2011 @ 3:31 pmYair 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:

^{[21]}).I use some Java-based stuff for presentations of interactive 3D data produced by Matlab. The software, which allows that is called Javaview (

^{[22]}).The way it works is, that the 3D-data of the Matlab figure can be exported to the *.stl file format (

^{[23]}). 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:

^{[20]}#4 CommentByYair AltmanOn January 22, 2011 @ 9:23 amthanks Sven – interesting indeed

#5 CommentByJan VidarOn January 25, 2011 @ 12:18 pmHi,

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

#6 CommentByYair AltmanOn January 25, 2011 @ 1:50 pm@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

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.pie#7 CommentBytotoOn March 11, 2011 @ 3:47 amhi, thank you for this tutorial. nice tutorial about matlab ..

#8 PingbackBySpecialized Matlab plots | Undocumented MatlabOn April 11, 2012 @ 10:45 am[…] plots that I would like to highlight today is based on the open-source JFreeChart, that I have described here last year. Sven Körner has used this to provide a dozen different customized Matlab plots […]

#9 CommentByRezaOn February 9, 2013 @ 10:12 amWow

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…

#10 CommentByRezaOn February 11, 2013 @ 11:08 amHi 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?

#11 CommentByYair AltmanOn February 11, 2013 @ 2:21 pm@Reza – try leaving Sven a comment on the relevant FEX page, or send him a query using his FEX homepage

#12 CommentByMathewOn August 14, 2013 @ 7:10 amHi 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”.

#13 CommentByYair AltmanOn August 14, 2013 @ 7:12 amRead the output – It tells you right there and there “Warning: Invalid file or directory”

#14 CommentByMatthewOn August 15, 2013 @ 11:34 pmThe 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

#15 CommentByYair AltmanOn August 16, 2013 @ 12:06 amyou did not follow the instructions in the article. you need to javaaddpath the jar files directly, not their folder

#16 CommentByMatthewOn August 19, 2013 @ 2:26 amHi 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

#17 PingbackByPlotly graphs | Undocumented MatlabOn December 4, 2013 @ 6:18 pm[…] who will describe the integration of Plotly graphs and charts in Matlab. Unlike the open-source JFreeChart and Waterloo plotting libraries that were described here in the past, Plotly is a commercial (not […]

#18 CommentByChadOn March 29, 2015 @ 4:10 pmHi Yair,

Thanks for your book and blog on using java with Matlab. I am using the dial gauge from jfreechart, and I’m overlaying multiple gauges in my UI layout to give a more realistic look of an assembled dashboard. However, to do this and make it look good, I need to have full transparency of the background for each chart. This is where I am stuck. Below, I’ve attempted to illustrate the key parts of the code that create these objects and I would expect to assign transparency. From what I’ve read, it seems like folks are able to get this to work for java, so I’m wondering if its a limitation of Matlab’s figure window? I appreciate any help.

Thanks for any help you may be able to offer.

Chad

#19 CommentByYair AltmanOn March 30, 2015 @ 12:38 am@Chad – Matlab uses an opaque panel for displaying Java components, so even if the component is made transparent, it would still just show the opaque panel beneath it. Perhaps there is a way around this, but I am not aware of it, at least on HG1 (R2014a or earlier), I haven’t tried really hard on HG2 (R2014b onward).

#20 CommentByChadOn March 31, 2015 @ 2:03 pm@ Yair, I really appreciate the quick response, and your comment about the opaque panel for Java components is valuable to know. For what I’m trying to achieve, I believe the way around this is to create the dial gauge assembly (using dial gauges from JFreeChart) using addLayer() to “stack” objects. It will make the code less modular and somewhat messier, but I think it should do the trick unless there’s something I’m overlooking. Thanks again.

#21 CommentByGillesOn July 13, 2015 @ 12:51 amHi Yair,

First of all, thank you for your fantastic book “Undocumented Secrets of MATLAB – Java programming”. It has really improved my programming skills. I have a problem that I don’t succeed to solve.

I created a program which uses jfreechart. So at the beginning of the program, I load jfreechart-1.0.13.jar and jcommon-1.0.16.jar by using javaaddpath i.e.

The first time I launch MATLAB and my program, everything works well but if I close the GUI and re-launch the program, I get a error message: Undefined variable org or class org.jfree.data.category.DefaultCategoryDataset. I really don’t understand why I get this error when I try to re-launch my program. So, I have to close MATLAB. re-open MATLAB and launch the program. Any suggestions?

By the way, I would to add my MATLAB code to a website. I don’t have the compiler and the (C#, java)-builder. Do you know how we can integrate MATLAB code to a website. If you write a book about how integrate MATLAB code to a website, I will be the first to buy it!

Again, congratulations for your fantastic blog,

Best regards,

Gilles

#22 CommentByYair AltmanOn July 13, 2015 @ 2:36 am@Gilles – perhaps your program bypasses the javaaddpath part in its startup if some condition occurs. Use Matlab editor’s integrated debugger to find out what exactly happens.

In general, it might be better to add these 2 JAR files to your static Java classpath (by editing

javaclasspath.txtin your startup folder) , rather than to the dynamic path (via).javaaddpathIf you found my book(s) useful, please add positive feedbacks on Amazon for the benefit of others – thanks!

*

^{[24]}*

^{[25]}I plan to write about displaying interactive Matlab GUI in a webpage later this year. Stay tuned!

#23 CommentByAnilkumar JangirOn November 17, 2015 @ 12:50 amHi,

Can you please provide few sample code for gauge chart.

#24 CommentByIvanOn May 14, 2016 @ 12:17 amHello,thank you it is fantastic! I’m trying to use the dialdemo4 in MATLAB, but I don’t understand why it gives me the error: ‘Undefined function or variable ‘DefaultValueDataset’. or the same thing with Dialplot fo example, but I downloaded all of these and I have in my current folder, I’m new with java so I really don’t know how to solve this problem also if probably it is something easy. Thank you!!

#25 CommentByYair AltmanOn May 14, 2016 @ 9:10 pm@Ivan – you probably forgot the

step (read the main article above for details).javaaddpathIf you need dedicated assistance for your specific project, email me for a consulting request.

#26 CommentByIvanOn May 23, 2016 @ 8:17 pmThank you for the availability!! Now I have a new problem, Matlab gives me the error:

”The CLASS function must be called from a class constructor.”

And after days of research I didn’t find what does it mean, can you help me??

#27 CommentByYair AltmanOn May 24, 2016 @ 10:32 am@Ivan – read my reply above. I am not a free public service.

#28 CommentByIvanOn May 26, 2016 @ 9:58 pmOf course you are right I’m sorry; I’m just doing it for my personal curiosity so it’s not necessary to disturb you anymore, however thank you a lot and congratulations for your blog and your books!!

#29 CommentByLeoOn October 19, 2016 @ 6:52 pmHi, Yair

I have a question, its possible add multiple subtask in gantt chart?

#30 CommentByCristian JecuOn July 21, 2017 @ 12:04 amHello,

Very useful, thank you for sharing. I am using the gauges but only in individual figures within a more complex GUI. I made a workaround to update values that are obtained from varargin of the GUI function (from a Simulink model), not the best optimized one, but got the job done.

What I would really like to obtain is to have several gauges in the main GUI figure (separate tabs). Unfortunately I wasn’t able to upload this to existing GUI axes as the jcontrol command will create new children on the specified target that “must”(?) be a figure.

Is it incompatible with axes and works only with figures or am I missing something ?

Thank you in advance for your time

#31 CommentByYair AltmanOn July 21, 2017 @ 8:23 am@Christian – you cannot place controls in axes, but you can place them inside panels and uitabs, not just figures.

#32 CommentByCristian JecuOn July 22, 2017 @ 7:25 pmYair,

Thank you for your fast and correct reply. This really motivated me to learn more about java objects and their integration with Matlab. Now I have a beautiful GUI with what I wasn’t able to obtain from Matalb.

Thank you so much again for sharing and for your answer. I am sure that time will prove that I’ll be visiting your sites for other tricks.

Best regards