Search code examples
javajfreechart

Java JFree chart realtime chart convert domain label from millis to HH:MM:SS


I created a real time chart with JFreechart where the Domain axis is epoch millis. I would like the labels to display HH:MM:SS.

Here is the block of code that I use to load the chart with data. I am very new to Java and any suggestions are very much appreciated.

Thread thread = new Thread(){
     public void run() {
         try (Scanner scanner = new Scanner(chosenPort.getInputStream())) {     // Read Data from Serial Port
             int x = 0;                                                         // Set data
             while(scanner.hasNextLine()) {
                 long epoch = System.currentTimeMillis();
                 chart.getXYPlot().getDomainAxis().setRange(epoch - 30000.00, epoch + 1000.00);
                 try{
                     String line = scanner.nextLine();
                     int number = Integer.parseInt(line); //
                     series.add(epoch,number);                                      // add Data to Chart
                     p1.repaint();
                 }catch(Exception e) {}
             }
         }
     }
};

Solution

  • I was using an XYseries Line chart instead of a time series chart. By using JFreeChart chart = ChartFactory.createTimeSeriesChart instead of JFreeChart chart = ChartFactory.createXYLineChart the correct date/time values were interpreted and displayed automatically.