首页>代码>基于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("ij��˾��Ա��֯���ͼ",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  LV15 2018年1月18日
悟道子  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日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友