2015年12月2日 星期三

[ Java 代碼範本 ] JFreeChart : Bar Chart - BarChartDemo5

UI Effect


Sample Code
  1. package bar.category  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5.   
  6. import javax.swing.JPanel;  
  7.   
  8. import org.jfree.chart.ChartFactory;  
  9. import org.jfree.chart.ChartPanel;  
  10. import org.jfree.chart.JFreeChart;  
  11. import org.jfree.chart.axis.AxisLocation;  
  12. import org.jfree.chart.axis.CategoryAxis;  
  13. import org.jfree.chart.axis.NumberAxis;  
  14. import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;  
  15. import org.jfree.chart.plot.CategoryPlot;  
  16. import org.jfree.chart.plot.PlotOrientation;  
  17. import org.jfree.chart.renderer.category.BarRenderer;  
  18. import org.jfree.chart.title.TextTitle;  
  19. import org.jfree.data.category.CategoryDataset;  
  20. import org.jfree.data.category.DefaultCategoryDataset;  
  21. import org.jfree.ui.ApplicationFrame;  
  22. import org.jfree.ui.RefineryUtilities;  
  23.   
  24. /** 
  25. * http://www.jfree.org/jfreechart/BarChartDemo5.java 
  26. */  
  27. class BarChartDemo5 extends ApplicationFrame{  
  28.     /** 
  29.      * Creates a new demo instance. 
  30.      * 
  31.      * @param title  the frame title. 
  32.      */  
  33.     public BarChartDemo5(String title) {  
  34.         super(title);  
  35.         JPanel chartPanel = createDemoPanel();  
  36.         chartPanel.setPreferredSize(new java.awt.Dimension(700500));  
  37.         setContentPane(chartPanel);  
  38.     }  
  39.   
  40.     /** 
  41.      * Creates a sample dataset. 
  42.      * 
  43.      * @return A sample dataset. 
  44.      */  
  45.     private static CategoryDataset createDataset() {  
  46.           
  47.         // The following data is obtained from:  
  48.         //  
  49.         // http://www.homeoffice.gov.uk/rds/pdfs2/r188.pdf  
  50.         //  
  51.         // An arbitrary selection of countries has been included in this  
  52.         // dataset, please refer to the original report for the complete  
  53.         // set.  
  54.         DefaultCategoryDataset dataset = new DefaultCategoryDataset();  
  55.         String seriesKey = "Prison Population Rates";  
  56.         dataset.addValue(59, seriesKey, "Norway");  
  57.         dataset.addValue(69, seriesKey, "Switzerland");  
  58.         dataset.addValue(85, seriesKey, "France");  
  59.         dataset.addValue(93, seriesKey, "Syria");  
  60.         dataset.addValue(96, seriesKey, "Germany");  
  61.         dataset.addValue(111, seriesKey, "China");  
  62.         dataset.addValue(116, seriesKey, "Australia");  
  63.         dataset.addValue(121, seriesKey, "Egypt");  
  64.         dataset.addValue(129, seriesKey, "England & Wales");  
  65.         dataset.addValue(157, seriesKey, "New Zealand");  
  66.         dataset.addValue(205, seriesKey, "Chile");  
  67.         dataset.addValue(229, seriesKey, "Iran");  
  68.         dataset.addValue(359, seriesKey, "Singapore");  
  69.         dataset.addValue(404, seriesKey, "South Africa");  
  70.         dataset.addValue(406, seriesKey, "Ukraine");  
  71.         dataset.addValue(686, seriesKey, "USA");  
  72.         return dataset;  
  73.     }  
  74.       
  75.     /** 
  76.      * Creates a chart. 
  77.      * 
  78.      * @param dataset  the dataset. 
  79.      * 
  80.      * @return A chart. 
  81.      */  
  82.     private static JFreeChart createChart(final CategoryDataset dataset) {  
  83.         // create the chart...  
  84.         JFreeChart chart = ChartFactory.createBarChart(  
  85.             "Prison Population Rates - Selected Countries",  // chart title  
  86.             "Country",                  // domain axis label  
  87.             "Prisoners Per 100,000 National Population",              // range axis label  
  88.             dataset,                     // data  
  89.             PlotOrientation.HORIZONTAL,  // orientation  
  90.             true,                        // include legend  
  91.             true,  
  92.             false  
  93.         );  
  94.         chart.addSubtitle(new TextTitle(  
  95.                 "Source: http://www.homeoffice.gov.uk/rds/pdfs2/r188.pdf",  
  96.                 new Font("Dialog", Font.ITALIC, 10)));  
  97.         chart.setBackgroundPaint(Color.white);  
  98.         CategoryPlot plot = (CategoryPlot) chart.getPlot();  
  99.         plot.setBackgroundPaint(Color.lightGray);  
  100.         plot.setRangeGridlinePaint(Color.white);  
  101.         plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);  
  102.         BarRenderer renderer = (BarRenderer) plot.getRenderer();  
  103.         renderer.setBaseItemLabelsVisible(true);  
  104.         renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());  
  105.         CategoryAxis categoryAxis = plot.getDomainAxis();  
  106.         categoryAxis.setCategoryMargin(0.0);  
  107.         categoryAxis.setUpperMargin(0.02);  
  108.         categoryAxis.setLowerMargin(0.02);  
  109.         NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();  
  110.         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());  
  111.         rangeAxis.setUpperMargin(0.10);  
  112.   
  113.         return chart;  
  114.     }  
  115.       
  116.     /** 
  117.      * Creates a panel for the demo (used by SuperDemo.java). 
  118.      * 
  119.      * @return A panel. 
  120.      */  
  121.     public static JPanel createDemoPanel() {  
  122.         JFreeChart chart = createChart(createDataset());  
  123.         return new ChartPanel(chart);  
  124.     }  
  125.       
  126.     /** 
  127.      * Starting point for the demonstration application. 
  128.      * 
  129.      * @param args  ignored. 
  130.      */  
  131.     public static void main(String[] args) {  
  132.   
  133.         BarChartDemo5 demo = new BarChartDemo5("Bar Chart Demo 5");  
  134.         demo.pack();  
  135.         RefineryUtilities.centerFrameOnScreen(demo);  
  136.         demo.setVisible(true);  
  137.   
  138.     }  
  139. }  

沒有留言:

張貼留言

[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...