image thumbnail

Easy MultipleXYSeries

by

 

Example Code of MultipleXYSeries using JFreeChart within Matlab

multiple_axis_demo_xy2.m
function multiple_axis_demo_xy2
% MultipleXYSeriesDemo al JFreeGraph-Demo
%
% The code behind is just a demo of what is possible with JFreeChart using it in Matlab. I played a little
% with codesnippets I found on the web and the API-Documentation
% (http://www.jfree.org/jfreechart/api/javadoc/index.html). When  you want to explore the whole functionality,
% I think it is better to buy the JFreeChart Developer Guide (http://www.jfree.org/jfreechart/devguide.html). 
%
% This function shows a multiple domain and range axis plot as an example of JFreeChart
% (http://www.jfree.org/). The Idea to this code is based on the UndocumentedMatlab-Blog of Yair Altman, who
% shows a sampleCode of JFreeChart for creating a PieChart
% (http://undocumentedmatlab.com/blog/jfreechart-graphs-and-gauges/#comments) 
%
% Within the plot you can zoom by pressing the left mouse button and moving the pointer. Also you have some
% properties by right-clicking on the chart.   
%
% Before this demo works, you need to download JFreeChart and make matlab get to know with it. There are 2
% ways you can do this:
% 1. Add the jcommon and jfreechart jar to the dynamic matlab JavaClassPath (uncommented lines in the first
%    cell an change path to your local installation path
% 2. Add the jcommon and jfreechart jar to the static matlab JavaClassPath (see Matlab Help, modify
%    classpath.txt on matlabroot\toolbox\local) 
%
% Finally you must donwload jcontrol from Malcom Lidierth
% (http://www.mathworks.com/matlabcentral/fileexchange/15580-using-java-swing-components-in-matlab).
% 
%
% Bugs and suggestions:
%    Please send to Sven Koerner: koerner(underline)sven(add)gmx.de
% 
% You need to download and install first:
%    http://sourceforge.net/projects/jfreechart/files/1.%20JFreeChart/1.0.13/ 
%    http://sourceforge.net/projects/jfreechart/files/1.%20JFreeChart/1.0.9/
%    http://www.mathworks.com/matlabcentral/fileexchange/15580-using-java-swing-components-in-matlab 
%
% Programmed by Sven Koerner: koerner(underline)sven(add)gmx.de
% Date: 2011/02/14 



%%  JFreeChart to matlab
%%%  Add the JavaPackages to the static javaclasspath (see Matlab Help, modify classpath.txt on
%%%  matlabroot\toolbox\local) or alternativ turn it to the dynamic path (uncomment the next and change path to jFreeeChart) 

% 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

% create dataset, Startvalue: 100, 200 values
dataset1 = createDatasetxy('1. Series', 100, 200 );

% generate chart 
% title: Multiple Axis Demo 2
% x-Label: x-Label
% y-Label: Primary Range Axis
chart            = org.jfree.chart.ChartFactory.createXYLineChart('Multiple Axis Demo 2', 'x-Label', 'Primary Range Axis', dataset1, org.jfree.chart.plot.PlotOrientation.VERTICAL, true, false, false);
background_color = chart.getBackgroundPaint;
chart.setBackgroundPaint(background_color.white);   % Background Color set to white
% cretae chart subtitle
chart.addSubtitle(org.jfree.chart.title.TextTitle('Five datasets, two domain axis and five range axes.'));  


% plot object of chart editing
plot_obj = chart.getXYPlot();
plot_obj.setOrientation(org.jfree.chart.plot.PlotOrientation.VERTICAL);       % set orientation
plot_obj.setBackgroundPaint(background_color.lightGray);                      % set background Color
plot_obj.setDomainGridlinePaint(background_color.white);                      % set gridlinecolor x-direction
plot_obj.setRangeGridlinePaint(background_color.white);                       % set gridlinecolor y-direction
%axis_spacer = plot_obj.getAxisOffset;
plot_obj.setAxisOffset(org.jfree.ui.RectangleInsets(5,5,5,5));                % set axis-offset between multiple axis
plot_obj.getRangeAxis().setFixedDimension(15.0);


% edit the datseries-design
Standard_renderer = org.jfree.chart.renderer.xy.XYLineAndShapeRenderer(true, false);  % set the renderer
Standard_renderer.setSeriesPaint(0, background_color.black);                          % 1. dataset draw black
renderer          = plot_obj.getRenderer;                                       
%renderer          = Standard_renderer;
renderer.setPaint(background_color.black);                                            % 1. dataset draw 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 = createDatasetxy('2. Series', 1000, 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 = createDatasetxy('3. Series', 10000,  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 = createDatasetxy('4. Series', 25, 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);


%% AXIS 5 for Secondary Domain
axis5 = org.jfree.chart.axis.NumberAxis('Range Axis 5');
axis5.setLabelPaint(background_color.cyan);
axis5.setTickLabelPaint(background_color.cyan);
plot_obj.setRangeAxis(4, axis5);

% Second DomainAxis
xAxis2 = org.jfree.chart.axis.NumberAxis('Domain Axis 2');
xAxis2.setLabelPaint(background_color.cyan);
xAxis2.setTickLabelPaint(background_color.cyan);
xAxis2.setAutoRangeIncludesZero(false);                                    
plot_obj.setDomainAxis(1, xAxis2);                                         % set secondary domain axis to plotobj
plot_obj.setDomainAxisLocation(1, org.jfree.chart.axis.AxisLocation.BOTTOM_OR_LEFT);  % location of secondary domain

% create new Dataset for 2. domain
dataset5 = createDatasetxy('5. Series', 25, 185 );
dataset5.getSeries(0).setKey(java.lang.String('5. Series on 2. Domain'));
plot_obj.setDataset(4, dataset5);  % Insert 5. Dataset
plot_obj.mapDatasetToDomainAxis(4, 1);  % set Dataset 5 to Domain Axis 2 (indexing starts with 0)
plot_obj.mapDatasetToRangeAxis(4, 4);   % set Dataset 5 to Range Axis 5 (indexing starts with 0)
renderer5 = org.jfree.chart.renderer.xy.XYLineAndShapeRenderer(true, true);   % Line and Marker
renderer5.setSeriesPaint(0, background_color.cyan);
plot_obj.setRenderer(4, renderer5);

%% Show graph
jPanel2 = org.jfree.chart.ChartPanel(chart);                         % create new panel
fh = figure('Units','normalized','position',[0.1,0.1,  0.7,  0.7]);  % create new figure
jp = jcontrol(fh, jPanel2,'Position',[0.01 0.01 0.98 0.98]);         % add the jPanel to figure


%% TODO
% Crosshair:
% plot_obj.setDomainGridlinePaint(background_color.white);
% plot_obj.setRangeGridlinePaint(background_color.white);
% plot_obj.setDomainCrosshairVisible(true);
% plot_obj.setDomainCrosshairLockedOnData(true);
% plot_obj.setRangeCrosshairVisible(false);  
% plot_obj.setRangeCrosshairLockedOnData(true);

% % %[jp,hp] = javacomponent(jPanel2,[10,10,580,580],gcf);
% % 
% % %[jp, hp] = javacomponent(jPanel2,java.awt.BorderLayout.NORTH,gcf);
% % % Add an Mouse-Listener
% % %jp.addChartMouseListener; 
% % % set Callback for Listener
% % %jp.MouseMovedCallback = @CMMCb;
% % %jp.MouseClickedCallback = @CMMCb;
% % % add Key listener
% % % jp.addKeyListener
% % % jp.KeyReleasedCallback = @CKMCb;




%% Function for XY-SeriesData Generation
function dataset = createDatasetxy(datasetname, value,  nr_values )        
% This function randomly generates datsets with seriesname: datasetname
% The starting value is: value 
% and the number of values are: nr_values
series      =  org.jfree.data.xy.XYSeries(java.lang.String(datasetname));  % create XYSeries
if ~strcmp(datasetname,'5. Series')
    for i =0:1:nr_values
        series.add(i, value);
        value = value * (1 + (rand(1) - 0.495) / 10.0);
    end;
else  
    for i =0:1:nr_values
        series.add(i*4, value);  % modify x-Value to get a new Domain-Range
        value = value * (1 + (rand(1) - 0.495) / 10.0);
    end;
    
end;
dataset_series = org.jfree.data.xy.XYSeriesCollection(series);        % dataset generation
dataset_series.removeAllSeries
dataset_series.addSeries(series);
dataset = dataset_series;

Contact us