User select range for time series...

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
mlavwilson
Posts: 3
Joined: Mon Mar 21, 2005 8:53 pm

User select range for time series...

Post by mlavwilson » Wed Mar 23, 2005 6:08 pm

I have a timeseries that I want to give the user an option to show the last 60, 120, 180...
I want to maintain all the data so that when the user selects 180 when it was 60 I do not have to fetch the data again.

Is there a more elegant way than what I have done below?

Code: Select all

import java.util.Locale;
import java.util.TimeZone;

import org.jfree.data.Range;
import org.jfree.data.time.TimePeriod;
import org.jfree.data.time.TimeTableXYDataset;

/**
 * @author Matthew L. Wilson
 * @version $Revision: 1.1 $ $Date: 2005/03/23 15:20:38 $
 */
public class BoundedTimeTableXYDataset extends TimeTableXYDataset
{
   /** The number of timePeriods to display. * */
   private int displayRange = -1;

   /** The max number of timePeriods alowed to display. * */
   private int maxDisplayRange = -1;

   /**
    * @see org.jfree.data.time.TimeTableXYDataset#BoundedTimeTableXYDataset()
    */
   public BoundedTimeTableXYDataset()
   {
      super();
   }

   /**
    * @see org.jfree.data.time.TimeTableXYDataset#BoundedTimeTableXYDataset(TimeZone)
    */
   public BoundedTimeTableXYDataset(TimeZone zone)
   {
      super(zone);
   }

   /**
    * @see org.jfree.data.time.TimeTableXYDataset#BoundedTimeTableXYDataset(TimeZone, Locale)
    */
   public BoundedTimeTableXYDataset(TimeZone zone, Locale locale)
   {
      super(zone, locale);
   }

   /**
    * Adds a new data item to the dataset and sends 
    * a {@link org.jfree.data.general.DatasetChangeEvent}
    * to all registered listeners.
    * 
    * @param period
    *           the time period.
    * @param y
    *           the value for this period.
    * @param seriesName
    *           the name of the series to add the value.
    */
   public void add(TimePeriod period, double y, String seriesName)
   {
      add(period, new Double(y), seriesName, false);

      if (maxDisplayRange > 0)
      {
         while (getItemCount() > maxDisplayRange)
         {
            TimePeriod timePeriod = getTimePeriod(0);
            remove(timePeriod, seriesName, false);
         }
      }
      
      fireDatasetChanged();
   }

   /**
    * Returns the range of the values in this dataset's domain.
    * 
    * @param includeInterval
    *           a flag that controls whether or not the x-intervals are taken into account.
    * @return The range.
    */
   public Range getDomainBounds(boolean includeInterval)
   {
      int count = getItemCount();
      if (count == 0)
      {
         return null;
      }

      if (displayRange >= 0)
      {
         TimePeriod first = getTimePeriod(0);
         TimePeriod last = getTimePeriod(count - 1);

         if (count > displayRange)
         {
            first = getTimePeriod(count - displayRange);
         }

         return new Range(first.getStart().getTime(), last.getEnd().getTime());
      }
      else
      {
         return super.getDomainBounds(includeInterval);
      }

   }

//Setters and Getters

   /**
    * @return Returns the displayRange.
    */
   public int getDisplayRange()
   {
      return displayRange;
   }

   /**
    * @param displayRange
    *           The displayRange to set.
    */
   public void setDisplayRange(int displayRange)
   {
      setDisplayRange(displayRange, true);
   }

   /**
    * @param displayRange
    *           The displayRange to set.
    */
   public void setDisplayRange(int displayRange, boolean notify)
   {
      this.displayRange = displayRange;
      if (notify)
      {
         fireDatasetChanged();
      }
   }

   /**
    * @return Returns the maxDisplayRange.
    */
   public int getMaxDisplayRange()
   {
      return maxDisplayRange;
   }

   /**
    * @param maxDisplayRange
    *           The maxDisplayRange to set.
    */
   public void setMaxDisplayRange(int maxDisplayRange)
   {
      this.maxDisplayRange = maxDisplayRange;
   }
}

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Fri Mar 25, 2005 12:03 am

Maybe just set the date axis range directly in response to the user input?
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked