Java JFreeChart:周期为n毫秒的DynamicMiseries

Java JFreeChart:周期为n毫秒的DynamicMiseries,java,jfreechart,Java,Jfreechart,我正在尝试定义一个接口,在这个接口中,我想绘制一些外部设备接收到的值。 这些值以可通过接口设置的频率接收。当然,绘图的周期应根据用户定义的周期进行更改。 因此,我开始定义以下图表: int periodMs = 200; MilliDTSC dataset = new MilliDTSC(1,100, new MultipleOfMillisecond(periodMs)); dataset.setTimeBase(new MultipleOfMillisecond(periodMs)) dat

我正在尝试定义一个接口,在这个接口中,我想绘制一些外部设备接收到的值。 这些值以可通过接口设置的频率接收。当然,绘图的周期应根据用户定义的周期进行更改。 因此,我开始定义以下图表:

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);
milldtsc
是建议的以下类别:

multipleofmillsecond
是以下类别:

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
看起来周期是1ms,但我添加的样本是200ms

我怎样才能解决这个问题? 如果我不清楚,请告诉我。
谢谢

我想因为你有这个:

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];
相反,使用原始的&
毫秒
,调用
advanceTime()
,并在追加新数据之前根据需要追加旧数据。以200毫秒为例,执行以下操作:

float[] newData = new float[1];
float[] oldData = new float[1];

@Override
public void actionPerformed(ActionEvent e) {
    newData[0] = randomValue();
    oldData[0] = newData[0];
    for (int i = 0; i < 200; i++) {
        dataset.advanceTime();
        dataset.appendData(oldData);
    }
    dataset.appendData(newData);
}
float[]newData=newfloat[1];
浮动[]旧数据=新浮动[1];
@凌驾
已执行的公共无效操作(操作事件e){
newData[0]=randomValue();
oldData[0]=新数据[0];
对于(int i=0;i<200;i++){
dataset.advanceTime();
dataset.appendData(旧数据);
}
dataset.appendData(新数据);
}
请注意,现在每秒有5个样本,间隔200ms


这是我实施的解决方案。 我只报告了我更改的方法。这是一个愚蠢的错误:D

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

public class MultipleOfMillisecond extends Millisecond {

  private static final long serialVersionUID = 1L;
  private int periodMs = 100;

  public MultipleOfMillisecond(int periodMs){
    super();
    this.periodMs = periodMs;
  }

  public MultipleOfMillisecond(int periodMs, int millisecond, Second second){
    super(millisecond, second);
    this.periodMs = periodMs;
  }

  @Override
  public RegularTimePeriod next() {
    
    RegularTimePeriod result = null;
    if(getMillisecond() + periodMs <= LAST_MILLISECOND_IN_SECOND){
        result = new MultipleOfMillisecond( periodMs, (int)(getMillisecond() + periodMs), getSecond());
    }else{
        Second next = (Second)getSecond().next();
        if(next != null){
            result = new MultipleOfMillisecond(periodMs, (int)(getMillisecond() + periodMs - LAST_MILLISECOND_IN_SECOND - 1), next);
        }
    }
    return result;
    
  }

  @Override
  public RegularTimePeriod previous() {
    
    RegularTimePeriod result = null;
    if(getMillisecond() - periodMs >= FIRST_MILLISECOND_IN_SECOND){
        result = new MultipleOfMillisecond(periodMs, (int)getMillisecond() - periodMs, getSecond());
    }else{
        Second previous = (Second)getSecond().previous();
        if(previous != null){
            result = new MultipleOfMillisecond(periodMs, (int)(getMillisecond() - periodMs + LAST_MILLISECOND_IN_SECOND + 1), previous);
        }
    }
    return result;
    
  } 
}
public millitdtsc(int n系列、int n组件、RegularTimePeriod时间样本){
超级(N系列、N组件、时间样本);
if(多个采样的时间实例毫秒){
this.pointsInTime=新的MultipleOfMillisecond[nmoots];
}else if(毫秒的时间采样实例){
this.pointsInTime=新的毫秒[N分钟];
} 
}
公共类MultipleOfMillisecond扩展毫秒{
私有静态最终长serialVersionUID=1L;
私有整数周期ms=100;
公共倍数毫秒(整数周期毫秒){
超级();
this.periodMs=periodMs;
}
公共多毫秒(整数周期毫秒、整数毫秒、秒){
超级(毫秒,秒);
this.periodMs=periodMs;
}
@凌驾
公共定期时段下一个(){
RegularTimePeriod结果=空;
if(getmillisond()+periodMs=FIRST_millisond_IN_SECOND){
结果=新的多毫秒(periodMs,(int)getmillis秒()-periodMs,getSecond());
}否则{
Second-previous=(Second)getSecond().previous();
如果(上一个!=null){
结果=新的多毫秒(periodMs,(int)(getmillisond()-periodMs+LAST\u毫秒+1),上一个);
}
}
返回结果;
} 
}
现在我在5秒内有10个样本,我将周期设置为500毫秒


