1

I have a program that receives temperature sensor data from serial communication that is being sent from an Arduino. I managed to plot temperature on Y Axis successfully, but now I want to plot on the X Axis the incoming data from a RTC Module which has a String format i.e. "15:48".

I figured out I can add numeric value on the X Axis with series.add(int number,int number2) but I don't know how to add a String value on the X Axis from the incoming RTC Value.(I really need to use the RTC Module since I'm writing data to an SD Card from Arduino and the values needs to match).

Here's the code I'm using.

package gráfica.temperatura;

import com.fazecast.jSerialComm.SerialPort;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;




public class GráficaTemperatura {

    static  SerialPort chosenPort;
    static int x=0;
    public static void main(String[] args) {
       JFrame window = new JFrame();
       window.setTitle("Monitor de Temperatura F&CS Mexico");
       window.setSize(600,400);
       window.setLayout(new BorderLayout());
       window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
       //CONEXIÓN SERIAL
       JComboBox<String> portList = new JComboBox<String>();
       JButton connectButton = new JButton("Conectar");
       JPanel topPanel = new JPanel();
       topPanel.add(portList);
       topPanel.add(connectButton);
       window.add(topPanel, BorderLayout.NORTH);
       
       //AÑADE OPCIONES A LISTA DE CONEXION
       SerialPort[] portNames = SerialPort.getCommPorts();
       for(int i = 0; i < portNames.length; i++)
           portList.addItem(portNames[i].getSystemPortName());
       

       
       
       XYSeries series = new XYSeries("Temperatura");
       XYSeries series2 = new XYSeries("Temperatura 2")
      
       XYSeriesCollection dataset = new XYSeriesCollection(series);
       dataset.addSeries(series2);
       JFreeChart chart = ChartFactory.createXYLineChart("FCS Temp. Monitor", "Hora", "Temperatura °C", dataset);
       window.add(new ChartPanel(chart), BorderLayout.CENTER);
       
//       

       


//CONFIGURACION DE BOTONES
       connectButton.addActionListener(new ActionListener(){
           @Override public void actionPerformed(ActionEvent arg0){
               if(connectButton.getText().equals("Conectar")){
                   //conectar serial
                   chosenPort = SerialPort.getCommPort(portList.getSelectedItem().toString());
                   chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
                   if(chosenPort.openPort()){
                       connectButton.setText("Desconectado");
                       portList.setEnabled(false);
                   }
                   
                   //RECIBIR DATOS
                   Thread thread = new Thread(){
                       @Override public void run(){
                           Scanner scanner = new Scanner(chosenPort.getInputStream());
                           
                           while(scanner.hasNextLine()){
                               try{
                               String line = scanner.nextLine();
                               float number = Float.parseFloat(line);   
                               String line2 =scanner.nextLine();
                               float number2 = Float.parseFloat(line2);
                               String tiempo =scanner.nextLine();
                               double tiemp = Double.parseDouble(tiempo);
                               
                               series.add(x++, number);
                               series2.add(x++, number2);
                               
                               
                               window.repaint();
                               
                           }catch(Exception e){}
                                
                           }
                           scanner.close();
                       }
                   };
                   thread.start();
               }else{
                   chosenPort.closePort();
                   portList.setEnabled(true);
                   connectButton.setText("Conectar");
                   
                   
               }
           }
       
       });
       
       window.setVisible(true);
       
       
    }
    
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Construct and manipulate Swing GUI objects _only_ on the [event dispatch thread](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html); a related example is shown [here](https://stackoverflow.com/a/13205322/230513). As discussed [here](https://stackoverflow.com/search?tab=newest&q=user%3a230513%20%5bjfreechart%5d%20epoch), the x value is the number of milliseconds from the epoch, which you can format with `setDateFormatOverride()`. – trashgod Jul 07 '21 at 17:06

1 Answers1

0

I managed to solve the problem by making a TimeSeries instead of a XYSeries, then i just scanned the incoming time String from arduino and parse it to a Date using:

//READ THE INCOMING SENSOR VALUE
String line = scanner.nextLine();
         double number = Double.parseDouble(line);
//READ THE INCOMING TIME STRING 
         String sDate1=scanner.nextLine();
         Date date1=new SimpleDateFormat("HH:mm").parse(sDate1);                                   
             //ADD IT TO A TIME SERIES CHART                     
          series.add(new Minute(date1), number);