image thumbnail

Dialplot Demo 2

by

 

ArcDialExample of using JFreeChart and Matlab

dialdemo2.m
function  dialdemo2
% Dialemo 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 Dial 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 modify the values for the displayed value by using the sliders.
%
% 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


%% Dialdemo2
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Point;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.chart.plot.dial.DialBackground;
import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.chart.plot.dial.DialCap;
import org.jfree.chart.plot.dial.DialPointer;
import org.jfree.chart.plot.dial.ArcDialFrame;
import org.jfree.chart.plot.dial.StandardDialFrame;
import org.jfree.chart.plot.dial.StandardDialScale;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.StandardGradientPaintTransformer;



%% Start
 this.dataset = DefaultValueDataset(50);  % Create Dataset
 % get data for diagrams
 plot = DialPlot();                       % Create DialplotObject
 plot.setView(0.78, 0.37, 0.22, 0.26);    % Setting Viewing Parameters
 plot.setDataset(this.dataset);           % Add dataset to plot
 
 
 dialFrame = ArcDialFrame(-10.0, 20.0);   % Create Frame for special Plot
 dialFrame.setInnerRadius(0.70);          % Set InnerRadius
 dialFrame.setOuterRadius(0.90);          % Set OuterRadius

 dialFrame.setForegroundPaint(Color.darkGray);     % Set ForgroundColor of the frame
 dialFrame.setStroke(java.awt.BasicStroke(1.0));   % Set the Stroke of the frame
 plot.setDialFrame(dialFrame);                     % Set the Frame to the plot

 gp = GradientPaint(Point(), Color(255/255, 0/255, 255/255), Point(), Color(120/255, 120/255, 120/255));   % Create Gradient-Color for DialBackground
 sdb = DialBackground(gp);          % Set the Color to DialBackground

 sdb.setGradientPaintTransformer(StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL));   % Set the GradiensPainTransformer to the DialBackground
 plot.addLayer(sdb);           % add the DialBackGround to the plot
  
 scale = StandardDialScale;    % Create the Scale
 % Setting Scale Parameter
 scale.setLowerBound(0);       
 scale.setUpperBound(100);
 scale.setStartAngle(-8);
 scale.setExtent(16); 
 scale.setTickRadius(0.82);
 scale.setTickLabelOffset(-0.04);
 scale.setMajorTickIncrement(25.0);
 %scale.setTickLabelPaint();
 plot.addScale(0, scale);     % Add scale to plot
  
 needle = javaObjectEDT('org.jfree.chart.plot.dial.DialPointer$Pin',0);  % create needle of pin-type
 needle.setRadius(0.84);   % set needle-radius
 plot.addLayer(needle);    % add to plot
 
%% Create Chart Area with Panel
chart1 = JFreeChart(plot);
chart1.setTitle('Dial Demo 2');
cp1 =  ChartPanel(chart1);

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


% Matlab-Slider
sh = uicontrol(fh,  'Style','slider',...
                    'Max',100,'Min',0,'Value',50,...
                    'SliderStep',[0.01 0.01],...
                    'Units','normalized', ...
                    'Position',[0.01 0.01 0.98 0.05], ...
                    'UserData', {plot}, ...                       % save the handle of the plot-object to Userdata to change values
                    'Callback',@sh_callback2);
            
            

%% Slider Callback for Changing Values
function sh_callback2(varargin)

hObject = varargin{1,1};  
% disp(['Slider moved to ' num2str(get(hObject,'Value'))]);   % 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

% Update Frame
plot_h.setDataset(org.jfree.data.general.DefaultValueDataset(get(hObject,'Value')));   % change value of dataset







            
            











Contact us