gagan93

Jfreechart

Apr 11th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. public class GenerateGraph
  2. {
  3.     /* No. of points displayed */
  4.     private static final int COUNT = 50;
  5.  
  6.     /* Speed of graph in milliseconds */
  7.     private static final int FAST = 100;
  8.  
  9.     /* to generate random no. (for test purpose only) */
  10.     private static final Random random = new Random();
  11.  
  12.     /* to run timer after 'FAST' seconds */
  13.     private Timer timer;
  14.  
  15.     /* hoolds the data to be displayed */
  16.     float data[] = new float[COUNT];
  17.  
  18.     JPanel graph()
  19.     {
  20.         /* init class MilliDTSC */
  21.         final MilliDTSC dataset = new MilliDTSC(50, COUNT, new Millisecond());
  22.  
  23.         /* add the dataset */
  24.         dataset.addSeries(data, 0, "");
  25.  
  26.         /* set time base = Millisecond */
  27.         dataset.setTimeBase(new Millisecond(100, new Second()));
  28.  
  29.         /* create a new chart with this information */
  30.         JFreeChart chart = ChartFactory.createTimeSeriesChart("", "Time (ms)",
  31.                 "Speed (kmph)", dataset);
  32.  
  33.         /* set X and Y axis properties */
  34.         XYPlot plot = chart.getXYPlot();
  35.         final ValueAxis domain = plot.getDomainAxis();
  36.  
  37.         domain.setAutoRange(true);
  38.        
  39.        
  40.         NumberAxis axis = new NumberAxis();
  41.         axis.setTickUnit(new NumberTickUnit(100));
  42.        
  43.            
  44.        
  45.        
  46.         /* what should be done here now */
  47.        
  48.        
  49.  
  50.         ValueAxis range = plot.getRangeAxis();
  51.         range.setRange(0, 100);
  52.  
  53.         /* Chart panel extends javax.swing.JPanel and holds a jfreechart */
  54.         MyChartPanel c = new MyChartPanel(chart);
  55.         /*
  56.          * used to run a timer thread (daemon) in infinite loop to append data
  57.          * to the chart
  58.          */
  59.         timer = new Timer(FAST, new ActionListener()
  60.         {
  61.  
  62.             float[] newData = new float[1];
  63.  
  64.             public void actionPerformed(ActionEvent e)
  65.             {
  66.                 /*
  67.                  * this flag is updated, if TestScreen panel is removed from the
  68.                  * main frame, so accordingly, this thread should stop
  69.                  */
  70.                 if (TestScreen.exitFlag)
  71.                     timer.stop();
  72.                 newData[0] = 15 + random.nextInt(25);
  73.                 dataset.advanceTime();
  74.                 dataset.appendData(newData);
  75.                 SwingUtilities.invokeLater(new Runnable()
  76.                 {
  77.                     public void run()
  78.                     {
  79.                         //TestScreen.leftPanel.repaint();
  80.                         //TestScreen.leftPanel.revalidate();
  81.                     }
  82.                 });
  83.                 ;
  84.             }
  85.         });
  86.         /* start timer thread */
  87.         timer.start();
  88.         return c;
  89.     }
Add Comment
Please, Sign In to add comment