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);
}
}