对不起。这对我不起作用。我有一个方法
update(float value)
,每次我收到新数据时都会调用它,所以每次都是200毫秒。如果我把你在actionPerformed方法中编写的代码放入update方法中,我的图表会报告一行。好的,我想你正在绘制相同的值200次,因此你的序列要比100长得多(正如我在代码中定义的那样)。无论如何,我不想看到值之间的步骤。这不是系统的真实行为。我想要一条斜线,它取决于以下两个值之间的差异。然后只需调用
advanceTime()
省略
appendData(oldData)`。谢谢,但它对我不起作用。我通过延长类毫秒找到了一个解决方案。我将在三小时内发布它。我没有足够的声誉,因此我无法在8小时内回答我的问题。你是对的,这是一个错误。无论如何,它不起作用。它引发了一个例外。我修复了例外。是在next和PREVIOUS方法中,return毫秒而不是multipleOfMills。无论如何它不起作用。是的,我可以,如果我更新问题或其他东西会更好吗?我想我找到了一个很好的解决方案我想更新问题,但是如果你已经解决了问题,也许你可以把它作为一个解决方案发布,也可能会接受。是的,我必须等待发布我的答案因为我没有足够的声誉。+1现在我明白你的意思了。这看起来比创建一个单独的
半秒
类更灵活。你可能还需要实现
可比
。可能是的。我认为以毫秒为单位实现的可比方法对我的类也有好处。顺便说一句,感谢你发布了你的解决方案。我建议接受@msandiford的有见地的答案,因为接受自己的答案以外的答案会得到两分。详情请参阅。
public MilliDTSC(int nSeries, int nMoments, RegularTimePeriod timeSample) {
  super(nSeries, nMoments, timeSample);
  if(timeSample instanceof MultipleOfMillisecond){
    this.pointsInTime = new MultipleOfMillisecond[nMoments];
  }else if (timeSample instanceof Millisecond) {
    this.pointsInTime = new Millisecond[nMoments];
  } 
}

public class MultipleOfMillisecond extends Millisecond {

  private static final long serialVersionUID = 1L;
  private int periodMs = 100;

  public MultipleOfMillisecond(int periodMs){
    super();
    this.periodMs = periodMs;
  }

  public MultipleOfMillisecond(int periodMs, int millisecond, Second second){
    super(millisecond, second);
    this.periodMs = periodMs;
  }

  @Override
  public RegularTimePeriod next() {
    
    RegularTimePeriod result = null;
    if(getMillisecond() + periodMs <= LAST_MILLISECOND_IN_SECOND){
        result = new MultipleOfMillisecond( periodMs, (int)(getMillisecond() + periodMs), getSecond());
    }else{
        Second next = (Second)getSecond().next();
        if(next != null){
            result = new MultipleOfMillisecond(periodMs, (int)(getMillisecond() + periodMs - LAST_MILLISECOND_IN_SECOND - 1), next);
        }
    }
    return result;
    
  }

  @Override
  public RegularTimePeriod previous() {
    
    RegularTimePeriod result = null;
    if(getMillisecond() - periodMs >= FIRST_MILLISECOND_IN_SECOND){
        result = new MultipleOfMillisecond(periodMs, (int)getMillisecond() - periodMs, getSecond());
    }else{
        Second previous = (Second)getSecond().previous();
        if(previous != null){
            result = new MultipleOfMillisecond(periodMs, (int)(getMillisecond() - periodMs + LAST_MILLISECOND_IN_SECOND + 1), previous);
        }
    }
    return result;
    
  } 
}