首页>代码>基于JFreeChart常用的统计分析图,饼图,柱形图>/JFreeCharDemo/src/com/jfreechartTest/JFreeChartTest.java
package com.jfreechartTest;

import java.awt.Font;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

public class JFreeChartTest {
	
public JFreeChartTest() {
		super();
		// TODO Auto-generated constructor stub
	}

/**
 * 形成饼图
 * @throws Exception 
 */
    public  void  getPieChart() throws Exception{
    	//创建饼图数据集
    	DefaultPieDataset ds=new DefaultPieDataset();
    	ds.setValue("java", 5500);
    	ds.setValue("c++", 6000);
    	ds.setValue("c#", 4500);
    	ds.setValue("object-c", 6000);
    	ds.setValue("php", 5000);
    	ds.setValue("其他", 4300);
    	//创建jfreeChart 对象
 // JFreeChart chart =ChartFactory.createPieChart("编程语言工资底薪图", ds, true, true, false);
    	//3D 饼图
  JFreeChart chart =ChartFactory.createPieChart3D("编程语言工资底薪图", ds, true, true, false);
  //设置标题的字体
  chart.getTitle().setFont(new Font("宋体",Font.BOLD, 30));
  //设置提示字体
  chart.getLegend().setItemFont(new Font("楷体", Font.ITALIC, 20));
  
/*  ChartFrame chartFrame=new ChartFrame("某公司人员组织数据图",chart); 
  //chart要放在Java容器组件中,ChartFrame继承自java的Jframe类。该第一个参数的数据是放在窗口左上角的,不是正中间的标题。
  chartFrame.pack(); //以合适的大小展现图形
  chartFrame.setVisible(true);//图形是否可见
*/  
  //得到绘图区
  PiePlot  plot=(PiePlot) chart.getPlot();
  plot.setLabelFont(new Font("宋体",Font.BOLD, 20));
  
  //设置绘图区的背景
  plot.setBackgroundImage(ImageIO.read(new File("G:/JAVAIMG/村庄.jpg")));
  plot.setBackgroundAlpha(0.7f);//半透明
  chart.setBackgroundImage(ImageIO.read(new File("G:/JAVAIMG/农场.jpg")));
  //设置分离出来
  //plot.setExplodePercent(0, 0.1);
/*  plot.setExplodePercent("java", 0.1);
  plot.setExplodePercent("c++", 0.1);
  plot.setExplodePercent("c#", 0.1);*/
  
  //设置前景色的apha
  plot.setForegroundAlpha(0.8f);
  //设置显示数据或百分比
/*  {0} 表示名称
  {1} 表示百分比率
  {2} 表示名称
  {3} 表示总和*/
  
 // plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}"));//名称
  //plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{1}"));//数字
 //plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{2}"));//百分比
  //plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{3}"));//总和
 //设置复杂的显示数
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1}/{3}--{2})"));//总和
  
  
  ChartUtilities.saveChartAsJPEG(new File("G:/JAVAIMG/pie.jpg"), chart, 800, 500);
    	
    }
	
    /**
     * 形成柱形图
     * @throws Exception 
     */
        public  void  getBarChart() throws Exception{
        	//创建柱形图数据集
        	DefaultCategoryDataset ds=new DefaultCategoryDataset();
           ds.addValue(3500, "IBM", "第一季度");
           ds.addValue(3200, "ORACLE", "第一季度");
           ds.addValue(3500, "用友", "第一季度");
           
           ds.addValue(3100, "IBM", "第二季度");
           ds.addValue(2500, "ORACLE", "第二季度");
           ds.addValue(3000, "用友", "第二季度");
           
           ds.addValue(2800, "IBM", "第三季度");
           ds.addValue(3200, "ORACLE", "第三季度");
           ds.addValue(3700, "用友", "第三季度");
        	//创建jfreeChart 对象
        String  title1="前三季度各大公司的销售额";
        String  title2="季度";
        String  title3="销量(单位:万台)";
      JFreeChart chart =ChartFactory.createBarChart3D(title1,title2,title3, ds, PlotOrientation.VERTICAL, true, false, false);
      //中文问题
    chart.getTitle().setFont(new Font("宋体",Font.BOLD,18));
      //提示条字体
      chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 18));
      //得到绘图区
         CategoryPlot polt=(CategoryPlot)chart.getCategoryPlot();
      //得到domain轴字体   横轴大标题
      polt.getDomainAxis().setLabelFont(new Font("楷体", Font.BOLD, 15));
      //小标题
      polt.getDomainAxis().setTickLabelFont(new Font("楷体", Font.ITALIC, 15));
      //设置 rang 轴的
      polt.getRangeAxis().setLabelFont(new Font("楷体", Font.BOLD, 15));
      ChartUtilities.saveChartAsJPEG(new File("G:/JAVAIMG/bar.jpg"), chart, 800, 500);
        }
    
        
        /**
         * 形成折线图
         * @throws Exception 
         */
            public  void  getLineChart() throws Exception{
            	//创建柱形图数据集
            	DefaultCategoryDataset ds=new DefaultCategoryDataset();
               ds.addValue(3500, "IBM", "第一季度");
               ds.addValue(3200, "ORACLE", "第一季度");
               ds.addValue(3500, "用友", "第一季度");
               
               ds.addValue(3100, "IBM", "第二季度");
               ds.addValue(2500, "ORACLE", "第二季度");
               ds.addValue(3000, "用友", "第二季度");
               
               ds.addValue(2800, "IBM", "第三季度");
               ds.addValue(3200, "ORACLE", "第三季度");
               ds.addValue(3700, "用友", "第三季度");

               ds.addValue(1800, "IBM", "第四季度");
               ds.addValue(3000, "ORACLE", "第四季度");
               ds.addValue(2500, "用友", "第四季度");
               
               ds.addValue(2700, "IBM", "第五季度");
               ds.addValue(2500, "ORACLE", "第五季度");
               ds.addValue(3300, "用友", "第五季度");
               
               ds.addValue(2800, "IBM", "第六季度");
               ds.addValue(3500, "ORACLE", "第六季度");
               ds.addValue(3800, "用友", "第六季度");
               
               ds.addValue(3900, "IBM", "第七季度");
               ds.addValue(3600, "ORACLE", "第七季度");
               ds.addValue(3550, "用友", "第七季度");
            	//创建jfreeChart 对象
            String  title1="前七季度各大公司的销售额";
            String  title2="季度";
            String  title3="销量(单位:万台)";
          JFreeChart chart =ChartFactory.createLineChart(title1,title2,title3, ds, PlotOrientation.VERTICAL, true, false, false);
          //中文问题
        chart.getTitle().setFont(new Font("宋体",Font.BOLD,18));
          //提示条字体
          chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 18));
          //得到绘图区
             CategoryPlot polt=(CategoryPlot)chart.getCategoryPlot();
          //得到domain轴字体   横轴大标题
          polt.getDomainAxis().setLabelFont(new Font("楷体", Font.BOLD, 15));
          //小标题
          polt.getDomainAxis().setTickLabelFont(new Font("楷体", Font.ITALIC, 15));
          //设置 rang 轴的
          polt.getRangeAxis().setLabelFont(new Font("楷体", Font.BOLD, 15));
          ChartUtilities.saveChartAsJPEG(new File("G:/JAVAIMG/line.jpg"), chart, 800, 500);
            }
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
        JFreeChartTest test=new JFreeChartTest();
         test.getPieChart();
         test.getBarChart();
         test.getLineChart();
	}

}
最近下载更多
chenghao4u  LV8 2020年11月25日
zhwang  LV19 2020年10月26日
Wave666  LV6 2020年8月20日
wl2301821  LV7 2019年11月20日
skipple3  LV39 2019年8月29日
lironggang  LV38 2018年12月22日
Weipeng_  LV14 2018年6月28日
1126055836  LV13 2018年1月18日
fengzf  LV16 2017年12月19日
mxl165856  LV12 2017年12月11日
最近浏览更多
fesfefe  LV13 2023年11月1日
Super强  LV13 2023年10月24日
189676630  LV4 2023年4月19日
1358849392  LV21 2022年11月23日
wusiyin  LV14 2022年9月15日
杠上炮  LV6 2022年6月28日
yanliang2377912054 2021年12月22日
暂无贡献等级
2295849967  LV1 2021年8月10日
huaua7676  LV30 2021年7月18日
heshiyang  LV1 2021年7月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友