当前位置: 首页 > 知识库问答 >
问题:

JFreeChart在从串口读取数据后没有更新ChartPanel

阎建德
2023-03-14

我试图从串口读取一些数据,并使用JFreeChart和ChartPanel显示相同的数据。在我单击按钮'Aquire'后,我从串口获得新的数据集。第一次显示面板时,显示从串行端口读取的值。在我单击按钮'Aquire'后,这些值将被填充到变量'DataSet'中,但它们不会在图中刷新。我哪里出错了?请帮忙。提前谢了。

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个答案

周作人
2023-03-14

我不知道SimplePortComm是如何工作的,但是您的ActionListener正在替换用于创建图表的DataSet。它第一次工作是因为原始数据集是XylineChart正在监听的数据集;图表不知道新的数据集。您可以使用plot.setDataSet;告诉图表有关更改

您的ActionListener也阻塞了事件分派线程,因此您可能希望使用SwingWorker,如图所示。

 类似资料:
  • 这是http://www.gravitech.us/7segmentshield.html给出的代码。 那么怎样才能逐行阅读呢?

  • 问题内容: 我有一个Java程序,必须读取Arduino发送的信息。我从这里获取了Java代码。现在,我不太了解它是如何工作的,但是我尝试对其进行修改,并且得到了以下信息: 我创建一个对象串行COM口,我需要在主程序,然后我使用和当我需要它。 效果很好,Arduino获取数据并将其显示在LCD显示屏中。问题是。程序运行时,它会不断从串行端口读取数据(大约每40毫秒一次),但这并不意味着Arduin

  • 我的jfreeChart来自网络摄像头的帧。我通过这个框架,它生成一个图形的R,G和B颜色的这个框架。我想这个图表做自动更新,因为我通过帧。我什么都试过了,但都没用。 }

  • 各位早上好,, 我刚完成React的培训,我想做一个项目来练习。首先,我只是在firebase数据库中发布数据。然后,我想使用钩子(useState和useEffect)获取数据并将其显示在react组件中。 问题是,在使用axios获取数据后,我最终陷入了经典的无限循环陷阱,但我不知道如何使用依赖性来阻止它。 请查找以下代码: 当我使用控制台日志类型时,数据是正确的。但当我控制台记录fetche

  • 问题内容: 我们正在使用Oracle作为Web应用程序的数据库。该应用程序在大多数情况下都运行良好,但是出现了“不再需要从套接字读取数据”错误。 我们使用spring,hibernate,并且在我的应用程序上下文文件中有以下数据源。 我不确定这是由于应用程序错误,数据库错误还是网络错误引起的。 我们在oracle日志上看到以下内容 Oracle版本:11.2.0.1.0 问题答案: 对于此类错误,

  • 我有一个Velleman k8090中继板,我试图从中读取一些数据。我可以很好地写入电路板,但每当我输出读取的数据时,我都会得到奇怪的字符,比如菱形或倒置的问号。以下是我的部分代码: 为什么我会有这些奇怪的角色?文件规定它应该返回年份和星期。 编辑:经过一些搜索和故障排除,我现在非常确定它与Python代码或数据类型有关,而不是与硬件有关。这个论坛包含了一个C语言的例子,它从黑板上读取。我从黑板上