程式扎記: [ Java 代碼範本 ] JFreeChart : MultiplePieChart - Demo 1 (多維度的圓餅圖)

標籤

2011年6月23日 星期四

[ Java 代碼範本 ] JFreeChart : MultiplePieChart - Demo 1 (多維度的圓餅圖)

前言 : 
考慮如果你要表達不同區域的每季的營業額, 而每個圓餅圖代表著一個區域. 這時你便需要不只一個圓餅圖來呈現你的圖表. 這理要介紹如何實現多維度的圓餅圖. 首先你的資料將會以二維的陣列來表達, 並透過容器 CategoryDataset 來存放你的資料與對應的 label. 而在建立該容器可透過 DatasetUtilities 的方法 [])]createCategoryDataset() 來完成. 有了資料, 接下來便是圖表的建立. 透過 ChartFactory 的方法 createMultiplePieChart() 來建立多維度圓餅圖的圖表. 

範例代碼 : 
底下為建立 多維度圓餅圖的範例代碼, 當中也介紹如何修改圖表 Label 的字型與大小與 Label 顯示的內容. 

- MultiplePieChartDemo1.java :
  1. package john.jfreechart.piechart;  
  2.   
  3. import java.awt.Font;  
  4.   
  5. import org.jfree.chart.ChartFactory;  
  6. import org.jfree.chart.ChartPanel;  
  7. import org.jfree.chart.JFreeChart;  
  8. import org.jfree.chart.labels.StandardPieSectionLabelGenerator;  
  9. import org.jfree.chart.plot.MultiplePiePlot;  
  10. import org.jfree.chart.plot.PiePlot;  
  11. import org.jfree.data.category.CategoryDataset;  
  12. import org.jfree.data.general.DatasetUtilities;  
  13. import org.jfree.ui.ApplicationFrame;  
  14. import org.jfree.ui.RefineryUtilities;  
  15. import org.jfree.util.TableOrder;  
  16.   
  17. /** 
  18. * A simple demonstration application showing how to create a chart consisting of multiple 
  19. * pie charts. 
  20. * @author John-Lee 
  21. * 
  22. */  
  23. public class MultiplePieChartDemo1 extends ApplicationFrame {  
  24.   
  25.     /** 
  26.      * Creates a new demo. 
  27.      * 
  28.      * @param title  the frame title. 
  29.      */  
  30.     public MultiplePieChartDemo1(String title) {  
  31.   
  32.         super(title);  
  33.         CategoryDataset dataset = createDataset();  
  34.         JFreeChart chart = createChart(dataset);  
  35.         ChartPanel chartPanel = new ChartPanel(chart, truetruetruefalsetrue);  
  36.         chartPanel.setPreferredSize(new java.awt.Dimension(650400));  
  37.         setContentPane(chartPanel);  
  38.   
  39.     }  
  40.       
  41.     /** 
  42.      * Creates a sample dataset. 
  43.      *  
  44.      * @return A sample dataset. 
  45.      */  
  46.     private CategoryDataset createDataset() {  
  47.         double[][] data = new double[][] {  
  48.             {3.04.03.05.0},  
  49.             {5.07.06.08.0},  
  50.             {5.07.03.08.0},  
  51.             {1.02.03.04.0},  
  52.             {2.03.02.03.0}  
  53.         };  
  54.         CategoryDataset dataset = DatasetUtilities.createCategoryDataset(  
  55.             "Region "// rowKeyPrefix (Index of different PieCharts)  
  56.             "Sales/Q"// columnKeyPrefix (Index of PieChart's content)  
  57.             data  
  58.         );  
  59.         return dataset;  
  60.     }  
  61.       
  62.     /** 
  63.      * Creates a sample chart with the given dataset. 
  64.      *  
  65.      * @param dataset  the dataset. 
  66.      *  
  67.      * @return A sample chart. 
  68.      */  
  69.     private JFreeChart createChart(CategoryDataset dataset) {  
  70.         JFreeChart chart = ChartFactory.createMultiplePieChart(  
  71.             "Multiple Pie Chart",  // chart title  
  72.             dataset,               // dataset  
  73.             TableOrder.BY_ROW,  
  74.             true,                  // include legend  
  75.             true,  
  76.             false  
  77.         );  
  78.         MultiplePiePlot plot = (MultiplePiePlot) chart.getPlot();  
  79.         JFreeChart subchart = plot.getPieChart();  
  80.         PiePlot p = (PiePlot) subchart.getPlot();  
  81.         p.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}"));  
  82.         p.setLabelFont(new Font("SansSerif", Font.PLAIN, 10));  
  83.         // This controls the space between the edges of the pie plot and the plot area itself   
  84.         //(the region where the section labels appear).  
  85.         //p.setInteriorGap(0.03);   
  86.           
  87.         return chart;  
  88.     }  
  89.       
  90.     /** 
  91.      * Starting point for the demonstration application. 
  92.      * 
  93.      * @param args  ignored. 
  94.      */  
  95.     public static void main(String[] args) {  
  96.         MultiplePieChartDemo1 demo = new MultiplePieChartDemo1("Multiple Pie Chart Demo 1");  
  97.         demo.pack();  
  98.         RefineryUtilities.centerFrameOnScreen(demo);  
  99.         demo.setVisible(true);  
  100.   
  101.     }  
  102. }  

執行結果 : 

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!