程式扎記: [ Java 代碼範本 ] JFreeChart : XYChart - XYTaskData set (1)

標籤

2014年6月11日 星期三

[ Java 代碼範本 ] JFreeChart : XYChart - XYTaskData set (1)

來源自 這裡
UI 效果: (jfreechart-1.0.14)


範例代碼:
- ScatterPlotDemo1.java
  1. package charts.xy;  
  2.   
  3. import java.awt.Color;  
  4.   
  5. import javax.swing.JPanel;  
  6.   
  7. import org.jfree.chart.ChartFactory;  
  8. import org.jfree.chart.ChartPanel;  
  9. import org.jfree.chart.JFreeChart;  
  10. import org.jfree.chart.axis.NumberAxis;  
  11. import org.jfree.chart.plot.PlotOrientation;  
  12. import org.jfree.chart.plot.XYPlot;  
  13. import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;  
  14. import org.jfree.data.xy.XYDataset;  
  15. import org.jfree.data.xy.XYSeries;  
  16. import org.jfree.data.xy.XYSeriesCollection;  
  17. import org.jfree.ui.ApplicationFrame;  
  18. import org.jfree.ui.RefineryUtilities;  
  19.   
  20. public class ScatterPlotDemo1 extends ApplicationFrame {  
  21.   
  22.     /** 
  23.      * A demonstration application showing a scatter plot. 
  24.      * 
  25.      * @param title  the frame title. 
  26.      */  
  27.     public ScatterPlotDemo1(String title) {  
  28.         super(title);  
  29.         JPanel chartPanel = createDemoPanel();  
  30.         chartPanel.setPreferredSize(new java.awt.Dimension(550350));  
  31.         setContentPane(chartPanel);  
  32.     }  
  33.   
  34.     private static XYDataset createData()  
  35.     {  
  36.         XYSeriesCollection my_data_series= new XYSeriesCollection();  
  37.         XYSeries s = new XYSeries("Cluster1");  
  38.         s.add(1,1);  
  39.         s.add(2,2);  
  40.         s.add(3,1);  
  41.         s.add(2,4);  
  42.         my_data_series.addSeries(s);  
  43.         s = new XYSeries("Cluster2");  
  44.         s.add(2,14);  
  45.         s.add(3,13);  
  46.         s.add(3,14);  
  47.         my_data_series.addSeries(s);  
  48.         s = new XYSeries("Cluster3");  
  49.         s.add(11,4);  
  50.         s.add(12,4);  
  51.         s.add(13,4);  
  52.         s.add(14,4);  
  53.         my_data_series.addSeries(s);  
  54.         s = new XYSeries("Cluster4");  
  55.         s.add(12,13);  
  56.         s.add(13,12);  
  57.         s.add(13,13);  
  58.         s.add(14,14);  
  59.         my_data_series.addSeries(s);  
  60.         return my_data_series;  
  61.     }  
  62.       
  63.     private static JFreeChart createChart(XYDataset dataset) {  
  64.         JFreeChart chart = ChartFactory.createScatterPlot("Scatter Plot Demo 1",  
  65.                 "X""Y", dataset, PlotOrientation.VERTICAL, truefalsefalse);  
  66.   
  67.         XYPlot plot = (XYPlot) chart.getPlot();  
  68.         plot.setNoDataMessage("NO DATA");  
  69.         plot.setDomainZeroBaselineVisible(true);  
  70.         plot.setRangeZeroBaselineVisible(true);  
  71.           
  72.         XYLineAndShapeRenderer renderer   
  73.                 = (XYLineAndShapeRenderer) plot.getRenderer();  
  74.         renderer.setSeriesOutlinePaint(0, Color.black);  
  75.         renderer.setUseOutlinePaint(true);  
  76.         NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();  
  77.         domainAxis.setAutoRangeIncludesZero(false);  
  78.         domainAxis.setTickMarkInsideLength(2.0f);  
  79.         domainAxis.setTickMarkOutsideLength(0.0f);  
  80.           
  81.         NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();  
  82.         rangeAxis.setTickMarkInsideLength(2.0f);  
  83.         rangeAxis.setTickMarkOutsideLength(0.0f);  
  84.           
  85.         return chart;  
  86.     }  
  87.       
  88.     /** 
  89.      * Creates a panel for the demo (used by SuperDemo.java). 
  90.      *  
  91.      * @return A panel. 
  92.      */  
  93.     public static JPanel createDemoPanel() {  
  94.         JFreeChart chart = createChart(createData());  
  95.         ChartPanel chartPanel = new ChartPanel(chart);  
  96.         //chartPanel.setVerticalAxisTrace(true);  
  97.         //chartPanel.setHorizontalAxisTrace(true);  
  98.         // popup menu conflicts with axis trace  
  99.         chartPanel.setPopupMenu(null);  
  100.           
  101.         chartPanel.setDomainZoomable(true);  
  102.         chartPanel.setRangeZoomable(true);  
  103.         return chartPanel;  
  104.     }  
  105.       
  106.     /** 
  107.      * Starting point for the demonstration application. 
  108.      * 
  109.      * @param args  ignored. 
  110.      */  
  111.     public static void main(String[] args) {  
  112.         ScatterPlotDemo1 demo = new ScatterPlotDemo1("Scatter Plot Demo 1");  
  113.         demo.pack();  
  114.         RefineryUtilities.centerFrameOnScreen(demo);  
  115.         demo.setVisible(true);  
  116.     }  
  117.   
  118. }  


沒有留言:

張貼留言

網誌存檔

關於我自己

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