JFreeChart简单小例子

 http://binfenghu.blog.163.com/blog/static/202820029201272124149479/

1.在D盘建一个temp文件夹,作为图标生成的位置

2.将以下代码复制到程序中运行

 

企业级项目开发流程详解:http://zz563143188.iteye.com/blog/1825168

收集五年的开发资料下载地址:   http://pan.baidu.com/share/home?uk=4076915866&view=share
 
 
import  java.awt.Font;
import  java.awt.RenderingHints;
import  java.io.File;
import  java.io.IOException;
 
import  org.jfree.chart.ChartColor;
import  org.jfree.chart.ChartFactory;
import  org.jfree.chart.ChartUtilities;
import  org.jfree.chart.JFreeChart;
import  org.jfree.chart.StandardChartTheme;
import  org.jfree.chart.plot.CategoryPlot;
import  org.jfree.chart.plot.PiePlot3D;
import  org.jfree.chart.plot.Plot;
import  org.jfree.chart.plot.PlotOrientation;
import  org.jfree.data.category.DefaultCategoryDataset;
import  org.jfree.data.general.DefaultPieDataset;
import  org.jfree.util.Rotation;
 
public   class   CreateChart {
 
         public   static   void  main(String[] args) {
            CreateChart cc =  new  CreateChart();
            cc.setChartTheme();
            cc.createPieChart();
            cc.createPieChart3D();
            cc.createColumnChart();
            cc.createColumnChart3D();
            cc.createLineChart();
            cc.createLineChart3D();
      }
 
         /**
       * 防止中文乱码,设置图表主题的默认实现
       *
       *  @return
       */
         @SuppressWarnings  ( "serial"   )
         public   void   setChartTheme() {
            StandardChartTheme theme =  new  StandardChartTheme( "CN"   ) {
                     // 重写apply(...)方法借机消除文字锯齿.VALUE_TEXT_ANTIALIAS_OFF
                     public   void   apply(JFreeChart chart) {
                        chart.getRenderingHints().put(
                                    RenderingHints.   KEY_TEXT_ANTIALIASING  ,
                                    RenderingHints.   VALUE_TEXT_ANTIALIAS_OFF  );
                           super  .apply(chart);
                  }
            };
               // 标题乱码解决
            theme.setExtraLargeFont(   new  Font( "宋体"   , Font.  PLAIN , 20));
               // 图例乱码解决
            theme.setRegularFont(   new  Font( "宋体"   , Font.  PLAIN , 12));
            theme.setLargeFont(   new  Font( "宋体"   , Font.  PLAIN , 14));
            theme.setSmallFont(   new  Font( "宋体"   , Font.  PLAIN , 10));
            theme.setBaselinePaint(java.awt.Color.   white  );
               // 应用主题样式
            ChartFactory. setChartTheme(theme);
      }
 
         /**
       * 饼状图
       */
         public   void   createPieChart() {
            DefaultPieDataset dataset =  new  DefaultPieDataset();
            dataset.setValue(   "苹果"  , 250);
            dataset.setValue(   "桔子"  , 350);
            dataset.setValue(   "梨子"  , 200);
            dataset.setValue(   "香蕉"  , 50);
            dataset.setValue(   "荔枝"  , 150);
            JFreeChart chart = ChartFactory. createPieChart(  "水果产量比率图"  , dataset,
                           true  ,  true   ,  true );
 
            Plot cp = chart.getPlot();
            cp.setBackgroundPaint(ChartColor.   WHITE  );  // 背景色设置
 
               // ChartFrame frame = new ChartFrame("水果产量比率图 ", chart, true);
               // frame.pack();
               // frame.setVisible(true);
 
               try  {
                  ChartUtilities. saveChartAsPNG(  new  File( "D:/temp/PieChart.png"   ),
                              chart, 800, 500);
            }  catch  (IOException e) {
                  e.printStackTrace();
            }
      }
 
         /**
       * 3D饼状图
       */
         public   void   createPieChart3D() {
            DefaultPieDataset dataset =  new  DefaultPieDataset();
            dataset.setValue(   "苹果"  , 250);
            dataset.setValue(   "桔子"  , 350);
            dataset.setValue(   "梨子"  , 200);
            dataset.setValue(   "香蕉"  , 50);
            dataset.setValue(   "荔枝"  , 150);
            JFreeChart chart = ChartFactory. createPieChart3D(  "水果产量比率图"  , dataset,
                           true  ,  true   ,  true );
            PiePlot3D plot = (PiePlot3D) chart.getPlot();
               // 设置开始角度
            plot.setStartAngle(150D);
               // 设置方向为"顺时针方向"
            plot.setDirection(Rotation.   CLOCKWISE  );
               // 设置透明度,0.5F为半透明,1为不透明,0为全透明
            plot.setForegroundAlpha(0.5F);
               // 没有数据的时候显示的内容
            plot.setNoDataMessage(   "无数据显示"   );
               // 背景色设置
            plot.setBackgroundPaint(ChartColor.   WHITE  );
               // ChartFrame frame = new ChartFrame("水果产量比率图 ", chart, true);
               // frame.pack();
               // frame.setVisible(true);
 
               try  {
                  ChartUtilities. saveChartAsPNG(  new  File( "D:/temp/PieChart3D.png"   ),
                              chart, 800, 500);
            }  catch  (IOException e) {
                  e.printStackTrace();
            }
      }
 
