英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论