gpt4 book ai didi

java - 如何在 JFreechart 图表上绘制多个图表

转载 作者:行者123 更新时间:2023-12-01 09:45:50 26 4
gpt4 key购买 nike

我正在开发一个arduino项目,我应该使用串行读取来绘制图表。我正在使用 JFreechart。在我的代码中,我只能绘制一张图。但我需要在同一张图上绘制 4 个或多个图。

我得到一些由“,”分隔的数值。以下是代码。请帮我绘制 4 个或多个图表。我可以绘制一个,但不能绘制多个。

import java.awt.BorderLayout;
import java.awt.Color;
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.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import com.fazecast.jSerialComm.SerialPort;

public class SerialDataRead {

static SerialPort chosenPort;

public static void main(String[] args) {
// TODO Auto-generated method stub
final JFrame window = new JFrame();
window.setTitle("Sensor graph GUI");
window.setSize(1200,800);
window.setLayout(new BorderLayout());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel basePanel = new JPanel();
basePanel.setSize(900, 700);

final JComboBox portList = new JComboBox();
final JButton connectButton = new JButton("Connect");
JPanel topPanel = new JPanel();
window.add(topPanel, BorderLayout.NORTH);
topPanel.add(portList);
topPanel.add(connectButton);


SerialPort[] portNames = SerialPort.getCommPorts();
for(int i =0; i < portNames.length; i++)
portList.addItem(portNames[i].getSystemPortName());



final XYSeries series = new XYSeries("Serial port data reading");
final XYSeries series2 = new XYSeries("Serial port data reading");
XYSeriesCollection dataset = new XYSeriesCollection(series);
JFreeChart chart = ChartFactory.createXYLineChart("Serial port reading", "Time (seconds)", "adc reading", dataset);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(Color.WHITE);
NumberAxis domain = (NumberAxis) plot.getDomainAxis();
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
//domain.setRange(0.00, 40.00);
rangeAxis.setRange(0.0, 505.0);
window.add(new ChartPanel(chart), BorderLayout.CENTER);


connectButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent arg0){
if(connectButton.getText().equals("Connect")){

chosenPort = SerialPort.getCommPort(portList.getSelectedItem().toString());
chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
if(chosenPort.openPort()){
connectButton.setText("Disconnect");
portList.setEnabled(false);

}

Thread thread = new Thread(){
@Override public void run(){
Scanner scanner = new Scanner(chosenPort.getInputStream());
int x =0;
while(scanner.hasNextLine()){

String line = scanner.nextLine();
String[] values = line.split(",");
int number = Integer.parseInt(values[0]);
int number2 = Integer.parseInt(values[1]);
series.add(x++, number);
//series2.add(x++, number2);
XYSeriesCollection my_data_series= new XYSeriesCollection();
// add series using addSeries method
my_data_series.addSeries(series);
// my_data_series.addSeries(series2);
// JFreeChart XYLineChart=ChartFactory.createXYLineChart("Team - Number of Wins","Year","Win Count",my_data_series,PlotOrientation.VERTICAL,true,true,false);
//window.repaint();
System.out.println(Integer.parseInt(values[1]));
}
scanner.close();
}
};

thread.start();


}else{

chosenPort.closePort();
portList.setEnabled(true);
connectButton.setText("Connect");
}

}
});

window.setVisible(true);
}

}

最佳答案

您的程序未正确同步,因为它从 event dispatch thread 以外的线程更新 Swing GUI 组件。 。使用SwingWorker ,如图here ,收集 doInBackground() 实现中的数据、publish() 临时结果,并更新 process() 实现中的每个系列,它将在事件调度线程上调用。您可以将 add() 值添加到构成 XYSeriesCollection 的各个 XYSeries 中,如图 here 所示。 .

关于java - 如何在 JFreechart 图表上绘制多个图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38036311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com