简体   繁体   English

如何在JFreechart图上绘制多个图

[英]How to plot more than one graph on JFreechart graph

I am working on an arduino project where Im supposed to plot graph using serial read. 我正在一个arduino项目中,我应该使用串行读取来绘制图形。 I am using JFreechart. 我正在使用JFreechart。 In my code I am able to plot only one graph. 在我的代码中,我只能绘制一个图形。 But I need to plot 4 or multiple plots on the same graph. 但是我需要在同一张图上绘制4个或多个图。

Im getting some numerical values which is delimited by ",". 我得到了一些以“,”为界的数值。 Following is the code. 以下是代码。 Please help me in plotting 4 or multiple graphs. 请帮助我绘制4个或多个图形。 I can plot one but not more than one. 我可以绘制一张,但不能超过一张。

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

} }

You program is incorrectly synchronized in that it updates Swing GUI components from a thread other than the event dispatch thread . 您的程序未正确同步,因为它从事件分配线程以外的线程更新Swing GUI组件。 Use SwingWorker , illustrated here , to collect data in your implementation of doInBackground() , publish() interim results, and update each series in your implementation of process() , which will be called on the event dispatch thread. 使用此处所示的SwingWorker来收集doInBackground()实现中的数据, publish()中间结果,并更新process()实现中的每个系列,这将在事件分发线程上调用。 You can add() values to the individual XYSeries that comprise your XYSeriesCollection , as shown here . 您可以add()值的个体XYSeries组成的XYSeriesCollection ,如图所示这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM