JFreeChart 及时推进?

JFreeChart advancing in time?

我正在开发一个应用程序,我需要在图表中显示捕获特定数据的实时时间 它“有效”,只是它跟不上实时,它一直在计数,好像时间已经过去了! 我知道它可能与此 dataset.advanceTime () 相关联,但如果没有它,图形将变为静态并且即使实时过去也不会再前进

package com.mycompany.moveplus;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.Date;
import java.util.TimeZone;
import javax.swing.JInternalFrame;
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.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.DynamicTimeSeriesCollection;
import org.jfree.data.time.Second;
import org.jfree.data.xy.XYDataset;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.HardwareAbstractionLayer;

public class Atol extends JInternalFrame {

    private static final float MINMAX = 100;
    private static final int COUNT = 2 * 60;
    private Timer timer;

    public Atol() {
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
        Date date = new Date();

        final DynamicTimeSeriesCollection dataset
                = new DynamicTimeSeriesCollection(1, 60, new Second());
        dataset.setTimeBase(new Second(date));
        dataset.addSeries(gaussianData(), 0, "Uso de CPU");
        JFreeChart chart = createChart(dataset);

        this.add(new ChartPanel(chart), BorderLayout.CENTER);
        JPanel btnPanel = new JPanel(new FlowLayout());

        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(100, 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);
            }
        });
        timer.start();
    }

    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(
                "", "hh:mm:ss", "CPU%", dataset, true, true, false);
        final XYPlot plot = result.getXYPlot();
        //      DateAxis axis = (DateAxis) plot.getDomainAxis();

        plot.setRangeGridlinePaint(Color.decode("#e8e8e8"));
        plot.setBackgroundPaint(Color.white);
        plot.setOutlinePaint(null);
        plot.setOutlinePaint(null);

        XYItemRenderer renderer = plot.getRenderer();
        renderer.setSeriesPaint(0, Color.decode("#1b6ca8"));

        ValueAxis domain = plot.getDomainAxis();
        domain.setAutoRange(true);
        ValueAxis range = plot.getRangeAxis();
        range.setRange(0, 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();

                demo.pack();

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

如何让它在时间真正流逝时才进步?

我正在重复使用 this 代码

按照建议, a javax.swing.Timer works well when collecting data in a continuous, synchronous manner. In contrast, collecting data in an ongoing, asynchronous manner may block the GUI thread. Switching to SwingWorker, as shown here, offers the chance to publish() only when needed. Ideally, your chosen library可能会提供合适的回调,或者您可以简单地等待新数据到达。

无论哪种情况,数据都会有时间差。你如何处理这个的具体细节将取决于你的用例,但我已经看到了一些常见的策略:

  • 使用可用功能在信号图表中导航整个数据集,如图 , and ; use null values, as suggested here, to interrupt the display if warranted; an example is illustrated

  • 将数据突发分成单独的数据集并添加导航控件,如图所示here and here