temp1.RealTimeChart.java Source code

Java tutorial

Introduction

Here is the source code for temp1.RealTimeChart.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package temp1;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

public class RealTimeChart extends ChartPanel implements Runnable {
    private static TimeSeries timeseries1;
    private static TimeSeries timeseries2;
    private static TimeSeries timeseries3;
    private static TimeSeries timeseries4;
    private static double lastValue1, lastValue2, lastValue3, lastValue4;
    private static Thread thread1;
    public static boolean pd = true, c1 = true, c2 = true, c3 = true, c4 = true;
    public static int time1;
    public static int l1[][] = new int[10000][4];
    public static int k1[] = new int[10000];
    public static int k2[] = new int[10000];
    public static int k3[] = new int[10000];
    public static int k4[] = new int[10000];
    public static int length1, eve1, eve2, eve3, eve4;
    public static boolean suspend = false;
    public int i = 0, j = 0;
    public static JFreeChart jfreechart;

    static void chan5() {
        suspend = !suspend;
    }

    public RealTimeChart(String chartContent, String title, String yaxisName) {
        super(createChart(chartContent, title, yaxisName));
    }

    //?JFREECHART?   
    public static JFreeChart createChart(String chartContent, String title, String yaxisName) {
        thread1 = new Thread();
        timeseries1 = new TimeSeries(chartContent, Millisecond.class);
        timeseries2 = new TimeSeries(chartContent, Millisecond.class);
        timeseries3 = new TimeSeries(chartContent, Millisecond.class);
        timeseries4 = new TimeSeries(chartContent, Millisecond.class);

        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(timeseries1);
        TimeSeriesCollection timeseriescollection1 = new TimeSeriesCollection(timeseries2);
        TimeSeriesCollection timeseriescollection2 = new TimeSeriesCollection(timeseries3);
        TimeSeriesCollection timeseriescollection3 = new TimeSeriesCollection(timeseries4);

        jfreechart = ChartFactory.createTimeSeriesChart("", "", "", timeseriescollection, false, false, false);

        final XYPlot xyplot = jfreechart.getXYPlot();
        xyplot.setOutlinePaint(Color.magenta);
        xyplot.setBackgroundPaint(Color.lightGray);
        xyplot.setRangeGridlinePaint(Color.gray);
        xyplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
        ValueAxis valueaxis = xyplot.getDomainAxis();
        valueaxis.setAutoRange(true);
        valueaxis.setFixedAutoRange(20000D);
        //Value
        valueaxis = xyplot.getRangeAxis();
        valueaxis.setRange(800D, 3300D);
        xyplot.setDataset(0, timeseriescollection);
        xyplot.setDataset(1, timeseriescollection1);
        xyplot.setDataset(2, timeseriescollection2);
        xyplot.setDataset(3, timeseriescollection3);
        XYLineAndShapeRenderer xylineandshaperenderer0 = new XYLineAndShapeRenderer();
        XYLineAndShapeRenderer xylineandshaperenderer1 = new XYLineAndShapeRenderer();
        XYLineAndShapeRenderer xylineandshaperenderer2 = new XYLineAndShapeRenderer();
        XYLineAndShapeRenderer xylineandshaperenderer3 = new XYLineAndShapeRenderer();
        xylineandshaperenderer0.setBaseShapesVisible(false);
        xylineandshaperenderer1.setBaseShapesVisible(false);
        xylineandshaperenderer2.setBaseShapesVisible(false);
        xylineandshaperenderer3.setBaseShapesVisible(false);
        xylineandshaperenderer0.setSeriesPaint(0, Color.RED);
        xylineandshaperenderer1.setSeriesPaint(0, Color.cyan);
        xylineandshaperenderer2.setSeriesPaint(0, Color.yellow);
        xylineandshaperenderer3.setSeriesPaint(0, Color.blue);
        xyplot.setRenderer(0, xylineandshaperenderer0);
        xyplot.setRenderer(1, xylineandshaperenderer1);
        xyplot.setRenderer(2, xylineandshaperenderer2);
        xyplot.setRenderer(3, xylineandshaperenderer3);
        //xylineandshaperenderer.setSeriesStroke(0, new BasicStroke(0.5F, 1, 1, 5F, new float[] { 5F, 10F }, 0.0F));
        return jfreechart;
    }

