动态JFreeChart TimeSeries [英] Dynamic JFreeChart TimeSeries

查看:85
本文介绍了动态JFreeChart TimeSeries的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此示例,我是

Based on this example, I am now able to collect data and display it in a chart. I don't know exactly how to integrate that code that generates the chart into my application.

/** @see http://stackoverflow.com/questions/5048852 */
public class Atol extends ApplicationFrame {

private static final String TITLE = "Dynamic Series";
private static final String START = "Start";
private static final String STOP = "Stop";
private static final float MINMAX = 100;
private static final int COUNT = 2 * 60;
private static final int FAST = 100;
private static final int SLOW = FAST * 5;
private static final Random random = new Random();
private Timer timer;

public Atol(final String title) {
    super(title);
    final DynamicTimeSeriesCollection dataset
            = new DynamicTimeSeriesCollection(1, COUNT, new Second());
    dataset.setTimeBase(new Second(0, 0, 0, 1, 1, 2011));
    dataset.addSeries(gaussianData(), 0, "Gaussian data");
    JFreeChart chart = createChart(dataset);

    final JButton run = new JButton(STOP);
    run.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();
            if (STOP.equals(cmd)) {
                timer.stop();
                run.setText(START);
            } else {
                timer.start();
                run.setText(STOP);
            }
        }
    });

    final JComboBox combo = new JComboBox();
    combo.addItem("Fast");
    combo.addItem("Slow");
    combo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if ("Fast".equals(combo.getSelectedItem())) {
                timer.setDelay(FAST);
            } else {
                timer.setDelay(SLOW);
            }
        }
    });
   this.add(new ChartPanel(chart), BorderLayout.CENTER);
   JPanel btnPanel = new JPanel(new FlowLayout());
   btnPanel.add(run);
    btnPanel.add(combo);
    this.add(btnPanel, BorderLayout.SOUTH);
    SystemInfo si = new SystemInfo();             //Criando uma nova classe de infos do Sistem
    HardwareAbstractionLayer hal = si.getHardware(); //Infos de Hardware do sistema
    CentralProcessor cpu = hal.getProcessor();      //E as informações da cpu
    long[] oldTricks = cpu.getSystemCpuLoadTicks();

    
    timer = new Timer(FAST, new ActionListener() {
        float cpu() {

            Double stats = cpu.getSystemCpuLoadBetweenTicks(oldTricks);
            //Convertendo o valor de uso da CPU
            stats = stats * 100d;
            double teste = Math.round(stats * 100.0) / 100.0;
            double d = teste;
            float f = (float) d;
            System.out.println(f);
            return f;
        }
        float[] newData = new float[1];

        @Override
        public void actionPerformed(ActionEvent e) {

            newData[0] = cpu();
            dataset.advanceTime();
            dataset.appendData(newData);
        }
    });
}


private float[] gaussianData() {

    float[] a = new float[COUNT];
    for (int i = 0; i < a.length; i++) {
        a[i] = 2;
    }
    return a;
}

private JFreeChart createChart(final XYDataset dataset) {
    final JFreeChart result = ChartFactory.createTimeSeriesChart(
            TITLE, "hh:mm:ss", "milliVolts", dataset, true, true, false);
    final XYPlot plot = result.getXYPlot();
    ValueAxis domain = plot.getDomainAxis();
    domain.setAutoRange(true);
    ValueAxis range = plot.getRangeAxis();
    range.setRange(-MINMAX, MINMAX);
    return result;
}

public void start() {
    timer.start();
}

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

        @Override
        public void run() {
           Atol demo = new Atol(TITLE);
            demo.pack();

            demo.setVisible(true);
            demo.start();
        }
    });
}

}

到我的JFrame代码(在另一个类中):

To my JFrame code (which is in another class):

   private void kButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // when I click in that button
    
    Atol demo = new Atol ("");       
   chartCPU.add(new ChartPanel(demo)); //Thats the JPanel the chart need to appear and readjust to it size

}

如果JFreeChart在一个构造函数中,如何在Atol类中调用该图表(JFreeChart图表)?我没得到! 如何使其显示在我的chartCPU JPanel中? (这是一个称为TelaLoginJFrame类,与Atol相同).

How can I call the chart (JFreeChart chart) in Atol class if the JFreeChart is in one constructor? I didn't get! How can I make it appear in my chartCPU JPanel? (which is one JFrame class called TelaLogin, not the same as the Atol one).

推荐答案

您的原始问题询问如何捕获CPU数据并显示在图表中由于相关方法的等待时间短,因此可以在javax.swing.Timer的动作侦听器中调用该方法,如此处所示.及以上.

Your original question asked how to capture CPU data and display it in a chart. Because the relevant method has low latency, it's possible to invoke the method in the action listener of a javax.swing.Timer as shown here and above.

具有不可预测的延迟的数据访问可能会阻塞事件分派线程,在这种情况下,您应该改用SwingWorker,如此处所示.

Data access with unpredictable latency may block the event dispatch thread, in which case you should use a SwingWorker instead, as shown here.

要将图表集成到您的应用程序中,请将示例的构造函数重构为工厂方法,并采用以下方法之一:

To integrate the chart into your application, refactor the example's constructor into a factory method and adopt an approach such as one of these:

  • CardLayout,显示在此处.

JTabbedPane,显示在此处.

JInternalFrame,在此处 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