JFree Chart
JFreeChart is a popular open source Java charting library that can generate most common chart types, including pie, bar, line, and Gantt charts. In addition, the JFreeChart API supports many interactive features, such as tool tips and zooming.
1-1st create dataset object "for different charts there are different dataset object" such as -DefaultCategoryDataset,DefaultPieDataset,XYDataset etc.
2-next we create JFreeChart object with the help of "DataSet"object using "ChartFactory" class methods.
Example
package ipg.chart;
import org.jfree.chart.*;
import org.jfree.data.category.*;
import org.jfree.chart.plot.*;
public class BarExample2{
public static void main(String arg[]){
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(6, "Java", "Arvind");
dataset.setValue(8, "liferay", "Ratan");
dataset.setValue(7, "physics", "Deepak");
dataset.setValue(6, "Maths", "Himanshu");
dataset.setValue(9, "English", "Abhishek");
JFreeChart chart = ChartFactory.createBarChart3D("Comparison between Students","Students", "Marks", dataset, PlotOrientation.VERTICAL, true,true, false);
//============ for applet ====================
/*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);*/
//=======================================
//======================for image ==========
try {
ChartUtilities.saveChartAsPNG(
new java.io.File("C:\\Documents and Settings\\Administrator\\Desktop\\bar.png"), chart, 500, 300);
//new java.io.File("bar.png"), chart, 500, 300);//this generate image in workspace in root project folder
} catch (java.io.IOException exc) {
System.err.println("Error writing image to file");
}
}
//========================================
}
JFreeChart is a popular open source Java charting library that can generate most common chart types, including pie, bar, line, and Gantt charts. In addition, the JFreeChart API supports many interactive features, such as tool tips and zooming.
Note: for this two jars are required :-(1) jfreechart and (2)jcommon jars
main steps
1-1st create dataset object "for different charts there are different dataset object" such as -DefaultCategoryDataset,DefaultPieDataset,XYDataset etc.
2-next we create JFreeChart object with the help of "DataSet"object using "ChartFactory" class methods.
package ipg.chart;
import org.jfree.chart.*;
import org.jfree.data.category.*;
import org.jfree.chart.plot.*;
public class BarExample2{
public static void main(String arg[]){
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(6, "Java", "Arvind");
dataset.setValue(8, "liferay", "Ratan");
dataset.setValue(7, "physics", "Deepak");
dataset.setValue(6, "Maths", "Himanshu");
dataset.setValue(9, "English", "Abhishek");
JFreeChart chart = ChartFactory.createBarChart3D("Comparison between Students","Students", "Marks", dataset, PlotOrientation.VERTICAL, true,true, false);
//============ for applet ====================
/*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);*/
//=======================================
//======================for image ==========
try {
ChartUtilities.saveChartAsPNG(
new java.io.File("C:\\Documents and Settings\\Administrator\\Desktop\\bar.png"), chart, 500, 300);
//new java.io.File("bar.png"), chart, 500, 300);//this generate image in workspace in root project folder
} catch (java.io.IOException exc) {
System.err.println("Error writing image to file");
}
}
//========================================
}
No comments:
Post a Comment