1

I'm trying to to make a chart that takes data from serial port and plot them in y axes and i want current time in x axes.. I think that i set my code correctly because i managed to run it as XY chart now in TimeSeries chart my only issue is that in method series.add(TIME, SERIALDATA); i dont know how to initialize TIME , i know that i want an object RegularTimePeriod but i dont know how to do that..

here is the code.. i know that only some lines are missing please help me to find them...

void initialize() {

    frame = new JFrame();
    frame.setBounds(100, 100, 817, 525);


    final TimeSeries series = new TimeSeries("Charts");
    final SerialDataReceived serialdataprint = new SerialDataReceived();


    final TimeSeriesCollection data = new TimeSeriesCollection(series);
    final JFreeChart chart = ChartFactory.createXYLineChart(
            "Tmperature IN",
            "Time", 
            "C", 
            data,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );




    final ChartPanel chartPanel = new ChartPanel(chart);
     chartPanel.setBounds(10, 11, 477, 224);
          chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
           chartPanel.setVisible(true);
           frame.getContentPane().setLayout(null);
         frame.getContentPane().add(chartPanel);
      chartPanel.setLayout(null);


     Thread outtempthread=new Thread() {  //THREAD THAT RUNS ALL THE TIME

        public void run() {

         try {
             while (true){
         Thread.sleep(2000);


     double intemp = serialdataprint.getintemp();  //THIS WHERE I TAKE MY SERIAL DATA
      series.add(I WANT TO  DISPLAY HERE LETS SAY 13:23:15, intemp);  //HERE IS MY PROBLEM



             }}
             catch (InterruptedException ie) {}
             }
             };

             outtempthread.start();

}
user3572380
  • 81
  • 3
  • 9

3 Answers3

0

I've only ever used TimeSeries measured in days so I used the org.jfree.data.time.Day class.

Here's the jfreechart javadoc for all the different time classes : http://www.jfree.org/jfreechart/api/javadoc/org/jfree/data/time/package-summary.html

Try out a few and see what's right for you.

Since you appear to only need hour,minute second of a single day, you might be able to use the Second class.

Here is how you would make a TimeSeries that way:

int todaysDay =...
int todaysMonth =...
int todaysYear =...

TimeSeries series = new TimeSeries(name, Second.class);

    //this should mark 'inTemp' as 13:23:15
    series.add(new Second(15,23,13,todaysDay, todaysMonth, todaysYear), 
               inTemp);
dkatzel
  • 31,188
  • 3
  • 63
  • 67
  • Thank you i had some progress but still i'm not sure how it works... the time i get in the x axes updates by the thread i have, but is not readable it's like this 1.403.128.800.000 and changes with the same style.. i only want the time when the serial thread updates.. why is it so difficult in visual basic it took me 2 seconds to do it.. the code is like this series.add(new Second(), intemp); i didnt put any parametres in new Second because it throws an exception.. – user3572380 Jun 18 '14 at 22:01
0

ok!! finally i found the solution! i don't know if is the correct way but it works an now i have real time in my chart every time my serial port updates here is the fix code!

String timeStamp1 = new SimpleDateFormat("mm").format(Calendar.getInstance().getTime());
         int minute = Integer.parseInt(timeStamp1);


double intemp = serialdataprint.getintemp();    
     series.addOrUpdate(new Minute(minute,hour), intemp);
user3572380
  • 81
  • 3
  • 9
0

A couple of pointers:

  1. The ChartFactory.createXYLineChart() method will create a line chart where both the X and Y axes are numerical. Try the createTimeSeriesChart() to get a chart that shows dates on the X axis (or create a new DateAxis() instance and call plot.setDomainAxis() to change the X axis);
  2. The TimeSeriesCollection class is a good dataset to use for time series data if you need the structure that it provides (it enforces a regular time period and prevents duplicates among other things). However, bear in mind that it is simply an implementation of the XYDataset interface where the x-values returned are "milliseconds since 1-Jan-1970" (the standard encoding of "dates" in Java). You can simplify your code by using an XYSeriesCollection (which also implements the XYDataset interface), and call System.currentTimeInMillis() to get the current x-value when new data comes in. The date axis on your chart will take care of presenting a date scale for this data.
David Gilbert
  • 4,427
  • 14
  • 22