`

JFreeChart使用 之 IT 分享心得

 
阅读更多

    2012年4月17日,嘿嘿,公司做到报表问题,结合公司,使用了JFreeChart,自己花了一个上午学习,总结一点出来了,希望能对所有在IT新接触人员有所帮助!

    首先,去官方网站:

  http://www.jfree.org/jfreechart/  下载,获取jar

 

 public class Test {

 /**
  * @author 钟艳明   QQ:962589149

  */
 public static void main(String[] args) {
  Test test=new Test();
  test.disPlay();
  test.display2();
  test.display3();
  test.display4(null);
 }
 //圆饼形状
 public void display4(CategoryDataset dataset)
 {
  DefaultCategoryDataset cds = new DefaultCategoryDataset();
     cds.setValue(20, "英语1", "英语");
     cds.setValue(10, "数学1", "数学");
     cds.setValue(15, "语文", "语文");
     cds.setValue(25, "d", "物理");
     cds.setValue(30, "e", "化学");
  if(dataset==null)
  {
   dataset=cds;
  }
    JFreeChart chart = ChartFactory.createBarChart("课程分布图","课程", "比例", dataset,   PlotOrientation.VERTICAL, true, true, true);
    CategoryPlot plot = chart.getCategoryPlot();
    CategoryAxis domainAxis = plot.getDomainAxis();//x轴
    ValueAxis numberaxis = plot.getRangeAxis(); //y轴
    TextTitle t = chart.getTitle();
    t.setFont(new Font("宋体",Font.BOLD,40));//标题文字
    domainAxis.setLabelFont(new Font("宋体",Font.BOLD,20));//x轴标题文字
    domainAxis.setTickLabelFont(new Font("黑体",Font.BOLD,10));//x轴坐标上文字
    numberaxis.setLabelFont(new Font("宋体",Font.BOLD,20));//y轴标题文字
    numberaxis.setTickLabelFont(new Font("黑体",Font.BOLD,10));//y轴坐标上文字
    chart.getLegend().setItemFont(new Font("黑体",Font.BOLD,12));//图例文字
    ChartFrame  frame=new ChartFrame ("CityInfoPort公司组织架构图 ",chart,true);
       frame.pack();
       frame.setVisible(true);

 }
 //保存为图片
 public void display3()
 {
  try{
   //初始化数据
   double[][] data = new double[][] {{1310, 1220, 1110, 1000},{720, 700, 680, 640},{1130, 1020, 980, 800},{440, 400, 360, 300}};
   String[] rowKeys = {"猪肉", "niurou","鸡肉", "鱼肉"};
   String[] columnKeys = {"广州", "shenzhen", "东莞", "佛山"};
   //创建数据源
   CategoryDataset dataset=DatasetUtilities.createCategoryDataset(rowKeys, columnKeys,data);
   //创建折线图
      JFreeChart chart = ChartFactory.createBarChart3D("全国食物统计","肉类","销量",dataset,PlotOrientation.VERTICAL,true,true,false);
      //获取绘图对象
      CategoryPlot plot = chart.getCategoryPlot();
      //获取X  Y轴
      CategoryAxis domainAxis = plot.getDomainAxis();
      NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
      //解决标题乱码
      chart.getTitle().setFont(new Font("宋体",Font.BOLD,16));
      //解决X轴乱码
      domainAxis.setLabelFont(new Font("宋体",Font.PLAIN, 16));
      domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
      //解决Y轴乱码
      numberaxis.setLabelFont(new Font("宋体",Font.PLAIN, 16));
      numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
      //解决底部乱码问题
      chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 16));

   //图片保存路径
   String imageURL ="D://yanming/201200021233.png";
   FileOutputStream jpg = null;
   jpg = new FileOutputStream(imageURL);
   int width=630;
   int height=400;
   ChartUtilities.writeChartAsPNG(jpg, chart, width,height);
   System.out.println("图片创建成功!");
  }catch(Exception ex)
  {
   ex.printStackTrace();
  }
  
 }
 //第二种显示方式
 public void display2()
 {
  //初始化
  double[][] data = new double[][] {{1310, 1220, 1110, 1000},{720, 700, 680, 640},{1130, 1020, 980, 800},{440, 400, 360, 300}};
  String[] rowKeys = {"猪肉", "niurou","鸡肉", "鱼肉"};
  String[] columnKeys = {"广州", "shenzhen", "东莞", "佛山"};
  //创建数据源
  CategoryDataset dataset=DatasetUtilities.createCategoryDataset(rowKeys, columnKeys,data);
  //创建折线图
    JFreeChart chart = ChartFactory.createBarChart3D("全国食物统计","肉类","销量",dataset,PlotOrientation.VERTICAL,true,true,false);
    //获取绘图对象
    CategoryPlot plot = chart.getCategoryPlot();
    //获取X  Y轴
    CategoryAxis domainAxis = plot.getDomainAxis();
    NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
    //解决标题乱码
    chart.getTitle().setFont(new Font("宋体",Font.BOLD,16));
    //解决X轴乱码
    domainAxis.setLabelFont(new Font("宋体",Font.PLAIN, 16));
    domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
    //解决Y轴乱码
    numberaxis.setLabelFont(new Font("宋体",Font.PLAIN, 16));
    numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
    //解决底部乱码问题
    chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 16));

    ChartFrame  frame=new ChartFrame ("CityInfoPort公司组织架构图 ",chart,true);
       frame.pack();
       frame.setVisible(true);
 }
 //最简单的一种现实方式,传入的是一个map
 public void disPlay()
 {
  // TODO 自动生成方法存根
  DefaultPieDataset dfp=new DefaultPieDataset();
        dfp.setValue("管理人员", 25);
        dfp.setValue("市场人员", 35);
        dfp.setValue("开发人员", 20);
        dfp.setValue("后勤人员", 5);
        dfp.setValue("财务人员", 15);
         //Create JFreeChart object
       JFreeChart chart =ChartFactory.createPieChart("CityInfoPort公司组织架构图",dfp, true, true, true);
       //解决乱码问题 标题
       //乱码解决开始
       TextTitle textTitle = chart.getTitle();  
       textTitle.setFont(new Font("宋体", Font.PLAIN, 20)); //标题文字乱码
       PiePlot plot=(PiePlot)chart.getPlot();
       plot.setLabelFont(new Font("宋体",Font.BOLD,20));//饼上的文字乱码

       chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12)); //图例文字乱码
      //乱码解决结束


       ChartFrame  frame=new ChartFrame ("CityInfoPort公司组织架构图 ",chart,true);
       frame.setFont(new Font("宋体", Font.BOLD, 15));
       frame.pack();
       frame.setVisible(true);
 }

}

 

下面是效果图





 
 
 

  • 大小: 27.2 KB
  • 大小: 18.2 KB
  • 大小: 5.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics