0

I am working on an arduino project where Im supposed to plot graph using serial read. I am using 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.

Im getting some numerical values which is delimited by ",". Following is the code. Please help me in plotting 4 or multiple graphs. 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);
}

}

Anupam
  • 25
  • 7

1 Answers1

2

You program is incorrectly synchronized in that it updates Swing GUI components from a thread other than the event dispatch thread. 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. You can add() values to the individual XYSeries that comprise your XYSeriesCollection, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045