X 轴上具有固定第二时间间隔的图表

Chart with Fixed Second Time Interval on X Axis

我正在尝试制作一个包含区间值从 0 到 120 的 x 图表的图表。数据将每秒更新一次。 图表类型与windows任务管理器中的CPU图表完全相同:


我查看了主题:

  1. Random errors when changing series using JFreeChart
  2. JFreeChart - 如何在 TimeSeries 的 X 轴上显示实时 图表

尽管这些主题的答案对我的理解很有帮助,但我无法达到我想做的事情。

提前致谢。

由于我没有答案,同时我解决了我的问题,如下所示。虽然,我认为有 - 应该有 - 更简单的方法 this.Since 我想出这个解决方案的时间有限。如果您有更好的地方,或者您认为需要改进的地方,请分享。

我添加到我的 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();
  }
}