程式扎記: [ Java 代碼範本 ] JFreeChart : Bar Chart (Stacked) - StackedBarChartDemo1

標籤

2016年1月11日 星期一

[ Java 代碼範本 ] JFreeChart : Bar Chart (Stacked) - StackedBarChartDemo1

Source From Here
UI Effect
 


Sample Code
  1. package bar.stacked;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Dimension;  
  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.labels.StandardCategoryItemLabelGenerator;  
  12. import org.jfree.chart.plot.CategoryPlot;  
  13. import org.jfree.chart.plot.PlotOrientation;  
  14. import org.jfree.chart.renderer.category.StackedBarRenderer;  
  15. import org.jfree.data.category.CategoryDataset;  
  16. import org.jfree.data.category.DefaultCategoryDataset;  
  17. import org.jfree.ui.ApplicationFrame;  
  18. import org.jfree.ui.RefineryUtilities;  
  19.   
  20. public class StackedBarChartDemo1 extends ApplicationFrame {  
  21.   
  22.     private static final long serialVersionUID = 1L;  
  23.   
  24.     public StackedBarChartDemo1(String s) {  
  25.         super(s);  
  26.         JPanel jpanel = createDemoPanel();  
  27.         jpanel.setPreferredSize(new Dimension(500270));  
  28.         setContentPane(jpanel);  
  29.     }  
  30.   
  31.     private static CategoryDataset createDataset() {  
  32.         DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();  
  33.         defaultcategorydataset.addValue(32.399999999999999D, "Series 1""Category 1");  
  34.         defaultcategorydataset.addValue(17.800000000000001D, "Series 2""Category 1");  
  35.         defaultcategorydataset.addValue(27.699999999999999D, "Series 3""Category 1");  
  36.         defaultcategorydataset.addValue(43.200000000000003D, "Series 1""Category 2");  
  37.         defaultcategorydataset.addValue(15.6D, "Series 2""Category 2");  
  38.         defaultcategorydataset.addValue(18.300000000000001D, "Series 3""Category 2");  
  39.         defaultcategorydataset.addValue(23D, "Series 1""Category 3");  
  40.         defaultcategorydataset.addValue(11.300000000000001D, "Series 2""Category 3");  
  41.         defaultcategorydataset.addValue(25.5D, "Series 3""Category 3");  
  42.         defaultcategorydataset.addValue(13D, "Series 1""Category 4");  
  43.         defaultcategorydataset.addValue(11.800000000000001D, "Series 2""Category 4");  
  44.         defaultcategorydataset.addValue(29.5D, "Series 3""Category 4");  
  45.         return defaultcategorydataset;  
  46.     }  
  47.   
  48.     private static JFreeChart createChart(CategoryDataset categorydataset) {  
  49.         JFreeChart jfreechart = ChartFactory.createStackedBarChart("Stacked Bar Chart Demo 1""Category""Value", categorydataset, PlotOrientation.VERTICAL, truetruefalse);  
  50.         jfreechart.setBackgroundPaint(Color.white);  
  51.         CategoryPlot categoryplot = (CategoryPlot) jfreechart.getPlot();  
  52.         categoryplot.setBackgroundPaint(Color.lightGray);  
  53.         categoryplot.setRangeGridlinePaint(Color.white);  
  54.         StackedBarRenderer stackedbarrenderer = (StackedBarRenderer) categoryplot.getRenderer();  
  55.         stackedbarrenderer.setDrawBarOutline(false);  
  56.         stackedbarrenderer.setBaseItemLabelsVisible(true);  
  57.         stackedbarrenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());  
  58.         return jfreechart;  
  59.     }  
  60.   
  61.     public static JPanel createDemoPanel() {  
  62.         JFreeChart jfreechart = createChart(createDataset());  
  63.         return new ChartPanel(jfreechart);  
  64.     }  
  65.   
  66.     public static void main(String args[]) {  
  67.         StackedBarChartDemo1 stackedbarchartdemo1 = new StackedBarChartDemo1("Stacked Bar Chart Demo 1");  
  68.         stackedbarchartdemo1.pack();  
  69.         RefineryUtilities.centerFrameOnScreen(stackedbarchartdemo1);  
  70.         stackedbarchartdemo1.setVisible(true);  
  71.     }  
  72. }  

沒有留言:

張貼留言

網誌存檔

關於我自己

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