Intra-Day Gap Removals Using SegmentedTimeline

Discussion about JFreeChart related to stockmarket charts.
Locked
Junaid
Posts: 13
Joined: Tue Jul 20, 2010 2:25 pm
antibot: No, of course not.
Contact:

Intra-Day Gap Removals Using SegmentedTimeline

Post by Junaid » Tue Aug 03, 2010 10:50 am

I am facing an issue in JFreeChart while trying to remove the gaps from TimeLine. There are following 2 things that I am trying to achieve:

1). Remove the days from timeline which are Saturday/Sunday or holidays

2). Remove the intra-day gaps for shifts, from the timeline. There are 3 shifts in any working day as following:
a) shift1: - 0000 – 0759 ( i.e. from midnight to 8 am in morning)
b) shift2: - 0800 – 1559 ( i.e. from 8 am in morning to 1559 pm in evening)
c) shift3: - 1600 – 2359 ( i.e. from 16 pm in evening to 2359 pm in night)

I am using the SegmentedTimeLine class to achieve the removal of gaps from time line.

There is a Calendar UI, which enables a user to select the start date and end date as a date range for which he wants to see data. The user selected criterion is represented by class FilterCriteria.

Calendar UI also allow user to de-select the week-days ( i.e. any day from Monday – Friday) for which he do not want to see the data and also provides the user to select any of the 3 shifts in a day (shift1, shift2 or shift3) for which he want data. Any shift selection requires removal of intra-day gaps.

For a test data I am taking the range as:
Start Date: - 14 Dec 2007 00:00:00
End Date: - 20 Dec 2007 23:59:59
The above dates are in 24 hour format.

There are 2 cases as below:
Case 1) User only de-selects the days for which he do not want to see data, for e.g. Saturday, Sunday and any other
holiday. Let’s say user de-selects Saturday( 15 Dec 2007 ) and Sunday ( 16 Dec 2007 )

Case 2) Along with the holidays, user also de-selects the shifts for which he do not want data i.e. intra-day charts. Let’s
say he de-selects Saturday, Sunday and Tuesday (18 Dec 2007) and selects only shift 2 (0800 – 1559) and shift 3
(1600 – 2359)

I am using two segmented time lines to form a compounded segmented time line to take care of Case 2 above. I have extended the class BaseDateAxis to override the method refreshTicksHorizontal, for removing the overlapping on time axis.

The code is as shown below:

Code: Select all

private void removeGapsFromXAxis(HttpSession httpSession) {
    // FilterCriteria is a class that represent the user chosen 
    // holidays or shifts
    long oneDaySegmentsIncluded = filterCriteria.getlstWeekDaysSelected().size() ; 
    long oneDaySegmentsExcluded = 7 - oneDaySegmentsIncluded;
    SegmentedTimeline segTimelineWithSegSizeAsOneDay = 
        new SegmentedTimeline(SegmentedTimeline.DAY_SEGMENT_SIZE
          , (int) oneDaySegmentsIncluded , (int) oneDaySegmentsExcluded);
    segTimelineWithSegSizeAsOneDay.setStartTime(myModel.getTimebase().getTime());
    
    if(shiftSelected){
      int oneHourSegmentsIncluded = 
        getTotalShiftHoursIncluded(filterCriteria.getTimeIntervals());
      int oneHourSegmentsExcluded = 24 - oneHourSegmentsIncluded;
      int selectedShiftStartTime = filterCriteria.getStartTimeHr();
      SegmentedTimeline segTimeLineWithSegSizeAsOneHour = 
          new SegmentedTimeline(SegmentedTimeline.HOUR_SEGMENT_SIZE
            , oneHourSegmentsIncluded, oneHourSegmentsExcluded);
      segTimeLineWithSegSizeAsOneHour.setStartTime(myModel.getTimebase().getTime() 
          + selectedShiftStartTime * segTimeLineWithSegSizeAsOneHour.getSegmentSize());
      segTimeLineWithSegSizeAsOneHour.setBaseTimeline(segTimelineWithSegSizeAsOneDay);
      for (Calendar calendar : lstHolidays) {
        Calendar gapCalendar = (Calendar)calendar.clone();
        segTimeLineWithSegSizeAsOneHour.addBaseTimelineException(gapCalendar.getTime());
      }
      // IntraDayHourGap is a class that represent the shifts 
      //(0000 - 0759), (0800 - 1559), (1600 - 2359), that user has selected
      List<IntraDayHourGap> lsIntraDayHrGap = filterCriteria.getIntraDayHourGap();
      for (IntraDayHourGap intraDayHourGap : lsIntraDayHrGap) {
        segTimeLineWithSegSizeAsOneHour.addException
          (intraDayHourGap.getStartDate().getTime(), intraDayHourGap.getEndDate().getTime());
        // e.g 14/Dec/2007 08:00:00 - 14/Dec/2007 16:00:00
      }
      double start = new Double(myModel.getTimebase().getTime() 
                    + selectedShiftStartTime * 
                      segTimeLineWithSegSizeAsOneHour.getSegmentSize()).doubleValue();
      double end = new Double(myModel.getTimebaseEnd().getTime()).doubleValue();
      Range range = new Range(start, end);
      xaxis.setRange(range, true, true); // xaxis is of type DateAxis
      xaxis.setTimeline(segTimeLineWithSegSizeAsOneHour);
    } else { //when no shift are selected, only holidays are selected
          List<Date> lstGapDates = new ArrayList<Date>();
          for(Calendar calendar : lstHolidays){
            Calendar cal = (Calendar)calendar.clone();
            lstGapDates.add(cal.getTime());
          }
          segTimelineWithSegSizeAsOneDay.addExceptions(lstGapDates);
        }
        double start = new Double(myModel.getTimebase().getTime());
        double end   = new Double(myModel.getTimebaseEnd().getTime()).doubleValue();
        Range range = new Range(start, end);
        xaxis.setRange(range, true, true);
        xaxis.setTimeline(segTimelineWithSegSizeAsOneDay);
        
    }

}
For the case 1, when user has de-selected, say Saturday (15 Dec 2007), Sunday (16 Dec 2007), the resulting graph rightly excludes 15 and 16 December, but it also excludes 19th Dec and 20th Dec. The chart shows data only for 14, 17 and 18 Dec. I do not understand why it is excluding 19th and 20th Dec? Please let me know if you can spot the bug in my code.

