2016-06-26 21 views
0

私はarduinoプロジェクトで、Imはシリアル読み取りを使用してグラフをプロットすることになっています。私はJFreechartを使用しています。私のコードでは、グラフを1つだけプロットすることができます。しかし、私は同じグラフに4つまたは複数のプロットをプロットする必要があります。JFreechartグラフに複数のグラフをプロットする方法

Im「、」で区切られたいくつかの数値を取得しています。以下はコードです。 4つまたは複数のグラフをプロットする際に私を助けてください。私は1つをプロットすることができますが、2つ以上はプロットできませ

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

}

答えて

1

あなたのプログラムが間違ってそれがevent dispatch thread以外のスレッドからのSwing GUIコンポーネントを更新していることで同期されます。 hereと示されたSwingWorkerを使用して、doInBackground()publish()中間結果のデータを収集し、実装内の各シリーズをprocess()で更新します。これらは、イベントディスパッチスレッドで呼び出されます。 add()の値は、XYSeriesCollectionを構成する個人XYSeriesの値で、hereと表示されます。

関連する問題