Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

JFreeChart graphs and gauges

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. 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

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

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

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. Pinning annotations to graphs – Annotation object can be programmatically set at, and pinned-to, plot axes data points. ...
  4. 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. ...
  5. Transparent uipanels – Matlab uipanels can be made transparent, for very useful effects. ...
  6. Customizing Matlab uipanels – Matlab uipanel controls can be customized using Java in ways that are impossible with plain Matlab. ...
GUI Java JFreeChart
Print Print
« Previous
Next »
32 Responses
  1. Scott Koch January 17, 2011 at 16:27 Reply

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

    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. Sven Körner January 21, 2011 at 07:55 Reply

    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;

    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. Sven Körner January 21, 2011 at 15:31 Reply

    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

    • Yair Altman January 22, 2011 at 09:23 Reply

      thanks Sven – interesting indeed 🙂

  4. Jan Vidar January 25, 2011 at 12:18 Reply

    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

    • Yair Altman January 25, 2011 at 13:50 Reply

      @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 March 11, 2011 at 03:47 Reply

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

  6. Specialized Matlab plots | Undocumented Matlab April 11, 2012 at 10:45 Reply

    […] 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 […]

  7. Reza February 9, 2013 at 10:12 Reply

    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 February 11, 2013 at 11:08 Reply

    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?

    • Yair Altman February 11, 2013 at 14:21 Reply

      @Reza – try leaving Sven a comment on the relevant FEX page, or send him a query using his FEX homepage

  9. Mathew August 14, 2013 at 07:10 Reply

    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”.

    • Yair Altman August 14, 2013 at 07:12 Reply

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

      • Matthew August 15, 2013 at 23:34

        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

      • Yair Altman August 16, 2013 at 00:06

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

      • Matthew August 19, 2013 at 02:26

        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. Plotly graphs | Undocumented Matlab December 4, 2013 at 18:18 Reply

    […] 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 […]

  11. Chad March 29, 2015 at 16:10 Reply

    Hi 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.

    % First the dial plot:
    plot = DialPlot();
    plot.setBackgroundAlpha(0);
    ...
    % Then the chart:
    chart1 = JFreeChart(plot);
    chart1.setBackgroundImageAlpha(0);
    chart1.setBackgroundPaint(Color(0,0,0,1));
    ...
    % Then use chartpanel class (IS THIS WHERE TRANSPARENCY GETS LOST?)
    cp1 =  ChartPanel(chart1);
    ...
    % Then adding it to the a figure
    fh = figure('position',[850, 300,  600,  600]);
    jp = jcontrol(fh, cp1,'Position',[.4/2, .45/2, .6, .6]);

    % First the dial plot: plot = DialPlot(); plot.setBackgroundAlpha(0); ... % Then the chart: chart1 = JFreeChart(plot); chart1.setBackgroundImageAlpha(0); chart1.setBackgroundPaint(Color(0,0,0,1)); ... % Then use chartpanel class (IS THIS WHERE TRANSPARENCY GETS LOST?) cp1 = ChartPanel(chart1); ... % Then adding it to the a figure fh = figure('position',[850, 300, 600, 600]); jp = jcontrol(fh, cp1,'Position',[.4/2, .45/2, .6, .6]);

    Thanks for any help you may be able to offer.
    Chad

    • Yair Altman March 30, 2015 at 00:38 Reply

      @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).

      • Chad March 31, 2015 at 14:03

        @ 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.

  12. Gilles July 13, 2015 at 00:51 Reply

    Hi 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.

    javaaddpath('O:UsersgcrMATLABjcommon-1.0.23jcommon-1.0.23jcommon-1.0.23.jar');
    javaaddpath('O:UsersgcrMATLABjfreechart-1.0.19jfreechart-1.0.19libjfreechart-1.0.19.jar');

    javaaddpath('O:UsersgcrMATLABjcommon-1.0.23jcommon-1.0.23jcommon-1.0.23.jar'); javaaddpath('O:UsersgcrMATLABjfreechart-1.0.19jfreechart-1.0.19libjfreechart-1.0.19.jar');

    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

    • Yair Altman July 13, 2015 at 02:36 Reply

      @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.txt in your startup folder) , rather than to the dynamic path (via javaaddpath).

      If you found my book(s) useful, please add positive feedbacks on Amazon for the benefit of others – thanks!
      * Matlab-Java Programming Secrets book
      * Matlab Performance Tuning book

      I plan to write about displaying interactive Matlab GUI in a webpage later this year. Stay tuned!

  13. Anilkumar Jangir November 17, 2015 at 00:50 Reply

    Hi,
    Can you please provide few sample code for gauge chart.

  14. Ivan May 14, 2016 at 00:17 Reply

    Hello,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!!

    • Yair Altman May 14, 2016 at 21:10 Reply

      @Ivan – you probably forgot the javaaddpath step (read the main article above for details).
      If you need dedicated assistance for your specific project, email me for a consulting request.

    • Ivan May 23, 2016 at 20:17 Reply

      Thank 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??

      • Yair Altman May 24, 2016 at 10:32

        @Ivan – read my reply above. I am not a free public service.

      • Ivan May 26, 2016 at 21:58

        Of 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!!

  15. Leo October 19, 2016 at 18:52 Reply

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

  16. Cristian Jecu July 21, 2017 at 00:04 Reply

    Hello,
    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.

    % New figure
    fh = figure('Units','normalized','position',[0.1,0.1,  0.2,  0.4]);
     
    % ChartPanel with JControl
    jp = jcontrol(fh, cp1,'Position',[0.01 0.07 0.98 0.88]);

    % New figure fh = figure('Units','normalized','position',[0.1,0.1, 0.2, 0.4]); % ChartPanel with JControl jp = jcontrol(fh, cp1,'Position',[0.01 0.07 0.98 0.88]);

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

    Thank you in advance for your time

    • Yair Altman July 21, 2017 at 08:23 Reply

      @Christian – you cannot place controls in axes, but you can place them inside panels and uitabs, not just figures.

    • Cristian Jecu July 22, 2017 at 19:25 Reply

      Yair,
      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

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitable (6) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top