For the case 2 which, is an intra-day gap removal case, suppose user has de-selected Saturday (15 Dec 2007), Sunday (16 Dec 2007), and Tuesday (18 Dec 2007) and wants data only for shift 2 (0800 – 1559) and shift 3 (1600 – 2359). In this case the resulting chart rightly removes the intra-day gap i.e shift1 and also correctly removes the days Saturday (15 Dec 2007) and Sunday (16 Dec 2007) from the resulting timeline, but it does not removes the 18 Dec 2007, which come as a gap in a chart. I do not understand this anomalous behavior. Any help will be appreciated. Thanks in advance.

I had posted this query earlier but I am doing it again to elaborate the situation more clearly.

Regards,
Junaid

Junaid
Posts: 13
Joined: Tue Jul 20, 2010 2:25 pm
antibot: No, of course not.
Contact:

Re: Intra-Day Gap Removals Using SegmentedTimeline

Post by Junaid » Sun Aug 15, 2010 8:48 am

Hi guys,

I am trying to combine 2 segmentedtimeline for removal of intra-day gap. The first segmentedtimeline removes the holidays and second segmentedtimeline is to remove the intra-day gaps. I am using version 1.0.13.

I have taken a data of 1 week.

When trying to remove the 4 days, which are holidays, and trying to remove the intra-day gap hours ( 0800 - 1600 assuming 24 hour clock) for non holidays, the timeline gets truncated. The first day data is shown correctly (0000 - 0800 & 1600 - 2300), but the second day only show data from 1600 - 2300 hours and the 3rd day data is not shown at all, as the timeline just truncates after 2nd day.

Please find the below image. I am sorry for not a very clear image:
Image


Please find below the code:

Code: Select all

//segmentedTimeline is to remove holidays, filtercriteria is a calendar type UI control, where user can select holidays
//and intra-day gap hours
private void removeGapHoursFromXAxis(SegmentedTimeline segmentedTimeline, HttpSession httpSession) {

  FilterCriteria filterCriteria = (FilterCriteria)httpSession.getAttribute("filterCriteria");
  int validDays = filterCriteria.getM_lstValidDates().size();
  int gapDays = filterCriteria.getM_lstInvalidDates().size();
  int oneHourSegmentsIncludedInADay = getTotalShiftHoursIncluded(filterCriteria.getTimeIntervals());
  int oneHourSegmentsExcludedInADay = 24 - oneHourSegmentsIncludedInADay;
  int totOneHourSegmentsIncluded = oneHourSegmentsIncludedInADay * validDays;
  int totOneHourSegmentsExcluded = oneHourSegmentsExcludedInADay * gapDays;

  int selectedShiftStartTime = filterCriteria.getTimeIntervals().get(0).getStartTimeHr();
  SegmentedTimeline segTimeLineWithSegSizeAsOneHour = 
      new SegmentedTimeline(SegmentedTimeline.HOUR_SEGMENT_SIZE
        , totOneHourSegmentsIncluded , totOneHourSegmentsExcluded);
  segTimeLineWithSegSizeAsOneHour.setStartTime(myModel.getTimebase().getTime() 
      + selectedShiftStartTime * segTimeLineWithSegSizeAsOneHour.getSegmentSize());
  segTimeLineWithSegSizeAsOneHour.setBaseTimeline(segmentedTimeline);
  // Add basetimeline exceptions for onedaysegmentedtimeline
  for (Calendar calendar : filterCriteria.getM_lstInvalidDates()) {
    Calendar gapCalendar = (Calendar)calendar.clone();
    segTimeLineWithSegSizeAsOneHour.addBaseTimelineException(gapCalendar.getTime());
  }
  List<IntraDayHourGap> lsIntraDayHrGap = filterCriteria.getIntraDayHourGap();
  // Add hourly exceptions.
  for (IntraDayHourGap intraDayHourGap : lsIntraDayHrGap) {
    segTimeLineWithSegSizeAsOneHour.addException(intraDayHourGap.getStartDate().getTime()
      , intraDayHourGap.getEndDate().getTime());// e.g 14/Dec/2010 08:00:00 - 14/Dec/2010 16:00:00
    logger.info(intraDayHourGap);
  }
  double start = new Double(myModel.getTimebase().getTime() 
                    + selectedShiftStartTime * segTimeLineWithSegSizeAsOneHour.getSegmentSize()).doubleValue();
  double end = new Double(myModel.getTimebaseEnd().getTime()).doubleValue();
  Range range = new Range(start, end);
  xaxis.setRange(range, true, true);
  xaxis.setTimeline(segTimeLineWithSegSizeAsOneHour);
}
Thanks for your time and pointers to the defect that is leading to truncation of x-axis (timeline).
Please note the range is set correctly.

Regards,
Junaid

Locked