簡體   English   中英

如何在JFreechart圖上繪制多個圖

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

我正在一個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);
}

}

您的程序未正確同步,因為它從事件分配線程以外的線程更新Swing GUI組件。 使用此處所示的SwingWorker來收集doInBackground()實現中的數據, publish()中間結果,並更新process()實現中的每個系列,這將在事件分發線程上調用。 您可以add()值的個體XYSeries組成的XYSeriesCollection ,如圖所示這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM