2011-02-02 48 views
0

我要压缩和实时线图的存储数据的水平线我试过,但没有成功压缩的实时图形

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) { 
       } 
      } 
     }); 


    } 
} 
+0

添加了jfreechart标签 – 2011-02-02 19:12:47

+2

您是否介意(对于压缩和存储实时线图的数据)进行了非常详细的阐述(相当多)。然后请详细说明“我尝试过但没有成功”。通常问题越好,答案越好。 – 2011-02-02 19:14:21

回答

0

...作为线路前进,前行值应不消失,但它应该开始压缩自己。

demoMemory Usage选项卡完全符合您的描述。