压缩实时图的水平线

时间:2011-02-02 19:08:37

标签: java graph jfreechart

我想压缩并存储我尝试但未成功的实时线图数据

public class DTest extends ApplicationFrame {

    javax.swing.Timer _timer;
    int nPoints = 200;
    float[] history;
    /** The most recent value added. */
    private float lastValue = (float) 100.0;
    DynamicTimeSeriesCollection dataset;
    JPanel content;
    private final ChartPanel chartPanel;

    public DTest(final String title) {

        super(title);
        history = new float[nPoints];
        dataset = new DynamicTimeSeriesCollection(
                1, nPoints, new Second()//here speed will set
                );

        dataset.setTimeBase(new Second(0,0,0,1,1,2000));
        dataset.addSeries(new float[]{0.0f}, 0, "S1");
        System.out.println("Series count = " + dataset.getSeriesCount());
        final JFreeChart chart = createChart(dataset);
        chartPanel = new ChartPanel(chart);
        content = new JPanel(new FlowLayout());

        final JButton btn = new JButton("Stop");
        btn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                _timer.stop();
            }
        });
        final JButton btn1 = new JButton("Run");
        btn1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // create new dataset and chart, set the new chart in the chartpanel


                //createChart(dataset);

                _timer.start();
            }
        });
        JComboBox comb = new JComboBox();
        comb.addItem("Select");
        comb.addItem("Joy Stick");

        content.add(chartPanel);//panel for chart
        JPanel btnPanel = new JPanel(new FlowLayout());
        btnPanel.add(btn);
        btnPanel.add(btn1);
        btnPanel.add(comb);

        Container pane = getContentPane();
        pane.setLayout(new BorderLayout());
        pane.add(content, BorderLayout.NORTH);
        pane.add(btnPanel, BorderLayout.CENTER);


        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        //setContentPane(content);
        comb.addActionListener(new ActionListener() {

            private float[] float_array;
            private int itemCount;

            public void actionPerformed(ActionEvent e) {
                JComboBox jComb = (JComboBox) e.getSource();
                if (jComb.getSelectedItem().equals("Joy Stick")) {
                    System.out.println("Joy Stick is Pressed");
                    try {
                        float_array = new float[1];
                        float_array[0] = 0;
                        itemCount = 0;
                        dataset.appendData(float_array);
                        dataset.addSeries(new float[]{0.0f}, 0, "S1");
                        _timer = new javax.swing.Timer(1, new ActionListener() { // 500ms

                            private int resizes;
                            private int inserted;
                            public void actionPerformed(ActionEvent e) {

                                double factor = 0.90 + 0.2 * Math.random();
                                lastValue = lastValue * (float) factor;
                                float_array[0] = lastValue;
                                System.out.println("lastValue is " + lastValue);

                                inserted++;
                                if ( inserted % (resizes+1)==0 )
                                    dataset.appendData(float_array, itemCount++, 1);
                                history[itemCount] = lastValue;
                                if (itemCount >= nPoints - 1) {
                                    resizes++;
                                    DynamicTimeSeriesCollection newSet = new DynamicTimeSeriesCollection(1, nPoints, new Second());
                                    newSet.setTimeBase(new Second(0,0,0,2,2,2000));
                                    newSet.addSeries(new float[]{0.0f}, 0, "S1");
                                    itemCount /= 2;
                                    for (int i = 1; i < nPoints; i++) {
                                        history[i / 2] = history[i];
                                        float_array[0]=history[i / 2];
                                        newSet.appendData(float_array, i/2, 1);
                                        history[i] = 0;
                                    }
                                    chartPanel.setChart(createChart(newSet));
                                    dataset = newSet;
                                    chartPanel.repaint();
                                }
                            }
                        });

                        _timer.setRepeats(true);
                        _timer.start();

                    } catch (NullPointerException ne) {
                        System.out.println("NullPointer Exception" + ne.toString());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                } else {  ;

                }

            }
        });
    }

    private JFreeChart createChart(final XYDataset dataset) {
        final JFreeChart result = ChartFactory.createTimeSeriesChart(
                "Dynamic Graph", "Time", "Value", dataset, true, true,
                false);
        final XYPlot plot = result.getXYPlot();
        ValueAxis axis = plot.getDomainAxis();
        //plot.setRangeAxis(WIDTH, axi)
        axis.setAutoRange(true);
        //axis.setFixedAutoRange(60.0); // 60 seconds
        axis = plot.getRangeAxis();
        axis.setRange(-100.0, 200.0);
        return result;
    }

    public static void main(final String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    final DTest demo = new DTest("Dynamic Graph");
                    demo.pack();
                    RefineryUtilities.centerFrameOnScreen(demo);

                    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                    demo.setVisible(true);

                } catch (Exception e) {
                }
            }
        });


    }
}

1 个答案:

答案 0 :(得分:0)

  

...当线向前移动时,前一个线值不应该消失,但它应该开始压缩自己。

demoMemory Usage标签完全符合您的描述。