Sometimes we need to create a Pie Chart in Java which we need to display either in result file of some operation or somewhere else.
We can create the Pie-Chart using jfree library which is freely available. Its simple, not a tricky task.
you can download the eclipse project as zip file. Click Here
Lets have a look on the code sample
you required two jar file to download and add reference of them into your project. You can download these two interlinked jar file from here Jar1 and Jar2
This is the output of the above program.
We can create the Pie-Chart using jfree library which is freely available. Its simple, not a tricky task.
you can download the eclipse project as zip file. Click Here
Lets have a look on the code sample
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
public class ToImage {
// create a array for color to be used in program.
private static Color colors[] = {new Color(92,151,70),new Color(255,140,0),
new Color(62,117,167), new Color(196,50,79)};
public static void main(String args[])
{
// instance of PieChartDatabase
DefaultPieDataset dataset = new DefaultPieDataset();
// added three values and name of the pie chart database, they will appear as Section of Piechart
dataset.setValue("First", 2);
dataset.setValue("Second", 4);
dataset.setValue("Third", 3);
dataset.setValue("Forth", 1);
// create Pie chart with name Sections
JFreeChart chart = ChartFactory.createPieChart("Sections", dataset, true, true, false);
//below three lines to customize the property of Section's Label
// now they will come in percentage. (first = 20%, second=40%)
//otherwise simple values (first = 2, second=4)
PiePlot pie = (PiePlot) chart.getPlot();
StandardPieSectionLabelGenerator labels = new StandardPieSectionLabelGenerator("{0} = {2}");
pie.setLabelGenerator(labels);
// this is to cut the "Second" second and display with gap in whole pie chart
pie.setExplodePercent(1, 0.2);
try {
// to set the all 4 sections color
pie.setSectionPaint(0, colors[0]);
pie.setSectionPaint(1, colors[1]);
pie.setSectionPaint(2, colors[2]);
pie.setSectionPaint(3, colors[3]);
//Create a png file chart.png
OutputStream out = new FileOutputStream(new File("chart.png"));
// now write the pie chart to blank png file create in prev. step
ChartUtilities.writeChartAsPNG(out, chart, 300, 300);
System.out.println("chart created..!!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
public class ToImage {
// create a array for color to be used in program.
private static Color colors[] = {new Color(92,151,70),new Color(255,140,0),
new Color(62,117,167), new Color(196,50,79)};
public static void main(String args[])
{
// instance of PieChartDatabase
DefaultPieDataset dataset = new DefaultPieDataset();
// added three values and name of the pie chart database, they will appear as Section of Piechart
dataset.setValue("First", 2);
dataset.setValue("Second", 4);
dataset.setValue("Third", 3);
dataset.setValue("Forth", 1);
// create Pie chart with name Sections
JFreeChart chart = ChartFactory.createPieChart("Sections", dataset, true, true, false);
//below three lines to customize the property of Section's Label
// now they will come in percentage. (first = 20%, second=40%)
//otherwise simple values (first = 2, second=4)
PiePlot pie = (PiePlot) chart.getPlot();
StandardPieSectionLabelGenerator labels = new StandardPieSectionLabelGenerator("{0} = {2}");
pie.setLabelGenerator(labels);
// this is to cut the "Second" second and display with gap in whole pie chart
pie.setExplodePercent(1, 0.2);
try {
// to set the all 4 sections color
pie.setSectionPaint(0, colors[0]);
pie.setSectionPaint(1, colors[1]);
pie.setSectionPaint(2, colors[2]);
pie.setSectionPaint(3, colors[3]);
//Create a png file chart.png
OutputStream out = new FileOutputStream(new File("chart.png"));
// now write the pie chart to blank png file create in prev. step
ChartUtilities.writeChartAsPNG(out, chart, 300, 300);
System.out.println("chart created..!!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
you required two jar file to download and add reference of them into your project. You can download these two interlinked jar file from here Jar1 and Jar2
This is the output of the above program.
Happy Code, Reading and blogging (Sharing).. :)
Nice post.!!
ReplyDelete