2016-05-27 61 views
0

我想從串口讀取一些數據並使用JFreeChart和ChartPanel顯示相同的數據。 當我點擊'Aquire'按鈕後,我從串口獲得了一組新的數據。首次顯示面板時,顯示從串口讀取的值。當我點擊'Aquire'按鈕後,這些值將被填充到變量'dataset'中,但它們不會在圖表上刷新。我哪裏錯了?請幫忙。提前致謝。JFreeChart沒有更新ChartPanel,從串口讀取數據後

import java.awt.BasicStroke; 
import java.awt.Button; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Label; 
import java.awt.TextField; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.geom.Rectangle2D; 

import javax.swing.GroupLayout; 
import javax.swing.JApplet; 

import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartMouseEvent; 
import org.jfree.chart.ChartMouseListener; 
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.chart.annotations.XYLineAnnotation; 
import org.jfree.chart.axis.ValueAxis; 
import org.jfree.chart.event.ChartChangeEvent; 
import org.jfree.chart.event.ChartChangeListener; 
import org.jfree.chart.panel.CrosshairOverlay; 
import org.jfree.chart.plot.Crosshair; 
import org.jfree.chart.plot.PlotOrientation; 
import org.jfree.chart.plot.XYPlot; 
import org.jfree.chart.renderer.xy.XYItemRenderer; 
import org.jfree.data.general.DatasetUtilities; 
import org.jfree.data.xy.XYDataset; 
import org.jfree.data.xy.XYSeriesCollection; 
import org.jfree.ui.RectangleEdge; 

public class DisplayPanel extends JApplet implements ActionListener 
{ 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    SimplePortComm spc; 
    JFreeChart xylineChart; 
    ChartPanel panel; 
    XYSeriesCollection dataset; 
    public static String sLLD; 
    public static String sULD; 

    Label lblTitle; 
    Label lblMax, lblMin, lblULD, lblLLD; 
    TextField txtMax, txtMin, txtULD, txtLLD; 
    Label lblInrangeCount; 
    TextField txtInrangeCount; 
    Button bAquire, bSave, bLoad, bQuit; 
    int i, nInRangeChannels; 
    private Crosshair xCrosshair; 
    private Crosshair yCrosshair; 


    double dULD,dLLD; 

