Modifying Date Axis

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
riggy
Posts: 1
Joined: Tue Jan 05, 2021 6:14 am
antibot: No, of course not.

Modifying Date Axis

Post by riggy » Tue Jan 05, 2021 6:23 am

I have a chart using a DynamicTimeSeries which plots dummy 'real-time' data every 5 seconds. This data represents the position of some vehicle between two points (0 and 100). Every 5 seconds this position is incremented to simulate its movement betwixt the points. I'd like to this data to be shown over the span of some amount of hours, such that you can see the charts data points being plotted over some course of time. I have an issue where when I try to set my DateAxis to be in hours I end up with no tick units on my x-axis and the data appears to move off of the screen instead of following it (the plot slowly disappears). I try to set the DateAxis because if I don't then it is shown in seconds which is not desired.

Code: Select all

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 float MINMAX = 100;
    private static final int COUNT = 10;
    private static final int FAST = 1000;
    private static final int SLOW = FAST * 5;
    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));
        //dataset.addSeries(gaussianData(), 0, "Gaussian data");
        float[] gateStartLoad = new float[1];
        gateStartLoad[0] = (float)gateStart;
        dataset.addSeries(gateStartLoad, 0, "Longwall Data");
        JFreeChart chart = createChart(dataset);
        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(combo);
        this.add(btnPanel, BorderLayout.SOUTH);

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

    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.HOUR, 1));
        

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

Locked