自动更新图表时出错

时间:2015-06-17 17:19:58

标签: java arduino

我正在编程一个传感器,它通过以太网端口将数据发送到我的计算机。显示收到的数据但绘制的图表卡住了。

但是,当我尝试使用随机值时,图表会正确显示所有值并自动更新。我的代码如下所示:

package projwellmonitoring;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.DynamicTimeSeriesCollection;
import org.jfree.data.time.Second;




public class DynamicTimeSeriesChart extends JPanel {

private DynamicTimeSeriesCollection dataset;
private JFreeChart chart = null;
Socket smtpSocket = null;  
DataOutputStream os = null;
DataInputStream is = null;


public DynamicTimeSeriesChart(final String title) throws IOException {

    smtpSocket = new  Socket("192.168.1.199",2015);

    dataset = new DynamicTimeSeriesCollection(1, 2000, new Second());
    dataset.setTimeBase(new Second(0, 0, 0, 1, 1, 1990)); // date 1st jan 0 mins 0 secs

    dataset.addSeries(new float[1], 0, title);
    chart = ChartFactory.createTimeSeriesChart(
        title, "Time", title, dataset, true,
        true, false);
    final XYPlot plot = chart.getXYPlot();

    ValueAxis axis = plot.getDomainAxis();
    axis.setAutoRange(true);
    axis.setFixedAutoRange(200000); // proportional to scroll speed
    axis = plot.getRangeAxis();

    final ChartPanel chartPanel = new ChartPanel(chart);
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    add(chartPanel);
}

public void update() throws IOException {
        os = new DataOutputStream(smtpSocket.getOutputStream());
        is = new DataInputStream(smtpSocket.getInputStream());

            if (smtpSocket!= null  && is != null) {
                try {
                    os.writeBytes("\n");
                    String responseLine;
                    System.out.println("DAQ - reading sensor informations");
                    while (true) {
                        responseLine = is.readLine();
                           float[] newData = new float[1];
                             newData[0] =Float.parseFloat(responseLine);
                          dataset.advanceTime();
                         dataset.appendData(newData);
                        System.out.println("Server: " +  newData[0]);
                    }} catch (IOException ex) {
                        Logger.getLogger(readSensor.class.getName()).log(Level.SEVERE, null, ex);
                    }
            }
}


public static void main(String[] args) throws IOException {

    JFrame frame = new JFrame("testing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    final DynamicTimeSeriesChart chart = new DynamicTimeSeriesChart("PIT LEVEL");
    frame.add(chart);
    frame.pack();
    frame.setVisible(true);
    Timer timer;
    timer = new Timer(1000, new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        chart.update();
                    } catch (IOException ex) {
                        Logger.getLogger(DynamicTimeSeriesChart.class.getName()).log(Level.SEVERE, null, ex);
                    }

                }
            });
        }
    });
    timer.start();
}

}

0 个答案:

没有答案