         /**
       * 柱状图
       */
         public   void   createColumnChart() {
            DefaultCategoryDataset dataset =  new  DefaultCategoryDataset();
            dataset.addValue(100,  "北京"  ,  "苹果"   );
            dataset.addValue(100,  "上海"  ,  "苹果"   );
            dataset.addValue(100,  "广州"  ,  "苹果"   );
            dataset.addValue(200,  "北京"  ,  "梨子"   );
            dataset.addValue(200,  "上海"  ,  "梨子"   );
            dataset.addValue(200,  "广州"  ,  "梨子"   );
            dataset.addValue(300,  "北京"  ,  "葡萄"   );
            dataset.addValue(300,  "上海"  ,  "葡萄"   );
            dataset.addValue(300,  "广州"  ,  "葡萄"   );
            dataset.addValue(400,  "北京"  ,  "香蕉"   );
            dataset.addValue(400,  "上海"  ,  "香蕉"   );
            dataset.addValue(400,  "广州"  ,  "香蕉"   );
            dataset.addValue(500,  "北京"  ,  "荔枝"   );
            dataset.addValue(500,  "上海"  ,  "荔枝"   );
            dataset.addValue(500,  "广州"  ,  "荔枝"   );
 
            JFreeChart chart = ChartFactory. createBarChart(  "水果产量图" ,  "水量"   ,  "产量"   ,
                        dataset, PlotOrientation.   VERTICAL  ,  true   ,  true ,  true  );
 
            CategoryPlot cp = chart.getCategoryPlot();
            cp.setBackgroundPaint(ChartColor.   WHITE  );  // 背景色设置
            cp.setRangeGridlinePaint(ChartColor.   GRAY  );  // 网格线色设置
 
               // ChartFrame frame=new ChartFrame ("水果产量图 ",chart,true);
               // frame.pack();
               // frame.setVisible(true);
 
               try  {
                  ChartUtilities. saveChartAsPNG(  new  File( "D:/temp/ColumnChart.png"   ),
                              chart, 800, 500);
            }  catch  (IOException e) {
                  e.printStackTrace();
            }
      }
 
         /**
       * 3D柱状图
       */
         public   void   createColumnChart3D() {
            DefaultCategoryDataset dataset =  new  DefaultCategoryDataset();
            dataset.addValue(100,  "北京"  ,  "苹果"   );
            dataset.addValue(100,  "上海"  ,  "苹果"   );
            dataset.addValue(100,  "广州"  ,  "苹果"   );
            dataset.addValue(200,  "北京"  ,  "梨子"   );
            dataset.addValue(200,  "上海"  ,  "梨子"   );
            dataset.addValue(200,  "广州"  ,  "梨子"   );
            dataset.addValue(300,  "北京"  ,  "葡萄"   );
            dataset.addValue(300,  "上海"  ,  "葡萄"   );
            dataset.addValue(300,  "广州"  ,  "葡萄"   );
            dataset.addValue(400,  "北京"  ,  "香蕉"   );
            dataset.addValue(400,  "上海"  ,  "香蕉"   );
            dataset.addValue(400,  "广州"  ,  "香蕉"   );
            dataset.addValue(500,  "北京"  ,  "荔枝"   );
            dataset.addValue(500,  "上海"  ,  "荔枝"   );
            dataset.addValue(500,  "广州"  ,  "荔枝"   );
            JFreeChart chart = ChartFactory. createBarChart3D(  "水果产量图" ,  "水果"  ,  "产量"   ,
                        dataset, PlotOrientation.   VERTICAL  ,  true   ,  true ,  true  );
 
            CategoryPlot cp = chart.getCategoryPlot();
            cp.setBackgroundPaint(ChartColor.   WHITE  );  // 背景色设置
            cp.setRangeGridlinePaint(ChartColor.   GRAY  );  // 网格线色设置
 
               // ChartFrame frame=new ChartFrame ("水果产量图 ",chart,true);
               // frame.pack();
               // frame.setVisible(true);
 
               try  {
                  ChartUtilities. saveChartAsPNG(
                                 new  File( "D:/temp/ColumnChart3D.png"   ), chart, 800, 500);
            }  catch  (IOException e) {
                  e.printStackTrace();
            }
      }
 
