我正在使用DynamicTimeSeriesCollection来绘制从串口到我的图表的测量结果。 我想使用我在另一篇文章中找到的代码。
我希望实现这样的目标: Using JFreeChart to display recent changes in a time series
我发现的其他帖子中的代码:
How to combine Java plot code and Java serial code to read Arduino sensor value?
1. Move dataset and newData up to become instance variables:
DynamicTimeSeriesCollection dataset;
float[] newData = new float[1];
2. Add a method that appends your data to the chart's dataset:
public synchronized void addData(byte[] chunk) {
for (int i = 0; i < chunk.length; i++) {
newData[0] = chunk[i];
dataset.advanceTime();
dataset.appendData(newData);
}
}
3. Invoke the method from serialEvent():
demo.addChunk(chunk);
这是SerialCommunication类中的串行事件方法:
public void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try
{
Date d = new Date();
SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String datetime = dt.format(d);
String inputLine = input.readLine();
String[] measurements = inputLine.split(",");
double humidity = Double.parseDouble(measurements[0]);
double temp = Double.parseDouble(measurements[1]);
double ldr = Double.parseDouble(measurements[2]);
humidityList.add(humidity);
tempList.add(temp);
ldrList.add(ldr);
System.out.println(datetime+" > "+inputLine);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
串行通信类中的ArrayList:
private ArrayList<Double> humidityList = new ArrayList<Double>();
private ArrayList<Double> tempList = new ArrayList<Double>();
private ArrayList<Double> ldrList = new ArrayList<Double>();
以下是我在GUI类中声明ArrayList的方法:
private static ArrayList<Double> m1;
private static ArrayList<Double> m2;
private static ArrayList<Double> m3;
public GUI(final String title,ArrayList<Double> humidity,ArrayList<Double> temp,ArrayList<Double> ldr)
{
super(title);
m1 = humidity;
m2 = temp;
m3 = ldr;
我确实尝试修改我用测量结果找到的代码,但没有运气。任何想法我怎么能实现这个目标?