由于我没有答案,同时我解决了我的问题,如下所示。不过,我认为有——应该有——更简单的方法来做到这一点。由于我的时间有限,我想出了这个解决方案。如果您有更好的地方,或者您认为需要改进的地方,请分享。
我添加到我的LinkedList,直到它变得像我想要的一样大。(在这种情况下为 120)。之后它删除最后一个条目,添加到开头。该方法每秒被另一个线程调用一次。
private final LinkedList<XYDataItem> countHistory = new LinkedList<>();
private static final int MAX_RANGE_IN_X_AXIS_FOR_XY_CHART = 121;
enter code here
public void getDataAndRefresh( final DefaultTableXYDataset xySeriesCollection)
{
synchronized ( this.adsbItemMap )
{
int count = 0;
if ( xySeriesCollection.getSeries( 0 ) != null )
{
final XYSeries xySeries = xySeriesCollection.getSeries( 0 );
try
{
if ( this.counter == MAX_RANGE_IN_X_AXIS_FOR_XY_CHART )
{
xySeries.getItems().forEach( item -> {
final XYDataItem xyItem = ( XYDataItem ) item;
this.countHistory.addLast(
new XYDataItem( xyItem.getXValue()+1,xyItem.getYValue()));
} );
//xySeries.clear();
this.countHistory.pollLast();
this.countHistory.addFirst( new XYDataItem( 0, count ) );
this.countHistory.forEach( xySeries::addOrUpdate );
this.countHistory.clear();
}
else
{
xySeries.getItems().forEach( item -> {
final XYDataItem xyItem = ( XYDataItem ) item;
this.countHistory.addLast(
new XYDataItem( xyItem.getXValue() + 1, xyItem.getYValue() ) );
} );
final XYDataItem countItem = new XYDataItem( 0, count );
this.countHistory.addFirst( countItem );
this.countHistory.forEach( xySeries::addOrUpdate );
this.countHistory.clear();
this.counter++;
}
}
catch ( final Exception e )
{
LOG.warn( "Something went wrong.", e );
}
}
//clear
total.setValue( count );
this.adsbItemMap.clear();
}
}