Java JFree chart实时图表,将域标签从毫秒转换为HH:MM:SS。

huangapple go评论74阅读模式
英文:

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

问题

我使用JFreeChart创建了一个实时图表其中域轴表示纪元毫秒我希望标签显示`HH:MM:SS`。

以下是我用于加载图表数据的代码块我对Java非常陌生非常感谢任何建议

Thread thread = new Thread(){
     public void run() {
         try (Scanner scanner = new Scanner(chosenPort.getInputStream())) {  	// 从串行端口读取数据
             int x = 0;															// 设置数据
             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);										// 将数据添加到图表
                     p1.repaint();
                 }catch(Exception e) {}
             }
         }
     }
};
英文:

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

答案1

得分: 1

我之前使用的是 XY 系列折线图,而不是时间序列图。通过使用 JFreeChart chart = ChartFactory.createTimeSeriesChart 代替 JFreeChart chart = ChartFactory.createXYLineChart,正确的日期/时间值被解释并自动显示出来。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月30日 01:23:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64124624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定