JFreeChart绘制图表的简单运用
1 绘制饼图
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
public class PieChart {
public static void main(String[] args){
DefaultPieDataset dataSet=PieChart.getDaultDataSet(); //获取数据集
@SuppressWarnings("unused")
/*
*
* 绘制3D饼图:ChartFactory.createPieChart3D(String title,DefaultPieDataset dataSet, boolean legend,boolean tooltips,boolean urls);
* 绘制 饼图:ChartFactory.createPieChar(String title,DefaultPieDataset dataSet, boolean legend,boolean tooltips,boolean urls);
*/
JFreeChart pieChart=ChartFactory.createPieChart3D("家庭水果存量",dataSet,true,true,true);
try {
/*
* 使用writeChartAsJPEG方法保存到文件
* ChartUtilities.writeChartAsJPEG(new FileOutputStream("D:/test.jpeg"), (float) 1.0, pieChart, 300, 300);
*/
/*
* 使用saveChartAsJPEG方法保存到文件
*/
ChartUtilities.saveChartAsJPEG(new File("D:/test.jpeg"), pieChart, 300, 300);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static DefaultPieDataset getDaultDataSet() {
// TODO Auto-generated method stub
DefaultPieDataset dataSet=new DefaultPieDataset();
dataSet.setValue("梨", 30);
dataSet.setValue("苹果", 20);
dataSet.setValue("香蕉",45);
return dataSet;
}
}
2 绘制条形图
import java.io.File;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class BarChart {
public static void main(String[] args) throws IOException{
DefaultCategoryDataset dataSet=BarChart.getDataSet();
/*
*
* 平面条形图
* JFreeChart barChart=ChartFactory.createBarChart("家庭水果存量","水果类别","数量",dataSet,PlotOrientation.VERTICAL,true,false,false);
*
*/
/*
*
* 3D条形图
*/
JFreeChart barChart=ChartFactory.createBarChart3D("家庭水果存量","水果类别","数量",dataSet,PlotOrientation.VERTICAL,true,false,false);
ChartUtilities.saveChartAsJPEG(new File("D:/barchart2.jpeg"), barChart, 300, 300);
}
public static DefaultCategoryDataset getDataSet(){
DefaultCategoryDataset dataSet=new DefaultCategoryDataset();
@SuppressWarnings("unused")
String[] dataName=new String[]{"梨" ,"苹果" ,"香蕉"};
int[] dataValue=new int[]{20,35,45};
for(int i=0;i<dataValue.length;i++){
dataSet.addValue(dataValue[i], dataName[i], dataName[i]);
}
return dataSet;
}
}
3 绘制线形图
import java.io.File;
import java.io.IOException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class LineChart {
public static void main(String[] args) throws IOException{
DefaultCategoryDataset dataSet=LineChart.getDataSet();
JFreeChart lineChart=ChartFactory.createLineChart("模拟考物理成绩", "广东模拟考试", "某生成绩", dataSet,PlotOrientation.VERTICAL, true, true, false);
ChartUtilities.saveChartAsJPEG(new File("D:/lineChart.jpeg"),lineChart,300,300);
}
public static DefaultCategoryDataset getDataSet(){
DefaultCategoryDataset dataSet=new DefaultCategoryDataset();
dataSet.addValue(60, "物理", "广1模");
dataSet.addValue(30, "物理", "广2模");
dataSet.addValue(45, "物理", "广3模");
dataSet.addValue(70, "物理", "广4模");
dataSet.addValue(74, "物理", "广5模");
dataSet.addValue(90, "物理", "广6模");
return dataSet;
}
}