Monday, August 18, 2008

Making Charts using JFreeChart JAVA

Hi All,
The previous articles have guided you how to deal with excel files using JAVA and how to read/write and create charts within excel file. Now I take you through how you can create chart panels in the JAVA frame (AWT/Swing).

The JFree.org provides a way to create chart panels in the JAVA platform. JFreeChart is a Java free chart library which supports pie charts (2D and 3D), bar charts (horizontal and vertical, regular and stacked), line charts, scatter plots, time series charts, high-low-open-close charts, candlestick plots, Gantt charts, combined plots, thermometers, dials and more. JFreeChart can be used in applications, applets, servlets and JSP. You can download the latest version of JFreeChart using the following link: http://www.jfree.org/jfreechart/download.html. JFreeChart requires the JCommon class library. The JCommon runtime jar file is included in the JFreeChart download. You can also download the class library of the JCommon class from here.

The API for JFreeChart is available in the following link:
http://www.jfree.org/jfreechart/api/javadoc/index.html

The below is source code for creating charts using JFreeChart.



import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

public class ChartDrawDemo extends JFrame{
private static final long serialVersionUID = 1L;
private DefaultPieDataset datasetcls;
private JFreeChart jfccls;
PiePlot ppcls;
public ChartDrawDemo() {
datasetcls = new DefaultPieDataset();
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
}

this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
setVisible(false);
}
});

this.setTitle("Demo Charts");
//this.setSize(700,450);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setValueClassification("A",new Double(10));
this.setValueClassification("B",new Double(20));

this.setCharClassification("Demo Chart");
this.Show();
}

public void setValueClassification(String title, Double numDouble) {
datasetcls.setValue(title, numDouble);
}
public void setCharClassification(String title) {
jfccls = ChartFactory.createPieChart(title, datasetcls, true, true, false);
ppcls = (PiePlot) jfccls.getPlot();
ppcls.setSectionOutlinesVisible(false);
ppcls.setCircular(true);
ppcls.setLabelFont(new Font("SansSerif", Font.PLAIN, 8));
ppcls.setNoDataMessage("No Data Available");
ppcls.setLabelGenerator(null);
ppcls.setLabelGap(0.02);
}

private JPanel createPanelClassification() {
return new ChartPanel(jfccls,400,400,400,400,400,400,true,true,true,true,true,true);
}

public void Show() {
setLayout(new GridBagLayout());
add(createPanelClassification());
pack();
setLocationRelativeTo(null);
setVisible(true);
}

public static void main(String[] args) {
ChartDrawDemo ce = new ChartDrawDemo();
}
}






Hope you all will find it useful.

1 comment:

AnupamSharma said...

hi snehanshu...thanks a lot for sharing information in this blog post...
i needed a bit of help related to jfreecharts...actually i want to fetch data from MS Excel & display charts using jfreecharts...
do you have any information on how that can be done?
thanks again...
- Anupam

Total Pageviews