使用JFreeChart创建饼图

来源:互联网 发布:qstring转char数组 编辑:程序博客网 时间:2024/05/02 12:44
  1. 转自  : http://hua04104.iteye.com/blog/704094
  2. package com.cs.jfreechart;  
  3.   
  4. import java.awt.Color;  
  5. import java.awt.Font;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8.   
  9. import org.jfree.chart.ChartFactory;  
  10. import org.jfree.chart.ChartUtilities;  
  11. import org.jfree.chart.JFreeChart;  
  12. import org.jfree.chart.plot.PiePlot;  
  13. import org.jfree.chart.title.LegendTitle;  
  14. import org.jfree.chart.title.TextTitle;  
  15. import org.jfree.data.general.DefaultPieDataset;  
  16.   
  17. public class PieChartDemo {  
  18.   
  19.     /** 
  20.      * @param args 
  21.      * @throws IOException  
  22.      */  
  23.     public static void main(String[] args) throws IOException {  
  24.         //生成饼图  
  25.         JFreeChart chart = ChartFactory.createPieChart(  
  26.                 "图书销售统计表",     //图表标题  
  27.                 getDateSet(),      //数据  
  28.                 true,             //是否显示图例  
  29.                 false,            //是否显示工具提示  
  30.                 false             //是否生成URL  
  31.         );  
  32.         //设置标题及标题字体  
  33.         chart.setTitle(new TextTitle("图书销售统计图",new Font("黑体",Font.ITALIC,22)));  
  34.         //建一个图例  
  35.         LegendTitle legendTitle = chart.getLegend(0);  
  36.         //设置图例字体  
  37.         legendTitle.setItemFont(new Font("宋体",Font.BOLD,14));  
  38.         //获取饼图plot对象  
  39.         PiePlot plot = (PiePlot) chart.getPlot();  
  40.         //根据key指定各个数据饼图的颜色  
  41.         plot.setSectionPaint("JAVA教程", Color.RED);  
  42.         plot.setSectionPaint("c++教程", Color.BLUE);  
  43.         plot.setSectionPaint("C#教程", Color.GREEN);  
  44.         plot.setSectionPaint("VC++教程", Color.ORANGE);  
  45.         //设置plot字体  
  46.         plot.setLabelFont(new Font("宋体",Font.BOLD,18));  
  47.         //设置背景透明度(0~1)  
  48.         plot.setBackgroundAlpha(0.9f);  
  49.         //输出文件  
  50.         FileOutputStream fos = new FileOutputStream("book.jpg");  
  51.         //用ChartUtilities工具输出  
  52.         ChartUtilities.writeChartAsJPEG(fos, chart, 800600);  
  53.         fos.close();  
  54.     }  
  55.       
  56.     private static DefaultPieDataset getDateSet() {  
  57.         //提供生成饼图的数据  
  58.         DefaultPieDataset dataset = new DefaultPieDataset();  
  59.         dataset.setValue("JAVA教程"47);  
  60.         dataset.setValue("c++教程"23);  
  61.         dataset.setValue("C#教程"20);  
  62.         dataset.setValue("VC++教程"10);  
  63.         return dataset;  
  64.     }  
  65.   
  66. }  
0 0
原创粉丝点击