jfreechart与时间序列

y1aodyip  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(339)

我正在创建一个jfreechart,我想每5秒左右提供一次“实时”虚拟数据。此数据表示车辆相对于两端的某个位置(介于100和0之间)。我想这个位置数据显示与y轴表示当前一天的小时数和x轴显示其位置。当前位置数据工作正常,以5秒的间隔添加,但是x轴(时间)以秒而不是小时显示。
编辑:使用时x轴没有范围或记号 ((DateAxis)(domain)).setTickUnit(new DateTickUnit(DateTickUnitType.DAY, 1)); ,只是空白

public class DTSCTest extends ApplicationFrame {

    private static final String TITLE = "Dynamic Series";
    private static final String START = "Start";
    private static final String STOP = "Stop";
    private static final int COUNT = 10;
    private static final int FAST = 5000;
    private static final Random random = new Random();
    private double gateStart = ThreadLocalRandom.current().nextInt(0, 101);
    private boolean returning = false;
    private Timer timer;

    public DTSCTest(final String title) {
        super(title);
        final DynamicTimeSeriesCollection dataset =
                new DynamicTimeSeriesCollection(1, COUNT, new Second());
        Date date = new Date();
        dataset.setTimeBase(new Second(date));
        float[] gateStartLoad = new float[1];
        gateStartLoad[0] = (float)gateStart;
        dataset.addSeries(gateStartLoad, 0, "Longwall Data");
        JFreeChart chart = createChart(dataset);
        this.add(new ChartPanel(chart), BorderLayout.CENTER);
        JPanel btnPanel = new JPanel(new FlowLayout());
        this.add(btnPanel, BorderLayout.SOUTH);

我在计时器上添加我的位置值(5000 ms)

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

            float[] newData = new float[1];

            @Override
            public void actionPerformed(ActionEvent e) {
                if(gateStart == 100){
                    returning = true;
                }else if(gateStart == 0){
                    returning = false;
                }
                if(returning){
                    gateStart--;
                }else{
                    gateStart++;
                }
                newData[0] = (float)gateStart;
                dataset.advanceTime();
                dataset.appendData(newData);
            }
        });
    }

然后,我尝试设置刻度单位,使x轴显示一天中的小时数,而不是一天中的每一秒

private JFreeChart createChart(final XYDataset dataset) {
        final JFreeChart result = ChartFactory.createTimeSeriesChart(
                TITLE, "hh:mm:ss", "Shearer Position", dataset, true, true, false);
        final XYPlot plot = result.getXYPlot();
        ValueAxis domain = plot.getDomainAxis();
        domain.setAutoRange(false);
        ((DateAxis)(domain)).setTickUnit(new DateTickUnit(DateTickUnitType.DAY, 1));
//        domain.setAutoRange(true);
        ValueAxis range = plot.getRangeAxis();
        range.setRange(0, 100);
        return result;
    }

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

    public static void main(final String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                DTSCTest demo = new DTSCTest(TITLE);
                demo.pack();
                RefineryUtilities.centerFrameOnScreen(demo);
                demo.setVisible(true);
                demo.start();
            }
        });
    }
}

似乎自己设置刻度单位不会影响y轴。如何使它显示当前一天的小时而不是秒?

irlmq6kh

irlmq6kh1#

使用允许指定 DateFormat ,并使用带有适当格式字符串(包括小时)的dateformat,例如“hh:mm:ss”。

相关问题