JFreeChart 饼图demo

package com.voole.jfreeChart;

 

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

import javax.swing.*;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

 

 

public class BingTu extends JFrame {
 public static void main(String[] args) throws IOException{

        PieDataset dataset = getDataSet();

        JFreeChart chart = ChartFactory.createPieChart3D(

                                                "招生信息总览", // 图表标题

                                                dataset, // 数据集

                                                true,  // 是否显示图例(对于简单的柱状图必须是false)

                                                true,  // 是否生成工具

                                                true  // 是否生成URL链接

                                                );

        chart.setBackgroundPaint(Color.WHITE);
        Font font = new Font("SimSun", 10, 20);
        TextTitle title = chart.getTitle();
        title.setFont(font);
        LegendTitle legendTitle = chart.getLegend();
        legendTitle.setItemFont(font);
        PiePlot pieplot = (PiePlot) chart.getPlot();
        pieplot.setLabelFont(font);
 

        // 设置图表的颜色

        org.jfree.chart.renderer.category.BarRenderer3D renderer;

        renderer = new org.jfree.chart.renderer.category.BarRenderer3D();

        renderer.setBaseOutlinePaint(Color.red);
 
        try {

          File file = new File("c:/student.png");

          ChartUtilities.saveChartAsPNG(file,chart,400,300);// 把报表保存为文件

        }catch (Exception e) {

                  String s = e.getLocalizedMessage();

                  s = e.getMessage();

                  s = e.toString();

        }

    // 将生成的报表放到预览窗口中

     ChartFrame  preview = new ChartFrame("招生信息",chart);

     preview.addWindowListener(new WindowAdapter() {

       public void windowClosing(final WindowEvent event) {

        // preview.dispose();

       }
      
     });

     preview.pack();

     // 调整预览窗口的大小和位置,适合屏幕,并且居中

     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

     preview.setSize(screenSize.width,screenSize.height-50);// 适合屏幕,50表示把工具栏要考虑在内

     Dimension frameSize = preview.getSize();

     if (frameSize.height > screenSize.height) {

       frameSize.height = screenSize.height;

     }

     if (frameSize.width > screenSize.width) {

       frameSize.width = screenSize.width;

     }

     preview.setLocation( (screenSize.width - frameSize.width) / 2,

                (screenSize.height - frameSize.height-50) / 2);

     // 显示报表预览窗口

     preview.setVisible(true);

}

/**
 *
 * 获取一个演示用的组合数据集对象
 *
 * @return
 *
 */

private static PieDataset getDataSet() {

 DefaultPieDataset dataset = new DefaultPieDataset();
  dataset.setValue("苹果", 100);
  dataset.setValue("梨", 200);
  dataset.setValue("葡萄", 300);

        return dataset;

}

 
}

你可能感兴趣的:(swing,jfreechart)