2011年6月2日 星期四

[ Java 代碼範本 ] JFreeChart : PieChart - Demo 4 (旋轉的圓餅圖)


前言 :
這裡要建立一個旋轉的 PieChart, 關鍵在於使用 javax.swing.Timer 搭配 javax.awt.event.ActionListener 建立的內部類別 Rotator 動態改變 Pie Chart 的角度, 使效果就像 Pie Chart 在旋轉. 而 PieChart 使用的資料容器為 PieDataset 而建立圖表方法為 createPieChart().

範例代碼 :
使用Packages : jcommon-1.0.16.jar, jfreechart-1.0.13.jar.
- PieChartDemo4.java :
  1. package john.jfreechart.piechart;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.event.ActionEvent;  
  5. import java.awt.event.ActionListener;  
  6. import java.text.NumberFormat;  
  7. import javax.swing.Timer;  
  8. import org.jfree.chart.ChartFactory;  
  9. import org.jfree.chart.ChartPanel;  
  10. import org.jfree.chart.JFreeChart;  
  11. import org.jfree.chart.labels.StandardPieSectionLabelGenerator;  
  12. import org.jfree.chart.plot.PiePlot;  
  13. import org.jfree.data.general.DefaultPieDataset;  
  14. import org.jfree.data.general.PieDataset;  
  15. import org.jfree.ui.ApplicationFrame;  
  16. import org.jfree.ui.RefineryUtilities;  
  17.   
  18. /** 
  19. * Source : http://www.koders.com/java/fidCA9ADCF38E1E1B2825657905E9A4FC95579E0E6B.aspx 
  20. * @author John-Lee 
  21. */  
  22. public class PieChartDemo4 extends ApplicationFrame{  
  23.   
  24.     /** 
  25.      * Default constructor. 
  26.      * 
  27.      * @param title  the frame title. 
  28.      */  
  29.     public PieChartDemo4(String title) {  
  30.   
  31.         super(title);  
  32.         PieDataset dataset = createDataset(14);  
  33.   
  34.         // create the chart...  
  35.         JFreeChart chart = ChartFactory.createPieChart(  
  36.             "Pie Chart Demo 4",  // chart title  
  37.             dataset,             // dataset  
  38.             false,                // include legend  
  39.             true,  
  40.             false  
  41.         );  
  42.   
  43.         // set the background color for the chart...  
  44.         chart.setBackgroundPaint(new Color(222222255));  
  45.         PiePlot plot = (PiePlot) chart.getPlot();  
  46.         plot.setBackgroundPaint(Color.white);  
  47.         plot.setCircular(true);  
  48.         // For the label format, use {0} where the pie section key should be inserted, {1}   
  49.         // for the absolute section value and {2} for the percent amount of the pie section,   
  50.         // e.g. "{0} = {1} ({2})" will display as apple = 120 (5%).  
  51.         plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} = {2}"));  
  52.         plot.setNoDataMessage("No data available");  
  53.   
  54.         // add the chart to a panel...  
  55.         ChartPanel chartPanel = new ChartPanel(chart);  
  56.         chartPanel.setPreferredSize(new java.awt.Dimension(500270));  
  57.         setContentPane(chartPanel);  
  58.           
  59.         Rotator rotator = new Rotator(plot);  
  60.         rotator.start();  
  61.   
  62.     }  
  63.   
  64.     /** 
  65.      * Creates a sample dataset. 
  66.      *  
  67.      * @param sections  the number of sections. 
  68.      *  
  69.      * @return A sample dataset. 
  70.      */  
  71.     private PieDataset createDataset(int sections) {  
  72.         DefaultPieDataset result = new DefaultPieDataset();  
  73.         for (int i = 0; i < sections; i++) {  
  74.             double value = 100.0 * Math.random();  
  75.             result.setValue("Section " + i, value);  
  76.         }  
  77.         return result;  
  78.     }  
  79.       
  80.     /** 
  81.      * Starting point for the demonstration application. 
  82.      * 
  83.      * @param args  ignored. 
  84.      */  
  85.     public static void main(String[] args) {          
  86.         PieChartDemo4 demo = new PieChartDemo4("Pie Chart Demo 4");  
  87.         demo.pack();  
  88.         RefineryUtilities.centerFrameOnScreen(demo);  
  89.         demo.setVisible(true);  
  90.   
  91.     }  
  92.   
  93.     /** 
  94.      * The rotator. 
  95.      * 
  96.      * @author David Gilbert 
  97.      */  
  98.     static class Rotator extends Timer implements ActionListener {  
  99.   
  100.         /** The plot. */  
  101.         private PiePlot plot;  
  102.   
  103.         /** The angle. */  
  104.         private int angle = 270;  
  105.   
  106.         /** 
  107.          * Constructor. 
  108.          * 
  109.          * @param plot  the plot. 
  110.          */  
  111.         Rotator(PiePlot plot) {  
  112.             super(100null);  
  113.             this.plot = plot;  
  114.             addActionListener(this); // every 100ms, trigger self.  
  115.         }  
  116.   
  117.         /** 
  118.          * Modifies the starting angle. 
  119.          * Ref: 
  120.          *  - javax.swing.Timer : http://download.oracle.com/javase/6/docs/api/javax/swing/Timer.html 
  121.          *  - javax.awt.event.ActionListener : http://download.oracle.com/javase/6/docs/api/java/awt/event/ActionListener.html   
  122.          * @param event  the action event. 
  123.          */  
  124.         public void actionPerformed(ActionEvent event) {  
  125.             this.plot.setStartAngle(angle);  
  126.             this.angle = this.angle + 1;  
  127.             if (this.angle == 360) {  
  128.                 this.angle = 0;  
  129.             }  
  130.         }  
  131.     }  
  132. }  

執行結果 :
This message was edited 5 times. Last update was at 02/06/2011 18:05:26

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...