image thumbnail

DifferenceChart Demo

by

 

Demonstration of DifferenceChart in TimeSeries. Uses JFreeChart and Matlab

differencechart_demo.m
function differencechart_demo
% Multiple TimeSeriesDemo 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 the different between 2 random series 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 sample Code 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/21 



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


%%
import java.awt.Color;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYDifferenceRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

%% Start

series_1 = createDataset('Random 1', 100, org.jfree.data.time.Day(java.util.Date), 200 );  % Create new Datasetseries
series_2 = createDataset('Random 2', 95, org.jfree.data.time.Day(java.util.Date), 200 );  % Create new Datasetseries

% Combine both datasetseries to one dataset with 2 series
dataset1 =  series_1;
dataset1.addSeries(series_2.getSeries(0));  % --> now, there is only one dataset

% generate chart and edit chart settings
chart = org.jfree.chart.ChartFactory.createTimeSeriesChart('DifferenceChartDemo', 'Time', 'Value', dataset1, true, true, false);
background_color = chart.getBackgroundPaint;
chart.setBackgroundPaint(background_color.white); 

% plot object of chart editing
plot_obj = chart.getXYPlot();

% Specify the DifferenceRenderer
plot_obj.setRenderer(XYDifferenceRenderer(Color.green, Color.red, false)); % is random 1 > random 2 (yes--> green, no --> red)

% Other Plot-stuff
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);

plot_obj.setBackgroundPaint(Color.lightGray);
plot_obj.setDomainGridlinePaint(Color.white);
plot_obj.setRangeGridlinePaint(Color.white);
plot_obj.setForegroundAlpha(0.7);  




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


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

Contact us