JFreeChart Pie图的开发

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.chart.plot.PiePlot;   
import org.jfree.data.general.DefaultPieDataset;   
import org.jfree.data.general.PieDataset;   
  
//JFreeChart Pie图范例   
public class CreateJFreeChartPie {   
  
    /**  
     * 使用JFreeChart 生成Pie  
     */  
    public static void main(String[] args) {   
        //步骤1:创建PieDataset对象(准备数据)   
        PieDataset dataset = createDataset();   
        //步骤2:根据Dataset 生成JFreeChart对象,以及做相应的设置   
        JFreeChart freeChart = createChart(dataset);   
        //步骤3:将JFreeChart对象输出到文件,Servlet输出流等   
        saveAsFile(freeChart, "c:\\jfreechart\\pie.png", 500, 400);   
    }   
       
    //保存为文件   
    public static void saveAsFile(JFreeChart chart, String outputPath, int weight, int height) {   
        FileOutputStream out = null;   
        try {   
            File outFile = new File(outputPath);   
            if (!outFile.getParentFile().exists()) {   
                outFile.getParentFile().mkdirs();   
            }   
            out = new FileOutputStream(outputPath);   
            //保存为PNG文件   
            ChartUtilities.writeChartAsPNG(out, chart, 300, 200);   
            //保存为JPEG文件   
            //ChartUtilities.writeChartAsJPEG(out, chart, 500, 400);   
            out.flush();   
        } catch (FileNotFoundException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        } finally {   
            if (out != null) {   
                try {   
                    out.close();   
                } catch (IOException e) {   
                    //do nothing   
                }   
            }   
        }   
    }   
  
    //从PieDataset生成JFreeChart对象   
    public static JFreeChart createChart(PieDataset pieDataset) {   
        JFreeChart jfreechart = ChartFactory.createPieChart("Pie Chart Demo",   
                pieDataset, // dataset   
                true, // legend   
                false, // tooltips   
                false); // URLs   
           
        //以下的设置可以由用户定制,也可以省略   
        //   
        PiePlot pieplot = (PiePlot) jfreechart.getPlot();   
        //背景色 透明度   
        pieplot.setBackgroundAlpha(0.5f);   
        //前景色 透明度   
        pieplot.setForegroundAlpha(0.5f);   
        //其它设置请参考 PiePlot类   
           
        return jfreechart;   
    }   
  
    /**  
     * 创建PieDataset对象(数据的准备)  
     *   
     */  
    public static PieDataset createDataset() {   
        DefaultPieDataset defaultpiedataset = new DefaultPieDataset();   
        defaultpiedataset.setValue("One", new Double(37.800000000003D));   
        defaultpiedataset.setValue("Two", new Double(20.002D));   
        defaultpiedataset.setValue("Three", new Double(17.504D));   
        defaultpiedataset.setValue("Four", new Double(47D));   
        return defaultpiedataset;   
    }   
  
}  

你可能感兴趣的:(java,C++,c,servlet,jfreechart)