         /**
       * 线状图
       */
         public   void   createLineChart() {
            DefaultCategoryDataset dataset =  new  DefaultCategoryDataset();
            dataset.addValue(100,  "北京"  ,  "苹果"   );
            dataset.addValue(200,  "上海"  ,  "苹果"   );
            dataset.addValue(300,  "广州"  ,  "苹果"   );
            dataset.addValue(400,  "北京"  ,  "梨子"   );
            dataset.addValue(500,  "上海"  ,  "梨子"   );
            dataset.addValue(600,  "广州"  ,  "梨子"   );
            dataset.addValue(700,  "北京"  ,  "葡萄"   );
            dataset.addValue(800,  "上海"  ,  "葡萄"   );
            dataset.addValue(900,  "广州"  ,  "葡萄"   );
            dataset.addValue(800,  "北京"  ,  "香蕉"   );
            dataset.addValue(700,  "上海"  ,  "香蕉"   );
            dataset.addValue(600,  "广州"  ,  "香蕉"   );
            dataset.addValue(500,  "北京"  ,  "荔枝"   );
            dataset.addValue(400,  "上海"  ,  "荔枝"   );
            dataset.addValue(300,  "广州"  ,  "荔枝"   );
            JFreeChart chart = ChartFactory. createLineChart(  "水果产量图 " ,  "水果" ,  "产量"  ,
                        dataset, PlotOrientation.   VERTICAL  ,  true   ,  true ,  true  );
 
            CategoryPlot cp = chart.getCategoryPlot();
            cp.setBackgroundPaint(ChartColor.   WHITE  );  // 背景色设置
            cp.setRangeGridlinePaint(ChartColor.   GRAY  );  // 网格线色设置
 
               // ChartFrame frame=new ChartFrame ("水果产量图 ",chart,true);
               // frame.pack();
               // frame.setVisible(true);
 
               try  {
                  ChartUtilities. saveChartAsPNG(  new  File( "D:/temp/LineChart.png"   ),
                              chart, 800, 500);
            }  catch  (IOException e) {
                  e.printStackTrace();
            }
      }
 
         /**
       * 3D线状图
       */
         public   void   createLineChart3D() {
            DefaultCategoryDataset dataset =  new  DefaultCategoryDataset();
            dataset.addValue(300,  "北京"  ,  "苹果"   );
            dataset.addValue(100,  "上海"  ,  "苹果"   );
            dataset.addValue(900,  "广州"  ,  "苹果"   );
            dataset.addValue(200,  "北京"  ,  "梨子"   );
            dataset.addValue(200,  "上海"  ,  "梨子"   );
            dataset.addValue(700,  "广州"  ,  "梨子"   );
            dataset.addValue(300,  "北京"  ,  "葡萄"   );
            dataset.addValue(300,  "上海"  ,  "葡萄"   );
            dataset.addValue(300,  "广州"  ,  "葡萄"   );
            dataset.addValue(400,  "北京"  ,  "香蕉"   );
            dataset.addValue(800,  "上海"  ,  "香蕉"   );
            dataset.addValue(400,  "广州"  ,  "香蕉"   );
            dataset.addValue(100,  "北京"  ,  "荔枝"   );
            dataset.addValue(500,  "上海"  ,  "荔枝"   );
            dataset.addValue(500,  "广州"  ,  "荔枝"   );
            JFreeChart chart = ChartFactory. createLineChart3D(  "水果产量图 "  ,  "水果"   ,  "产量"   ,
                        dataset, PlotOrientation.   VERTICAL  ,  true   ,  true ,  true  );
 
            CategoryPlot cp = chart.getCategoryPlot();
            cp.setBackgroundPaint(ChartColor.   WHITE  );  // 背景色设置
            cp.setRangeGridlinePaint(ChartColor.   GRAY  );  // 网格线色设置
 
               // ChartFrame frame=new ChartFrame ("水果产量图 ",chart,true);
               // frame.pack();
               // frame.setVisible(true);
 
               try  {
                  ChartUtilities. saveChartAsPNG(  new  File( "D:/temp/LineChart3D.png"   ),
                              chart, 800, 500);
            }  catch  (IOException e) {
                  e.printStackTrace();
            }
      }
}
 
3.代码生成的图表示例
JFreeChart简单小例子_第1张图片
JFreeChart简单小例子_第2张图片
JFreeChart简单小例子_第3张图片
JFreeChart简单小例子_第4张图片
JFreeChart简单小例子_第5张图片
JFreeChart简单小例子_第6张图片
 
 
 
 
 
 
 
 
 
 
 
 

你可能感兴趣的:(UI,jfreechart,Flash,gwt)