关闭

JFreeChart简单小例子

339人阅读 评论(0) 收藏 举报
分类:
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 truetrue );
 
            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 truetrue );
 
            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 truetrue );
 
            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 truetrue );
 
            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.代码生成的图表示例
0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:57985次
    • 积分:970
    • 等级:
    • 排名:千里之外
    • 原创:37篇
    • 转载:29篇
    • 译文:0篇
    • 评论:5条