    public static JFreeChart getcha() {
        return jfreechart;
    }

    public static void startThread() {
        thread1.start();
    }

    public static boolean getpd() {
        return pd;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("EXIT")) {
            thread1.interrupt();
            System.exit(0);
        }
    }

    private static final String control = "";

    public static void setpdt() {
        if (pd == false) {
            synchronized (control) {
                control.notifyAll();
                pd = true;
            }
        }
    }

    public static void setpdf() {
        if (pd) {
            synchronized (control) {
                control.notifyAll();
                pd = false;
            }
        }
    }

    //?
    @Override
    public void run() {
        try {
            for (i = 0; i <= length1; i++) {
                time1 = JFrame1.getjs();
                c1 = JFrame1.getc1();
                c2 = JFrame1.getc2();
                c3 = JFrame1.getc3();
                c4 = JFrame1.getc4();
                if (pd) {
                    lastValue1 = k1[i];
                    lastValue2 = k2[i];
                    lastValue3 = k3[i];
                    lastValue4 = k4[i];

                    if (i > 1 && i < length1 - 5)
                        JFrame1.comp(k1, k2, k3, k4, length1, i, eve1, eve2, eve3, jfreechart);
                    if (c1)
                        timeseries1.addOrUpdate(new Millisecond(), lastValue1);
                    if (c2)
                        timeseries2.addOrUpdate(new Millisecond(), lastValue2);
                    if (c3)
                        timeseries3.addOrUpdate(new Millisecond(), lastValue3);
                    if (c4)
                        timeseries4.addOrUpdate(new Millisecond(), lastValue4);
                    if (pd == false) {
                        i = 0;
                        thread1.interrupt();
                        System.exit(0);
                    }
                    if (i == (length1 - 1)) {
                        thread1.interrupt();
                        pd = false;
                        break;
                    }
                    Thread.sleep(time1);
                }
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(RealTimeChart.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //?
    public static int[][] readTxtFile(String filePath) {
        InputStream in = null;
        byte[] tempByte = new byte[1];
        int byteread = 0;
        int data2 = 0;
        int i = 0, j = 0, k = 0;
        try {
            in = new FileInputStream(filePath);
            while ((byteread = in.read(tempByte)) != -1) {
                int data1 = tempByte[0] & 0xff;
                if (data1 != 32 && data1 != 10 && data1 != 13 && data1 != 9) {
                    data1 = data1 - 48;
                    data2 = data2 * 10 + data1;
                } else {
                    if (data2 != 0) {
                        l1[i][j] = data2;
                        j++;
                        if (j == 4) {
                            j = 0;
                            i++;
                        }
                        length1 = i - 1;
                    }

                    data2 = 0;
                }
            }
            for (i = 0; i <= length1; i++) {
                for (j = 0; j < 4; j++) {
                    if (j == 0)
                        k1[k] = l1[i][j];
                    if (j == 1)
                        k2[k] = l1[i][j];
                    if (j == 2)
                        k3[k] = l1[i][j];
                    if (j == 3)
                        k4[k] = l1[i][j];
                }
                k++;
            }
            int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;
            for (j = 0; j < 5; j++) {
                sum1 = sum1 + k1[j];
                sum2 = sum2 + k2[j];
                sum3 = sum3 + k3[j];
                sum4 = sum4 + k4[j];
            }
            eve1 = sum1 / 5;
            eve2 = sum2 / 5;
            eve3 = sum3 / 5;
            eve4 = sum4 / 5;
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
        return l1;
    }
}