程式扎記: [ Java 代碼範本 ] JFreeChart : BarChart/CategoryPlot - LayeredBarChartDemo2

標籤

2016年1月10日 星期日

[ Java 代碼範本 ] JFreeChart : BarChart/CategoryPlot - LayeredBarChartDemo2

Source From Here
UI Effect
 


Sample Code:
  1. package report.demo  
  2.   
  3. import java.awt.*;  
  4. import javax.swing.JPanel;  
  5. import org.jfree.chart.*;  
  6. import org.jfree.chart.axis.NumberAxis;  
  7. import org.jfree.chart.plot.CategoryPlot;  
  8. import org.jfree.chart.plot.PlotOrientation;  
  9. import org.jfree.chart.renderer.category.LayeredBarRenderer;  
  10. import org.jfree.data.category.CategoryDataset;  
  11. import org.jfree.data.category.DefaultCategoryDataset;  
  12. import org.jfree.ui.ApplicationFrame;  
  13. import org.jfree.ui.RefineryUtilities;  
  14. import org.jfree.util.SortOrder;  
  15.   
  16. class LayeredBarChartDemo2 extends ApplicationFrame{  
  17.     /** 
  18.      * 
  19.      */  
  20.     private static final long serialVersionUID = 1L;  
  21.      
  22.     public LayeredBarChartDemo2(String s)  
  23.     {  
  24.         super(s);  
  25.         JPanel jpanel = createDemoPanel();  
  26.         jpanel.setPreferredSize(new Dimension(700400));  
  27.         setContentPane(jpanel);  
  28.     }  
  29.      
  30.     private static CategoryDataset createDataset()  
  31.     {  
  32.         String s = "First";  
  33.         String s1 = "Second";  
  34.         String s2 = "Third";  
  35.         String s3 = "Category 1";  
  36.         String s4 = "Category 2";  
  37.         String s5 = "Category 3";  
  38.         String s6 = "Category 4";  
  39.         String s7 = "Category 5";  
  40.         DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();  
  41.         defaultcategorydataset.addValue(1.0D, s, s3);  
  42.         defaultcategorydataset.addValue(4D, s, s4);  
  43.         defaultcategorydataset.addValue(3D, s, s5);  
  44.         defaultcategorydataset.addValue(5D, s, s6);  
  45.         defaultcategorydataset.addValue(5D, s, s7);  
  46.         defaultcategorydataset.addValue(5D, s1, s3);  
  47.         defaultcategorydataset.addValue(7D, s1, s4);  
  48.         defaultcategorydataset.addValue(6D, s1, s5);  
  49.         defaultcategorydataset.addValue(8D, s1, s6);  
  50.         defaultcategorydataset.addValue(4D, s1, s7);  
  51.         defaultcategorydataset.addValue(4D, s2, s3);  
  52.         defaultcategorydataset.addValue(3D, s2, s4);  
  53.         defaultcategorydataset.addValue(2D, s2, s5);  
  54.         defaultcategorydataset.addValue(3D, s2, s6);  
  55.         defaultcategorydataset.addValue(6D, s2, s7);  
  56.         return defaultcategorydataset;  
  57.     }  
  58.      
  59.     private static JFreeChart createChart(CategoryDataset categorydataset)  
  60.     {  
  61.         JFreeChart jfreechart = ChartFactory.createBarChart("Layered Bar Chart Demo 2""Category""Value", categorydataset, PlotOrientation.HORIZONTAL, truetruefalse);  
  62.         jfreechart.setBackgroundPaint(Color.white);  
  63.         CategoryPlot categoryplot = jfreechart.getCategoryPlot();  
  64.         categoryplot.setBackgroundPaint(Color.lightGray);  
  65.         categoryplot.setDomainGridlinePaint(Color.white);  
  66.         categoryplot.setDomainGridlinesVisible(true);  
  67.         categoryplot.setRangeGridlinePaint(Color.white);  
  68.         NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();  
  69.         numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());  
  70.         LayeredBarRenderer layeredbarrenderer = new LayeredBarRenderer();  
  71.         layeredbarrenderer.setDrawBarOutline(false);  
  72.         categoryplot.setRenderer(layeredbarrenderer);  
  73.         categoryplot.setRowRenderingOrder(SortOrder.DESCENDING);  
  74.         GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F, Color.blue, 0.0F, 0.0F, new Color(0064));  
  75.         GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, Color.green, 0.0F, 0.0F, new Color(0640));  
  76.         GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F, Color.red, 0.0F, 0.0F, new Color(6400));  
  77.         layeredbarrenderer.setSeriesPaint(0, gradientpaint);  
  78.         layeredbarrenderer.setSeriesPaint(1, gradientpaint1);  
  79.         layeredbarrenderer.setSeriesPaint(2, gradientpaint2);  
  80.         return jfreechart;  
  81.     }  
  82.      
  83.     public static JPanel createDemoPanel()  
  84.     {  
  85.         JFreeChart jfreechart = createChart(createDataset());  
  86.         return new ChartPanel(jfreechart);  
  87.     }  
  88.      
  89.     public static void main(args)  
  90.     {  
  91.         LayeredBarChartDemo2 layeredbarchartdemo2 = new LayeredBarChartDemo2("Layered Bar Chart Demo 2");  
  92.         layeredbarchartdemo2.pack();  
  93.         RefineryUtilities.centerFrameOnScreen(layeredbarchartdemo2);  
  94.         layeredbarchartdemo2.setVisible(true);  
  95.     }  
  96. }  

沒有留言:

張貼留言

網誌存檔

關於我自己

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