image thumbnail

Crosshair Demo

by

 

Demonstration of crosshair in figure. Uses JFreeChart and Matlab

crosshairdemo.m
function crosshairdemo
% CrosshairDemo 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 single domain and range axis plot (Sinus function) with crosshair-functionality 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.   
% With the slider or by mousclick in the chart you can set the position of the crosshair. The actual values of
% the crosshair are displayed in the table.
%
% 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: 0, 64 values
dataset1 = createDatasetxy('Sinus', 0, 64 );

% generate chart 
chart            = org.jfree.chart.ChartFactory.createXYLineChart('CrosshairDemo 1', 'x-Label', 'y-Label', 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('Subtitle - Crosshair'));  

% 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
plot_obj.getRangeAxis().setAutoRangeIncludesZero(false);                      % set Range include Zero to false
plot_obj.getDomainAxis().setUpperBound(dataset1.getDomainUpperBound(1));      % set range upper bound to data maximum

% edit the datseries-design
Standard_renderer = org.jfree.chart.renderer.xy.XYLineAndShapeRenderer(true, false);  % set the renderer
Standard_renderer.setSeriesPaint(0, background_color.red);                            % 1. dataset seriescolor red
renderer          = plot_obj.getRenderer;                                       
%renderer          = Standard_renderer;
renderer.setPaint(background_color.red);                                              % 1. dataset draw red


%% 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.08 0.98 0.91]);         % add the jPanel to figure
jp.MouseClickedCallback = @sh_callback_mouse;                        % add a mouseclickedcallback to the jpanel


%% Datahandling

series   = dataset1.getSeries(0);        % get the first series object of dataset1
nr_items = series.getItemCount-1;        % get number of items
sh_val   = round(double(nr_items)/2);    % get the middle of numer of items


% UiTable for Crosshair-Value
cnames = {'X-Data','Y-Data'};                                        %column-Names for UITable
rnames = {'1. Series'};                                              % there is only one dataseries
dat = [ double(dataset1.getSeries(0).getX(sh_val)) double(dataset1.getSeries(0).getY(sh_val)) ];  % get the data of the actual slider value

% create table
th = uitable('Parent',fh, ...
             'ColumnName',cnames,...
             'Data',dat, ...    
             'RowName',rnames,...
             'Units','normalized', 'Position',[0.01 0.033 0.18  0.045]);

% Matlab-Slider
sh = uicontrol(fh,'Style','slider',...
                'Max',nr_items,'Min',0,'Value',round(double(nr_items)/2),...
                'SliderStep',[1/nr_items   1/nr_items   ],...
                'Units','normalized', ...
                'Position',[0.01 0.01 0.98 0.02], ...
                'UserData', [{plot_obj} {th} ] , ...                       % save the handle of the plot-object and the uitable to Userdata of the slider to change values
                'Callback',@sh_callback2);

% modify GridlineStyle:
plot_obj.setDomainGridlinePaint(background_color.white);
plot_obj.setRangeGridlinePaint(background_color.white);

% add the crosshair:
plot_obj.setDomainCrosshairVisible(true);
plot_obj.setDomainCrosshairLockedOnData(true);
plot_obj.setRangeCrosshairVisible(false);  
plot_obj.setRangeCrosshairLockedOnData(true);
plot_obj.setDomainCrosshairValue(double(dataset1.getSeries(0).getX(sh_val)));       % set Crooshair to the domain Value of actual slider item-position






%% 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
for i =0:1:nr_values
%     series.add(i*0.7, value);
%     value = value * (1 + (rand(1) - 0.495) / 10.0);
    series.add(i*0.1, sin(i*0.1));     % Sinusfunction for value generation
end;
dataset_series = org.jfree.data.xy.XYSeriesCollection(series);        % dataset generation
dataset_series.removeAllSeries
dataset_series.addSeries(series);
dataset = dataset_series;



%% Slider Callback for Changing Crosshair
function sh_callback2(varargin)
hObject = varargin{1,1};  
i       = (get(hObject,'Value')); % Slider Value
% disp(['Slider moved to Item Number ' num2str(i)]);   % diplay stuff in Matlab Command Window

% Get Handle from java plot object
plot_cell = get(hObject,'Userdata' );
plot_h    = plot_cell{1,1};    % handle of plot_object
th        = plot_cell{1,2};    % handle of uitable

% get the Values
xydataset  = plot_h.getDataset();                 % get the actual datset
comparable = xydataset.getSeriesKey(0);           % get the serieskey of first series
series     = xydataset.getSeries(comparable);     % get the first series
x_value    = series.getX(i);                      % get the actual X_value
y_value    = series.getY(i);                      % get the actual Y_value

plot_h.setDomainCrosshairValue(double(x_value));    % change crosshairvalue
set(th,'Data',[ double(x_value) double(y_value)]);  % set values to uitable



%% MouseClicked Callback for Changing Crosshair
function sh_callback_mouse(varargin)
% hObject = varargin{1,1};  
% get the sliderhandle
sh        = findobj(gcf,'Style','slider');   % There is only one slider in the figure
% get handle of uitable and plot_object
plot_cell = get(sh,'Userdata' );
plot_h    = plot_cell{1,1};    % handle of plot_object
th        = plot_cell{1,2};    % handle of uitable

% get the Values 
xydataset  = plot_h.getDataset();                       % get the actual datset
comparable = xydataset.getSeriesKey(0);                 % get the serieskey of first series
series     = xydataset.getSeries(comparable);           % get the first series
pause(0.05);   % if there is no pause the crosshairvalue isn't up to date

d          = plot_h.getDomainCrosshairValue();          % xValue of Datset
i          = series.indexOf(java.lang.Double(d));       % Search for Index of xValue = d

% get Values from Index
x_value    = series.getX(i);                            % get the actual X_value
y_value    = series.getY(i);                            % get the actual Y_value
set(th,'Data',[ double(x_value) double(y_value)]);      % set values to uitable
set(sh,'Value',double(i));                              % set the slider value to series index





 

Contact us