0

I have a Jfreechart which is plotting some mock real-time data. When I have my domain axis set to auto, the data can be seen updating every second. However, I wish to plot the data over a wider range (say the whole day). When I change the range, I am then unable to see the plot unless I zoom in.

Once zoomed in, the line does not cover the whole graph, but only a portion. This line then moves across the graph instead of growing/drawing along it Moving line

/** @see http://stackoverflow.com/questions/5048852 */
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));
        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();
                System.out.println(dataset.getNewestTime());
                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();
        DateAxis domain = (DateAxis)plot.getDomainAxis();
        Calendar calendar = Calendar.getInstance();
        calendar.set(2021, 0, 6);
        System.out.println(new Date());
        System.out.println(calendar.getTime());
        domain.setRange(new Date(), calendar.getTime());
        domain.setDateFormatOverride(new SimpleDateFormat("HH:mm:ss"));

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

How do I make it so that the line is continuous (shows every observed point of data in the series), and how do I make it visible when I manually set the range

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Rigg97
  • 83
  • 1
  • 13
  • Have you tried one of the approaches suggested [here](https://stackoverflow.com/a/31932309/230513) and cited [here](https://stackoverflow.com/q/5048852/230513)? – trashgod Jan 05 '21 at 14:29
  • @trashgod I don't think the first approach is of any use to me, I create the dataset on the current date. As for the second approach I used advanceTime() and then appendData(). My solution works until I set the date range then the plot is no longer visible and it is not continuous – Rigg97 Jan 05 '21 at 22:28

1 Answers1

0

I removed domain.setRange(new Date(), calendar.getTime()); and changed nMoments in my DynamicTimeSeriesCollection(1, COUNT, new Second());, making COUNT a multiple of 60 and increasing its value. My x-axis now properly shows time however the plot is not continuous, it disappears after a time

Rigg97
  • 83
  • 1
  • 13