private int calculateNumLabels() { CalculateNext NextMajor = GetNextMajorFunction(); DateTime v = ScaleMinimum; int i; for (i = 0; ; i++) { if (v > ScaleMaximum) { break; } v = NextMajor(v); } return(i + 1); }
private CalculateNext GetNextMajorFunction() { // Create a function that, applied to a date/time, returns the // "next" value for major labels. CalculateNext NextMajor = null; if (axisType == AxisType.Quarters) { NextMajor = delegate(DateTime dt) { return(cal.AddMonths(dt, 3)); } } ; else if (axisType == AxisType.DailyWithHourlyTicks || axisType == AxisType.DailyWithHourlyLabels) { NextMajor = delegate(DateTime dt) { return(cal.AddDays(dt, 1)); } } ; else { NextMajor = delegate(DateTime dt) { return(cal.AddMonths(dt, 1)); } }; return(NextMajor); }
public void DrawX(Graphics g, AdvancedRect area, AdvancedRect plotArea) { drawArea = area; //using (Brush br = new SolidBrush(Color.Green)) // g.FillRectangle(br, area.Rect); GraphicsState _s = g.Save(); using (Brush br = labelFont.CreateBrush()) using (Font f = labelFont.CreateFont()) using (Font f2 = smallLabelFont.CreateFont()) using (Pen p = tickPen.CreatePen()) using (Pen pgrid = gridlinePen.CreatePen()) using (Pen pminor = minorTickPen.CreatePen()) { CalculateNext nextMajor = GetNextMajorFunction(); // Same dealy-o, but for minor ticks / labels. CalculateNext nextMinor = null; if (axisType == AxisType.MonthsHorizontalWithDailyTicks || axisType == AxisType.MonthsHorizontalWithDailyLabels) { nextMinor = delegate(DateTime dt) { return(cal.AddDays(dt, 1)); } } ; else if (axisType == AxisType.DailyWithHourlyTicks || axisType == AxisType.DailyWithHourlyLabels) { nextMinor = delegate(DateTime dt) { return(cal.AddHours(dt, 1)); } } ; else if (axisType == AxisType.Quarters) { nextMinor = delegate(DateTime dt) { return(cal.AddMonths(dt, 1)); } } ; // Determine major label format -- {0} is the major label v, // {1} is the quarter string majorLabel = null; if (axisType == AxisType.Quarters) { majorLabel = "Q{1} {0:\\'yy}"; } else if (axisType == AxisType.DailyWithHourlyTicks || axisType == AxisType.DailyWithHourlyLabels) { majorLabel = "{0:MMM %d \\'yy}"; } else { majorLabel = "{0:MMM \\'yy}"; } // Determine minor label format: string minorLabel = null; if (axisType == AxisType.MonthsHorizontalWithDailyLabels) { minorLabel = "{0:%d}"; } else if (axisType == AxisType.DailyWithHourlyLabels) { minorLabel = "{0:%h%t}"; } // Add some extra spacing if putting minor labels in. float minorLabelSpacing = 0; if (nextMinor != null && minorLabel != null) { minorLabelSpacing = 1f / 16; } // StringFormat for drawing major labels: StringFormat majorForm = new StringFormat(); if (axisType == AxisType.MonthsVertical || axisType == AxisType.Quarters) { majorForm.FormatFlags = StringFormatFlags.DirectionVertical; } // Determine first major label value. DateTime dtLeft; if (axisType == AxisType.Quarters) { // set v to beginning of quarter which ScaleMinimum is in dtLeft = new DateTime(ScaleMinimum.Year, (((ScaleMinimum.Month - 1) / 3) * 3) + 1, 1); } else if (axisType == AxisType.DailyWithHourlyTicks || axisType == AxisType.DailyWithHourlyLabels) { // set v to beginning of day dtLeft = new DateTime(ScaleMinimum.Year, ScaleMinimum.Month, ScaleMinimum.Day); } else { // set v to beginning of month dtLeft = new DateTime(ScaleMinimum.Year, ScaleMinimum.Month, 1); } for (int i = 0; i < calculateNumLabels(); i++) { float xLeft = DataToCoordinate(dtLeft, area); xLeft = Math.Max(xLeft, area.TopLeft.X); if (xLeft > area.BottomRight.X) { break; } DateTime dtRight = nextMajor(dtLeft); float xRight = DataToCoordinate(dtRight, area); xRight = Math.Min(xRight, area.BottomRight.X); // Major ticks & gridlines: g.DrawLine(p, xLeft, area.TopLeft.Y, xLeft, area.TopLeft.Y + tickLength); if (gridlinesEnabled && xLeft > area.TopLeft.X && xLeft < area.BottomRight.X) { g.DrawLine(pgrid, xLeft, plotArea.TopLeft.Y, xLeft, plotArea.BottomRight.Y); } // Minor ticks: if (nextMinor != null) { DateTime dtMinorLeft = nextMinor(dtLeft); while (dtMinorLeft < dtRight) { float xMinorLeft = DataToCoordinate(dtMinorLeft, area); if (xMinorLeft > area.TopLeft.X && xMinorLeft < area.BottomRight.X) { g.DrawLine(pminor, xMinorLeft, area.TopLeft.Y, xMinorLeft, area.TopLeft.Y + minorTickLength); } dtMinorLeft = nextMinor(dtMinorLeft); } } if (minorLabel != null && nextMinor != null) { DateTime dtMinorLeft = dtLeft; while (dtMinorLeft < dtRight) { DateTime dtMinorRight = nextMinor(dtMinorLeft); float xMinorLeft = DataToCoordinate(dtMinorLeft, area); float xMinorRight = DataToCoordinate(dtMinorRight, area); if (xMinorLeft >= area.TopLeft.X && xMinorRight <= area.BottomRight.X) { string dtxt = String.Format(minorLabel, dtMinorLeft); SizeF dsz = g.MeasureString(dtxt, f2); float xlabel = ((xMinorLeft + xMinorRight) / 2) - (dsz.Width / 2); if (xlabel > area.TopLeft.X && (xlabel + dsz.Width) < area.BottomRight.X) { g.DrawString(dtxt, f2, br, ((xMinorLeft + xMinorRight) / 2) - (dsz.Width / 2), area.TopLeft.Y); } } dtMinorLeft = dtMinorRight; } } // Draw major label: string txt = String.Format(majorLabel, dtLeft, (dtLeft.Month / 3) + 1); SizeF sz = g.MeasureString(txt, f, 100, majorForm); float draw_x = ((xLeft + xRight) / 2) - (sz.Width / 2); if (draw_x > area.TopLeft.X && (draw_x + sz.Width) < area.BottomRight.X) { g.DrawString(txt, f, br, ((xLeft + xRight) / 2) - (sz.Width / 2), area.TopLeft.Y + tickLength + minorLabelSpacing, majorForm); } dtLeft = dtRight; } // one final tick to signify end of last visible month float xL = DataToCoordinate(dtLeft, area); if (xL < area.BottomRight.X) { g.DrawLine(p, xL, area.TopLeft.Y, xL, area.TopLeft.Y + tickLength); if (gridlinesEnabled) { g.DrawLine(pgrid, xL, plotArea.TopLeft.Y, xL, plotArea.BottomRight.Y); } } } g.Restore(_s); }