/// <summary> /// Creates List[Chunk], with regular start and end dates for required intervals. Amount = 0 for all Chunks. /// </summary> /// <param name="startDate"></param> /// <param name="endDate"></param> /// <param name="interval"></param> /// <returns>List[Chunk] for required duration, each Chunk covering one interval.</returns> public List <Chunk> setupDatedChunksForApportionToPeriod(DateTime startDate, DateTime endDate, AppotionmentPeriod interval) { ///calendar object used for adding days/months/years to start date System.Globalization.Calendar calendar = System.Globalization.CultureInfo.CurrentCulture.Calendar; ///set up apportionment requirements List <Chunk> datedChunks = new List <Chunk>(); int periodType = (int)interval; ///i used to keep track of how many Chunks have already been created & adds appropriate no. of intervals to start date. int i = 0; ///create list of chunks with correct required start dates do { Chunk chunk = new Chunk(); TimeSpan oneDay = new TimeSpan(1, 0, 0); ///one day timespan needed to calculate end dates switch (periodType) { case 1: ///interval is daily /// chunk.StartDate = calendar.AddDays(startDate, i); chunk.EndDate = calendar.AddDays(chunk.StartDate, 1); break; case 2: ///interval is weekly /// chunk.StartDate = calendar.AddWeeks(startDate, i); chunk.EndDate = calendar.AddWeeks(chunk.StartDate, 1); break; case 3: ///interval is monthly /// chunk.StartDate = calendar.AddMonths(startDate, i); chunk.EndDate = calendar.AddMonths(chunk.StartDate, 1); break; case 4: ///interval is quarterly /// chunk.StartDate = calendar.AddMonths(startDate, 3 * i); chunk.EndDate = calendar.AddMonths(chunk.StartDate, 3 * 1); break; case 5: ///interval is annually /// chunk.StartDate = calendar.AddYears(startDate, i); chunk.EndDate = calendar.AddYears(chunk.StartDate, 1); break; } ///take on day off each date date to avoid overlapping date ranges chunk.EndDate = chunk.EndDate.Subtract(oneDay); datedChunks.Add(chunk); i++; } while (datedChunks.ElementAt(i - 1).EndDate <= endDate); return(datedChunks); }
public Image DrawSchema(out CoordsMaps coordsMaps, int colHeight, DateTime startDate, DateTime endDate, RowsFlags Pflags) { System.Globalization.CultureInfo UICulture = System.Threading.Thread.CurrentThread.CurrentUICulture; System.Globalization.Calendar cal = UICulture.Calendar; ganttStart = startDate; ganttEnd = endDate; int days = endDate.Subtract(startDate).Days; int rowCount = 0; if ((Pflags & RowsFlags.NoYears) != RowsFlags.NoYears) { yearRow = (rowCount++) * rowsSize + top; } int startYear = startDate.Year; int firstColYear = 0; if ((Pflags & RowsFlags.NoMonths) != RowsFlags.NoMonths) { monthRow = (rowCount++) * rowsSize + top; } int startMonth = startDate.Month; int firstColMonth = 0; if ((Pflags & RowsFlags.NoWeeks) != RowsFlags.NoWeeks) { weekRow = (rowCount++) * rowsSize + top; } int startWeek = cal.GetWeekOfYear(startDate, UICulture.DateTimeFormat.CalendarWeekRule, UICulture.DateTimeFormat.FirstDayOfWeek); int firstColWeek = 0; if ((Pflags & RowsFlags.NoDays) != RowsFlags.NoDays) { dayRow = (rowCount++) * rowsSize + top; } this.ganttHeight = colHeight + dayRow + 1; Image img = new Bitmap(days * dayColumnSize + 1, this.ganttHeight); g = Graphics.FromImage(img); g.Clear(Color.White); SolidBrush brush = new SolidBrush(Color.Black); SolidBrush dayBrush = new SolidBrush(Color.LightGray); SolidBrush weekendBrush = new SolidBrush(Color.DarkGray); sf = new StringFormat(StringFormatFlags.NoClip | StringFormatFlags.NoWrap | StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.MeasureTrailingSpaces); Font yearFont = FontToFitVertical(new FontFamily("Arial"), FontStyle.Regular, rowsSize, sf); monthFont = FontToFitVertical(new FontFamily("Arial"), FontStyle.Regular, rowsSize, sf); descFont = FontToFitVertical(new FontFamily("Arial"), FontStyle.Regular, rowsSize, sf); descFontBold = FontToFitVertical(new FontFamily("Arial"), FontStyle.Bold, rowsSize, sf); monthDayFont = FontToFitHorizontal(new FontFamily("Arial"), FontStyle.Regular, dayColumnSize, sf); Pen pen = new Pen(Color.DarkGray, 1F); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; int dayLeft = 0; Rectangle ra = new Rectangle(); CoordsMaps cm = new CoordsMaps(); for (int i = 0; i <= days; i++) { int currentPixel = i * dayColumnSize; DateTime todayDate = cal.AddDays(startDate, i); if (yearRow != -1 && (todayDate.Year != startYear || i == days)) { ra = new Rectangle(left + firstColYear, yearRow, currentPixel - firstColYear, rowsSize); g.DrawRectangle(pen, ra); g.DrawString(startYear.ToString(), monthFont, brush, ra, sf); cm.Add(new CoordsMap(RowTypes.Years, ra, startYear)); startYear = todayDate.Year; firstColYear = currentPixel; } if (monthRow != -1 && (todayDate.Day == 1 || i == days)) { ra = new Rectangle(left + firstColMonth, monthRow, currentPixel - firstColMonth, rowsSize); string month = UICulture.DateTimeFormat.GetMonthName(startMonth); if (yearRow == -1) { month += " " + todayDate.Year; } g.DrawRectangle(pen, ra); if (g.MeasureString(month, monthFont, 100000, sf).Width < ra.Width + 4) { g.DrawString(month, monthFont, brush, ra, sf); } else if (g.MeasureString(UICulture.DateTimeFormat.GetMonthName(startMonth), monthFont, 100000, sf).Width < ra.Width + 4) { g.DrawString(UICulture.DateTimeFormat.GetMonthName(startMonth), monthFont, brush, ra, sf); } cm.Add(new CoordsMap(RowTypes.Months, ra, startMonth)); startMonth = todayDate.Month; firstColMonth = currentPixel; } if (weekRow != -1 && (todayDate.DayOfWeek == UICulture.DateTimeFormat.FirstDayOfWeek || i == days)) { ra = new Rectangle(left + firstColWeek, weekRow, currentPixel - firstColWeek, rowsSize); g.DrawRectangle(pen, ra); g.DrawString(startWeek.ToString(), monthFont, brush, ra, sf); cm.Add(new CoordsMap(RowTypes.Weeks, ra, startWeek)); startWeek++; firstColWeek = currentPixel; } if (dayRow != -1 && i != days) { ra = new Rectangle(left + dayLeft, dayRow, dayColumnSize, rowsSize); dayLeft += (dayColumnSize); Rectangle r = ra; r.Height = colHeight; if (todayDate.DayOfWeek == DayOfWeek.Sunday || todayDate.DayOfWeek == DayOfWeek.Saturday) { g.FillRectangle(dayBrush, r); } g.DrawRectangle(pen, r); cm.Add(new CoordsMap(RowTypes.Days, r, i)); g.DrawString(cal.GetDayOfMonth(todayDate).ToString(), monthDayFont, brush, ra, sf); Rectangle r2 = ra; r2.Y += monthDayFont.Height; g.DrawString(UICulture.DateTimeFormat.GetShortestDayName(cal.GetDayOfWeek(todayDate)).ToUpper().Substring(0, 1), monthDayFont, brush, r2, sf); if (todayDate.ToShortDateString() == DateTime.Now.ToShortDateString()) { g.DrawLine(new Pen(Color.Red), currentPixel, dayRow, currentPixel, dayRow + colHeight); } } } dayRow += monthDayFont.Height; coordsMaps = cm; return(img); }