Search code examples
javajfreechart

Chart with Fixed Second Time Interval on X Axis


I am trying to have a chart that is having an x chart with interval values from 0 to 120. The data will be updated every second. The type of chart is exactly like CPU Chart in windows task manager: this


I have checked the topics:

  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

Even though the answers to those topics were really helpful for me the understand yet I could not reach what I want to do.

Thank you in advance.


Solution

  • Since I had no answer, in the mean time I solved my problem like following. Though, I think that there is, -there should be-, easier way to do this.Since I was limited on time I came up with this solution. If you have anything better, or something you see to be improved, please share so.

    I add to my LinkedList until it gets as big as I want.(120 in this case). After that It removes the last entry, adds to the beginning. This method is called every second by another thread.

       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();
      }
    }