    public DisplayPanel() { 

     dataset = new XYSeriesCollection(); 

     spc = new SimplePortComm(); 


     lblTitle = new Label("Data Aquisition System for MCA"); 
     lblTitle.setPreferredSize(new Dimension(600, 20)); 

     lblMax = new Label("Max:"); 
     lblMin = new Label("Min:"); 
     lblULD = new Label("ULD:"); 
     lblLLD = new Label("LLD:"); 
     lblInrangeCount = new Label("Count in Range:"); 
     lblInrangeCount.setPreferredSize(new Dimension(60, 20)); 

     txtMax = new TextField(10); 
     txtMax.setEditable(false); 
     txtMin = new TextField(10); 
     txtMin.setEditable(false); 
     txtULD = new TextField(10); 
     txtULD.setText("240.0"); 
     txtLLD = new TextField(10); 
     txtLLD.setText("20.0"); 
     txtInrangeCount = new TextField(10); 
     txtInrangeCount.setPreferredSize(new Dimension(60, 20)); 
     txtInrangeCount.setEditable(false); 

     dULD = Double.parseDouble(txtULD.getText()); 
     dLLD = Double.parseDouble(txtLLD.getText()); 
     drawChart(); 

     bAquire = new Button("Aquire");  
     bAquire.setPreferredSize(new Dimension(20, 60)); 
     bAquire.addActionListener(
      new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent aquire) { 
       //panel.setRefreshBuffer(true); 
       System.setSecurityManager(null);// with out this statement, set_ports() throws null pointer exception 
       spc.set_ports(dULD, dLLD); 
       dataset = SimplePortComm.dataset; 

       panel.repaint(); 
       panel.updateUI(); 

       System.out.print("Button: Aquire"); 
      } 
      } 
     ); 

     bSave = new Button("Save"); 
     bSave.setPreferredSize(null); 
     bSave.addActionListener(
      new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent save) { 
       System.out.print("Button: Save"); 
      } 
      } 
     ); 

     bLoad = new Button("Load"); 
     bLoad.setPreferredSize(null); 
     bLoad.addActionListener(
      new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent load) { 
       System.out.print("Button: Load"); 
       sLLD = txtLLD.getText(); 
       sULD = txtULD.getText(); 

       txtInrangeCount.setText(Integer.toString(spc.getInRangeChannels()));     
      } 
      } 
     ); 

     bQuit = new Button("Quit"); 
     bQuit.setPreferredSize(new Dimension(10, 10)); 
     bQuit.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent quit) { 
       System.out.print("Button: Quit"); 
       System.exit(0); 
      } 
     }); 

     GroupLayout layout = new GroupLayout(getContentPane());  
     layout.setAutoCreateGaps(true); 
     layout.setAutoCreateContainerGaps(true); 

     layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
      .addComponent(lblTitle) 
      .addComponent(panel) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(bAquire) 
       .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
        .addComponent(lblMax) 
        .addComponent(lblMin)) 
       .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
        .addComponent(txtMax) 
        .addComponent(txtMin)) 
       .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
        .addComponent(lblULD) 
        .addComponent(lblLLD)) 
       .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) 
        .addComponent(txtULD) 
        .addComponent(txtLLD))) 
     .addGroup(layout.createSequentialGroup() 
      .addComponent(lblInrangeCount) 
      .addComponent(txtInrangeCount)) 
     .addGroup(layout.createSequentialGroup() 
      .addComponent(bSave) 
      .addComponent(bLoad) 
      .addComponent(bQuit)) 
    ); 

     layout.setVerticalGroup(layout.createSequentialGroup() 
      .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
       .addGroup(layout.createSequentialGroup() 
        .addComponent(lblTitle))) 
      .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
       .addGroup(layout.createSequentialGroup() 
        .addComponent(panel))) 
      .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
       .addGroup(layout.createSequentialGroup() 
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
         .addComponent(bAquire)) 
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
         .addComponent(lblMax) 
         .addComponent(txtMax) 
         .addComponent(lblULD) 
         .addComponent(txtULD))       
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
         .addComponent(lblMin) 
         .addComponent(txtMin) 
         .addComponent(lblLLD) 
         .addComponent(txtLLD)))) 
      .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
        .addComponent(lblInrangeCount) 
        .addComponent(txtInrangeCount)) 
      .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
        .addComponent(bSave) 
        .addComponent(bLoad) 
        .addComponent(bQuit)) 
      ); 

     getContentPane().setLayout(layout);  
     getContentPane().setEnabled(true); 
     getContentPane().setSize(1500, 1500); 


    } 
    public void drawChart() 
    { 
     System.setSecurityManager(null);// with out this statement, set_ports() throws null pointer exception 
     spc.set_ports(dULD, dLLD); 
     dataset = SimplePortComm.dataset; 

     xylineChart = ChartFactory.createXYLineChart(
       "Data Aquired" , 
       "Time" , 
       "Amplitude" , 
       dataset , 
       PlotOrientation.VERTICAL , 
       true , true , false); 

     panel = new ChartPanel(xylineChart, 650, 450, 180, 180, 800, 600, false, true, false, false, false, false); 
     //panel.setRefreshBuffer(true); 

     CrosshairOverlay crosshairOverlay = new CrosshairOverlay(); 
     this.xCrosshair = new Crosshair(Double.NaN, Color.BLACK, 
       new BasicStroke(0f)); 
     this.xCrosshair.setLabelVisible(true); 
     this.yCrosshair = new Crosshair(Double.NaN, Color.BLACK, 
       new BasicStroke(0f)); 
     this.yCrosshair.setLabelVisible(true); 
     crosshairOverlay.addDomainCrosshair(xCrosshair); 
     crosshairOverlay.addRangeCrosshair(yCrosshair); 
     panel.addOverlay(crosshairOverlay); 

     panel.addChartMouseListener(new ChartMouseListener() {   
      @Override 
      public void chartMouseMoved(ChartMouseEvent event) { 
       Rectangle2D dataArea = panel.getScreenDataArea(); 
       JFreeChart chart = event.getChart(); 
       XYPlot plot = (XYPlot) chart.getPlot(); 
       ValueAxis xAxis = plot.getDomainAxis(); 
       double x = xAxis.java2DToValue(event.getTrigger().getX(), dataArea, 
         RectangleEdge.BOTTOM); 
       // make the crosshairs disappear if the mouse is out of range 
       if (!xAxis.getRange().contains(x)) { 
        x = Double.NaN;     
       } 
       double y = DatasetUtilities.findYValue(plot.getDataset(), 0, x); 
       xCrosshair.setValue(x); 
       yCrosshair.setValue(y);   
      }   
      @Override 
      public void chartMouseClicked(ChartMouseEvent event) { 
       Rectangle2D dataArea = panel.getScreenDataArea(); 
       System.out.println("Mouse clicked on the graph"); 
       XYPlot plot = (XYPlot) xylineChart.getPlot(); 

       ValueAxis xAxis = plot.getDomainAxis(); 
       double x = xAxis.java2DToValue(event.getTrigger().getX(), dataArea, 
         RectangleEdge.BOTTOM); 
       // make the crosshairs disappear if the mouse is out of range 
       if (!xAxis.getRange().contains(x)) { 
        x = Double.NaN;     
       } 
       double y = DatasetUtilities.findYValue(plot.getDataset(), 0, x); 

       System.out.println("Graph click: " + x + ":" + y); 

       xCrosshair.setValue(x); 
       yCrosshair.setValue(y); 

       XYItemRenderer renderer = plot.getRenderer(); 
       renderer.removeAnnotations(); 
       XYLineAnnotation mark = new XYLineAnnotation(x,0,x,250, new BasicStroke(), Color.BLACK); 
       renderer.addAnnotation(mark); 

       XYLineAnnotation low = new XYLineAnnotation(0.0f,dULD,400f,dULD, new BasicStroke(), Color.BLUE); 
       renderer.addAnnotation(low); 
       low.setToolTipText("low"); 
       XYLineAnnotation high = new XYLineAnnotation(0.0f,dLLD,400f,dLLD, new BasicStroke(), Color.MAGENTA); 
       renderer.addAnnotation(high); 
       high.setToolTipText("high"); 
       plot.setRenderer(renderer); 
      } 
     }); 

    } 

    public void init() 
    { 
     setBackground(Color.WHITE); 

     System.setSecurityManager(null); 
     spc = new SimplePortComm();    
    } 

    public static void main(String args[]) 
    {  
     DisplayPanel ds = new DisplayPanel(); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     // TODO Auto-generated method stub 

    } 

} 

回答

1

我不知道該怎麼SimplePortComm的作品,但你ActionListener正在取代用於創建圖表的dataset。它首次運作,因爲原始數據集是xylineChart正在收聽的數據集;圖表不知道新的dataset。您可以通過使用plot.setDataset(dataset);

ActionListener也擋住了event dispatch thread,所以你可能需要使用SwingWorker像他們表現出here講述的變化圖表。

+0

感謝噸卡塔利娜,我宣佈情節爲全局變量,並稱爲plot.setDataset(數據集);在ActionListener中。它的工作就像一個魅力。 –

+0

不客氣。這聽起來像延遲並不重要。當你準備好時,你可以[接受](http://meta.stackexchange.com/a/5235)這個答案。 –