ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Getting the weeks to show properly in a gantt (StartOfWeekFilter)
Posted by geoff on Oct-16-2007 01:28
I produce a gantt chart that show a month at a time, with zones for each of the weeks. The problem is that the first day of the week isn't showing correctly. For example, for the month of October the first day of the week shows as:

Oct 7, Oct 16, Oct 22, and Oct 28.

The real first days of the week are:

Oct 7, Oct 14, Oct 21, Oct 28.

I'm not sure why the start of the mont isn't showing properly.

Also, I'd like to be able to shade the weekends grey if possible too, but I figure I need to get this stright first.

My code is below:

        DateTime scaleOrigin = new DateTime(ganttParams.month.Year, ganttParams.month.Month, 1);

        DateTime scaleEnd = new DateTime(ganttParams.month.Year, ganttParams.month.Month, scaleOrigin.AddMonths(1).AddDays(-1).Day, 23,59,59);

        c.yAxis().setDateScale(scaleOrigin, scaleEnd);

        c.yAxis().setMultiFormat(Chart.StartOfWeekFilter(), "<*font=Arial Bold*>{value|mmm, d}");

  Re: Getting the weeks to show properly in a gantt (StartOfWeekFilter)
Posted by geoff on Oct-16-2007 04:08
I have bascially figured out the first issue using RegularSpacingFilter, but I still can't block out the weekends on a gantt.

addZone seems to only take doubles, btu I need the zone to be a date range, not a range of numbers. I tried casting the dates to doubles, but that didn't work.

I guess I could use an additional spacing filter, but I'm not sure if that will work properly.

  Re: Getting the weeks to show properly in a gantt (StartOfWeekFilter)
Posted by Peter Kwan on Oct-16-2007 07:28
Hi geoff,

You may use Chart.CTime to convert a .NET DateTime to a double. For example:

c.yAxis().addZone(Chart.CTime(startDateTime), Chart.CTime(endDateTime), 0xcccccc);

Hope this can help.

Regards
Peter Kwan

  Re: Getting the weeks to show properly in a gantt (StartOfWeekFilter)
Posted by geoff on Oct-17-2007 04:13
Thanks, now I just need to work on a good algorithim for fiding the weekends in a month ;)

  Re: Getting the weeks to show properly in a gantt (StartOfWeekFilter)
Posted by geoff on Oct-17-2007 04:41
Actually I;ve got it except I'm not quite sue how ot handle when a saturday falls on the last day of the month or sunday falls on the first day of the month, other than that I've got it.

  Re: Getting the weeks to show properly in a gantt (StartOfWeekFilter)
Posted by Peter Kwan on Oct-17-2007 12:14
Hi Geoff,

You may try something like the followings:

for (DateTime d = firstDate.AddDays(-(int)firstDate.DayOfWeek - 1); d < lastDate; d = d.AddDays(7))
    c.yAxis().addZone(Chart.CTime(d), Chart.CTime(d.AddDays(2)), 0xcccccc);

I think the above will work regardless of what is the day of week for the firstDate or lastDate.

Hope this can help.

Regards
Peter Kwan

  Re: Getting the weeks to show properly in a gantt (StartOfWeekFilter)
Posted by geoff on Oct-18-2007 01:17
Actually I don't think that will work either.

I can get it to work, I just need to know the number that I need to add to the CTime in order to add 23.99 hours to a day.

The whole problem is just complicated by having to deal with the time portion of the datetime, and really adding 2 days is even wrong, I really need to add 1 day and 23.999 hours, because if Saturday falls on October 30th, for example, then I can't add 2 days, and really I shouldn't ever add 2 days anyway, its just that it basically works out if you do it that way, even though its techincally wrong.

If Saturday falls on October 31st, then I obviously can't even add one day, I just need to end the zone at the last minute of that day, etc.

Here is the code I have now (using a custom method which returns the numeric day of the first Saturday of a given month)

scaleOrigin = mm/01/yyyy 00:00:00
scaleEnd = mm/XX/yyyy 59:59:59 (where XX = the last day of tha month)

for (i = DateUtils.FirstSaturday(scaleOrigin); i < scaleEnd.Day; i += 7)
        {
            DateTime zoneStart = new DateTime(scaleOrigin.Year, scaleOrigin.Month, i);

            if (zoneStart.Day == scaleEnd.Day)
                c.yAxis().addZone(Chart.CTime(zoneStart), Chart.CTime(zoneStart)+???, 0xc0c0c0);
            else
                c.yAxis().addZone(Chart.CTime(zoneStart), Chart.CTime(zoneStart)+???, 0xc0c0c0);
        }

The easiest thing to do would simply be to add the appropriate decimal value after converting to CTime. Do you know what that value is, i.e. what CTime equals 24 hours, and then I can simply subtract 1 from that, right?

Thanks

  Re: Getting the weeks to show properly in a gantt (StartOfWeekFilter)
Posted by geoff on Oct-18-2007 01:24
Nevermind, I just realized that 86400 is the number of seconds in a day, thus I need to add either 86399 or 172799.

Thanks

  Re: Getting the weeks to show properly in a gantt (StartOfWeekFilter)
Posted by Peter Kwan on Oct-18-2007 02:25
Hi Geoff,

I think theorectically, you need to add 172799.999999999999999999999999999 (infinite number of 9). This is practically the same as 172800 (2 days). Adding 172799 will also work in a chart, because the pixels on the screen do not have sufficient resolution to distinguish between 172800 and 172799, so they are effectively the same.

If you are at Oct 30, but your x-axis ends at Oct 31, then you can add 2 days anyway. If the zone exceeds either end of the axis, ChartDirector will automatically trim the zones.

Actually, in my original code, the first zone is likely to be before the firstDate, and the last zone may fall beyond the lastDate, but this should not matter if firstDate and lastDate are the end points of the axis, as ChartDirector will trim the zones automatically.

Hope this can help.

Regards
Peter Kwan

  Re: Getting the weeks to show properly in a gantt (StartOfWeekFilter)
Posted by geoff on Oct-18-2007 02:32
Oh, is there something that you have to do in order to get ChartDirector to trim the ends? I had experienced where if the date exceeded the Plot Area then the bar overshot the chart area. If it will automaticlaly trim, then yes I can elimiante the if statement.

Thanks

  Re: Getting the weeks to show properly in a gantt (StartOfWeekFilter)
Posted by Peter Kwan on Oct-18-2007 03:32
Hi Geoff,

By default, ChartDirector will only trim the zones (which are considered as part of the plot area background), but not the contents (the bars). However, you can ask ChartDirector to trim the bars by using:

c.setClipping();

With the above API, basically, ChartDirector will cut any plot area content that is outside the plot area.

Hope this can help.

Regards
Peter Kwan

  Getting the weekends shaded in JSP gantt charts
Posted by Bas on Jun-25-2011 19:07
Hey Peter

I am struggling to get my JSP Gantt chart to show shaded weekends using the addzone method. I have managed to create the Gantt the way I need but could use some help:

Basically I have the axis configured between two Date objects (Java). Do you have a ready method to create a shading for all Saturdays and Sundays between dFromDate and dTodate?

An additional question: Does the CD date axis handling support week numbers?

Most appreciate your input

Bas

  Re: Getting the weekends shaded in JSP gantt charts
Posted by Peter Kwan on Jun-27-2011 02:53
Hi Bas,

To shading all Saturdays and Sundays during a date range, just iterate through the date range to get all Saturdays and Sundays, and use them to addZones. You can use a simple "for" loop to iterate through the date range.

In the following, I assume dFromDate and dTodate are Calendar objects, as the concept of Saturdays and Sundays is represented in Calendar object in Java.

for (dFromDate.add(Calendar.DATE, (7 - dFromDate.get(Calendar.DAY_OF_WEEK)) % 6); dFromDate.before(dTodate); dFromDate.add(Calendar.DATE, 7))
     c.yAxis().addZone(Chart.CTime(dFromDate), Math.min(Chart.CTime(dTodate), Chart.CTime(dFromDate) + 86400 * 2), 0x80ffcccc);

By "CD date axis handling", do you mean "CD date axis label formatting" or some other things?

CD date axis handles Java Date objects. It supports whatever Java Date object supports.

