JFreeChart动态生成饼图

一:普通饼图(动态)

这里写图片描述

package test;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JPanel;
import javax.swing.Timer;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class PieChartTest extends ApplicationFrame {


    /**
     * serialVersionUID = 24654896134861354L;
     */
    private static final long serialVersionUID = 24654896134861354L;
    public static Timer timer = null;
    public static DefaultPieDataset dataset = null;
     static PiePlot pieplot = null;
    //中文乱码  
    public static final Font font = new Font("宋体", Font.BOLD, 16);  

   public PieChartTest( String title ){
      super( title ); 
      setContentPane(createDemoPanel( ));
   }
   private static PieDataset createDataset( ){
      dataset = new DefaultPieDataset( );
      dataset.setValue( "2014年收入" , new Double( new Random().nextInt(100) ) );  
      dataset.setValue( "2015年收入" , new Double( new Random().nextInt(100) ) );   
      dataset.setValue( "2016年收入" , new Double( new Random().nextInt(100) ) );    
      dataset.setValue( "2017年收入" , new Double( new Random().nextInt(100) ) );  
      return dataset;         
   }
   private static JFreeChart createChart( PieDataset dataset ){
      JFreeChart chart = ChartFactory.createPieChart(      
         "简单的饼图测试",  // chart title 
         dataset,        // data    
         true,           // include legend   
         true, 
         false);

      return chart;
   }
   private static JFreeChart chart = null;

   public static JPanel createDemoPanel( ){
      chart = createChart(createDataset( ) );
      //得到绘图区  
      pieplot = (PiePlot) chart.getPlot();
      //设置标签字体   
      pieplot.setLabelFont(font);  
    //得到标题  
      TextTitle texttitle = chart.getTitle();  
      //标题  
      texttitle.setFont(font); 

      pieplot.setNoDataMessage("No data available");  
      pieplot.setCircular(false);  
      pieplot.setLabelGap(0.02);  
    //提示条字体  
      chart.getLegend().setItemFont(font);

      return new ChartPanel( chart ); 
   }
   public static void main( String[ ] args ){
       PieChartTest demo = new PieChartTest( "简单的饼图测试" );  
      demo.setSize( 560 , 367 );    
      RefineryUtilities.centerFrameOnScreen( demo );    
      demo.setVisible( true ); 

      refresh();
   }

    // 调用刷新方法
    public static void refresh(){
        // 获取刷新间隔时间
        int delay = 2000; // seconds
        ActionListener taskTimer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if(pieplot!=null){
                    pieplot.setDataset(createDataset());
                }
            }
        };
        // 初始化定时器
        timer = new Timer(delay,taskTimer);
        // 启动定时器
        timer.start();
    }
}

二:取出饼图中的一块

这里写图片描述

package test;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JPanel;
import javax.swing.Timer;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class PieChartTest extends ApplicationFrame {


    /**
     * serialVersionUID = 24654896134861354L;
     */
    private static final long serialVersionUID = 24654896134861354L;
    public static Timer timer = null;
    public static DefaultPieDataset dataset = null;
     static PiePlot pieplot = null;
    //中文乱码  
    public static final Font font = new Font("宋体", Font.BOLD, 16);  

   public PieChartTest( String title ){
      super( title ); 
      setContentPane(createDemoPanel( ));
   }
   private static PieDataset createDataset( ){
      dataset = new DefaultPieDataset( );
      dataset.setValue( "2014年收入" , new Double( new Random().nextInt(100) ) );  
      dataset.setValue( "2015年收入" , new Double( new Random().nextInt(100) ) );   
      dataset.setValue( "2016年收入" , new Double( new Random().nextInt(100) ) );    
      dataset.setValue( "2017年收入" , new Double( new Random().nextInt(100) ) );  
      return dataset;         
   }
   private static JFreeChart createChart( PieDataset dataset ){
      JFreeChart chart = ChartFactory.createPieChart(      
         "简单的饼图测试",  // chart title 
         dataset,        // data    
         true,           // include legend   
         true, 
         false);

      return chart;
   }
   private static JFreeChart chart = null;

   public static JPanel createDemoPanel( ){
      chart = createChart(createDataset( ) );
      //得到绘图区  
      pieplot = (PiePlot) chart.getPlot();
      //设置标签字体   
      pieplot.setLabelFont(font);  
      //得到标题  
      TextTitle texttitle = chart.getTitle();  
      //标题  
      texttitle.setFont(font); 
      //将第2个片区取出显示。后面一个参数是取出的距离,是一个比例数 
      pieplot.setExplodePercent("2015年收入", 0.5);

      pieplot.setNoDataMessage("No data available");  
      pieplot.setCircular(false);  
      pieplot.setLabelGap(0.02);  
    //提示条字体  
      chart.getLegend().setItemFont(font);

      return new ChartPanel( chart ); 
   }
   public static void main( String[ ] args ){
       PieChartTest demo = new PieChartTest( "简单的饼图测试" );  
      demo.setSize( 560 , 367 );    
      RefineryUtilities.centerFrameOnScreen( demo );    
      demo.setVisible( true ); 

      refresh();
   }

// 调用刷新方法
    public static void refresh(){
        // 获取刷新间隔时间
        int delay = 2000; // seconds
        ActionListener taskTimer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                if(pieplot!=null){
                    pieplot.setDataset(createDataset());
                }
            }
        };
        // 初始化定时器
        timer = new Timer(delay,taskTimer);
        // 启动定时器
        timer.start();
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值