【问题标题】:Chart with Fixed Second Time Interval on X AxisX 轴上具有固定第二时间间隔的图表
【发布时间】:2019-02-27 23:42:24
【问题描述】:

我正在尝试创建一个图表,其中包含一个 x 图表,其间隔值从 0 到 120。数据将每秒更新一次。 图表类型与windows任务管理器中的CPU Chart一模一样:


我已经检查了主题:

  1. Random errors when changing series using JFreeChart
  2. JFreechart series movement with fixed x-axis
  3. JFreeChart - How to show real-time on the X-Axis of a TimeSeries chart

即使这些主题的答案对我的理解很有帮助,但我还是无法达到我想要做的事情。

提前谢谢你。

【问题讨论】:

    标签: java jfreechart


    【解决方案1】:

    由于我没有答案,同时我解决了我的问题,如下所示。不过,我认为有——应该有——更简单的方法来做到这一点。由于我的时间有限,我想出了这个解决方案。如果您有更好的地方,或者您认为需要改进的地方,请分享。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多