JFreeChart satyajohnny@live.com
Dataset:
·
It contains the Data
which we want to display in the Chart
·
We have Dif. Types of
Datasets for Diff.types of Charts
·
The default Dataset is “DefaultPieDataset”
Ex:
DefaultPieDataset
dataset = new DefaultPieDataset();
dataset.setValue("Linux",
29);
dataset.setValue("Mac", 20);
dataset.setValue("Windows",
51);
JFreeChart:
·
This is a class use to
create a Chart
·
We will pass the Dataset
& Chart title to create Chart
Ex:
JFreeChart
chart = createChart(dataset, chartTitle);
ChartPanel:
·
After creation of chart
we have to place that in a place.
·
For that we use
ChartPanel
·
After creating the
chart,we place that chart in chartPanel by passing chart
Ex:
ChartPanel chartPanel
= new ChartPanel(chart);
JPanel:
·
If we want to place chart
in JFrame, we can’t directly add the chart to Jframe
·
We take support of JPanel
·
We add our chartPanel to
JPanel
·
Then JPanel is add to
JFrame
Ex:
jPanel4.setLayout(new BorderLayout());
jPanel4.add(chartpanel,
BorderLayout.NORTH);
this.add(jPanel4);
this.pack();
this.setVisible(true);
ChartFactory:
This
class contains all static methods for creating charts.it returns JfreeChart
object after creating the chart
Sample Example
public class PieChart extends JFrame {
private static final long serialVersionUID = 1L;
public PieChart(String applicationTitle, String chartTitle) {
super(applicationTitle);
// This will create the dataset
PieDataset dataset =
createDataset();
// based on the dataset we create the chart
JFreeChart chart =
createChart(dataset, chartTitle);
// we put the chart into a panel
ChartPanel
chartPanel = new ChartPanel(chart);
// default size
chartPanel.setPreferredSize(new java.awt.Dimension(500,
270));
// add it to our application
setContentPane(chartPanel);
}
//Creates
a sample dataset
private PieDataset createDataset()
{
DefaultPieDataset
result = new DefaultPieDataset();
result.setValue("Linux", 29);
result.setValue("Mac", 20);
result.setValue("Windows", 51);
return result;
}
/** * Creates a chart
*/
private JFreeChart createChart(PieDataset dataset, String title) {
JFreeChart chart = ChartFactory.createPieChart3D(title, // chart title
dataset, // data
true, // include legend
true,
false);
PiePlot3D plot =
(PiePlot3D) chart.getPlot();
plot.setStartAngle(290);
plot.setDirection(Rotation.CLOCKWISE);
plot.setForegroundAlpha(0.5f);
return chart;
}
}
1.Pie Chart:
IMP:
·
DefalultPieDataSet
·
setValue()
·
ChartFactory.createPieChart(-,-,-,-,-)
1.Create Dataset.
DefaultPieDataset pieDataset = new DefaultPieDataset();
2. add the data in the data set by invoking the method
setValue()
DefaultPieDataset pieDataset = new
DefaultPieDataset();
pieDataset.setValue("One", new Integer(10));
pieDataset.setValue("Two", new Integer(20));
pieDataset.setValue("Three", new Integer(30));
3. After added the data in dataset we create the Pie Chart by
invoking the createPieChart() method of ChartFactory
JFreeChart chart = ChartFactory.createPieChart ("Pie
Chart using JFreeChart", pieDataset, true,true,true);
4.Add that Chart to ChartPanel
5.Add ChartPanel to JPanel
6.Add JPanel to JFrame
2.3D Pie Chart Example
Changes:
·
ChartFactory.createPieChart3D
·
PiePlot3D
p=(PiePlot3D)chart.getPlot();
·
p.setForegroundAlpha(0.5f);
1.Create Dataset.
DefaultPieDataset pieDataset = new DefaultPieDataset();
2. add the data in the data set by invoking the method
setValue()
DefaultPieDataset pieDataset = new
DefaultPieDataset();
pieDataset.setValue("One", new Integer(10));
pieDataset.setValue("Two", new Integer(20));
pieDataset.setValue("Three", new Integer(30));
3. After added the data in dataset we create the Pie Chart by
invoking the createPieChart3D() method of ChartFactory
JFreeChart chart =
ChartFactory.createPieChart3D("3D Pie Chart", pieDataset, true, true,
true);
PiePlot3D
p=(PiePlot3D)chart.getPlot();
Above method is used to
get the object of the plot for 3D Pie Chart. For this we have to invoke
JfreeChart class method getPlot(). It returns the reference of Plot but we have
to typecast it as a PiePlot3D.
p.setForegroundAlpha(0.5f);
Above method is used to
set the alpha-transparency for the plot. It takes the float type argument.
4.Add that Chart to ChartPanel
5.Add ChartPanel to JPanel
6.Add JPanel to JFrame
3.Area Chart Example
Changes:
1.Create Dataset.
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
2. add the data in the data set by invoking the method
setValue()
dataset.addValue(4.0, "Science", "Rahul");
dataset.addValue(3.0, "Maths", "Rahul");
dataset.addValue(5.0, "Science", "Vinod");
dataset.addValue(3.0, "Maths", "Rahul");
dataset.addValue(5.0, "Science", "Vinod");
3. After added the data in dataset we create the Pie Chart by
invoking the createAreaChart() method
of ChartFactory
JFreeChart chart = ChartFactory.createAreaChart("Comp","Students", "Marks ",
dataset, PlotOrientation.VERTICAL, true,true, false);
dataset, PlotOrientation.VERTICAL, true,true, false);
4. chart.setBackgroundPaint(Color.yellow);
Above method is used to set the background
color of chart.
5.
chart.getTitle().setPaint(Color.blue);
Above method is used to set the color of
chart title.
6.
CategoryPlot p = chart.getCategoryPlot();
Above method is used to get the object of the
Plot for Bar Chart.
7.
p.setForegroundAlpha(0.4f);
Above method is used to set the alpha-transparency
for the plot. It takes the float type argument.
8.
p.setRangeGridlinePaint(Color.red);
Above method is used to set the color of plot
Gridlines.
9.
CategoryItemRenderer renderer = p.getRenderer();
Above method is used to get the reference to
the renderer for the plot.,
10.
renderer.setSeriesPaint(0,Color.red);
Above method is used set the color of the
area chart.
11.
ChartFrame frame1=new ChartFrame("Bar Chart",chart);
After this we create the object of
ChartFrame. It used to display a chart.
4.XYArea
Chart Example
1.XYSeries series = new
XYSeries("Average Weight");
For defining a set of x,y coordinates we use
an object of XYSeries class.
2.Then we add the data in the XYSeries object
by invoking add().
series.add(20.0, 50.0);
3.
XYDataset xyDataset = new XYSeriesCollection(series);
Now we have to create the object of XYDataset
type of XYSeriesCollection and add the XYSeries object in the dataset.
4. JFreeChart chart =
ChartFactory.createXYAreaChart("XY Chart using JFreeChart",
"Age", "Weight", xyDataset, PlotOrientation.VERTICAL, true,
true, false);
5.After creating the dataset we create the
XYArea Chart by invoking the createXYAreaChart() method. This is a static
method of ChartFactory class and its returns the object of JFreeChart type.This
method syntax is:
6. Public static JFreeChart
createXYAreaChart(java.lang.String title, java.lang.String xAxisLabel,
java.lang.String yAxisLabel, XYDataset dataset, PlotOrientation orientation,
boolean legend, boolean tooltips, boolean urls)
7. ChartFrame frame1=new
ChartFrame("XYArea Chart",chart);
5.XYLine
Chart Example
1.XYSeries series = new
XYSeries("Average Weight");
For defining a set of x,y coordinates we use
an object of XYSeries class.
2.Then we add the data in the XYSeries object
by invoking add() method.
series.add(20.0, 50.0);
3.XYDataset
xyDataset = new XYSeriesCollection(series);
Now we have to create the object of XYDataset
type of XYSeriesCollection and add the XYSeries object in the dataset.
4.JFreeChart
chart = ChartFactory.createXYLineChart("XYLine Chart using
JFreeChart", "Age", "Weight", xyDataset,
PlotOrientation.VERTICAL, true, true, false);
5.After creating the dataset we create the
XYLine Chart by invoking the createXYLineChart() method. This method is a
static method of ChartFactory class and its returns the object of JFreeChart
type.This method syntax is:
6.Public static JFreeChart
createXYLineChart(java.lang.String title, java.lang.String xAxisLabel, java.lang.String
yAxisLabel, XYDataset dataset, PlotOrientation orientation, boolean legend,
boolean tooltips, boolean urls)
7.
ChartFrame frame1=new ChartFrame("XYLine Chart",chart);
After this we create the object of
ChartFrame. It used to display a chart.
6.Bar
Chart Example
1. For
defining a dataset for a Bar
DefaultCategoryDataset dataset = new
DefaultCategoryDataset();
2. After
creating the instance of dataset then we have to add the data in the data set
by invoking the method setValue().
3. setValue(6,
“Marks”, “Rahul”);
4. FreeChart
chart = ChartFactory.createBarChart("BarChart using JFreeChart",
"Student", "Marks", dataset, PlotOrientation.VERTICAL,
true, true, false);
5. After
added the data in dataset we create the Bar chart by invoking the createBarChart()
method.
6. Public
static JFreeChart createBarChart(java.lang.String title, java.lang.String
categoryAxisLabel, java.lang.String valueAxisLabel, CategoryDataset dataset,
PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls);
7. chart.setBackgroundPaint(Color.yellow);
Above method is used to set the color of
chart background
8. chart.getTitle().setPaint(Color.blue);
Above method is used
to set the color of chart title
9. CategoryPlot
p = chart.getCategoryPlot();
Above method is used to get the object of the
Plot for Bar Chart
10. p.setRangeGridlinePaint(Color.red);
Above method is used
to set the color of plot Gridlines
11. ChartFrame
frame1=new ChartFrame("Bar Chart",chart)
7.3D
Bar Chart Example
1. For
defining a dataset for a Bar chart we have to create an object of
DefaultCategoryDataset type :
DefaultCategoryDataset dataset = new
DefaultCategoryDataset();
2. After
creating the instance of dataset then we have to add the data in the data set
by invoking the method setValue(). In this example we show more than one set of
bars for the same chart. This can done by the following modification :
dataset.setValue(6,
"Science", "Rahul");
dataset.setValue(8,
"Maths", "Rahul");
dataset.setValue(5,
"Science", "Deepak");
dataset.setValue(3,
"Maths", "Deepak");
3. First
argument specifies the total marks obtained by the student and the second
argument specify what will appear in the legend to the meaning of the bar.
4. JFreeChart
chart = ChartFactory.createBarChart3D("Comparison between Students",
"Student", "Marks", dataset, PlotOrientation.VERTICAL,
true, true, false);
5. After
added the data in dataset we create the 3D Bar chart by invoking the
createBarChart3D() method.
6. This
method is a static method of ChartFactory class and its returns the object of
JFreeChart type.This method syntax is:
7. Public
static JFreeChart createBarChart3D(java.lang.String title, java.lang.String
categoryAxisLabel, java.lang.String valueAxisLabel, CategoryDataset dataset,
PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls);
8. chart.setBackgroundPaint(Color.yellow);
Above method is used to set the color of
chart background
9. chart.getTitle().setPaint(Color.blue);
Above method is used to set the color of
chart title
10. CategoryPlot
p = chart.getCategoryPlot();
Above method is used to get the object of the
Plot for Bar Chart
11. p.setRangeGridlinePaint(Color.red)
Above method is used to set the color of plot
Gridlines
8.WaterFall
Chart Example
1. For
defining a dataset for WaterFall chart we have to create an object of
DefaultCategoryDataset type :
DefaultCategoryDataset dataset = new
DefaultCategoryDataset();
2. After
creating the instance of dataset then we have to add the data in the data set
by invoking the method addValue().
addValue(3.0, ?Salary?, ?Rahul?);
3. First
argument specifies the salary of a employee and the second argument specify
what will appear in the legend to the meaning of the Waterfall chart.
4. JFreeChart
chart = ChartFactory.createWaterfallChart("Comparison between
Employee", "Employee", "Salary", dataset,
PlotOrientation.VERTICAL, true, true, false);
5. After
added the data in dataset we create the WaterFall chart by invoking the
createWaterfallChart() method.
6. This method is a static method of ChartFactory
class and its returns the object of JFreeChart type.This method syntax is:
Public static JFreeChart
createWaterfallChart(java.lang.String title, java.lang.String
categoryAxisLabel, java.lang.String valueAxisLabel, CategoryDataset dataset,
PlotOrientation orientation, boolean legend, boolean tooltips, boolean urls);
7. chart.getTitle().setPaint(Color.blue);
Above method is used to set the color of
chart title
8. CategoryPlot
p = chart.getCategoryPlot();
Above method is used
to get the object of the Plot for Waterfall Chart. GetCategoryPlot() is method
of the JFreeChart class and it returns the object of CategoryPlot type.
9. p.setRangeGridlinePaint(Color.red);
Above method is used
to set the color of plot Gridlines.
10. p.setDomainGridlinesVisible(true);
Above method is used
to set the flag that controls the Gridlines are drawn or not against the domain
axis.
11. p.setDomainGridlinePaint(Color.black);
Above method is used
to set the color of Gridlines against the domain axis.
12. ChartFrame
frame1=new ChartFrame("WaterFall Chart",chart);
After this we create
the object of ChartFrame. It used to display a chart.
9.Polar
Chart Example
For More : http://www.roseindia.net/chartgraphs/
All Example codes:
//1.pie chart
import java.awt.*;
import org.jfree.chart.*;
import org.jfree.chart.title.*;
import
org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.*;
public class pie{
public static void main(String arg[]){
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("One", new Integer(10));
pieDataset.setValue("Two", new Integer(20));
pieDataset.setValue("Three", new Integer(30));
pieDataset.setValue("Four", new Integer(10));
pieDataset.setValue("Five", new Integer(20));
pieDataset.setValue("Six", new Integer(10));
JFreeChart chart = ChartFactory.createPieChart
("Pie Chart using JFreeChart", pieDataset,
true,true,true);
ChartFrame frame1=new ChartFrame("Pie Chart",chart);
frame1.setVisible(true);
frame1.setSize(300,300);
}
}
//2.3dpie chart
import java.awt.*;
import org.jfree.chart.*;
import org.jfree.chart.title.*;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.*;
import org.jfree.chart.plot.*;
import org.jfree.util.*;
public class Pie3D{
public static void main(String arg[]){
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("One",
new Integer(10));
pieDataset.setValue("Two", new Integer(20));
pieDataset.setValue("Three", new Integer(30));
pieDataset.setValue("Four", new Integer(10));
pieDataset.setValue("Five", new Integer(20));
pieDataset.setValue("Six", new Integer(10));
JFreeChart chart = ChartFactory.createPieChart3D
("3D Pie Chart", pieDataset, true,true,true);
PiePlot3D p=(PiePlot3D)chart.getPlot();
p.setForegroundAlpha(0.5f);
ChartFrame frame1=new ChartFrame("3D Pie Chart",chart);
frame1.setVisible(true);
frame1.setSize(300,300);
}
}
//3.Area Chart Example
import java.awt.*;
import java.io.*;
import org.jfree.chart.*;
import org.jfree.data.category.*;
import
org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.*;
import org.jfree.chart.renderer.category.*;
import org.jfree.chart.plot.*;
public class Area{
public static void main(String arg[]){
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(4.0, "Science", "Rahul");
dataset.addValue(3.0,
"Maths", "Rahul");
dataset.addValue(5.0, "Science", "Vinod");
dataset.addValue(2.0,"Maths", "Vinod");
dataset.addValue(3.0, "Science", "Prashant");
dataset.addValue(5.0, "Maths", "Prashant");
dataset.addValue(6.0, "Science", "Tapan");
dataset.addValue(2.0, "Maths", "Tapan");
dataset.addValue(3.0,"Science", "Santosh");
dataset.addValue(5.0, "Maths", "Santosh");
JFreeChart chart = ChartFactory.createAreaChart
("Comparison between Students Marks","Students",
"Marks ",
dataset, PlotOrientation.VERTICAL, true,true, false);
chart.setBackgroundPaint(Color.yellow);
chart.getTitle().setPaint(Color.blue);
CategoryPlot p = chart.getCategoryPlot();
p.setForegroundAlpha(0.7f);
p.setRangeGridlinePaint(Color.red);
p.setDomainGridlinesVisible(true);
p.setDomainGridlinePaint(Color.black);
CategoryItemRenderer renderer = p.getRenderer();
renderer.setSeriesPaint(1, Color.red);
renderer.setSeriesPaint(0, Color.green);
ChartFrame frame1=new ChartFrame("Area Chart",chart);
frame1.setVisible(true);
frame1.setSize(300,300);
}
}
//XYArea Chart Example
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import
org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.*;
import org.jfree.data.*;
public class xyArea{
public static void main(String arg[]){
XYSeries series = new XYSeries("Average Weight");
series.add(20.0, 20.0);
series.add(40.0, 25.0);
series.add(55.0, 50.0);
series.add(70.0, 65.0);
XYDataset xyDataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createXYAreaChart
("XY Chart using JFreeChart", "Age",
"Weight",
xyDataset, PlotOrientation.VERTICAL, true,
true,
false);
ChartFrame frame1=new ChartFrame("XYArea Chart",chart);
frame1.setVisible(true);
frame1.setSize(300,300);
}
}
5//XYLine Chart Example
import
org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import
org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.*;
import org.jfree.data.*;
public class xyLine{
public static void main(String arg[]){
XYSeries series = new XYSeries("Average Weight");
series.add(20.0, 20.0);
series.add(40.0, 25.0);
series.add(55.0, 50.0);
series.add(70.0, 65.0);
XYDataset xyDataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createXYLineChart
("XYLine Chart using JFreeChart", "Age",
"Weight",
xyDataset, PlotOrientation.VERTICAL, true,
true, false);
ChartFrame frame1=new ChartFrame("XYLine Chart",chart);
frame1.setVisible(true);
frame1.setSize(300,300);
}
}
6//Bar Chart Example
import
org.jfree.chart.*;
import org.jfree.data.category.*;
import
org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.*;
import org.jfree.data.*;
import org.jfree.chart.renderer.category.*;
import org.jfree.chart.plot.*;
import java.awt.*;
public class BarExample1{
public static void main(String arg[]){
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(2, "Marks", "Rahul");
dataset.setValue(7, "Marks", "Vinod");
dataset.setValue(4, "Marks", "Deepak");
dataset.setValue(9, "Marks", "Prashant");
dataset.setValue(6, "Marks", "Chandan");
JFreeChart chart = ChartFactory.createBarChart
("BarChart using JFreeChart","Student",
"Marks", dataset,
PlotOrientation.VERTICAL, false,true, false);
chart.setBackgroundPaint(Color.yellow);
chart.getTitle().setPaint(Color.blue);
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.red);
ChartFrame frame1=new ChartFrame("Bar Chart",chart);
frame1.setVisible(true);
frame1.setSize(400,350);
}
}
7//3D Bar Chart Example
import
org.jfree.chart.*;
import org.jfree.data.category.*;
import
org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.*;
import org.jfree.data.*;
import org.jfree.chart.renderer.category.*;
import org.jfree.chart.plot.*;
import java.awt.*;
public class BarExample2{
public static void main(String arg[]){
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(6, "Science", "Rahul");
dataset.setValue(8, "Maths", "Rahul");
dataset.setValue(5, "Science", "Deepak");
dataset.setValue(3, "Maths", "Deepak");
dataset.setValue(6, "Science", "Vinod");
dataset.setValue(9, "Maths", "Vinod");
dataset.setValue(2, "Science", "Chandan");
dataset.setValue(4, "Maths", "Chandan");
JFreeChart chart = ChartFactory.createBarChart3D
("Comparison between Students","Students",
"Marks",
dataset, PlotOrientation.VERTICAL, true,true, false);
chart.setBackgroundPaint(Color.yellow);
chart.getTitle().setPaint(Color.blue);
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.red);
ChartFrame frame1=new ChartFrame("3D Bar Chart",chart);
frame1.setVisible(true);
frame1.setSize(300,300);
}
}
8//WaterFall Chart Example
import
java.awt.*;
import org.jfree.chart.*;
import org.jfree.data.category.*;
import
org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.*;
import org.jfree.data.*;
import org.jfree.chart.renderer.category.*;
import org.jfree.chart.plot.*;
public class Waterfall{
public static void main(String arg[]){
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(3.0, "Salary", "Rahul");
dataset.addValue(3.0, "Salary", "Prashant");
dataset.addValue(2.0, "Salary", "Chandan");
dataset.addValue(2.0, "Salary", "Vinod");
dataset.addValue(10.0, "Salary", "Total");
JFreeChart chart = ChartFactory.createWaterfallChart
("Comparison between Employees","Employee",
"Salary",
dataset, PlotOrientation.VERTICAL, true,true, false);
chart.getTitle().setPaint(Color.blue);
CategoryPlot p = chart.getCategoryPlot();
p.setRangeGridlinePaint(Color.red);
p.setDomainGridlinesVisible(true);
p.setDomainGridlinePaint(Color.black);
ChartFrame frame1=new ChartFrame("WaterFall Chart",chart);
frame1.setVisible(true);
frame1.setSize(400,350);
}
}
9//Polar Chart Example
10//Bar Chart Example using JFreeChart
11//Horizontal Bar Chart Example using
JFreeChart
12//Stacked 3d Bar Chart Example using
JFreeChart
13//Stacked Bar Chart using JFreeChart
14//Stacked Bar Chart Example using
JFreeChart