For labelling the axis, ChartDirector does not have a built-in label field for "week numbers", because there is no standard by what "week numbers" mean.

(In Java GregorianCalendar object, there is a "week numbers" of which the exact definition is configurable. However, no matter how they are configured, the Java "week numbers" are inconsistent with international standard ISO 8601. It is unsure if actual corporations use the ISO standard or some proprietary definitions of week numbers.)

If you would like to label the axis using a method not built-in in ChartDirector, you may use Axis.setDateScale2 to specify your own custom labels.

Hope this can help.

Regards
Peter Kwan

  Re: Getting the weekends shaded in JSP gantt charts
Posted by Bas on Jun-27-2011 03:22
Hey Peter, first of all thanks for your help.


The dates are given to my JSP as parameters (String, which I then cast to Date):

  String      strFromDate               = request.getParameter("FROMDATE");
  String      strToDate                 = request.getParameter("TODATE");
  DateFormat  sdf                       = new SimpleDateFormat("dd-MMM-yyyy");
  Date        dFromDate                 = sdf.parse(strFromDate);
  Date        dToDate                   = sdf.parse(strToDate);



It doesnt quite work though. Is it because I have a different date handling? See below the error:


org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 240 in the jsp file: /analysispack/woplan/ganttwoplan.jsp
The method get(int) is undefined for the type Date
237:     }
238:
239:
240: for (dFromDate.add(Calendar.DATE, (7 - dFromDate.get(Calendar.DAY_OF_WEEK)) % 6); dFromDate.before(dToDate); dFromDate.add(Calendar.DATE, 7))
241:      c3.yAxis().addZone(Chart.CTime(dFromDate), Math.min(Chart.CTime(dToDate), Chart.CTime(dFromDate) + 86400 * 2), 0x80ffcccc);
242:
243:


An error occurred at line: 240 in the jsp file: /analysispack/woplan/ganttwoplan.jsp
The method add(int, int) is undefined for the type Date
237:     }
238:
239:
240: for (dFromDate.add(Calendar.DATE, (7 - dFromDate.get(Calendar.DAY_OF_WEEK)) % 6); dFromDate.before(dToDate); dFromDate.add(Calendar.DATE, 7))
241:      c3.yAxis().addZone(Chart.CTime(dFromDate), Math.min(Chart.CTime(dToDate), Chart.CTime(dFromDate) + 86400 * 2), 0x80ffcccc);
242:
243:

  Re: Getting the weekends shaded in JSP gantt charts
Posted by Bas on Jun-27-2011 16:34
Attachments:
Hey Peter, I changed the Date to Calendar and now I get the weekends shaded. Thanks for that. One last comment: The weekend is painted on Sunday and Monday instead of Saturday and Sunday.

I changed your code slightly to (actually I changed %6 into %7):

for (cFromDate.add(Calendar.DATE, (7 - cFromDate.get(Calendar.DAY_OF_WEEK)) % 7); cFromDate.before(cToDate); cFromDate.add(Calendar.DATE, 7))
     c3.yAxis().addZone(Chart.CTime(cFromDate), Math.min(Chart.CTime(cToDate), Chart.CTime(cFromDate) + 86400 * 2), 0x80ffcccc);

This makes the weekends show up on the right dates, only the very first one is not shown. See the picture attached. May 1st is a Sunday but isnt shaded. All other weekends seem to be okay.

Any idea? Thanks anyway, your input already helped a lot.

Bas
calendar.png

  Re: Getting the weekends shaded in JSP gantt charts
Posted by Peter Kwan on Jun-27-2011 17:58
Hi Bas,

May be you can try:

for (firstDate.add(Calendar.DATE, (8 - firstDate.get(Calendar.DAY_OF_WEEK)) % 7 - 1); firstDate.before(lastDate); firstDate.add(Calendar.DATE, 7))
c.yAxis().addZone(Chart.CTime(firstDate), Chart.CTime(firstDate) + 86400 * 2, 0x80ffcccc);

Hope this can help.

Regards
Peter Kwan

  Re: Getting the weekends shaded in JSP gantt charts
Posted by Bas on Jun-27-2011 18:11
Peter, you're a star! Perfect, and thanks for the continuing & great support!