java - JFreeChart:周期为 n 毫秒的 DynamicTimeSeries

标签 java jfreechart

我正在尝试定义一个接口(interface),我想在其中绘制外部设备接收到的一些值。 这些值的接收频率可以通过接口(interface)设置。当然,绘图的周期应该根据用户定义的周期而改变。 所以我开始定义以下图表:

int periodMs = 200;
MilliDTSC dataset = new MilliDTSC(1,100, new MultipleOfMillisecond(periodMs));
dataset.setTimeBase(new MultipleOfMillisecond(periodMs))
dataset.addSeries(zeroSeries()),0,"Zero data") // zeroSeries returs a series with values set to 0
JFreeChart chart = createChart(dataset) // create the chart and set ranges and legends
ChartPanel panel = new ChartPanel(panel);

MilliDTSC 是以下类,如建议的 here :

public class MilliDTSC extends DynamicTimeSeriesCollection{
  public MilliDTSC(int nSeries, int nMoments, RegularTimePeriod timeSample){
    super(nSeries, nMoments, timeSample);
    if(timeSample instanceof Millisecond)
      this.pointsInTime = new Millisecond[nMoments]
    else if (timeSample instanceof MultipleOfMillisecond)
      this.pointsInTime = new MultipleOfMillisecond[nMoments]
  }
}

MultipleOfMillisecond 是以下类:

public class MultipleOfMilliseconds extends Millisecond{
  MulitpleOfMilliseconds(int periodMs){
    this.periodMs = periodMs
  }

  public RegularTimePeriod previous(){
    RegularTimePeriod result = null;
    if(getMillisecond() - periodMs >= FIRST_MILLISECOND_IN_SECOND)
      result = new Millisecond((int)getMillisecond - periodMs, getSecond());
    else{
      Second previous = (Second)getSecond().previous();
      if(previous!=null)
        result = new Millisecond((int)(getMillisecond() - periodMS + LAST_MILLISECOND_IN_SECOND + 1), previous);
    }
    return result;
  }
  // similar for next()
}

我通过以下方式将示例添加到该系列中:

dataset.advanceTime();
dataset.appendData(newData);

我期望的是,一旦我将周期固定为 200 毫秒,图表就会在 X 标签上报告或多或少 5 个时间值:

00:00:00.000 00:00:05.000 00:00:10.000 00:00:15.000 00:00:20.000

我预计每个“空间”中有 25 个样本。

实际情况是,每个“空间”有 25 个样本,但图表在 X 标签上报告了以下值:

00:00:00.000 00:00:00.025 00:00:00.050 00:00:00.075 00:00:00.100

看起来周期是 1 毫秒,但我添加的样本非常多 200 毫秒。

如何解决这个问题? 如果我不清楚,请告诉我。 谢谢!!

最佳答案

我想因为你有这个:

public class MultipleOfMilliseconds extends Millisecond
//                                          ^^^^^^^^^^^

这是真实:

if(timeSample instanceof Millisecond)

如果您更改测试的顺序,您可能会有更好的运气:

if(timeSample instanceof MultipleOfMillisecond)
  this.pointsInTime = new MultipleOfMillisecond[nMoments];
else if (timeSample instanceof Millisecond)
  this.pointsInTime = new Millisecond[nMoments];

关于java - JFreeChart:周期为 n 毫秒的 DynamicTimeSeries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6856979/

相关文章:

java - JFreeChart - 自定义 RingChart

java - 在运行时更改java类

java - 如何从 Java 运行脚本

java - 在 Hibernate 上更新分离对象的某些字段的最佳方法?

java - 使用JLine在一行完成多条命令

java - 自定义具有多个系列、轴和形状的图表

java - Jfreechart:如何获取 BoxAndWhiskerChart 中的各个项目

java - 使用集合缓存数据

Java生成原理图/图表,运行时编辑和打印选项

java - JFreeChart 中的基本直方图