使用来自输入流的字符串在 XYLineChart(JFreeChart) 上设置 X 轴

Set X-Axis on XYLineChart(JFreeChart) with String coming from inputstream

我有一个程序可以从 Arduino 发送的串行通信中接收温度传感器数据。我成功地在 Y 轴上绘制了温度,但现在我想在 X 轴上绘制来自 RTC 模块的输入数据,该模块具有字符串格式,即“15:48”。

我发现我可以使用 series.add(int number,int number2) 在 X 轴上添加数值,但我不知道如何从传入的 RTC 值在 X 轴上添加字符串值。(我真的需要使用 RTC 模块,因为我正在将数据从 Arduino 写入 SD 卡并且值需要匹配)。

这是我正在使用的代码。

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

我设法通过制作 TimeSeries 而不是 XYSeries 来解决问题,然后我只是扫描了来自 arduino 的传入时间字符串并使用以下方法将其解析为日期:

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