Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GenerateGraph
- {
- /* No. of points displayed */
- private static final int COUNT = 50;
- /* Speed of graph in milliseconds */
- private static final int FAST = 100;
- /* to generate random no. (for test purpose only) */
- private static final Random random = new Random();
- /* to run timer after 'FAST' seconds */
- private Timer timer;
- /* hoolds the data to be displayed */
- float data[] = new float[COUNT];
- JPanel graph()
- {
- /* init class MilliDTSC */
- final MilliDTSC dataset = new MilliDTSC(50, COUNT, new Millisecond());
- /* add the dataset */
- dataset.addSeries(data, 0, "");
- /* set time base = Millisecond */
- dataset.setTimeBase(new Millisecond(100, new Second()));
- /* create a new chart with this information */
- JFreeChart chart = ChartFactory.createTimeSeriesChart("", "Time (ms)",
- "Speed (kmph)", dataset);
- /* set X and Y axis properties */
- XYPlot plot = chart.getXYPlot();
- final ValueAxis domain = plot.getDomainAxis();
- domain.setAutoRange(true);
- NumberAxis axis = new NumberAxis();
- axis.setTickUnit(new NumberTickUnit(100));
- /* what should be done here now */
- ValueAxis range = plot.getRangeAxis();
- range.setRange(0, 100);
- /* Chart panel extends javax.swing.JPanel and holds a jfreechart */
- MyChartPanel c = new MyChartPanel(chart);
- /*
- * used to run a timer thread (daemon) in infinite loop to append data
- * to the chart
- */
- timer = new Timer(FAST, new ActionListener()
- {
- float[] newData = new float[1];
- public void actionPerformed(ActionEvent e)
- {
- /*
- * this flag is updated, if TestScreen panel is removed from the
- * main frame, so accordingly, this thread should stop
- */
- if (TestScreen.exitFlag)
- timer.stop();
- newData[0] = 15 + random.nextInt(25);
- dataset.advanceTime();
- dataset.appendData(newData);
- SwingUtilities.invokeLater(new Runnable()
- {
- public void run()
- {
- //TestScreen.leftPanel.repaint();
- //TestScreen.leftPanel.revalidate();
- }
- });
- ;
- }
- });
- /* start timer thread */
- timer.start();
- return c;
- }
Add Comment
Please, Sign In to add comment