【发布时间】:2021-07-07 16:11:32
【问题描述】:
我有一个程序从 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);
}
}
【问题讨论】:
-
在event dispatch thread上仅构造和操作Swing GUI对象;一个相关的例子显示在here。正如here 所讨论的,x 值是从纪元开始的毫秒数,您可以使用
setDateFormatOverride()对其进行格式化。
标签: jfreechart